Search in sources :

Example 11 with Vcenter

use of com.emc.storageos.db.client.model.Vcenter in project coprhd-controller by CoprHD.

the class AbstractDiscoveryAdapter method processHostChanges.

public void processHostChanges(List<HostStateChange> changes, List<URI> deletedHosts, List<URI> deletedClusters, boolean isVCenter) {
    log.info("There are " + changes.size() + " changes");
    // Iterate through all host state changes and create states for all of the affected export groups
    for (HostStateChange change : changes) {
        log.info("HostChange: " + change);
        Host host = dbClient.queryObject(Host.class, change.getHost().getId());
        // For every host change (added/removed initiator, cluster change), get all exports that this host
        // currently belongs to
        List<Initiator> newInitiatorObjects = dbClient.queryObject(Initiator.class, change.getNewInitiators());
        List<Initiator> oldInitiatorObjects = dbClient.queryObject(Initiator.class, change.getOldInitiators());
        if (newInitiatorObjects.isEmpty() && !oldInitiatorObjects.isEmpty()) {
            List<URI> hostInitiators = ComputeSystemHelper.getChildrenUris(dbClient, host.getId(), Initiator.class, "host");
            if (hostInitiators.size() == oldInitiatorObjects.size()) {
                log.info("No initiators were re-discovered for host " + host.getId() + " so we will not delete its initiators");
                DiscoveryStatusUtils.markAsFailed(getModelClient(), host, "No initiators were discovered", null);
                continue;
            }
        }
        // 3) If no datacenter or cluster change, make sure we have updated atleast the new datacenter for the host
        if (!NullColumnValueGetter.isNullURI(change.getOldDatacenter()) && !NullColumnValueGetter.isNullURI(change.getNewDatacenter()) && !change.getOldDatacenter().toString().equalsIgnoreCase(change.getNewDatacenter().toString())) {
            VcenterDataCenter oldDatacenter = dbClient.queryObject(VcenterDataCenter.class, change.getOldDatacenter());
            VcenterDataCenter currentDatacenter = dbClient.queryObject(VcenterDataCenter.class, change.getNewDatacenter());
            Cluster cluster = null;
            if (!NullColumnValueGetter.isNullURI(change.getNewCluster())) {
                cluster = dbClient.queryObject(Cluster.class, change.getNewCluster());
            }
            URI oldClusterURI = change.getOldCluster();
            Cluster oldCluster = null;
            if (!NullColumnValueGetter.isNullURI(oldClusterURI)) {
                oldCluster = dbClient.queryObject(Cluster.class, oldClusterURI);
            }
            if (!oldDatacenter.getVcenter().toString().equals(currentDatacenter.getVcenter().toString())) {
                Vcenter oldVcenter = dbClient.queryObject(Vcenter.class, oldDatacenter.getVcenter());
                Vcenter currentVcenter = dbClient.queryObject(Vcenter.class, currentDatacenter.getVcenter());
                EventUtils.createActionableEvent(dbClient, EventUtils.EventCode.HOST_VCENTER_CHANGE, host.getTenant(), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostVcenterChangeLabel", oldVcenter.getLabel(), currentVcenter.getLabel()), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostVcenterChangeDescription", host.getLabel(), oldCluster == null ? "N/A" : oldCluster.getLabel(), cluster == null ? " N/A " : cluster.getLabel()), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostVcenterChangeWarning"), host, Lists.newArrayList(host.getId(), host.getCluster(), cluster == null ? NullColumnValueGetter.getNullURI() : cluster.getId()), EventUtils.hostVcenterChange, new Object[] { host.getId(), cluster != null ? cluster.getId() : NullColumnValueGetter.getNullURI(), currentDatacenter.getId(), isVCenter }, EventUtils.hostVcenterChangeDecline, new Object[] { host.getId(), cluster != null ? cluster.getId() : NullColumnValueGetter.getNullURI(), currentDatacenter.getId(), isVCenter });
            } else {
                EventUtils.createActionableEvent(dbClient, EventUtils.EventCode.HOST_DATACENTER_CHANGE, host.getTenant(), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostDatacenterChangeLabel", oldDatacenter.getLabel(), currentDatacenter.getLabel()), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostDatacenterChangeDescription", host.getLabel(), oldCluster == null ? "N/A" : oldCluster.getLabel(), cluster == null ? " N/A " : cluster.getLabel()), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostDatacenterChangeWarning"), host, Lists.newArrayList(host.getId(), host.getCluster(), cluster == null ? NullColumnValueGetter.getNullURI() : cluster.getId()), EventUtils.hostDatacenterChange, new Object[] { host.getId(), cluster != null ? cluster.getId() : NullColumnValueGetter.getNullURI(), currentDatacenter.getId(), isVCenter }, EventUtils.hostDatacenterChangeDecline, new Object[] { host.getId(), cluster != null ? cluster.getId() : NullColumnValueGetter.getNullURI(), currentDatacenter.getId(), isVCenter });
            }
        } else if ((change.getOldCluster() == null && change.getNewCluster() != null) || (change.getOldCluster() != null && change.getNewCluster() == null) || (change.getOldCluster() != null && change.getNewCluster() != null && !change.getOldCluster().toString().equals(change.getNewCluster().toString()))) {
            Cluster cluster = null;
            if (!NullColumnValueGetter.isNullURI(change.getNewCluster())) {
                cluster = dbClient.queryObject(Cluster.class, change.getNewCluster());
            }
            URI oldClusterURI = change.getOldCluster();
            Cluster oldCluster = null;
            if (!NullColumnValueGetter.isNullURI(oldClusterURI)) {
                oldCluster = dbClient.queryObject(Cluster.class, oldClusterURI);
            }
            boolean oldClusterInUse = oldCluster == null ? false : ComputeSystemHelper.isClusterInExport(dbClient, oldCluster.getId());
            boolean newClusterInUse = cluster == null ? false : ComputeSystemHelper.isClusterInExport(dbClient, cluster.getId());
            if ((cluster != null || oldCluster != null) && (oldClusterInUse || newClusterInUse)) {
                String name = null;
                String description = null;
                if (cluster != null && oldCluster == null) {
                    name = ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeAddedLabel", cluster.getLabel());
                    description = ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeAddedDescription", host.getLabel(), cluster.getLabel());
                } else if (cluster == null && oldCluster != null) {
                    name = ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeRemovedLabel", oldCluster.getLabel());
                    description = ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeRemovedDescription", host.getLabel(), oldCluster.getLabel());
                } else {
                    name = ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeMovedLabel", oldCluster.getLabel(), cluster.getLabel());
                    description = ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeMovedDescription", host.getLabel(), oldCluster.getLabel(), cluster.getLabel());
                }
                EventUtils.createActionableEvent(dbClient, EventUtils.EventCode.HOST_CLUSTER_CHANGE, host.getTenant(), name, description, ComputeSystemDialogProperties.getMessage("ComputeSystem.hostClusterChangeWarning"), host, Lists.newArrayList(host.getId(), host.getCluster(), cluster == null ? NullColumnValueGetter.getNullURI() : cluster.getId()), EventUtils.hostClusterChange, new Object[] { host.getId(), cluster != null ? cluster.getId() : NullColumnValueGetter.getNullURI(), NullColumnValueGetter.isNullURI(change.getNewDatacenter()) ? NullColumnValueGetter.getNullURI() : change.getNewDatacenter(), isVCenter }, EventUtils.hostClusterChangeDecline, new Object[] { host.getId(), cluster != null ? cluster.getId() : NullColumnValueGetter.getNullURI(), NullColumnValueGetter.isNullURI(change.getNewDatacenter()) ? NullColumnValueGetter.getNullURI() : change.getNewDatacenter(), isVCenter });
            } else {
                host.setCluster(cluster == null ? NullColumnValueGetter.getNullURI() : cluster.getId());
                dbClient.updateObject(host);
                ComputeSystemHelper.updateHostAndInitiatorClusterReferences(dbClient, host.getCluster(), host.getId());
                if (cluster != null) {
                    ComputeSystemHelper.updateHostVcenterDatacenterReference(dbClient, host.getId(), cluster != null ? cluster.getVcenterDataCenter() : NullColumnValueGetter.getNullURI());
                }
            }
        } else if (!NullColumnValueGetter.isNullURI(change.getNewDatacenter())) {
            VcenterDataCenter currentDatacenter = dbClient.queryObject(VcenterDataCenter.class, change.getNewDatacenter());
            host.setTenant(currentDatacenter.getTenant());
            host.setVcenterDataCenter(currentDatacenter.getId());
            dbClient.updateObject(host);
        }
        if (ComputeSystemHelper.isHostInUse(dbClient, host.getId())) {
            for (Initiator oldInitiator : oldInitiatorObjects) {
                EventUtils.createActionableEvent(dbClient, EventUtils.EventCode.HOST_INITIATOR_DELETE, host.getTenant(), ComputeSystemDialogProperties.getMessage("ComputeSystem.removeInitiatorLabel", oldInitiator.getInitiatorPort()), ComputeSystemDialogProperties.getMessage("ComputeSystem.removeInitiatorDescription", oldInitiator.getInitiatorPort()), ComputeSystemDialogProperties.getMessage("ComputeSystem.removeInitiatorWarning"), host, Lists.newArrayList(host.getId(), oldInitiator.getId()), EventUtils.removeInitiator, new Object[] { oldInitiator.getId() }, EventUtils.removeInitiatorDecline, new Object[] { oldInitiator.getId() });
            }
            for (Initiator newInitiator : newInitiatorObjects) {
                EventUtils.createActionableEvent(dbClient, EventUtils.EventCode.HOST_INITIATOR_ADD, host.getTenant(), ComputeSystemDialogProperties.getMessage("ComputeSystem.addInitiatorLabel", newInitiator.getInitiatorPort()), ComputeSystemDialogProperties.getMessage("ComputeSystem.addInitiatorDescription", newInitiator.getInitiatorPort()), ComputeSystemDialogProperties.getMessage("ComputeSystem.addInitiatorWarning"), host, Lists.newArrayList(host.getId(), newInitiator.getId()), EventUtils.addInitiator, new Object[] { newInitiator.getId() }, EventUtils.addInitiatorDecline, new Object[] { newInitiator.getId() });
            }
        } else {
            for (Initiator oldInitiator : oldInitiatorObjects) {
                info("Deleting Initiator %s because it was not re-discovered and is not in use by any export groups", oldInitiator.getId());
                dbClient.removeObject(oldInitiator);
            }
        }
    }
    log.info("Number of undiscovered hosts: " + deletedHosts.size());
    Set<URI> incorrectDeletedHosts = Sets.newHashSet();
    for (URI deletedHost : deletedHosts) {
        Host host = dbClient.queryObject(Host.class, deletedHost);
        URI clusterId = host.getCluster();
        List<URI> clusterHosts = Lists.newArrayList();
        if (!NullColumnValueGetter.isNullURI(clusterId)) {
            clusterHosts = ComputeSystemHelper.getChildrenUris(dbClient, clusterId, Host.class, "cluster");
        }
        if (clusterHosts.contains(deletedHost) && deletedHosts.containsAll(clusterHosts)) {
            incorrectDeletedHosts.add(deletedHost);
            DiscoveryStatusUtils.markAsFailed(getModelClient(), host, "Error discovering host cluster", null);
            log.info("Host " + host.getId() + " is part of a cluster that was not re-discovered. Fail discovery and keep the host in our database");
        } else {
            Vcenter vcenter = ComputeSystemHelper.getHostVcenter(dbClient, host);
            EventUtils.createActionableEvent(dbClient, EventUtils.EventCode.UNASSIGN_HOST_FROM_VCENTER, host.getTenant(), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostVcenterUnassignLabel", vcenter == null ? "N/A" : vcenter.getLabel()), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostVcenterUnassignDescription", host.getLabel(), vcenter == null ? "N/A" : vcenter.getLabel()), ComputeSystemDialogProperties.getMessage("ComputeSystem.hostVcenterUnassignWarning"), host, Lists.newArrayList(host.getId(), host.getCluster()), EventUtils.hostVcenterUnassign, new Object[] { deletedHost }, EventUtils.hostVcenterUnassignDecline, new Object[] { deletedHost });
        }
    }
    // delete clusters that don't contain any hosts, don't have any exports, and don't have any pending events
    for (URI clusterId : deletedClusters) {
        List<URI> hostUris = ComputeSystemHelper.getChildrenUris(dbClient, clusterId, Host.class, "cluster");
        if (hostUris.isEmpty() && !ComputeSystemHelper.isClusterInExport(dbClient, clusterId) && EventUtils.findAffectedResourcePendingEvents(dbClient, clusterId).isEmpty()) {
            Cluster cluster = dbClient.queryObject(Cluster.class, clusterId);
            info("Deactivating Cluster: " + clusterId);
            ComputeSystemHelper.doDeactivateCluster(dbClient, cluster);
        } else {
            info("Unable to delete cluster " + clusterId);
        }
    }
}
Also used : Vcenter(com.emc.storageos.db.client.model.Vcenter) Initiator(com.emc.storageos.db.client.model.Initiator) Cluster(com.emc.storageos.db.client.model.Cluster) Host(com.emc.storageos.db.client.model.Host) VcenterDataCenter(com.emc.storageos.db.client.model.VcenterDataCenter) DataObject(com.emc.storageos.db.client.model.DataObject) URI(java.net.URI)

