use of com.vmware.vim25.mo.Datacenter in project photon-model by vmware.
the class EnumerationClientTest method test.
@Test
public void test() throws Exception {
String url = System.getProperty(TestProperties.VC_URL);
if (url == null) {
return;
}
String username = System.getProperty(TestProperties.VC_USERNAME);
String password = System.getProperty(TestProperties.VC_PASSWORD);
String datacenter = System.getProperty(TestProperties.VC_DATECENTER_ID);
ManagedObjectReference datacenterMoRef = VimUtils.convertStringToMoRef(datacenter);
BasicConnection conn = new BasicConnection();
conn.setURI(URI.create(url));
conn.setUsername(username);
conn.setPassword(password);
conn.setIgnoreSslErrors(true);
conn.setRequestTimeout(30, TimeUnit.SECONDS);
conn.connect();
ComputeStateWithDescription parent = new ComputeStateWithDescription();
ComputeDescription desc = new ComputeDescription();
parent.description = desc;
EnumerationClient client = new EnumerationClient(conn, parent);
PropertyFilterSpec spec = client.createResourcesFilterSpec();
for (List<ObjectContent> page : client.retrieveObjects(spec)) {
for (ObjectContent cont : page) {
this.logger.info(VimUtils.convertMoRefToString(cont.getObj()));
}
}
}
use of com.vmware.vim25.mo.Datacenter in project photon-model by vmware.
the class GetMoRef method getVMTraversalSpec.
/**
* @return TraversalSpec specification to get to the VirtualMachine managed
* object.
*/
public TraversalSpec getVMTraversalSpec() {
// Create a traversal spec that starts from the 'root' objects
// and traverses the inventory tree to get to the VirtualMachines.
// Build the traversal specs bottoms up
// Traversal to get to the VM in a VApp
TraversalSpec vAppToVM = new TraversalSpecBuilder().name("vAppToVM").type("VirtualApp").path("vm");
// Traversal spec for VApp to VApp
TraversalSpec vAppToVApp = new TraversalSpecBuilder().name("vAppToVApp").type("VirtualApp").path("resourcePool").selectSet(// SelectionSpec for both VApp to VApp and VApp to VM
new SelectionSpecBuilder().name("vAppToVApp"), new SelectionSpecBuilder().name("vAppToVM"));
// This SelectionSpec is used for recursion for Folder recursion
SelectionSpec visitFolders = new SelectionSpecBuilder().name("VisitFolders");
// Traversal to get to the vmFolder from DataCenter
TraversalSpec dataCenterToVMFolder = new TraversalSpecBuilder().name("DataCenterToVMFolder").type("Datacenter").path("vmFolder").skip(false).selectSet(visitFolders);
// TraversalSpec to get to the DataCenter from rootFolder
return new TraversalSpecBuilder().name("VisitFolders").type("Folder").path("childEntity").skip(false).selectSet(visitFolders, dataCenterToVMFolder, vAppToVM, vAppToVApp);
}
use of com.vmware.vim25.mo.Datacenter in project photon-model by vmware.
the class Lister method listFolder.
private List<Element> listFolder() throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, FinderException {
PropertyFilterSpec spec = new PropertyFilterSpec();
ObjectSpec objSpec = new ObjectSpec();
objSpec.setObj(this.start);
TraversalSpec selectionSpec = new TraversalSpec();
selectionSpec.setPath("childEntity");
selectionSpec.setType("Folder");
selectionSpec.setSkip(false);
objSpec.getSelectSet().add(selectionSpec);
spec.getObjectSet().add(objSpec);
// Retrieve all objects that we can deal with
String[] childTypes = { "Folder", "Datacenter", "VirtualMachine", "Network", "ComputeResource", "ClusterComputeResource", "Datastore" };
for (String t : childTypes) {
PropertySpec pspec = new PropertySpec();
pspec.setType(t);
pspec.getPathSet().add("name");
// Additional basic properties.
if (t.equals("ComputeResource") || t.equals("ClusterComputeResource")) {
// The ComputeResource and ClusterComputeResource are dereferenced in
// the ResourcePoolFlag. Make sure they always have their resourcePool
// field populated.
pspec.getPathSet().add("resourcePool");
}
spec.getPropSet().add(pspec);
}
return callPropertyCollectorAndConvert(spec);
}
use of com.vmware.vim25.mo.Datacenter in project photon-model by vmware.
the class DiskContext method populateContextThen.
/**
* Populates the given initial context and invoke the onSuccess handler when built. At every
* step, if failure occurs the DiskContext's errorHandler is invoked to cleanup.
*/
public static void populateContextThen(Service service, DiskContext ctx, Consumer<DiskContext> onSuccess) {
// Step 1: Get disk details
if (ctx.diskState == null) {
URI diskUri = createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(ctx.diskReference));
AdapterUtils.getServiceState(service, diskUri, op -> {
ctx.diskState = op.getBody(DiskService.DiskStateExpanded.class);
EnumSet<DiskService.DiskType> notSupportedTypes = EnumSet.of(DiskService.DiskType.SSD, DiskService.DiskType.NETWORK);
if (notSupportedTypes.contains(ctx.diskState.type)) {
ctx.fail(new IllegalStateException(String.format("Not supported disk type %s.", ctx.diskState.type)));
return;
}
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
// the disk.
if (ctx.datastoreName == null && ctx.diskInstanceRequest.requestType == DiskInstanceRequest.DiskRequestType.CREATE) {
if (ctx.diskState.storageDescription != null) {
ctx.datastoreName = ctx.diskState.storageDescription.id;
populateContextThen(service, ctx, onSuccess);
} else if (ctx.diskState.resourceGroupStates != null && !ctx.diskState.resourceGroupStates.isEmpty()) {
// There will always be only one resource group state existing for a disk
ResourceGroupState resource = ctx.diskState.resourceGroupStates.iterator().next();
ClientUtils.getDatastoresForProfile(service, resource.documentSelfLink, ctx.diskState.endpointLink, ctx.diskState.tenantLinks, ctx.errorHandler, (result) -> {
if (result.documents != null && result.documents.size() > 0) {
// pick the first datastore and proceed.
StorageDescription dsStorageDesc = Utils.fromJson(result.documents.values().iterator().next(), StorageDescription.class);
ctx.datastoreName = dsStorageDesc.id;
ctx.diskState.storageDescriptionLink = dsStorageDesc.documentSelfLink;
} else {
// Since no result found default to the available datastore.
ctx.datastoreName = "";
}
populateContextThen(service, ctx, onSuccess);
});
} else if (CustomProperties.of(ctx.diskState).getString(CustomProperties.DISK_DATASTORE_NAME) != null) {
ctx.datastoreName = CustomProperties.of(ctx.diskState).getString(CustomProperties.DISK_DATASTORE_NAME);
populateContextThen(service, ctx, onSuccess);
} else {
// Mark empty so that it can fall back to any available datastore from the system.
ctx.datastoreName = "";
populateContextThen(service, ctx, onSuccess);
}
return;
}
// Step 3: Get Credentials
if (ctx.vSphereCredentials == null) {
if (IAAS_API_ENABLED) {
if (ctx.operation == null) {
ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
return;
}
SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
if (throwable != null) {
ctx.errorHandler.accept(throwable);
return;
}
ctx.vSphereCredentials = authCredentialsServiceState;
populateContextThen(service, ctx, onSuccess);
});
} else {
if (ctx.diskState.authCredentialsLink == null || ctx.diskState.authCredentialsLink.isEmpty()) {
ctx.fail(new IllegalArgumentException("Auth credentials cannot be empty"));
return;
}
URI credUri = createInventoryUri(service.getHost(), ctx.diskState.authCredentialsLink);
AdapterUtils.getServiceState(service, credUri, op -> {
ctx.vSphereCredentials = op.getBody(AuthCredentialsServiceState.class);
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
}
return;
}
// Step 4: Get the endpoint compute link
if (ctx.endpointComputeLink == null) {
URI endpointUri = createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), ctx.diskState.endpointLink));
AdapterUtils.getServiceState(service, endpointUri, op -> {
EndpointService.EndpointState endpointState = op.getBody(EndpointService.EndpointState.class);
ctx.endpointComputeLink = endpointState.computeLink;
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
// Step 5: Get the adapter reference to from the endpoint compute link
if (ctx.adapterManagementReference == null) {
URI computeUri = createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), ctx.endpointComputeLink));
AdapterUtils.getServiceState(service, computeUri, op -> {
ComputeService.ComputeState computeState = op.getBody(ComputeService.ComputeState.class);
ctx.adapterManagementReference = computeState.adapterManagementReference;
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
// Step 6: Obtain reference to the datacenter moref.
if (ctx.datacenterMoRef == null) {
try {
ctx.datacenterMoRef = VimUtils.convertStringToMoRef(ctx.diskState.regionId);
} catch (IllegalArgumentException ex) {
ctx.fail(ex);
return;
}
}
onSuccess.accept(ctx);
}
use of com.vmware.vim25.mo.Datacenter in project photon-model by vmware.
the class InstanceClient method createFullCloneAndAttach.
private VirtualDeviceConfigSpec createFullCloneAndAttach(String sourcePath, DiskStateExpanded ds, String dir, VirtualDevice scsiController, int unitNumber, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws Exception {
ManagedObjectReference diskManager = this.connection.getServiceContent().getVirtualDiskManager();
String dsDirForDisk = getDatastorePathForDisk(ds, dir);
// put full clone in the vm folder
String destName = makePathToVmdkFile(ds.name, dsDirForDisk);
// all ops are within a datacenter
ManagedObjectReference sourceDc = this.ctx.datacenterMoRef;
ManagedObjectReference destDc = sourceDc;
Boolean force = true;
// spec is not supported, should use null for now
VirtualDiskSpec spec = null;
ManagedObjectReference task = getVimPort().copyVirtualDiskTask(diskManager, sourcePath, sourceDc, destName, destDc, spec, force);
// wait for the disk to be copied
TaskInfo taskInfo = waitTaskEnd(task);
if (taskInfo.getState() == TaskInfoState.ERROR) {
return VimUtils.rethrow(taskInfo.getError());
}
VirtualDiskFlatVer2BackingInfo backing = new VirtualDiskFlatVer2BackingInfo();
backing.setDiskMode(getDiskMode(ds));
VirtualDiskType provisionType = getDiskProvisioningType(ds);
if (provisionType != null) {
backing.setThinProvisioned(provisionType == VirtualDiskType.THIN);
backing.setEagerlyScrub(provisionType == VirtualDiskType.EAGER_ZEROED_THICK);
}
backing.setFileName(destName);
backing.setDatastore(getDataStoreForDisk(ds, pbmSpec));
VirtualDisk disk = new VirtualDisk();
disk.setBacking(backing);
disk.setStorageIOAllocation(getStorageIOAllocationInfo(ds));
disk.setControllerKey(scsiController.getKey());
disk.setUnitNumber(unitNumber);
fillInControllerUnitNumber(ds, unitNumber);
disk.setKey(-1);
VirtualDeviceConfigSpec change = new VirtualDeviceConfigSpec();
change.setDevice(disk);
// Add storage policy spec
if (pbmSpec != null) {
pbmSpec.stream().forEach(sp -> {
change.getProfile().add(sp);
});
}
change.setOperation(VirtualDeviceConfigSpecOperation.ADD);
return change;
}
Aggregations