Search in sources :

Example 26 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureComputeDiskDay2Service method updateDiskState.

/**
 * Update status and LUN of DiskState
 */
private DeferredResult<Operation> updateDiskState(AzureComputeDiskDay2Context context) {
    DiskState diskState = context.diskState;
    if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
        diskState.status = DiskService.DiskStatus.ATTACHED;
    } else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
        diskState.status = DiskService.DiskStatus.AVAILABLE;
        diskState.customProperties.remove(DISK_CONTROLLER_NUMBER);
    }
    if (!context.request.isMockRequest) {
        DataDisk dataDisk = context.provisionedVm.inner().storageProfile().dataDisks().stream().filter(dd -> diskState.name.equalsIgnoreCase(dd.name())).findFirst().orElse(null);
        if (dataDisk != null) {
            if (diskState.customProperties == null) {
                diskState.customProperties = new HashMap<>();
            }
            diskState.customProperties.put(DISK_CONTROLLER_NUMBER, String.valueOf(dataDisk.lun()));
        }
    }
    Operation diskPatchOp = null;
    if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
        diskPatchOp = Operation.createPatch(createInventoryUri(this.getHost(), diskState.documentSelfLink)).setBody(diskState).setReferer(this.getUri());
    } else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
        diskPatchOp = Operation.createPut(createInventoryUri(this.getHost(), diskState.documentSelfLink)).setBody(diskState).setReferer(this.getUri());
    }
    return this.sendWithDeferredResult(diskPatchOp);
}
Also used : DataDisk(com.microsoft.azure.management.compute.DataDisk) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 27 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureSubscriptionEndpointCreationService method handleSubscriptionEndpointCreationRequest.

private void handleSubscriptionEndpointCreationRequest(AzureSubscriptionEndpointCreationRequest request, Operation parentOp) {
    // If request.subscriptionId is null, just complete the parentOp
    if (request.subscriptionId == null) {
        parentOp.complete();
        return;
    }
    // Fail the request if resourceReference to Azure EA endpoint is not provided
    if (request.resourceReference == null) {
        parentOp.fail(new IllegalArgumentException("reference to parent endpointLink is required"));
    }
    Operation endpointStateOp = Operation.createGet(request.resourceReference).setCompletion((o, e) -> {
        if (e != null) {
            parentOp.fail(e);
            return;
        }
        EndpointState endpointState = o.getBody(EndpointState.class);
        createSubscriptionEndpoint(endpointState, request, parentOp);
    });
    this.sendRequest(endpointStateOp);
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Operation(com.vmware.xenon.common.Operation)

Example 28 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureSubscriptionEndpointCreationService method createSubscriptionEndpoint.

private void createSubscriptionEndpoint(EndpointState azureEaEndpoint, AzureSubscriptionEndpointCreationRequest request, Operation parentOp) {
    Operation authOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), AuthCredentialsService.FACTORY_LINK));
    Operation cdOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), ComputeDescriptionService.FACTORY_LINK));
    Operation csOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), ComputeService.FACTORY_LINK));
    Operation endPointOp = Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), EndpointService.FACTORY_LINK));
    AuthCredentialsServiceState authCredentialsState = createAuthCredentialsState(azureEaEndpoint, request);
    EndpointState endpointState = createEndpointState(azureEaEndpoint, request);
    ComputeDescription computeDescState = AzureUtils.constructAzureSubscriptionComputeDescription(endpointState.documentSelfLink, azureEaEndpoint.tenantLinks, request.subscriptionId, null, null, endpointState.computeLink);
    authOp.setBody(authCredentialsState);
    OperationSequence sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            handleFailure(exs, parentOp);
            return;
        }
        Operation o = ops.get(authOp.getId());
        AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
        computeDescState.authCredentialsLink = authState.documentSelfLink;
        endpointState.authCredentialsLink = authState.documentSelfLink;
        cdOp.setBody(computeDescState);
    }).next(cdOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            handleFailure(exs, parentOp);
            return;
        }
        Operation o = ops.get(cdOp.getId());
        ComputeDescription cd = o.getBody(ComputeDescription.class);
        ComputeState cs = AzureUtils.constructAzureSubscriptionComputeState(endpointState.documentSelfLink, cd.documentSelfLink, azureEaEndpoint.tenantLinks, request.subscriptionId, azureEaEndpoint.resourcePoolLink, getCustomPropertiesMap(endpointState, request), null, endpointState.computeLink);
        csOp.setBody(cs);
        endpointState.computeDescriptionLink = cd.documentSelfLink;
    }).next(csOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            handleFailure(exs, parentOp);
            return;
        }
        Operation o = ops.get(csOp.getId());
        ComputeState cs = o.getBody(ComputeState.class);
        endpointState.computeLink = cs.documentSelfLink;
        endPointOp.setBody(endpointState);
    }).next(endPointOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            handleFailure(exs, parentOp);
            return;
        }
        Operation o = ops.get(endPointOp.getId());
        EndpointState es = o.getBody(EndpointState.class);
        parentOp.setBody(es);
        parentOp.complete();
    });
    sequence.sendWith(this);
}
Also used : EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) OperationSequence(com.vmware.xenon.common.OperationSequence) Operation(com.vmware.xenon.common.Operation)

Example 29 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureSubscriptionsEnumerationService method createResources.

private void createResources(AzureSubscriptionsEnumerationContext enumerationContext, AzureCostComputeEnumerationStages nextStage) {
    // Go through new subscriptions and create corresponding ComputeState
    // and ComputeDescription for them
    Collection<ComputeState> computesToCreate = new ArrayList<>();
    // Create ComputeDescription
    Collection<Operation> createComputeDescOps = enumerationContext.idToSubscription.values().stream().map(subscription -> Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), ComputeDescriptionService.FACTORY_LINK)).setBody(AzureUtils.constructAzureSubscriptionComputeDescription(enumerationContext.parent.endpointLink, enumerationContext.parent.tenantLinks, subscription.entityId, null, null, enumerationContext.parent.documentSelfLink)).setCompletion((o, e) -> {
        if (e != null) {
            logSevere(() -> String.format("Compute description creation " + " failed for azure subscription %s", subscription.entityId));
            return;
        }
        ComputeDescription cd = o.getBody(ComputeDescription.class);
        String csName = AzureUtils.constructSubscriptionName(subscription);
        computesToCreate.add(AzureUtils.constructAzureSubscriptionComputeState(enumerationContext.parent.endpointLink, cd.documentSelfLink, enumerationContext.parent.tenantLinks, csName, enumerationContext.parent.resourcePoolLink, getPropertiesMap(enumerationContext, subscription, true), null, enumerationContext.parent.documentSelfLink));
    })).collect(Collectors.toList());
    joinOperationAndSendRequest(createComputeDescOps, enumerationContext, (subsEnumCtx) -> {
        // Now create the ComputeState
        Collection<Operation> createComputeOps = computesToCreate.stream().map(computeState -> Operation.createPost(UriUtils.extendUri(getInventoryServiceUri(), ComputeService.FACTORY_LINK)).setBody(computeState).setCompletion((o, e) -> {
            if (e != null) {
                logSevere(() -> String.format("Compute state creation failed for azure" + " subscription %s", computeState.name));
            }
        })).collect(Collectors.toList());
        joinOperationAndSendRequest(createComputeOps, subsEnumCtx, (enumCtx) -> {
            enumCtx.stage = nextStage;
            handleAzureSubscriptionsEnumerationRequest(enumCtx);
        });
    });
}
Also used : QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) HashMap(java.util.HashMap) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) BaseAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ResourceRequest(com.vmware.photon.controller.model.adapterapi.ResourceRequest) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) Utils(com.vmware.xenon.common.Utils) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Example 30 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureComputeHostStatsGatherer method handleComputeQueryCompletion.

/**
 * Get all the children computes and create a query task for each to query the metrics.
 */
private void handleComputeQueryCompletion(QueryTask queryTask, Throwable failure, AzureStatsDataHolder statsData) {
    if (failure != null) {
        logSevere(failure.getMessage());
        statsData.error = failure;
        statsData.stage = ComputeHostMetricsStages.ERROR;
        handleMetricDiscovery(statsData);
        return;
    }
    if (queryTask == null || queryTask.results == null) {
        sendFailurePatch(statsData, new RuntimeException(String.format("Unexpected query result for '%s'", queryTask.documentSelfLink)));
        return;
    }
    if (queryTask.results.documentLinks != null) {
        statsData.childComputeLinks.addAll(queryTask.results.documentLinks);
    }
    if (queryTask.results.nextPageLink != null) {
        Operation op = Operation.createGet(createInventoryUri(this.getHost(), queryTask.results.nextPageLink));
        sendWithDeferredResult(op).whenComplete((o, e) -> handleComputeQueryCompletion(o.getBody(QueryTask.class), e, statsData));
        return;
    }
    int computeCount = Math.toIntExact(statsData.childComputeLinks.size());
    // No children found, proceed to finish
    if (computeCount <= 0) {
        statsData.stage = ComputeHostMetricsStages.FINISHED;
        handleMetricDiscovery(statsData);
        return;
    }
    // Create multiple operations, one each for a VM compute.
    List<Operation> statOperations = new ArrayList<>(computeCount);
    for (String computeLink : statsData.childComputeLinks) {
        Operation statsOp = getStatsQueryTaskOperation(statsData, computeLink);
        statOperations.add(statsOp);
    }
    OperationJoin.create(statOperations).setCompletion((ops, failures) -> handleQueryTaskResponseAndConsolidateStats(ops, failures, statsData)).sendWith(this, 50);
}
Also used : ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) HashMap(java.util.HashMap) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) URI(java.net.URI) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) ExecutorService(java.util.concurrent.ExecutorService) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) AzureConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.getQueryResultLimit) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) Consumer(java.util.function.Consumer) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Aggregations

Operation (com.vmware.xenon.common.Operation)391 URI (java.net.URI)142 ArrayList (java.util.ArrayList)132 QueryTask (com.vmware.xenon.services.common.QueryTask)118 List (java.util.List)118 Utils (com.vmware.xenon.common.Utils)111 StatelessService (com.vmware.xenon.common.StatelessService)108 UriUtils (com.vmware.xenon.common.UriUtils)106 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)98 Map (java.util.Map)92 HashMap (java.util.HashMap)90 OperationJoin (com.vmware.xenon.common.OperationJoin)86 Query (com.vmware.xenon.services.common.QueryTask.Query)86 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)82 Collectors (java.util.stream.Collectors)79 HashSet (java.util.HashSet)78 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)73 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)70 DeferredResult (com.vmware.xenon.common.DeferredResult)69 Consumer (java.util.function.Consumer)69