Example 12 with Vcenter

use of com.emc.storageos.db.client.model.Vcenter in project coprhd-controller by CoprHD.

the class AssetLoader method saveVCenter.

public Vcenter saveVCenter(String tenant, VCenterDef def) {
    Vcenter vcenter = new Vcenter();
    if (StringUtils.isNotBlank(tenant)) {
        vcenter.addAcl(BasePermissionsHelper.getTenantUsePermissionKey(tenant), ACL.USE.name());
    }
    vcenter.setLabel(def.name);
    vcenter.setIpAddress(def.hostname);
    vcenter.setPortNumber(def.port);
    vcenter.setUseSSL(def.useHttps);
    vcenter.setUsername(def.username);
    vcenter.setPassword(def.password);
    client.save(vcenter);
    return vcenter;
}
Also used : Vcenter(com.emc.storageos.db.client.model.Vcenter)

Example 13 with Vcenter

use of com.emc.storageos.db.client.model.Vcenter in project coprhd-controller by CoprHD.

the class VirtualMachineTest method createVCenterWithLabel.

private Vcenter createVCenterWithLabel(String label) {
    Vcenter model = new Vcenter();
    model.setId(URIUtil.createId(Vcenter.class));
    model.setLabel(label);
    model.setIpAddress("my hostname");
    model.setPassword("my password");
    model.setPortNumber(42);
    model.setUseSSL(true);
    model.setUsername("my username");
    return model;
}
Also used : Vcenter(com.emc.storageos.db.client.model.Vcenter)

