mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	Adding vSphere storage policy to disk on start command and attach volume command
This commit is contained in:
		
							parent
							
								
									1e4e5cf4c0
								
							
						
					
					
						commit
						46b5322d9b
					
				| @ -64,6 +64,7 @@ public class VolumeObjectTO implements DataTO { | |||||||
|     private boolean directDownload; |     private boolean directDownload; | ||||||
|     private boolean deployAsIs; |     private boolean deployAsIs; | ||||||
|     private String updatedDataStoreUUID; |     private String updatedDataStoreUUID; | ||||||
|  |     private String vSphereStoragePolicyId; | ||||||
| 
 | 
 | ||||||
|     public VolumeObjectTO() { |     public VolumeObjectTO() { | ||||||
| 
 | 
 | ||||||
| @ -105,6 +106,7 @@ public class VolumeObjectTO implements DataTO { | |||||||
|         this.migrationOptions = volume.getMigrationOptions(); |         this.migrationOptions = volume.getMigrationOptions(); | ||||||
|         this.directDownload = volume.isDirectDownload(); |         this.directDownload = volume.isDirectDownload(); | ||||||
|         this.deployAsIs = volume.isDeployAsIs(); |         this.deployAsIs = volume.isDeployAsIs(); | ||||||
|  |         this.vSphereStoragePolicyId = volume.getvSphereStoragePolicyId(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public String getUuid() { |     public String getUuid() { | ||||||
| @ -329,4 +331,11 @@ public class VolumeObjectTO implements DataTO { | |||||||
|         this.updatedDataStoreUUID = updatedDataStoreUUID; |         this.updatedDataStoreUUID = updatedDataStoreUUID; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public String getvSphereStoragePolicyId() { | ||||||
|  |         return vSphereStoragePolicyId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setvSphereStoragePolicyId(String vSphereStoragePolicyId) { | ||||||
|  |         this.vSphereStoragePolicyId = vSphereStoragePolicyId; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -87,4 +87,6 @@ public interface VolumeInfo extends DataObject, Volume { | |||||||
|     boolean isDeployAsIs(); |     boolean isDeployAsIs(); | ||||||
| 
 | 
 | ||||||
|     String getDeployAsIsConfiguration(); |     String getDeployAsIsConfiguration(); | ||||||
|  | 
 | ||||||
|  |     public String getvSphereStoragePolicyId(); | ||||||
| } | } | ||||||
|  | |||||||
| @ -20,12 +20,18 @@ import java.util.Date; | |||||||
| 
 | 
 | ||||||
| import javax.inject.Inject; | import javax.inject.Inject; | ||||||
| 
 | 
 | ||||||
|  | import com.cloud.dc.VsphereStoragePolicyVO; | ||||||
|  | import com.cloud.dc.dao.VsphereStoragePolicyDao; | ||||||
|  | import com.cloud.service.dao.ServiceOfferingDetailsDao; | ||||||
| import com.cloud.storage.MigrationOptions; | import com.cloud.storage.MigrationOptions; | ||||||
| import com.cloud.storage.VMTemplateVO; | import com.cloud.storage.VMTemplateVO; | ||||||
| import com.cloud.storage.VolumeDetailVO; | import com.cloud.storage.VolumeDetailVO; | ||||||
| import com.cloud.storage.dao.VMTemplateDao; | import com.cloud.storage.dao.VMTemplateDao; | ||||||
| import com.cloud.storage.dao.VolumeDetailsDao; | import com.cloud.storage.dao.VolumeDetailsDao; | ||||||
| import com.cloud.vm.VmDetailConstants; | import com.cloud.vm.VmDetailConstants; | ||||||
|  | import org.apache.cloudstack.api.ApiConstants; | ||||||
|  | import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao; | ||||||
|  | import org.apache.commons.lang.StringUtils; | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| 
 | 
 | ||||||
| import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectInStore; | import org.apache.cloudstack.engine.subsystem.api.storage.DataObjectInStore; | ||||||
| @ -81,9 +87,17 @@ public class VolumeObject implements VolumeInfo { | |||||||
|     VMTemplateDao templateDao; |     VMTemplateDao templateDao; | ||||||
|     @Inject |     @Inject | ||||||
|     VolumeDetailsDao volumeDetailsDao; |     VolumeDetailsDao volumeDetailsDao; | ||||||
|  |     @Inject | ||||||
|  |     ServiceOfferingDetailsDao serviceOfferingDetailsDao; | ||||||
|  |     @Inject | ||||||
|  |     DiskOfferingDetailsDao diskOfferingDetailsDao; | ||||||
|  |     @Inject | ||||||
|  |     VsphereStoragePolicyDao vsphereStoragePolicyDao; | ||||||
|  | 
 | ||||||
|     private Object payload; |     private Object payload; | ||||||
|     private MigrationOptions migrationOptions; |     private MigrationOptions migrationOptions; | ||||||
|     private boolean directDownload; |     private boolean directDownload; | ||||||
|  |     private String vSphereStoragePolicyId; | ||||||
| 
 | 
 | ||||||
|     public VolumeObject() { |     public VolumeObject() { | ||||||
|         _volStateMachine = Volume.State.getStateMachine(); |         _volStateMachine = Volume.State.getStateMachine(); | ||||||
| @ -780,6 +794,29 @@ public class VolumeObject implements VolumeInfo { | |||||||
| 
 | 
 | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public String getvSphereStoragePolicyId() { | ||||||
|  |         if (StringUtils.isEmpty(vSphereStoragePolicyId)) { | ||||||
|  |             if (Volume.Type.ROOT == getVolumeType()) { | ||||||
|  |                 Long vmId = volumeVO.getInstanceId(); | ||||||
|  |                 if (vmId != null) { | ||||||
|  |                     VMInstanceVO vm = vmInstanceDao.findByIdIncludingRemoved(vmId); | ||||||
|  |                     String storagePolicyVOid = serviceOfferingDetailsDao.getDetail(vm.getServiceOfferingId(), | ||||||
|  |                             ApiConstants.STORAGE_POLICY); | ||||||
|  |                     VsphereStoragePolicyVO vsphereStoragePolicyVO = vsphereStoragePolicyDao.findById(Long.parseLong(storagePolicyVOid)); | ||||||
|  |                     vSphereStoragePolicyId = vsphereStoragePolicyVO.getPolicyId(); | ||||||
|  | 
 | ||||||
|  |                 } | ||||||
|  |             } else { | ||||||
|  |                 String storagePolicyVOid = diskOfferingDetailsDao.getDetail(volumeVO.getDiskOfferingId(), | ||||||
|  |                         ApiConstants.STORAGE_POLICY); | ||||||
|  |                 VsphereStoragePolicyVO vsphereStoragePolicyVO = vsphereStoragePolicyDao.findById(Long.parseLong(storagePolicyVOid)); | ||||||
|  |                 vSphereStoragePolicyId = vsphereStoragePolicyVO.getPolicyId(); | ||||||
|  | 
 | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return vSphereStoragePolicyId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public ImageFormat getFormat() { |     public ImageFormat getFormat() { | ||||||
|         return volumeVO.getFormat(); |         return volumeVO.getFormat(); | ||||||
|  | |||||||
| @ -219,6 +219,7 @@ import com.cloud.hypervisor.vmware.mo.HostMO; | |||||||
| import com.cloud.hypervisor.vmware.mo.HostStorageSystemMO; | import com.cloud.hypervisor.vmware.mo.HostStorageSystemMO; | ||||||
| import com.cloud.hypervisor.vmware.mo.HypervisorHostHelper; | import com.cloud.hypervisor.vmware.mo.HypervisorHostHelper; | ||||||
| import com.cloud.hypervisor.vmware.mo.NetworkDetails; | import com.cloud.hypervisor.vmware.mo.NetworkDetails; | ||||||
|  | import com.cloud.hypervisor.vmware.mo.PbmProfileManagerMO; | ||||||
| import com.cloud.hypervisor.vmware.mo.TaskMO; | import com.cloud.hypervisor.vmware.mo.TaskMO; | ||||||
| import com.cloud.hypervisor.vmware.mo.StoragepodMO; | import com.cloud.hypervisor.vmware.mo.StoragepodMO; | ||||||
| import com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType; | import com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType; | ||||||
| @ -289,6 +290,7 @@ import com.vmware.vim25.HostInternetScsiHba; | |||||||
| import com.vmware.vim25.HostPortGroupSpec; | import com.vmware.vim25.HostPortGroupSpec; | ||||||
| import com.vmware.vim25.ManagedObjectReference; | import com.vmware.vim25.ManagedObjectReference; | ||||||
| import com.vmware.vim25.NasDatastoreInfo; | import com.vmware.vim25.NasDatastoreInfo; | ||||||
|  | import com.vmware.vim25.VirtualMachineDefinedProfileSpec; | ||||||
| import com.vmware.vim25.ObjectContent; | import com.vmware.vim25.ObjectContent; | ||||||
| import com.vmware.vim25.OptionValue; | import com.vmware.vim25.OptionValue; | ||||||
| import com.vmware.vim25.PerfCounterInfo; | import com.vmware.vim25.PerfCounterInfo; | ||||||
| @ -1798,6 +1800,11 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa | |||||||
|         VirtualMachineFileInfo existingVmFileInfo = null; |         VirtualMachineFileInfo existingVmFileInfo = null; | ||||||
|         VirtualMachineFileLayoutEx existingVmFileLayout = null; |         VirtualMachineFileLayoutEx existingVmFileLayout = null; | ||||||
|         List<DatastoreMO> existingDatastores = new ArrayList<DatastoreMO>(); |         List<DatastoreMO> existingDatastores = new ArrayList<DatastoreMO>(); | ||||||
|  |         String diskStoragePolicyId = null; | ||||||
|  |         String vmStoragePolicyId = null; | ||||||
|  |         VirtualMachineDefinedProfileSpec diskProfileSpec = null; | ||||||
|  |         VirtualMachineDefinedProfileSpec vmProfileSpec = null; | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
|         DeployAsIsInfoTO deployAsIsInfo = vmSpec.getDeployAsIsInfo(); |         DeployAsIsInfoTO deployAsIsInfo = vmSpec.getDeployAsIsInfo(); | ||||||
|         boolean deployAsIs = deployAsIsInfo != null; |         boolean deployAsIs = deployAsIsInfo != null; | ||||||
| @ -2218,8 +2225,20 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa | |||||||
| 
 | 
 | ||||||
|                     VirtualDevice device = VmwareHelper.prepareDiskDevice(vmMo, null, controllerKey, diskChain, volumeDsDetails.first(), deviceNumber, i + 1); |                     VirtualDevice device = VmwareHelper.prepareDiskDevice(vmMo, null, controllerKey, diskChain, volumeDsDetails.first(), deviceNumber, i + 1); | ||||||
| 
 | 
 | ||||||
|                     if (vol.getType() == Volume.Type.ROOT) |                     diskStoragePolicyId = volumeTO.getvSphereStoragePolicyId(); | ||||||
|  |                     if (!StringUtils.isEmpty(diskStoragePolicyId)) { | ||||||
|  |                         PbmProfileManagerMO profMgrMo = new PbmProfileManagerMO(context); | ||||||
|  |                         diskProfileSpec = profMgrMo.getProfileSpec(diskStoragePolicyId); | ||||||
|  |                         deviceConfigSpecArray[i].getProfile().add(diskProfileSpec); | ||||||
|  |                         if (s_logger.isDebugEnabled()) { | ||||||
|  |                             s_logger.debug(String.format("Adding vSphere storage profile: %s to virtual disk [%s]", diskStoragePolicyId, _gson.toJson(device))); | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                     if (vol.getType() == Volume.Type.ROOT) { | ||||||
|                         rootDiskTO = vol; |                         rootDiskTO = vol; | ||||||
|  |                         vmStoragePolicyId = diskStoragePolicyId; | ||||||
|  |                         vmProfileSpec = diskProfileSpec; | ||||||
|  |                     } | ||||||
|                     deviceConfigSpecArray[i].setDevice(device); |                     deviceConfigSpecArray[i].setDevice(device); | ||||||
|                     deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.ADD); |                     deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.ADD); | ||||||
| 
 | 
 | ||||||
| @ -2393,6 +2412,12 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa | |||||||
|                 setBootOptions(vmSpec, bootMode, vmConfigSpec); |                 setBootOptions(vmSpec, bootMode, vmConfigSpec); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|  |             if (!StringUtils.isEmpty(vmStoragePolicyId)) { | ||||||
|  |                 vmConfigSpec.getVmProfile().add(vmProfileSpec); | ||||||
|  |                 if (s_logger.isTraceEnabled()) { | ||||||
|  |                     s_logger.trace(String.format("Configuring the VM %s with storage policy: %s", vmInternalCSName, vmStoragePolicyId)); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|             // |             // | ||||||
|             // Configure VM |             // Configure VM | ||||||
|             // |             // | ||||||
|  | |||||||
| @ -2052,6 +2052,7 @@ public class VmwareStorageProcessor implements StorageProcessor { | |||||||
|         VolumeObjectTO volumeTO = (VolumeObjectTO)disk.getData(); |         VolumeObjectTO volumeTO = (VolumeObjectTO)disk.getData(); | ||||||
|         DataStoreTO primaryStore = volumeTO.getDataStore(); |         DataStoreTO primaryStore = volumeTO.getDataStore(); | ||||||
|         String volumePath = volumeTO.getPath(); |         String volumePath = volumeTO.getPath(); | ||||||
|  |         String storagePolicyId = volumeTO.getvSphereStoragePolicyId(); | ||||||
| 
 | 
 | ||||||
|         String vmdkPath = isManaged ? resource.getVmdkPath(volumePath) : null; |         String vmdkPath = isManaged ? resource.getVmdkPath(volumePath) : null; | ||||||
| 
 | 
 | ||||||
| @ -2193,7 +2194,7 @@ public class VmwareStorageProcessor implements StorageProcessor { | |||||||
|                     diskController = vmMo.getRecommendedDiskController(null); |                     diskController = vmMo.getRecommendedDiskController(null); | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs, diskController); |                 vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs, diskController, storagePolicyId); | ||||||
|                 VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder(); |                 VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder(); | ||||||
|                 VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumePath, dsMo.getName()); |                 VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volumePath, dsMo.getName()); | ||||||
|                 chainInfo = _gson.toJson(diskInfo); |                 chainInfo = _gson.toJson(diskInfo); | ||||||
|  | |||||||
| @ -27,6 +27,7 @@ import com.vmware.pbm.PbmProfileResourceType; | |||||||
| import com.vmware.pbm.PbmProfileResourceTypeEnum; | import com.vmware.pbm.PbmProfileResourceTypeEnum; | ||||||
| import com.vmware.vim25.ManagedObjectReference; | import com.vmware.vim25.ManagedObjectReference; | ||||||
| 
 | 
 | ||||||
|  | import com.vmware.vim25.VirtualMachineDefinedProfileSpec; | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| 
 | 
 | ||||||
| import java.util.Collections; | import java.util.Collections; | ||||||
| @ -89,6 +90,15 @@ public class PbmProfileManagerMO extends BaseMO { | |||||||
|         resourceType.setResourceType(PbmProfileResourceTypeEnum.STORAGE.value()); |         resourceType.setResourceType(PbmProfileResourceTypeEnum.STORAGE.value()); | ||||||
|         return resourceType; |         return resourceType; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     public VirtualMachineDefinedProfileSpec getProfileSpec(String profileId) throws Exception { | ||||||
|  |         VirtualMachineDefinedProfileSpec profileSpec = new VirtualMachineDefinedProfileSpec(); | ||||||
|  |         PbmProfile profile = getStorageProfile(profileId); | ||||||
|  |         profileSpec.setProfileId(profile.getProfileId().getUniqueId()); | ||||||
|  |         return profileSpec; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -105,6 +105,7 @@ import com.vmware.vim25.VirtualMachineSnapshotInfo; | |||||||
| import com.vmware.vim25.VirtualMachineSnapshotTree; | import com.vmware.vim25.VirtualMachineSnapshotTree; | ||||||
| import com.vmware.vim25.VirtualSCSIController; | import com.vmware.vim25.VirtualSCSIController; | ||||||
| import com.vmware.vim25.VirtualSCSISharing; | import com.vmware.vim25.VirtualSCSISharing; | ||||||
|  | import com.vmware.vim25.VirtualMachineDefinedProfileSpec; | ||||||
| 
 | 
 | ||||||
| import com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo; | import com.cloud.hypervisor.vmware.mo.SnapshotDescriptor.SnapshotInfo; | ||||||
| import com.cloud.hypervisor.vmware.util.VmwareContext; | import com.cloud.hypervisor.vmware.util.VmwareContext; | ||||||
| @ -1293,10 +1294,10 @@ public class VirtualMachineMO extends BaseMO { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs) throws Exception { |     public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs) throws Exception { | ||||||
|         attachDisk(vmdkDatastorePathChain, morDs, null); |         attachDisk(vmdkDatastorePathChain, morDs, null, null); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController) throws Exception { |     public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController, String storagePolicyId) throws Exception { | ||||||
| 
 | 
 | ||||||
|         if(s_logger.isTraceEnabled()) |         if(s_logger.isTraceEnabled()) | ||||||
|             s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " |             s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " | ||||||
| @ -1337,7 +1338,14 @@ public class VirtualMachineMO extends BaseMO { | |||||||
| 
 | 
 | ||||||
|             deviceConfigSpec.setDevice(newDisk); |             deviceConfigSpec.setDevice(newDisk); | ||||||
|             deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD); |             deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD); | ||||||
| 
 |             if (!StringUtils.isEmpty(storagePolicyId)) { | ||||||
|  |                 PbmProfileManagerMO profMgrMo = new PbmProfileManagerMO(getContext()); | ||||||
|  |                 VirtualMachineDefinedProfileSpec diskProfileSpec = profMgrMo.getProfileSpec(storagePolicyId); | ||||||
|  |                 deviceConfigSpec.getProfile().add(diskProfileSpec); | ||||||
|  |                 if (s_logger.isDebugEnabled()) { | ||||||
|  |                     s_logger.debug(String.format("Adding vSphere storage profile: %s to volume [%s]", storagePolicyId, vmdkDatastorePathChain[0])); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|             reConfigSpec.getDeviceChange().add(deviceConfigSpec); |             reConfigSpec.getDeviceChange().add(deviceConfigSpec); | ||||||
| 
 | 
 | ||||||
|             ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec); |             ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user