Search in sources :

Example 1 with DatacenterLister

use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister in project photon-model by vmware.

the class DatacenterEnumeratorService method handlePatch.

@Override
public void handlePatch(Operation patch) {
    if (!patch.hasBody()) {
        patch.fail(new IllegalArgumentException("body is required"));
        return;
    }
    VSphereIOThreadPoolAllocator.getPool(this).submit(() -> {
        EnumerateDatacentersRequest req = patch.getBody(EnumerateDatacentersRequest.class);
        BasicConnection connection = new BasicConnection();
        try {
            EnumerateDatacentersResponse res = new EnumerateDatacentersResponse();
            if (req.isMock) {
                res.datacenters = Collections.singletonList("dc-1");
                res.moRefs = Collections.singletonList("Datacenter:dc-1");
            } else {
                connection.setURI(URI.create("https://" + req.host + "/sdk"));
                connection.setUsername(req.username);
                connection.setPassword(req.password);
                connection.setIgnoreSslErrors(true);
                connection.connect();
                DatacenterLister lister = new DatacenterLister(connection);
                res.moRefs = new ArrayList<>();
                res.datacenters = new ArrayList<>();
                lister.listAllDatacenters().stream().forEach(el -> {
                    res.datacenters.add(DatacenterLister.prettifyPath(el.path));
                    res.moRefs.add(VimUtils.convertMoRefToString(el.object));
                });
            }
            patch.setBody(res);
            patch.complete();
        } catch (Exception e) {
            patch.fail(e);
        } finally {
            connection.closeQuietly();
        }
    });
}
Also used : BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)

Example 2 with DatacenterLister

use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister 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 DatacenterLister

use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister 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)

Example 4 with DatacenterLister

use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister in project photon-model by vmware.

the class VSphereRegionEnumerationAdapterService method handlePost.

@Override
public void handlePost(Operation post) {
    if (!post.hasBody()) {
        post.fail(new IllegalArgumentException("body is required"));
        return;
    }
    EndpointState request = post.getBody(EndpointState.class);
    DeferredResult<AuthCredentialsServiceState> credentialsDr;
    if (request.authCredentialsLink == null) {
        credentialsDr = new DeferredResult<>();
        credentialsDr.complete(new AuthCredentialsServiceState());
    } else {
        Operation getCredentials = Operation.createGet(createInventoryUri(this.getHost(), request.authCredentialsLink));
        credentialsDr = sendWithDeferredResult(getCredentials, AuthCredentialsServiceState.class);
    }
    credentialsDr.whenComplete((AuthCredentialsServiceState creds, Throwable t) -> {
        if (t != null) {
            post.fail(t);
            return;
        }
        VSphereIOThreadPoolAllocator.getPool(this).submit(() -> {
            BasicConnection connection = new BasicConnection();
            try {
                EndpointAdapterUtils.Retriever retriever = EndpointAdapterUtils.Retriever.of(request.endpointProperties);
                VSphereEndpointAdapterService.endpoint().accept(request, retriever);
                VSphereEndpointAdapterService.credentials().accept(creds, retriever);
                connection.setURI(URI.create("https://" + request.endpointProperties.get(HOST_NAME_KEY) + "/sdk"));
                connection.setUsername(creds.privateKeyId);
                connection.setPassword(EncryptionUtils.decrypt(creds.privateKey));
                connection.setIgnoreSslErrors(true);
                connection.connect();
                DatacenterLister lister = new DatacenterLister(connection);
                RegionEnumerationResponse res = new RegionEnumerationResponse();
                res.regions = lister.listAllDatacenters().stream().map(dc -> new RegionEnumerationResponse.RegionInfo(DatacenterLister.prettifyPath(dc.path), VimUtils.convertMoRefToString(dc.object))).collect(Collectors.toList());
                post.setBody(res);
                post.complete();
            } catch (Exception e) {
                post.fail(e);
            } finally {
                connection.closeQuietly();
            }
        });
    });
}
Also used : RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) Operation(com.vmware.xenon.common.Operation) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) DatacenterLister(com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister) EndpointAdapterUtils(com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils)

Aggregations

DatacenterLister (com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister)4 BasicConnection (com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection)2 Element (com.vmware.photon.controller.model.adapters.vsphere.util.finders.Element)2 VapiConnection (com.vmware.photon.controller.model.adapters.vsphere.vapi.VapiConnection)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 RegionEnumerationResponse (com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse)1 EndpointAdapterUtils (com.vmware.photon.controller.model.adapters.util.EndpointAdapterUtils)1 LibraryClient (com.vmware.photon.controller.model.adapters.vsphere.vapi.LibraryClient)1 RpcException (com.vmware.photon.controller.model.adapters.vsphere.vapi.RpcException)1 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)1 AboutInfo (com.vmware.vim25.AboutInfo)1 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)1 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)1 Operation (com.vmware.xenon.common.Operation)1 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)1 IOException (java.io.IOException)1