Search in sources :

Example 1 with VmwareDatacenterZoneMapVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO in project cloudstack by apache.

the class VMwareGuru method getVmwareDatacenter.

/**
 * Get vmware datacenter mapped to the zoneId
 */
private VmwareDatacenterVO getVmwareDatacenter(long zoneId) {
    VmwareDatacenterZoneMapVO zoneMap = vmwareDatacenterZoneMapDao.findByZoneId(zoneId);
    long vmwareDcId = zoneMap.getVmwareDcId();
    return vmwareDatacenterDao.findById(vmwareDcId);
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO)

Example 2 with VmwareDatacenterZoneMapVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO in project cloudstack by apache.

the class VmwareManagerImpl method listVmwareDatacenters.

@Override
public List<? extends VmwareDatacenter> listVmwareDatacenters(ListVmwareDcsCmd cmd) throws CloudRuntimeException, InvalidParameterValueException {
    Long zoneId = cmd.getZoneId();
    List<VmwareDatacenterVO> vmwareDcList = new ArrayList<VmwareDatacenterVO>();
    VmwareDatacenterZoneMapVO vmwareDcZoneMap;
    VmwareDatacenterVO vmwareDatacenter;
    long vmwareDcId;
    // Validate if zone id parameter passed to API is valid
    doesZoneExist(zoneId);
    // Check if zone is associated with VMware DC
    vmwareDcZoneMap = vmwareDatacenterZoneMapDao.findByZoneId(zoneId);
    if (vmwareDcZoneMap == null) {
        return null;
    }
    // Retrieve details of VMware DC associated with zone.
    vmwareDcId = vmwareDcZoneMap.getVmwareDcId();
    vmwareDatacenter = vmwareDcDao.findById(vmwareDcId);
    vmwareDcList.add(vmwareDatacenter);
    // Returning list of VmwareDatacenterVO objects, in-line with future requirements, if any, like participation of multiple VMware DCs in a zone.
    return vmwareDcList;
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) ArrayList(java.util.ArrayList)

Example 3 with VmwareDatacenterZoneMapVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO in project cloudstack by apache.

the class VmwareManagerImpl method importVsphereStoragePolicies.

