use of com.cloud.hypervisor.vmware.VmwareDatacenterVO 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;
}
use of com.cloud.hypervisor.vmware.VmwareDatacenterVO in project cloudstack by apache.
the class ResultWrapper method getLoginInfo.
private LoginInfo getLoginInfo(long zoneId) {
VmwareDatacenterZoneMapVO vmwareDcZoneMap = vmwareDcZoneMapDao.findByZoneId(zoneId);
Long associatedVmwareDcId = vmwareDcZoneMap.getVmwareDcId();
VmwareDatacenterVO associatedVmwareDc = vmwareDcDao.findById(associatedVmwareDcId);
String host = associatedVmwareDc.getVcenterHost();
String username = associatedVmwareDc.getUser();
String password = associatedVmwareDc.getPassword();
return new LoginInfo(host, username, password);
}
use of com.cloud.hypervisor.vmware.VmwareDatacenterVO in project cloudstack by apache.
the class VmwareManagerImpl method importVsphereStoragePoliciesInternal.
public List<? extends VsphereStoragePolicy> importVsphereStoragePoliciesInternal(Long zoneId, Long vmwareDcId) {
// Get DC associated with this zone
VmwareDatacenterVO vmwareDatacenter = vmwareDcDao.findById(vmwareDcId);
String vmwareDcName = vmwareDatacenter.getVmwareDatacenterName();
String vCenterHost = vmwareDatacenter.getVcenterHost();
String userName = vmwareDatacenter.getUser();
String password = vmwareDatacenter.getPassword();
List<PbmProfile> storageProfiles = null;
try {
s_logger.debug(String.format("Importing vSphere Storage Policies for the vmware DC %d in zone %d", vmwareDcId, zoneId));
VmwareContext context = VmwareContextFactory.getContext(vCenterHost, userName, password);
PbmProfileManagerMO profileManagerMO = new PbmProfileManagerMO(context);
storageProfiles = profileManagerMO.getStorageProfiles();
s_logger.debug(String.format("Import vSphere Storage Policies for the vmware DC %d in zone %d is successful", vmwareDcId, zoneId));
} catch (Exception e) {
String msg = String.format("Unable to list storage profiles from DC %s due to : %s", vmwareDcName, VmwareHelper.getExceptionMessage(e));
s_logger.error(msg);
throw new CloudRuntimeException(msg);
}
for (PbmProfile storageProfile : storageProfiles) {
VsphereStoragePolicyVO storagePolicyVO = vsphereStoragePolicyDao.findByPolicyId(zoneId, storageProfile.getProfileId().getUniqueId());
if (storagePolicyVO == null) {
storagePolicyVO = new VsphereStoragePolicyVO(zoneId, storageProfile.getProfileId().getUniqueId(), storageProfile.getName(), storageProfile.getDescription());
vsphereStoragePolicyDao.persist(storagePolicyVO);
} else {
storagePolicyVO.setDescription(storageProfile.getDescription());
storagePolicyVO.setName(storageProfile.getName());
vsphereStoragePolicyDao.update(storagePolicyVO.getId(), storagePolicyVO);
}
}
List<VsphereStoragePolicyVO> allStoragePolicies = vsphereStoragePolicyDao.listAll();
List<PbmProfile> finalStorageProfiles = storageProfiles;
List<VsphereStoragePolicyVO> needToMarkRemoved = allStoragePolicies.stream().filter(existingPolicy -> !finalStorageProfiles.stream().anyMatch(storageProfile -> storageProfile.getProfileId().getUniqueId().equals(existingPolicy.getPolicyId()))).collect(Collectors.toList());
for (VsphereStoragePolicyVO storagePolicy : needToMarkRemoved) {
vsphereStoragePolicyDao.remove(storagePolicy.getId());
}
List<VsphereStoragePolicyVO> storagePolicies = vsphereStoragePolicyDao.listAll();
return storagePolicies;
}
use of com.cloud.hypervisor.vmware.VmwareDatacenterVO in project cloudstack by apache.
the class VmwareManagerImplTest method beforeTest.
@Before
public void beforeTest() {
VmwareDatacenterZoneMapVO vmwareDatacenterZoneMap = new VmwareDatacenterZoneMapVO();
vmwareDatacenterZoneMap.setZoneId(1);
vmwareDatacenterZoneMap.setVmwareDcId(1);
VmwareDatacenterVO vmwareDatacenterVO = new VmwareDatacenterVO(1, "some-guid", "some-name", "10.1.1.1", "username", "password");
Mockito.doReturn(vmwareDatacenterZoneMap).when(vmwareDatacenterZoneMapDao).findByZoneId(Mockito.anyLong());
Mockito.doReturn(vmwareDatacenterVO).when(vmwareDcDao).findById(Mockito.anyLong());
Mockito.doReturn(1L).when(updateVmwareDcCmd).getZoneId();
}
Aggregations