Example 14 with Vcenter

use of com.emc.storageos.db.client.model.Vcenter in project coprhd-controller by CoprHD.

the class VirtualMachineTest method testFindByDatacenter.

@Test
public void testFindByDatacenter() throws Exception {
    _logger.info("Starting findByDatacenter test");
    ModelClient modelClient = getModelClient();
    Vcenter v2 = createVCenterWithLabel("v2");
    modelClient.save(v2);
    VcenterDataCenter dc2 = createDatacenterWithLabel("dc2");
    dc2.setVcenter(v2.getId());
    modelClient.save(dc2);
    VirtualMachine vm1 = createWithLabel("vm1");
    vm1.setDatacenterId(new NamedURI(dc2.getId(), dc2.getLabel()));
    modelClient.save(vm1);
    VcenterDataCenter dc3 = createDatacenterWithLabel("dc3");
    dc3.setVcenter(v2.getId());
    modelClient.save(dc3);
    VirtualMachine vm2 = createWithLabel("vm2");
    vm2.setDatacenterId(new NamedURI(dc3.getId(), dc3.getLabel()));
    modelClient.save(vm2);
    VirtualMachine vm3 = createWithLabel("vm3");
    vm3.setDatacenterId(new NamedURI(dc3.getId(), dc3.getLabel()));
    modelClient.save(vm3);
    List<VirtualMachine> virtualMachines = modelClient.virtualMachines().findByDatacenter(dc2.getId());
    Assert.assertNotNull(virtualMachines);
    Assert.assertEquals(1, virtualMachines.size());
    virtualMachines = modelClient.virtualMachines().findByDatacenter(dc3.getId());
    Assert.assertNotNull(virtualMachines);
    Assert.assertEquals(2, virtualMachines.size());
}
Also used : Vcenter(com.emc.storageos.db.client.model.Vcenter) NamedURI(com.emc.storageos.db.client.model.NamedURI) ModelClient(com.emc.sa.model.dao.ModelClient) VcenterDataCenter(com.emc.storageos.db.client.model.VcenterDataCenter) VirtualMachine(com.emc.storageos.db.client.model.uimodels.VirtualMachine) Test(org.junit.Test)

