Search in sources :

Example 1 with VmwareDatacenterVO

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

the class AddVmwareDcCmd method execute.

@Override
public void execute() {
    try {
        VmwareDatacenterResponse response = new VmwareDatacenterResponse();
        VmwareDatacenterVO result = _vmwareDatacenterService.addVmwareDatacenter(this);
        if (result != null) {
            response.setId(result.getUuid());
            response.setName(result.getVmwareDatacenterName());
            response.setResponseName(getCommandName());
            response.setObjectName("vmwaredc");
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add VMware Datacenter to zone.");
        }
        this.setResponseObject(response);
    } catch (DiscoveryException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
    } catch (ResourceInUseException ex) {
        s_logger.warn("Exception: ", ex);
        ServerApiException e = new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
        for (String proxyObj : ex.getIdProxyList()) {
            e.addProxyObject(proxyObj);
        }
        throw e;
    } catch (IllegalArgumentException ex) {
        throw new IllegalArgumentException(ex.getMessage());
    } catch (CloudRuntimeException runtimeEx) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeEx.getMessage());
    }
}
Also used : VmwareDatacenterResponse(org.apache.cloudstack.api.response.VmwareDatacenterResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceInUseException(com.cloud.exception.ResourceInUseException) DiscoveryException(com.cloud.exception.DiscoveryException)

Example 2 with VmwareDatacenterVO

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

the class VMwareGuru method getDatacenterMO.

/**
 * Get Vmware datacenter MO
 */
private DatacenterMO getDatacenterMO(long zoneId) throws Exception {
    VmwareDatacenterVO vmwareDatacenter = getVmwareDatacenter(zoneId);
    VmwareContext context = VmwareContextFactory.getContext(vmwareDatacenter.getVcenterHost(), vmwareDatacenter.getUser(), vmwareDatacenter.getPassword());
    DatacenterMO dcMo = new DatacenterMO(context, vmwareDatacenter.getVmwareDatacenterName());
    ManagedObjectReference dcMor = dcMo.getMor();
    if (dcMor == null) {
        String msg = "Error while getting Vmware datacenter " + vmwareDatacenter.getVmwareDatacenterName();
        s_logger.error(msg);
        throw new InvalidParameterValueException(msg);
    }
    return dcMo;
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 3 with VmwareDatacenterVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterVO 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 4 with VmwareDatacenterVO

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

the class VmwareManagerImpl method updateVmwareDatacenter.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ZONE_EDIT, eventDescription = "updating VMware datacenter")
public VmwareDatacenter updateVmwareDatacenter(UpdateVmwareDcCmd cmd) {
    final Long zoneId = cmd.getZoneId();
    final String userName = cmd.getUsername();
    final String password = cmd.getPassword();
    final String vCenterHost = cmd.getVcenter();
    final String vmwareDcName = cmd.getName();
    final Boolean isRecursive = cmd.isRecursive();
    final VmwareDatacenterZoneMap vdcMap = vmwareDatacenterZoneMapDao.findByZoneId(zoneId);
    final VmwareDatacenterVO vmwareDc = vmwareDcDao.findById(vdcMap.getVmwareDcId());
    if (vmwareDc == null) {
        throw new CloudRuntimeException("VMWare datacenter does not exist by provided ID");
    }
    final String oldVCenterHost = vmwareDc.getVcenterHost();
    if (StringUtils.isNotEmpty(userName)) {
        vmwareDc.setUser(userName);
    }
    if (StringUtils.isNotEmpty(password)) {
        vmwareDc.setPassword(password);
    }
    if (StringUtils.isNotEmpty(vCenterHost)) {
        vmwareDc.setVcenterHost(vCenterHost);
    }
    if (StringUtils.isNotEmpty(vmwareDcName)) {
        vmwareDc.setVmwareDatacenterName(vmwareDcName);
    }
    vmwareDc.setGuid(String.format("%s@%s", vmwareDc.getVmwareDatacenterName(), vmwareDc.getVcenterHost()));
    return Transaction.execute(new TransactionCallback<VmwareDatacenter>() {

        @Override
        public VmwareDatacenter doInTransaction(TransactionStatus status) {
            if (vmwareDcDao.update(vmwareDc.getId(), vmwareDc)) {
                if (isRecursive) {
                    for (final Cluster cluster : clusterDao.listByDcHyType(zoneId, Hypervisor.HypervisorType.VMware.toString())) {
                        final Map<String, String> clusterDetails = clusterDetailsDao.findDetails(cluster.getId());
                        clusterDetails.put("username", vmwareDc.getUser());
                        clusterDetails.put("password", vmwareDc.getPassword());
                        final String clusterUrl = clusterDetails.get("url");
                        if (!oldVCenterHost.equals(vmwareDc.getVcenterHost()) && StringUtils.isNotEmpty(clusterUrl)) {
                            clusterDetails.put("url", clusterUrl.replace(oldVCenterHost, vmwareDc.getVcenterHost()));
                        }
                        clusterDetailsDao.persist(cluster.getId(), clusterDetails);
                    }
                    for (final Host host : hostDao.listAllHostsByZoneAndHypervisorType(zoneId, HypervisorType.VMware)) {
                        final Map<String, String> hostDetails = hostDetailsDao.findDetails(host.getId());
                        hostDetails.put("username", vmwareDc.getUser());
                        hostDetails.put("password", vmwareDc.getPassword());
                        final String hostGuid = hostDetails.get("guid");
                        if (StringUtils.isNotEmpty(hostGuid)) {
                            hostDetails.put("guid", hostGuid.replace(oldVCenterHost, vmwareDc.getVcenterHost()));
                        }
                        hostDetailsDao.persist(host.getId(), hostDetails);
                    }
                }
                importVsphereStoragePoliciesInternal(zoneId, vmwareDc.getId());
                return vmwareDc;
            }
            return null;
        }
    });
}
Also used : VmwareDatacenterZoneMap(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMap) TransactionStatus(com.cloud.utils.db.TransactionStatus) Cluster(com.cloud.org.Cluster) Host(com.cloud.host.Host) ManagementServerHost(org.apache.cloudstack.management.ManagementServerHost) VmwareDatacenterVO(com.cloud.hypervisor.vmware.VmwareDatacenterVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VmwareDatacenter(com.cloud.hypervisor.vmware.VmwareDatacenter) VmwareDatacenterZoneMap(com.cloud.hypervisor.vmware.VmwareDatacenterZoneMap) Map(java.util.Map) HashMap(java.util.HashMap) ActionEvent(com.cloud.event.ActionEvent)

Example 5 with VmwareDatacenterVO

use of com.cloud.hypervisor.vmware.VmwareDatacenterVO 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)

Aggregations

VmwareDatacenterVO (com.cloud.hypervisor.vmware.VmwareDatacenterVO)9 VmwareDatacenterZoneMapVO (com.cloud.hypervisor.vmware.VmwareDatacenterZoneMapVO)6 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 ResourceInUseException (com.cloud.exception.ResourceInUseException)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 DiscoveryException (com.cloud.exception.DiscoveryException)3 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)3 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)3 ActionEvent (com.cloud.event.ActionEvent)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2 DiscoveredWithErrorException (com.cloud.exception.DiscoveredWithErrorException)2 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)2 Host (com.cloud.host.Host)2 TransactionStatus (com.cloud.utils.db.TransactionStatus)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 RemoteException (java.rmi.RemoteException)2 CollectionUtils (com.amazonaws.util.CollectionUtils)1 AgentManager (com.cloud.agent.AgentManager)1 Listener (com.cloud.agent.Listener)1 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1