Search in sources :

Example 1 with VapiConnection

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

the class VSphereIncrementalEnumerationService method handlePatch.

@Override
public void handlePatch(Operation patch) {
    // complete the patch immediately.
    patch.complete();
    logInfo("Received PATCH for incremental enumeration!");
    VSphereIncrementalEnumerationRequest enumerationRequest = patch.getBody(VSphereIncrementalEnumerationRequest.class);
    ComputeEnumerateResourceRequest request = enumerationRequest.request;
    URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
    logInfo("Creating task manager!");
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    logInfo(" Requesting GET on compute state with description!.");
    Operation.createGet(parentUri).setCompletion(o -> {
        logInfo("Submitting job to threadpool!");
        VsphereEnumerationHelper.submitWorkToVSpherePool(this, () -> {
            logInfo("Incremental enumeration job started for endpoint %s", enumerationRequest.request.endpointLink);
            ComputeStateWithDescription computeStateWithDesc = o.getBody(ComputeStateWithDescription.class);
            VapiConnection vapiConnection = VapiConnection.createFromVimConnection(this.connection);
            logInfo("Establishing VAPI connection for endpoint %s", enumerationRequest.request.endpointLink);
            try {
                vapiConnection.login();
            } catch (IOException | RpcException rpce) {
                logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(rpce)));
                mgr.patchTaskToFailure(rpce);
                // self delete service so that full enumeration kicks in next invocation.
                selfDeleteService();
                return;
            }
            try {
                // Get instanceUuid of the vCenter
                AboutInfo vCenter = this.connection.getServiceContent().getAbout();
                for (CollectorDetails collectorDetails : this.collectors) {
                    logInfo("Retrieving  resources incremental data for data center: %s", collectorDetails.datacenter);
                    EnumerationProgress enumerationProgress = new EnumerationProgress(new HashSet<>(), request, computeStateWithDesc, vapiConnection, collectorDetails.datacenter, vCenter.getInstanceUuid());
                    EnumerationClient client = new EnumerationClient(this.connection, computeStateWithDesc, VimUtils.convertStringToMoRef(collectorDetails.datacenter));
                    List<ObjectUpdate> resourcesUpdates = collectResourcesData(collectorDetails);
                    List<ObjectUpdate> vmUpdates = collectVMData(collectorDetails);
                    logInfo("Received resources updates for datacenter: %s : %s", collectorDetails.datacenter, resourcesUpdates.size());
                    logInfo("Received vm updates for datacenter: %s : %s", collectorDetails.datacenter, vmUpdates.size());
                    logInfo("Resources Updates: %s", Utils.toJson(resourcesUpdates));
                    logInfo("VM Updates: %s", Utils.toJson(vmUpdates));
                    if (!resourcesUpdates.isEmpty()) {
                        SegregatedOverlays segregatedOverlays = segregateObjectUpdates(enumerationProgress, resourcesUpdates);
                        this.logInfo("Processing incremental changes for folders for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereFolderEnumerationHelper.handleFolderChanges(this, segregatedOverlays.folders, enumerationProgress, client);
                        logInfo("Processing incremental changes for networks for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereNetworkEnumerationHelper.handleNetworkChanges(this, segregatedOverlays.networks, enumerationProgress, client);
                        logInfo("Processing incremental changes for Datastores for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereDatastoreEnumerationHelper.handleDatastoreChanges(this, segregatedOverlays.datastores, enumerationProgress);
                        logInfo("Processing incremental changes for compute resource for datacenter [%s]!", collectorDetails.datacenter);
                        VsphereComputeResourceEnumerationHelper.handleComputeResourceChanges(this, segregatedOverlays.clusters, enumerationProgress, client);
                        logInfo("Processing incremental changes for host system for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereHostSystemEnumerationHelper.handleHostSystemChanges(this, segregatedOverlays.hosts, enumerationProgress, client);
                        logInfo("Processing incremental changes for resource pool for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereResourcePoolEnumerationHelper.handleResourcePoolChanges(this, segregatedOverlays.resourcePools, enumerationProgress, client);
                    }
                    if (!vmUpdates.isEmpty()) {
                        logInfo("Processing incremental changes for virtual machines for datacenter [%s]!", collectorDetails.datacenter);
                        VSphereVirtualMachineEnumerationHelper.handleVMChanges(this, vmUpdates, enumerationProgress, client);
                    }
                    // sync storage profiles
                    logInfo("Syncing storage profiles for datacenter [%s]!", collectorDetails.datacenter);
                    VsphereStoragePolicyEnumerationHelper.syncStorageProfiles(this, client, enumerationProgress);
                }
                mgr.patchTask(TaskStage.FINISHED);
            } catch (Exception exception) {
                String msg = "Error processing PropertyCollector results during incremental retrieval";
                logWarning(() -> msg + ": " + exception.toString());
                mgr.patchTaskToFailure(exception);
                // self delete service so that full enumeration kicks in next invocation.
                // TODO: This is not complete. We need to enable owner selection on this service.
                selfDeleteService();
                return;
            } finally {
                vapiConnection.close();
            }
        });
    }, mgr).sendWith(this);
}
Also used : Arrays(java.util.Arrays) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) TagCache(com.vmware.photon.controller.model.adapters.vsphere.tagging.TagCache) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) UpdateSet(com.vmware.vim25.UpdateSet) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) Utils(com.vmware.xenon.common.Utils) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) Map(java.util.Map) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) Collectors(java.util.stream.Collectors) List(java.util.List) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) AboutInfo(com.vmware.vim25.AboutInfo) DiskService(com.vmware.photon.controller.model.resources.DiskService) MoRefKeyedMap(com.vmware.photon.controller.model.adapters.vsphere.util.MoRefKeyedMap) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) HashMap(java.util.HashMap) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) TYPE_SERVER_DISK(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_SERVER_DISK) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) Operation(com.vmware.xenon.common.Operation) ObjectUpdateIterator(com.vmware.photon.controller.model.adapters.vsphere.EnumerationClient.ObjectUpdateIterator) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) PropertyFilterUpdate(com.vmware.vim25.PropertyFilterUpdate) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ObjectUpdate(com.vmware.vim25.ObjectUpdate) OperationJoin(com.vmware.xenon.common.OperationJoin) VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) AboutInfo(com.vmware.vim25.AboutInfo) URI(java.net.URI) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) IOException(java.io.IOException) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 2 with VapiConnection

