Search in sources :

Example 21 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereIncrementalEnumerationService method handleStart.

@Override
public void handleStart(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    VSphereIncrementalEnumerationRequest enumerationRequest = op.getBody(VSphereIncrementalEnumerationRequest.class);
    ComputeEnumerateResourceRequest request = enumerationRequest.request;
    validate(request);
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
    Operation.createGet(parentUri).setCompletion(o -> {
        thenWithParentState(request, o.getBody(ComputeService.ComputeStateWithDescription.class), mgr);
    }, 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) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) URI(java.net.URI) ComputeService(com.vmware.photon.controller.model.resources.ComputeService)

Example 22 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method makeVmFromResults.

/**
 * Make a ComputeState from the request and a vm found in vsphere.
 */
static ComputeState makeVmFromResults(EnumerationProgress enumerationProgress, VmOverlay vm) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    ComputeState state = new ComputeState();
    state.type = ComputeType.VM_GUEST;
    state.environmentName = ComputeDescription.ENVIRONMENT_NAME_ON_PREMISE;
    state.endpointLink = request.endpointLink;
    AdapterUtils.addToEndpointLinks(state, request.endpointLink);
    state.adapterManagementReference = request.adapterManagementReference;
    state.parentLink = request.resourceLink();
    state.resourcePoolLink = request.resourcePoolLink;
    state.instanceAdapterReference = enumerationProgress.getParent().description.instanceAdapterReference;
    state.enumerationAdapterReference = enumerationProgress.getParent().description.enumerationAdapterReference;
    state.powerAdapterReference = enumerationProgress.getParent().description.powerAdapterReference;
    state.regionId = enumerationProgress.getRegionId();
    state.cpuCount = (long) vm.getNumCpu();
    state.totalMemoryBytes = vm.getMemoryBytes();
    state.hostName = vm.getHostName();
    state.powerState = vm.getPowerState();
    state.primaryMAC = vm.getPrimaryMac();
    if (!vm.isTemplate()) {
        state.address = vm.guessPublicIpV4Address();
    }
    state.id = vm.getInstanceUuid();
    state.name = vm.getName();
    CustomProperties.of(state).put(CustomProperties.MOREF, vm.getId()).put(CustomProperties.TYPE, VimNames.TYPE_VM).put(CustomProperties.VM_SOFTWARE_NAME, vm.getOS()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.DATACENTER, enumerationProgress.getRegionId()).put(CustomProperties.VM_MEMORY_IN_GB, AdapterUtils.convertBytesToGB(vm.getMemoryBytes()));
    VsphereEnumerationHelper.populateResourceStateWithAdditionalProps(state, enumerationProgress.getVcUuid());
    String selfLink = enumerationProgress.getHostSystemTracker().getSelfLink(vm.getHost());
    if (null != selfLink) {
        CustomProperties.of(state).put(ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME, selfLink);
    }
    return state;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)

Example 23 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VsphereComputeResourceEnumerationHelper method processFoundComputeResource.

/**
 * Either creates a new Compute or update an already existing one. Existence is checked by
 * querying for a compute with id equals to moref value of a cluster whose parent is the Compute
 * from the request.
 */
