Search in sources :

Example 6 with Operation

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

the class AWSEnumerationAndCreationAdapterService method refreshLoadBalancerInformation.

private void refreshLoadBalancerInformation(EnumerationCreationContext aws, AWSEnumerationRefreshSubStage next) {
    AWSLoadBalancerEnumerationRequest awsLoadBalancerEnumerationRequest = new AWSLoadBalancerEnumerationRequest();
    awsLoadBalancerEnumerationRequest.computeRequest = aws.request;
    awsLoadBalancerEnumerationRequest.enumeratedNetworks = aws.enumeratedNetworks;
    awsLoadBalancerEnumerationRequest.enumeratedSecurityGroups = aws.enumeratedSecurityGroups;
    Operation patchLoadBalancerOperation = Operation.createPatch(this, AWSLoadBalancerEnumerationAdapterService.SELF_LINK).setBody(awsLoadBalancerEnumerationRequest).setReferer(UriUtils.buildUri(getHost().getPublicUri(), getSelfLink()));
    this.getHost().sendWithDeferredResult(patchLoadBalancerOperation).thenAccept(ignore -> {
        logFine(() -> "Successfully enumerated load balancer states");
        aws.refreshSubStage = next;
        processRefreshSubStages(aws);
    }).exceptionally(throwable -> {
        logWarning("Failed to enumerate load balancer states: %s ", throwable.getLocalizedMessage());
        aws.error = throwable;
        aws.refreshSubStage = next;
        processRefreshSubStages(aws);
        return null;
    });
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) ServiceMetadata(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata) QueryTask(com.vmware.xenon.services.common.QueryTask) StringUtils(org.apache.commons.lang3.StringUtils) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) Utils(com.vmware.xenon.common.Utils) StartServicesHelper(com.vmware.photon.controller.model.util.StartServicesHelper) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) ResourceEnumerationTaskService(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService) Reservation(com.amazonaws.services.ec2.model.Reservation) DescribeAvailabilityZonesRequest(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesRequest) AWSComputeDescriptionCreationState(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSComputeDescriptionEnumerationAdapterService.AWSComputeDescriptionCreationState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AWSNetworkEnumerationRequest(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkEnumerationRequest) Collectors(java.util.stream.Collectors) AWSConstants.getQueryPageSize(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryPageSize) List(java.util.List) Stream(java.util.stream.Stream) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) ZoneData(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.ZoneData) AWSLoadBalancerEnumerationRequest(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSLoadBalancerEnumerationAdapterService.AWSLoadBalancerEnumerationRequest) OperationContext(com.vmware.xenon.common.OperationContext) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Filter(com.amazonaws.services.ec2.model.Filter) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) OperationSequence(com.vmware.xenon.common.OperationSequence) Instance(com.amazonaws.services.ec2.model.Instance) AWSSecurityGroupEnumerationResponse(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSSecurityGroupEnumerationAdapterService.AWSSecurityGroupEnumerationResponse) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) AWSNetworkEnumerationResponse(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSNetworkStateEnumerationAdapterService.AWSNetworkEnumerationResponse) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) ServiceMetadata.service(com.vmware.photon.controller.model.util.StartServicesHelper.ServiceMetadata.service) Consumer(java.util.function.Consumer) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) AsyncHandler(com.amazonaws.handlers.AsyncHandler) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) AWSComputeStateCreationRequest(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSComputeStateCreationAdapterService.AWSComputeStateCreationRequest) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Collections(java.util.Collections) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) Operation(com.vmware.xenon.common.Operation) AWSLoadBalancerEnumerationRequest(com.vmware.photon.controller.model.adapters.awsadapter.enumeration.AWSLoadBalancerEnumerationAdapterService.AWSLoadBalancerEnumerationRequest)

Example 7 with Operation

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

the class AWSEnumerationAndDeletionAdapterService method retireComputeStates.

/**
 * Creates operations to retire all the compute states in the local system for which the AWS
 * instance has been terminated/missing from the remote instance.
 */
private void retireComputeStates(EnumerationDeletionContext context) {
    List<Operation> operations = new ArrayList<>();
    // Disks.
    for (ComputeState cs : context.instancesToBeDeleted) {
        ComputeState cps = new ComputeState();
        cps.powerState = PowerState.OFF;
        cps.lifecycleState = LifecycleState.RETIRED;
        Operation operation = Operation.createPatch(this.getHost(), cs.documentSelfLink).setBody(cps).setReferer(getHost().getUri());
        operations.add(operation);
    }
    // Kick off patch operations with a join handler.
    if (operations == null || operations.size() == 0) {
        logFine(() -> "No local compute states to be deleted.");
        deleteResourcesInLocalSystem(context);
        return;
    }
    OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
        if (exc != null) {
            logSevere(() -> String.format("Failure retiring local compute states: %s ", Utils.toString(exc)));
            deleteResourcesInLocalSystem(context);
            return;
        }
        logFine(() -> "Successfully retired local compute states.");
        deleteResourcesInLocalSystem(context);
        return;
    };
    OperationJoin joinOp = OperationJoin.create(operations);
    joinOp.setCompletion(joinCompletion);
    joinOp.sendWith(getHost());
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Arrays(java.util.Arrays) OperationContext(com.vmware.xenon.common.OperationContext) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) Filter(com.amazonaws.services.ec2.model.Filter) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) Instance(com.amazonaws.services.ec2.model.Instance) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) Reservation(com.amazonaws.services.ec2.model.Reservation) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) AWS_INVALID_INSTANCE_ID_ERROR_CODE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INVALID_INSTANCE_ID_ERROR_CODE) AWS_INSTANCE_ID_PREFIX(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_INSTANCE_ID_PREFIX) List(java.util.List) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) AWSUtils.getAWSNonTerminatedInstancesFilter(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getAWSNonTerminatedInstancesFilter) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) AsyncHandler(com.amazonaws.handlers.AsyncHandler) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation)

