use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VMwareUtil method getVMTraversalSpec.
private static TraversalSpec getVMTraversalSpec() {
// Create a TraversalSpec that starts from the 'root' objects
// and traverses the inventory tree to get to the VirtualMachines.
// Build the traversal specs bottoms up
// TraversalSpec to get to the VM in a vApp
TraversalSpec vAppToVM = new TraversalSpec();
vAppToVM.setName("vAppToVM");
vAppToVM.setType("VirtualApp");
vAppToVM.setPath("vm");
// TraversalSpec for vApp to vApp
TraversalSpec vAppToVApp = new TraversalSpec();
vAppToVApp.setName("vAppToVApp");
vAppToVApp.setType("VirtualApp");
vAppToVApp.setPath("resourcePool");
// SelectionSpec for vApp-to-vApp recursion
SelectionSpec vAppRecursion = new SelectionSpec();
vAppRecursion.setName("vAppToVApp");
// SelectionSpec to get to a VM in the vApp
SelectionSpec vmInVApp = new SelectionSpec();
vmInVApp.setName("vAppToVM");
// SelectionSpec for both vApp to vApp and vApp to VM
List<SelectionSpec> vAppToVMSS = new ArrayList<>();
vAppToVMSS.add(vAppRecursion);
vAppToVMSS.add(vmInVApp);
vAppToVApp.getSelectSet().addAll(vAppToVMSS);
// This SelectionSpec is used for recursion for Folder recursion
SelectionSpec sSpec = new SelectionSpec();
sSpec.setName("VisitFolders");
// Traversal to get to the vmFolder from DataCenter
TraversalSpec dataCenterToVMFolder = new TraversalSpec();
dataCenterToVMFolder.setName("DataCenterToVMFolder");
dataCenterToVMFolder.setType("Datacenter");
dataCenterToVMFolder.setPath("vmFolder");
dataCenterToVMFolder.setSkip(false);
dataCenterToVMFolder.getSelectSet().add(sSpec);
// TraversalSpec to get to the DataCenter from rootFolder
TraversalSpec traversalSpec = new TraversalSpec();
traversalSpec.setName("VisitFolders");
traversalSpec.setType("Folder");
traversalSpec.setPath("childEntity");
traversalSpec.setSkip(false);
List<SelectionSpec> sSpecArr = new ArrayList<>();
sSpecArr.add(sSpec);
sSpecArr.add(dataCenterToVMFolder);
sSpecArr.add(vAppToVM);
sSpecArr.add(vAppToVApp);
traversalSpec.getSelectSet().addAll(sSpecArr);
return traversalSpec;
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VMwareGuru method attachRestoredVolumeToVirtualMachine.
@Override
public boolean attachRestoredVolumeToVirtualMachine(long zoneId, String location, Backup.VolumeInfo volumeInfo, VirtualMachine vm, long poolId, Backup backup) throws Exception {
DatacenterMO dcMo = getDatacenterMO(zoneId);
VirtualMachineMO vmRestored = findVM(dcMo, location);
VirtualMachineMO vmMo = findVM(dcMo, vm.getInstanceName());
VirtualDisk restoredDisk = findRestoredVolume(volumeInfo, vmRestored);
String diskPath = vmRestored.getVmdkFileBaseName(restoredDisk);
s_logger.debug("Restored disk size=" + toHumanReadableSize(restoredDisk.getCapacityInKB()) + " path=" + diskPath);
// Detach restored VM disks
vmRestored.detachAllDisks();
String srcPath = getVolumeFullPath(restoredDisk);
String destPath = getDestVolumeFullPath(restoredDisk, vmRestored, vmMo);
VirtualDiskManagerMO virtualDiskManagerMO = new VirtualDiskManagerMO(dcMo.getContext());
// Copy volume to the VM folder
virtualDiskManagerMO.moveVirtualDisk(srcPath, dcMo.getMor(), destPath, dcMo.getMor(), true);
try {
// Attach volume to VM
vmMo.attachDisk(new String[] { destPath }, getDestStoreMor(vmMo));
} catch (Exception e) {
s_logger.error("Failed to attach the restored volume: " + diskPath, e);
return false;
} finally {
// Destroy restored VM
vmRestored.destroy();
}
VirtualDisk attachedDisk = getAttachedDisk(vmMo, diskPath);
if (attachedDisk == null) {
s_logger.error("Failed to get the attached the (restored) volume " + diskPath);
return false;
}
createVolume(attachedDisk, vmMo, vm.getDomainId(), vm.getDataCenterId(), vm.getAccountId(), vm.getId(), poolId, vm.getTemplateId(), backup, false);
return true;
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class VmwareContext method getHostMorByPath.
public ManagedObjectReference getHostMorByPath(String inventoryPath) throws Exception {
assert (inventoryPath != null);
String[] tokens;
if (inventoryPath.startsWith("/"))
tokens = inventoryPath.substring(1).split("/");
else
tokens = inventoryPath.split("/");
ManagedObjectReference mor = getRootFolder();
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
List<ObjectContent> ocs;
PropertySpec pSpec = null;
ObjectSpec oSpec = null;
if (mor.getType().equalsIgnoreCase("Datacenter")) {
pSpec = new PropertySpec();
pSpec.setAll(false);
pSpec.setType("ManagedEntity");
pSpec.getPathSet().add("name");
TraversalSpec dcHostFolderTraversal = new TraversalSpec();
dcHostFolderTraversal.setType("Datacenter");
dcHostFolderTraversal.setPath("hostFolder");
dcHostFolderTraversal.setName("dcHostFolderTraversal");
oSpec = new ObjectSpec();
oSpec.setObj(mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(dcHostFolderTraversal);
} else if (mor.getType().equalsIgnoreCase("Folder")) {
pSpec = new PropertySpec();
pSpec.setAll(false);
pSpec.setType("ManagedEntity");
pSpec.getPathSet().add("name");
TraversalSpec folderChildrenTraversal = new TraversalSpec();
folderChildrenTraversal.setType("Folder");
folderChildrenTraversal.setPath("childEntity");
folderChildrenTraversal.setName("folderChildrenTraversal");
oSpec = new ObjectSpec();
oSpec.setObj(mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(folderChildrenTraversal);
} else if (mor.getType().equalsIgnoreCase("ClusterComputeResource")) {
pSpec = new PropertySpec();
pSpec.setType("ManagedEntity");
pSpec.getPathSet().add("name");
TraversalSpec clusterHostTraversal = new TraversalSpec();
clusterHostTraversal.setType("ClusterComputeResource");
clusterHostTraversal.setPath("host");
clusterHostTraversal.setName("folderChildrenTraversal");
oSpec = new ObjectSpec();
oSpec.setObj(mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(clusterHostTraversal);
} else {
s_logger.error("Invalid inventory path, path element can only be datacenter and folder");
return null;
}
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
ocs = getService().retrieveProperties(getPropertyCollector(), pfSpecArr);
if (ocs != null && ocs.size() > 0) {
boolean found = false;
for (ObjectContent oc : ocs) {
String name = oc.getPropSet().get(0).getVal().toString();
if (name.equalsIgnoreCase(token) || name.equalsIgnoreCase("host")) {
mor = oc.getObj();
found = true;
if (name.equalsIgnoreCase("host"))
i--;
break;
}
}
if (!found) {
s_logger.error("Path element points to an un-existing inventory entity");
return null;
}
} else {
s_logger.error("Path element points to an un-existing inventory entity");
return null;
}
}
return mor;
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class DatacenterMO method getHostPropertiesOnDatacenterHostFolder.
public List<ObjectContent> getHostPropertiesOnDatacenterHostFolder(String[] propertyPaths) throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("HostSystem");
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
TraversalSpec computeResource2HostTraversal = new TraversalSpec();
computeResource2HostTraversal.setType("ComputeResource");
computeResource2HostTraversal.setPath("host");
computeResource2HostTraversal.setName("computeResource2HostTraversal");
SelectionSpec recurseFolders = new SelectionSpec();
recurseFolders.setName("folder2childEntity");
TraversalSpec folder2childEntity = new TraversalSpec();
folder2childEntity.setType("Folder");
folder2childEntity.setPath("childEntity");
folder2childEntity.setName(recurseFolders.getName());
folder2childEntity.getSelectSet().add(recurseFolders);
folder2childEntity.getSelectSet().add(computeResource2HostTraversal);
TraversalSpec dc2HostFolderTraversal = new TraversalSpec();
dc2HostFolderTraversal.setType("Datacenter");
dc2HostFolderTraversal.setPath("hostFolder");
dc2HostFolderTraversal.setName("dc2HostFolderTraversal");
dc2HostFolderTraversal.getSelectSet().add(folder2childEntity);
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(dc2HostFolderTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
}
use of com.vmware.vim25.mo.Folder in project cloudstack by apache.
the class DatacenterMO method getVmPropertiesOnDatacenterVmFolder.
public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths) throws Exception {
PropertySpec pSpec = new PropertySpec();
pSpec.setType("VirtualMachine");
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
TraversalSpec dc2VmFolderTraversal = new TraversalSpec();
dc2VmFolderTraversal.setType("Datacenter");
dc2VmFolderTraversal.setPath("vmFolder");
dc2VmFolderTraversal.setName("dc2VmFolderTraversal");
SelectionSpec recurseFolders = new SelectionSpec();
recurseFolders.setName("folder2childEntity");
TraversalSpec folder2childEntity = new TraversalSpec();
folder2childEntity.setType("Folder");
folder2childEntity.setPath("childEntity");
folder2childEntity.setName(recurseFolders.getName());
folder2childEntity.getSelectSet().add(recurseFolders);
dc2VmFolderTraversal.getSelectSet().add(folder2childEntity);
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(_mor);
oSpec.setSkip(Boolean.TRUE);
oSpec.getSelectSet().add(dc2VmFolderTraversal);
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
pfSpec.getPropSet().add(pSpec);
pfSpec.getObjectSet().add(oSpec);
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
pfSpecArr.add(pfSpec);
return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
}
Aggregations