use of com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection 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 3 with VapiConnection

use of com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection 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)

Example 4 with VapiConnection

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

the class VSphereIncrementalEnumerationService method refreshResourcesOnce.

/**
 * This method executes in a thread managed by {@link VSphereIOThreadPoolAllocator}
 */
private void refreshResourcesOnce(Set<String> resourceLinks, ComputeEnumerateResourceRequest request, Connection connection, ComputeService.ComputeStateWithDescription parent, TaskManager mgr) {
    VapiConnection vapiConnection = VapiConnection.createFromVimConnection(connection);
    try {
        vapiConnection.login();
    } catch (IOException | RpcException e) {
        logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(e)));
        mgr.patchTaskToFailure(e);
        return;
    }
    DatacenterLister lister = new DatacenterLister(connection);
    try {
        // Get instanceUuid of the vCenter
        AboutInfo vCenter = this.connection.getServiceContent().getAbout();
        for (Element element : lister.listAllDatacenters()) {
            ManagedObjectReference datacenter = element.object;
            log(Level.INFO, "Processing datacenter %s (%s)", element.path, VimUtils.convertMoRefToString(element.object));
            EnumerationClient client = new EnumerationClient(connection, parent, datacenter);
            EnumerationProgress enumerationProgress = new EnumerationProgress(resourceLinks, request, parent, vapiConnection, VimUtils.convertMoRefToString(datacenter), vCenter.getInstanceUuid());
            // since we are processing DC sequentially one at a time
            enumerationProgress.expectDatacenterCount(1);
            VsphereDatacenterEnumerationHelper.processDatacenterInfo(this, element, enumerationProgress);
            enumerationProgress.getDcTracker().await();
            logInfo("Proceeding to refresh resources on datacenter: %s", enumerationProgress.getDcLink());
            refreshResourcesOnDatacenter(client, enumerationProgress, mgr);
        }
    } catch (Exception e) {
        logWarning(String.format("Error during enumeration: %s", Utils.toString(e)));
        mgr.patchTaskToFailure(e);
    }
    try {
        vapiConnection.close();
    } catch (Exception e) {
        logWarning(() -> String.format("Error occurred when closing vAPI connection: %s", Utils.toString(e)));
    }
    // after all dc's are enumerated untouched resource links are the only ones left
    // in resourceLinks
    garbageCollectUntouchedComputeResources(request, resourceLinks, mgr);
    // cleanup the connection if the enumeration action is REFRESH.
    if (EnumerationAction.REFRESH.equals(request.enumerationAction)) {
        cleanupConnection();
    }
}
Also used : VapiConnection(com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) Element(com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element) AboutInfo(com.vmware.vim25.AboutInfo) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) IOException(java.io.IOException) RpcException(com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException) IOException(java.io.IOException) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VapiConnection (com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 DatacenterLister (com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)3 Element (com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element)3 LibraryClient (com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient)2 RpcException (com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException)2 AboutInfo (com.vmware.vim25.AboutInfo)2 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)1 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)1 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)1 TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)1 ObjectUpdateIterator (com.vmware.photon.controller.model.adapters.vsphere.EnumerationClient.ObjectUpdateIterator)1 VsphereEnumerationHelper.convertOnlyResultToDocument (com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument)1 VsphereEnumerationHelper.withTaskResults (com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults)1 ResourceCleanRequest (com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest)1 TagCache (com.vmware.photon.controller.model.adapters.vsphere.tagging.TagCache)1 MoRefKeyedMap (com.vmware.photon.controller.model.adapters.vsphere.util.MoRefKeyedMap)1 VimNames (com.vmware.photon.controller.model.adapters.vsphere.util.VimNames)1