use of com.cloud.dc.VsphereStoragePolicyVO 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;
}
Aggregations