Search in sources :

Example 1 with LibraryClient

use of com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient in project photon-model by vmware.

the class VSphereAdapterImageEnumerationService method refreshResourcesOnce.

private void refreshResourcesOnce(Set<String> oldImages, ImageEnumerateRequest request, ComputeStateWithDescription parent, Connection connection, TaskManager mgr) {
    DatacenterLister lister = new DatacenterLister(connection);
    try {
        for (Element element : lister.listAllDatacenters()) {
            ManagedObjectReference datacenter = element.object;
            try {
                EnumerationClient client = new EnumerationClient(connection, parent, datacenter);
                processAllTemplates(oldImages, request.resourceLink(), request.taskLink(), client, parent.tenantLinks);
            } catch (Throwable e) {
                mgr.patchTaskToFailure("Error processing vm templates in " + element.path, e);
                return;
            }
        }
    } catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg e) {
        mgr.patchTaskToFailure("Error getting datacenters", e);
        return;
    }
    VapiConnection vapi = VapiConnection.createFromVimConnection(connection);
    try {
        vapi.login();
        LibraryClient libraryClient = vapi.newLibraryClient();
        processAllLibraries(oldImages, request.resourceLink(), request.taskLink(), libraryClient, parent.tenantLinks);
        mgr.patchTask(TaskStage.FINISHED);
    } catch (Throwable t) {
        mgr.patchTaskToFailure("Error processing library items", t);
        return;
    } finally {
        vapi.close();
    }
    // garbage collection runs async
    garbageCollectUntouchedImages(oldImages);
}
Also used : VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) LibraryClient(com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with LibraryClient

use of com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient in project photon-model by vmware.

the class VSphereAdapterImageEnumerationService method processAllLibraries.

private void processAllLibraries(Set<String> oldImages, String endpointLink, String taskLink, LibraryClient libraryClient, List<String> tenantLinks) throws IOException, RpcException {
    Phaser phaser = new Phaser(1);
    for (String libId : libraryClient.listLibs()) {
        ObjectNode libModel = libraryClient.loadLib(libId);
        for (String itemId : libraryClient.listItemsInLib(libId)) {
            ObjectNode itemModel = libraryClient.loadItem(itemId);
            String type = VapiClient.getString(itemModel, "type", VapiClient.K_OPTIONAL);
            if (!"ovf".equals(type)) {
                // only ovfs can be deployed
                continue;
            }
            ImageState state = makeImageFromItem(libModel, itemModel);
            state.documentSelfLink = buildStableImageLink(endpointLink, state.id);
            state.endpointLink = endpointLink;
            state.tenantLinks = tenantLinks;
            state.regionId = "";
            oldImages.remove(state.documentSelfLink);
            phaser.register();
            Operation.createPost(PhotonModelUriUtils.createInventoryUri(getHost(), ImageService.FACTORY_LINK)).setBody(state).setCompletion((o, e) -> phaser.arrive()).sendWith(this);
        }
    }
    phaser.arriveAndAwaitAdvance();
}
Also used : PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) VapiClient(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiClient) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) HashSet(java.util.HashSet) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) Query(com.vmware.xenon.services.common.QueryTask.Query) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) ImageService(com.vmware.photon.controller.model.resources.ImageService) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) ObjectContent(com.vmware.vim25.ObjectContent) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) IOException(java.io.IOException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) LibraryClient(com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Phaser(java.util.concurrent.Phaser) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) ImageEnumerateRequest(com.vmware.photon.controller.model.adapterapi.ImageEnumerateRequest) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Phaser(java.util.concurrent.Phaser) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState)

Example 3 with LibraryClient

use of com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient in project photon-model by vmware.

the class InstanceClient method createInstanceFromLibraryItem.

public ComputeState createInstanceFromLibraryItem(ImageState image) throws Exception {
    VapiConnection vapi = VapiConnection.createFromVimConnection(this.connection);
    vapi.login();
    try {
        LibraryClient client = vapi.newLibraryClient();
        List<VirtualMachineDefinedProfileSpec> pbmSpec = getPbmProfileSpec(this.bootDisk);
        ManagedObjectReference datastore = getDataStoreForDisk(this.bootDisk, pbmSpec);
        Map<String, String> mapping = new HashMap<>();
        ObjectNode result = client.deployOvfLibItem(image.id, this.ctx.child.name, getVmFolder(), datastore, pbmSpec != null && !pbmSpec.isEmpty() ? pbmSpec.iterator().next() : null, getResourcePool(), mapping, getDiskProvisioningType(this.bootDisk));
        if (!result.get("succeeded").asBoolean()) {
            // Log here to understand why deploy from library fails.
            logger.warn("Error deploying from library {}", result.toString());
            throw new Exception("Error deploying from library");
        }
        ManagedObjectReference ref = new ManagedObjectReference();
        ref.setType(VimNames.TYPE_VM);
        ref.setValue(VapiClient.getString(result, "resource_id", VapiClient.K_OPTIONAL, VapiClient.K_STRUCTURE, "com.vmware.vcenter.ovf.library_item.deployable_identity", "id"));
        this.vm = ref;
        customizeAfterClone();
        ComputeState state = new ComputeState();
        state.resourcePoolLink = VimUtils.firstNonNull(this.ctx.child.resourcePoolLink, this.ctx.parent.resourcePoolLink);
        return state;
    } finally {
        vapi.close();
    }
}
Also used : VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LibraryClient(com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient) VirtualMachineDefinedProfileSpec(com.vmware.vim25.VirtualMachineDefinedProfileSpec) FinderException(com.vmware.photon.controller.model.adapters.vsphere.util.finders.FinderException) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

LibraryClient (com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient)3 VapiConnection (com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection)3 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DatacenterLister (com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)2 Element (com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element)2 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)2 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)2 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)1 ImageEnumerateRequest (com.vmware.photon.controller.model.adapterapi.ImageEnumerateRequest)1 TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)1 Connection (com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection)1 FinderException (com.vmware.photon.controller.model.adapters.vsphere.util.finders.FinderException)1 RpcException (com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException)1 VapiClient (com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiClient)1 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)1 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)1 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)1 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)1 ImageService (com.vmware.photon.controller.model.resources.ImageService)1