}
@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 = _vmwareDcZoneMapDao.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());