Example 15 with Vcenter

use of com.emc.storageos.db.client.model.Vcenter in project coprhd-controller by CoprHD.

the class BasePermissionsHelper method getTenantResourceTenantIds.

/**
 * Get tenant id from a project id retrieved from uri
 *
 * @param childId
 * @return
 */
public Set<URI> getTenantResourceTenantIds(String childId) {
    if (childId == null) {
        return null;
    }
    try {
        URI id = URI.create(childId);
        Vcenter ret = null;
        if (URIUtil.isType(id, Vcenter.class)) {
            ret = getObjectById(id, Vcenter.class);
        }
        return getUsageURIsFromAcls(ret.getAcls());
    } catch (DatabaseException ex) {
        throw SecurityException.fatals.failedGettingTenant(ex);
    }
}
Also used : Vcenter(com.emc.storageos.db.client.model.Vcenter) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) FatalDatabaseException(com.emc.storageos.db.exceptions.FatalDatabaseException)

Aggregations

Vcenter (com.emc.storageos.db.client.model.Vcenter)35 VcenterDataCenter (com.emc.storageos.db.client.model.VcenterDataCenter)25 Host (com.emc.storageos.db.client.model.Host)18 DeviceControllerException (com.emc.storageos.exceptions.DeviceControllerException)15 InternalException (com.emc.storageos.svcs.errorhandling.resources.InternalException)15 Cluster (com.emc.storageos.db.client.model.Cluster)12 VcenterControllerException (com.emc.storageos.vcentercontroller.exceptions.VcenterControllerException)11 VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)11 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)11 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)11 VcenterApiClient (com.emc.storageos.vcentercontroller.VcenterApiClient)10 HostSystem (com.vmware.vim25.mo.HostSystem)8 VCenterAPI (com.iwave.ext.vmware.VCenterAPI)7 URI (java.net.URI)7 ComputeSystemControllerException (com.emc.storageos.computesystemcontroller.exceptions.ComputeSystemControllerException)5 GetVcenter (com.emc.sa.service.vmware.tasks.GetVcenter)4 CoordinatorException (com.emc.storageos.coordinator.exceptions.CoordinatorException)4 ClientControllerException (com.emc.storageos.exceptions.ClientControllerException)4 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)4 ControllerException (com.emc.storageos.volumecontroller.ControllerException)4