Search in sources :

Example 61 with Datacenter

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()));
        }
    }
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Test(org.junit.Test)

Example 62 with Datacenter

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);
}
Also used : TraversalSpec(com.vmware.vim25.TraversalSpec) SelectionSpec(com.vmware.vim25.SelectionSpec) TraversalSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.TraversalSpecBuilder) SelectionSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.SelectionSpecBuilder)

Example 63 with Datacenter

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);
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec)

Example 64 with Datacenter

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);
}
Also used : AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DiskInstanceRequest(com.vmware.photon.controller.model.adapterapi.DiskInstanceRequest) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Consumer(java.util.function.Consumer) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) URI(java.net.URI) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) EnumSet(java.util.EnumSet) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) URI(java.net.URI) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)

Example 65 with Datacenter

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;
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDiskType(com.vmware.vim25.VirtualDiskType) VirtualDiskSpec(com.vmware.vim25.VirtualDiskSpec) VirtualDisk(com.vmware.vim25.VirtualDisk) ClientUtils.getListOfVirtualDisk(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.getListOfVirtualDisk) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)28 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)23 TraversalSpec (com.vmware.vim25.TraversalSpec)21 ObjectSpec (com.vmware.vim25.ObjectSpec)20 PropertySpec (com.vmware.vim25.PropertySpec)20 HostSystem (com.vmware.vim25.mo.HostSystem)20 ArrayList (java.util.ArrayList)17 RemoteException (java.rmi.RemoteException)15 ObjectContent (com.vmware.vim25.ObjectContent)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)11 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)11 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)11 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)11 SelectionSpec (com.vmware.vim25.SelectionSpec)11 ClusterComputeResource (com.vmware.vim25.mo.ClusterComputeResource)11 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)10 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)10 VcenterDataCenter (com.emc.storageos.db.client.model.VcenterDataCenter)10 Vcenter (com.emc.storageos.db.client.model.Vcenter)9