Example 8 with Operation

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

the class AWSMissingResourcesEnumerationService method createLinkedComputeStates.

/**
 *  creates new linked account endpoint states of a primary account
 * @param context
 */
private void createLinkedComputeStates(AwsMissingResourcesEnumContext context) {
    AtomicInteger completionCounter = new AtomicInteger(context.request.missingLinkedAccountIds.size());
    AtomicBoolean isSuccessful = new AtomicBoolean(true);
    context.request.missingLinkedAccountIds.forEach(linkedAccountId -> {
        ComputeDescription computeDescription = populateComputeDescription(context, linkedAccountId);
        ComputeState computeState = populateComputeState(context, linkedAccountId);
        computeState.descriptionLink = computeDescription.documentSelfLink;
        Operation csOp = Operation.createPost(this, ComputeService.FACTORY_LINK).setBody(computeState);
        Operation cdOp = Operation.createPost(this, ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
        JoinedCompletionHandler completionHandler = (ops, exs) -> {
            if (exs != null && !exs.isEmpty()) {
                logWarning(() -> String.format("Error creating missing resources for" + " account with ID %s.", linkedAccountId));
                isSuccessful.set(false);
            } else {
                logInfo(() -> String.format("Created missing resources for account " + "with ID %s.", linkedAccountId));
            }
            if (completionCounter.decrementAndGet() == 0) {
                if (isSuccessful.get()) {
                    context.requestOp.complete();
                } else {
                    context.requestOp.fail(new Exception("Failed to create missing resources " + "for atleast one linked account."));
                }
            }
        };
        Operation.createGet(this, computeDescription.documentSelfLink).setReferer(this.getUri()).setCompletion((o, e) -> {
            if (e != null && (e instanceof ServiceNotFoundException || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND)) {
                OperationSequence.create(cdOp).next(csOp).setCompletion(completionHandler).sendWith(this);
            } else {
                OperationSequence.create(csOp).setCompletion(completionHandler).sendWith(this);
            }
        }).sendWith(this);
    });
}
Also used : JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) UUID(java.util.UUID) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) StandardCharsets(java.nio.charset.StandardCharsets) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) Utils(com.vmware.xenon.common.Utils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) OperationSequence(com.vmware.xenon.common.OperationSequence) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException) Operation(com.vmware.xenon.common.Operation) ServiceNotFoundException(com.vmware.xenon.common.ServiceHost.ServiceNotFoundException)

Example 9 with Operation

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

the class AWSRegionEnumerationAdapterService method handlePost.

@Override
public void handlePost(Operation post) {
    List<RegionInfo> regions = Arrays.stream(Regions.values()).map(r -> new RegionInfo(r.getName(), r.getName())).collect(Collectors.toList());
    RegionEnumerationResponse result = new RegionEnumerationResponse();
    result.regions = regions;
    post.setBody(result);
    post.complete();
}
Also used : Arrays(java.util.Arrays) List(java.util.List) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo) Collectors(java.util.stream.Collectors) AWSUriPaths(com.vmware.photon.controller.model.adapters.awsadapter.AWSUriPaths) Regions(com.amazonaws.regions.Regions) RegionEnumerationResponse(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse) RegionInfo(com.vmware.photon.controller.model.adapterapi.RegionEnumerationResponse.RegionInfo)

Example 10 with Operation

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

the class TestAWSEnumerationDocumentCountInLongRun method storeDocumentLinksFromNetworkInterfaceStates.

/**
 * Gets and stores network interface ids from network interface links and security group links, subnet
 * links by querying network interface ids.
 */
private void storeDocumentLinksFromNetworkInterfaceStates() {
    // If there are no network interface links, return.
    if (this.networkInterfaceLinks.isEmpty()) {
        return;
    }
    // Get network interface IDs from network interface links.
    for (String s : this.networkInterfaceLinks) {
        Operation op = Operation.createGet(UriUtils.buildUri(this.host.getUri(), s)).setReferer(this.host.getUri());
        Operation response = this.host.waitForResponse(op);
        Assert.assertTrue("Error retrieving network interface IDs", response.getStatusCode() == 200);
        NetworkInterfaceState state = response.getBody(NetworkInterfaceState.class);
        this.networkInterfaceIds.add(state.id);
    }
    // Query all network interface documents associated with list of network interface IDs.
    QueryTask.Query networkInterfaceQuery = QueryTask.Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, this.networkInterfaceIds).build();
    QueryTask q = QueryTask.Builder.createDirectTask().setQuery(networkInterfaceQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
    Operation queryNetworkInterface = QueryUtils.createQueryTaskOperation(this.host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
    Operation queryResponse = this.host.waitForResponse(queryNetworkInterface);
    Assert.assertTrue("Error retrieving network interface states", queryResponse.getStatusCode() == 200);
    QueryTask qt = queryResponse.getBody(QueryTask.class);
    // Store security group links and subnet links.
    for (String documentLink : qt.results.documentLinks) {
        NetworkInterfaceState nis = Utils.fromJson(qt.results.documents.get(documentLink), NetworkInterfaceState.class);
        this.securityGroupLinks.addAll(nis.securityGroupLinks);
        this.subnetLinks.add(nis.subnetLink);
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) 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