public static void processFoundComputeResource(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, ComputeResourceOverlay cr, EnumerationClient client) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    QueryTask task = queryForCluster(enumerationProgress, request.resourceLink(), cr.getId().getValue());
    withTaskResults(service, task, result -> {
        try {
            if (result.documentLinks.isEmpty()) {
                createNewComputeResource(service, enumerationProgress, cr, client);
            } else {
                ComputeState oldDocument = convertOnlyResultToDocument(result, ComputeState.class);
                updateCluster(service, oldDocument, enumerationProgress, cr, client, true);
            }
            if (!cr.getVsanHostConfig().isEmpty()) {
                processServerDisks(service, cr.getVsanHostConfig(), enumerationProgress);
            }
        } catch (Exception e) {
            // if there's an error while processing compute resource
            service.logSevere("Error occurred while processing update of compute resource!", e);
            enumerationProgress.getComputeResourceTracker().track(cr.getId(), ResourceTracker.ERROR);
        }
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)

Example 24 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereAdapterResourceEnumerationService method handlePatch.

/**
 * On every enumeration request, we check if there's an existing stateless service and delete it.
 * This is needed to ensure no two enumeration processes are happening for same endpoint.
 * After deleting the service, we create a new stateless service by issuing POST on the factory of
 * stateless service and passing the last path segment of endpoint document selflink.
 *
 * @param op the enumeration operation request
 */
@Override
public void handlePatch(Operation op) {
    if (!op.hasBody()) {
        op.fail(new IllegalArgumentException("body is required"));
        return;
    }
    ComputeEnumerateResourceRequest request = op.getBody(ComputeEnumerateResourceRequest.class);
    validate(request);
    op.setStatusCode(Operation.STATUS_CODE_CREATED);
    op.complete();
    TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
    if (request.isMockRequest) {
        // just finish the mock request
        mgr.patchTask(TaskState.TaskStage.FINISHED);
        return;
    }
    String eventBasedEnumerationServiceURI = UriUtils.buildUriPath(VSphereIncrementalEnumerationService.FACTORY_LINK, UriUtils.getLastPathSegment(request.endpointLink));
    URI uri = buildUri(this.getHost(), eventBasedEnumerationServiceURI);
    VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest enumerationRequest = new VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest();
    enumerationRequest.documentSelfLink = UriUtils.getLastPathSegment(eventBasedEnumerationServiceURI);
    enumerationRequest.request = request;
    // if yes, patch it. If no, create new one via post to factory.
    if (EnumerationAction.START.equals(request.enumerationAction)) {
        Operation.createGet(uri).setCompletion((o, e) -> {
            if (null != e || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) {
                // service not found, create a new one
                createNewEnumerationService(enumerationRequest, mgr);
            } else {
                logInfo("Patching incremental service for incremental enumeration for %s", enumerationRequest.documentSelfLink);
                Operation patchRequest = Operation.createPatch(uri).setBody(enumerationRequest).setCompletion((operation, err) -> {
                    if (err != null) {
                        logSevere("Unable to send enumeration request to enumeration" + " service for endpoint %s", enumerationRequest.documentSelfLink);
                        mgr.patchTaskToFailure(err);
                    }
                });
                patchRequest.sendWith(this);
            }
        }).sendWith(this);
    } else {
        // Stop any existing enumeration process when REFRESH is received.
        logFine("Deleting the incremental enumeration service.");
        Operation deleteRequest = Operation.createDelete(uri).setCompletion((o, e) -> {
            if (null != e) {
                logWarning("Delete of enumeration service failed for endpoint " + enumerationRequest.documentSelfLink + " Message: %s", e.getMessage());
            }
            // The delete service call will take care of stopping any running enumeration.
            if (request.enumerationAction == EnumerationAction.STOP) {
                logInfo("Successfully stopped enumeration for endpoint " + request.endpointLink);
                mgr.patchTask(TaskState.TaskStage.FINISHED);
            } else {
                logInfo("Creating the enumeration service for endpoint %s", request.endpointLink);
                createNewEnumerationService(enumerationRequest, mgr);
            }
        });
        deleteRequest.sendWith(this);
    }
}
Also used : VSphereIncrementalEnumerationRequest(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest) Utils(com.vmware.xenon.common.Utils) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) UriUtils(com.vmware.xenon.common.UriUtils) UriUtils.buildFactoryUri(com.vmware.xenon.common.UriUtils.buildFactoryUri) UriUtils.buildUri(com.vmware.xenon.common.UriUtils.buildUri) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) TaskState(com.vmware.xenon.common.TaskState) URI(java.net.URI) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) VSphereIncrementalEnumerationRequest(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest) VSphereIncrementalEnumerationRequest(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest)

Example 25 with ComputeEnumerateResourceRequest

use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.

the class VSphereHostSystemEnumerationHelper method handleHostSystemChanges.

public static void handleHostSystemChanges(VSphereIncrementalEnumerationService service, List<HostSystemOverlay> hostSystemOverlays, EnumerationProgress enumerationProgress, EnumerationClient client) {
    enumerationProgress.expectHostSystemCount(hostSystemOverlays.size());
    for (HostSystemOverlay hs : hostSystemOverlays) {
        try {
            if (ObjectUpdateKind.ENTER.equals(hs.getObjectUpdateKind())) {
                createNewHostSystem(service, enumerationProgress, hs, client);
            } else {
                ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
                QueryTask task = queryForHostSystem(enumerationProgress, request.resourceLink(), hs.getId().getValue());
                withTaskResults(service, task, result -> {
                    if (!result.documentLinks.isEmpty()) {
                        try {
                            ComputeState oldDocument = convertOnlyResultToDocument(result, ComputeState.class);
                            if (ObjectUpdateKind.MODIFY.equals(hs.getObjectUpdateKind())) {
                                updateHostSystem(service, oldDocument, enumerationProgress, hs, false, client);
                            } else {
                                deleteHostSystem(service, enumerationProgress, hs, oldDocument);
                            }
                        } catch (Exception ex) {
                            service.logSevere("Error occurred while processing host system!", ex);
                            enumerationProgress.getHostSystemTracker().track();
                        }
                    } else {
                        enumerationProgress.getHostSystemTracker().track();
                    }
                });
            }
        } catch (Exception ex) {
            service.logSevere("Error occurred while processing host system!", ex);
            enumerationProgress.getHostSystemTracker().track();
        }
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)

Aggregations

ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)29 QueryTask (com.vmware.xenon.services.common.QueryTask)16 Operation (com.vmware.xenon.common.Operation)12 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)11 List (java.util.List)11 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)10 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)10 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)10 Utils (com.vmware.xenon.common.Utils)10 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)9 VsphereEnumerationHelper.convertOnlyResultToDocument (com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument)8 VsphereEnumerationHelper.withTaskResults (com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults)8 ObjectUpdateKind (com.vmware.vim25.ObjectUpdateKind)8 CompletionHandler (com.vmware.xenon.common.Operation.CompletionHandler)8 UriUtils (com.vmware.xenon.common.UriUtils)8 Map (java.util.Map)8 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)7 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)7 VimNames (com.vmware.photon.controller.model.adapters.vsphere.util.VimNames)7 ResourceGroupState (com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState)7