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());
}
}
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;
}
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;
}
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;
}
});
}
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;
}
Aggregations