@Override
public List<? extends VsphereStoragePolicy> importVsphereStoragePolicies(ImportVsphereStoragePoliciesCmd cmd) {
    Long zoneId = cmd.getZoneId();
    // Validate Id of zone
    doesZoneExist(zoneId);
    final VmwareDatacenterZoneMapVO vmwareDcZoneMap = vmwareDatacenterZoneMapDao.findByZoneId(zoneId);
    // Check if zone is associated with VMware DC
    if (vmwareDcZoneMap == null) {
        throw new CloudRuntimeException("Zone " + zoneId + " is not associated with any VMware datacenter.");
    }
    final long vmwareDcId = vmwareDcZoneMap.getVmwareDcId();
    return importVsphereStoragePoliciesInternal(zoneId, vmwareDcId);
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 4 with VmwareDatacenterZoneMapVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO in project cloudstack by apache.

the class VmwareManagerImpl method addVmwareDatacenter.

@Override
@DB
public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws ResourceInUseException {
    VmwareDatacenterVO vmwareDc = null;
    Long zoneId = cmd.getZoneId();
    String userName = cmd.getUsername();
    String password = cmd.getPassword();
    String vCenterHost = cmd.getVcenter();
    String vmwareDcName = cmd.getName();
    // Validate username, password, VMware DC name and vCenter
    if (userName == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter username.");
    }
    if (password == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter username.");
    }
    if (vmwareDcName == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter name. Please provide valid VMware datacenter name.");
    }
    if (vCenterHost == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter name. " + "Please provide valid VMware vCenter server's IP address or fully qualified domain name.");
    }
    if (zoneId == null) {
        throw new InvalidParameterValueException("Missing or invalid parameter name. " + "Please provide valid zone id.");
    }
    // Zone validation
    validateZone(zoneId);
    VmwareDatacenterZoneMapVO vmwareDcZoneMap = vmwareDatacenterZoneMapDao.findByZoneId(zoneId);
    // Check if zone is associated with VMware DC
    if (vmwareDcZoneMap != null) {
        // Check if the associated VMware DC matches the one specified in API params
        // This check would yield success as the association exists between same entities (zone and VMware DC)
        // This scenario would result in if the API addVmwareDc is called more than once with same parameters.
        Long associatedVmwareDcId = vmwareDcZoneMap.getVmwareDcId();
        VmwareDatacenterVO associatedVmwareDc = vmwareDcDao.findById(associatedVmwareDcId);
        if (associatedVmwareDc.getVcenterHost().equalsIgnoreCase(vCenterHost) && associatedVmwareDc.getVmwareDatacenterName().equalsIgnoreCase(vmwareDcName)) {
            s_logger.info("Ignoring API call addVmwareDc, because VMware DC " + vCenterHost + "/" + vmwareDcName + " is already associated with specified zone with id " + zoneId);
            return associatedVmwareDc;
        } else {
            throw new CloudRuntimeException("Zone " + zoneId + " is already associated with a VMware datacenter. " + "Only 1 VMware DC can be associated with a zone.");
        }
    }
    // Zone validation to check if the zone already has resources.
    // Association of VMware DC to zone is not allowed if zone already has resources added.
    validateZoneWithResources(zoneId, "add VMware datacenter to zone");
    // Check if DC is already part of zone
    // In that case vmware_data_center table should have the DC
    vmwareDc = vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + vCenterHost);
    if (vmwareDc != null) {
        throw new ResourceInUseException("This DC is already part of other CloudStack zone(s). Cannot add this DC to more zones.");
    }
    VmwareContext context = null;
    DatacenterMO dcMo = null;
    String dcCustomFieldValue;
    boolean addDcCustomFieldDef = false;
    boolean dcInUse = false;
    String guid;
    ManagedObjectReference dcMor;
    try {
        context = VmwareContextFactory.create(vCenterHost, userName, password);
        // Check if DC exists on vCenter
        dcMo = new DatacenterMO(context, vmwareDcName);
        dcMor = dcMo.getMor();
        if (dcMor == null) {
            String msg = "Unable to find VMware DC " + vmwareDcName + " in vCenter " + vCenterHost + ". ";
            s_logger.error(msg);
            throw new InvalidParameterValueException(msg);
        }
        // Check if DC is already associated with another cloudstack deployment
        // Get custom field property cloud.zone over this DC
        guid = vmwareDcName + "@" + vCenterHost;
        dcCustomFieldValue = dcMo.getCustomFieldValue(CustomFieldConstants.CLOUD_ZONE);
        if (dcCustomFieldValue == null) {
            addDcCustomFieldDef = true;
        }
        dcInUse = Boolean.parseBoolean(dcCustomFieldValue);
        if (dcInUse) {
            throw new ResourceInUseException("This DC is being managed by other CloudStack deployment. Cannot add this DC to zone.");
        }
        // Add DC to database into vmware_data_center table
        vmwareDc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, userName, password);
        vmwareDc = vmwareDcDao.persist(vmwareDc);
        // Map zone with vmware datacenter
        vmwareDcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDc.getId());
        vmwareDcZoneMap = vmwareDatacenterZoneMapDao.persist(vmwareDcZoneMap);
        // Set custom field for this DC
        if (addDcCustomFieldDef) {
            dcMo.ensureCustomFieldDef(CustomFieldConstants.CLOUD_ZONE);
        }
        dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "true");
    } catch (Throwable e) {
        String msg = "Failed to add VMware DC to zone ";
        if (e instanceof RemoteException) {
            msg = "Encountered remote exception at vCenter. " + VmwareHelper.getExceptionMessage(e);
        } else {
            msg += "due to : " + e.getMessage();
        }
        throw new CloudRuntimeException(msg);
    } finally {
        if (context != null) {
            context.close();
        }
        context = null;
    }
    importVsphereStoragePoliciesInternal(zoneId, vmwareDc.getId());
    return vmwareDc;
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) RemoteException(java.rmi.RemoteException) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DB(com.cloud.utils.db.DB)

Example 5 with VmwareDatacenterZoneMapVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO in project cloudstack by apache.

the class VmwareManagerImpl method removeVmwareDatacenter.

@Override
public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUseException {
    Long zoneId = cmd.getZoneId();
    // Validate Id of zone
    doesZoneExist(zoneId);
    // Zone validation to check if the zone already has resources.
    // Association of VMware DC to zone is not allowed if zone already has resources added.
    validateZoneWithResources(zoneId, "remove VMware datacenter to zone");
    // Get DC associated with this zone
    VmwareDatacenterVO vmwareDatacenter;
    String vmwareDcName;
    String vCenterHost;
    String userName;
    String password;
    DatacenterMO dcMo = null;
    final VmwareDatacenterZoneMapVO vmwareDcZoneMap = vmwareDatacenterZoneMapDao.findByZoneId(zoneId);
    // Check if zone is associated with VMware DC
    if (vmwareDcZoneMap == null) {
        throw new CloudRuntimeException("Zone " + zoneId + " is not associated with any VMware datacenter.");
    }
    final long vmwareDcId = vmwareDcZoneMap.getVmwareDcId();
    vmwareDatacenter = vmwareDcDao.findById(vmwareDcId);
    vmwareDcName = vmwareDatacenter.getVmwareDatacenterName();
    vCenterHost = vmwareDatacenter.getVcenterHost();
    userName = vmwareDatacenter.getUser();
    password = vmwareDatacenter.getPassword();
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            // Remove the VMware datacenter entry in table vmware_data_center
            vmwareDcDao.remove(vmwareDcId);
            // Remove the map entry in table vmware_data_center_zone_map
            vmwareDatacenterZoneMapDao.remove(vmwareDcZoneMap.getId());
        }
    });
    // Construct context
    VmwareContext context = null;
    try {
        context = VmwareContextFactory.create(vCenterHost, userName, password);
        // Check if DC exists on vCenter
        try {
            dcMo = new DatacenterMO(context, vmwareDcName);
        } catch (Throwable t) {
            String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost;
            s_logger.error(msg);
            throw new DiscoveryException(msg);
        }
        assert (dcMo != null);
        // Reset custom field property cloud.zone over this DC
        dcMo.setCustomFieldValue(CustomFieldConstants.CLOUD_ZONE, "false");
        s_logger.info("Sucessfully reset custom field property cloud.zone over DC " + vmwareDcName);
    } catch (Exception e) {
        String msg = "Unable to reset custom field property cloud.zone over DC " + vmwareDcName + " due to : " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg);
        throw new CloudRuntimeException(msg);
    } finally {
        if (context != null) {
            context.close();
        }
        context = null;
    }
    return true;
}
Also used : VmwareDatacenterZoneMapVO(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DiscoveredWithErrorException(com.cloud.exception.DiscoveredWithErrorException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ResourceInUseException(com.cloud.exception.ResourceInUseException) URISyntaxException(java.net.URISyntaxException) DiscoveryException(com.cloud.exception.DiscoveryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) RemoteException(java.rmi.RemoteException) ConfigurationException(javax.naming.ConfigurationException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DiscoveryException(com.cloud.exception.DiscoveryException) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Aggregations

VmwareDatacenterZoneMapVO (com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO)7 VmwareDatacenterVO (com.cloud.hypervisor.vmware.VmwareDatacenterVO)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 ResourceInUseException (com.cloud.exception.ResourceInUseException)2 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)2 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)2 RemoteException (java.rmi.RemoteException)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)1 DiscoveredWithErrorException (com.cloud.exception.DiscoveredWithErrorException)1 DiscoveryException (com.cloud.exception.DiscoveryException)1 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)1 DB (com.cloud.utils.db.DB)1 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1