Search in sources :

Example 46 with DeferredResult

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

the class CreatePortgroupFlow method createPortgroupInVsphere.

private ConnectionCallback createPortgroupInVsphere(DeferredResult<Void> result) {
    return (connection, error) -> {
        if (error != null) {
            result.fail(error);
            return;
        }
        // extract moref of the parent DVS switch
        ManagedObjectReference dvsRef = CustomProperties.of(this.networkState).getMoRef(CustomProperties.MOREF);
        DVPortgroupConfigSpec pgSpec = createDefaultPortgroupSpec();
        ManagedObjectReference task;
        try {
            task = connection.getVimPort().createDVPortgroupTask(dvsRef, pgSpec);
        } catch (Exception e) {
            result.fail(e);
            return;
        }
        TaskInfo taskInfo;
        try {
            taskInfo = VimUtils.waitTaskEnd(connection, task);
        } catch (Exception e) {
            result.fail(e);
            return;
        }
        if (taskInfo.getState() != TaskInfoState.SUCCESS) {
            IllegalStateException e = new IllegalStateException(taskInfo.getError().getLocalizedMessage());
            result.fail(e);
            return;
        }
        ManagedObjectReference pg = (ManagedObjectReference) taskInfo.getResult();
        AssertUtil.assertNotNull(pg, "MoRef of dvPortGroup");
        String pgKey = null;
        String dvsUuid = null;
        try {
            GetMoRef get = new GetMoRef(connection);
            Map<String, Object> propValues = get.entityProps(pg, VimPath.pg_config_key, VimPath.pg_config_distributedVirtualSwitch);
            pgKey = (String) propValues.get(VimPath.pg_config_key);
            ManagedObjectReference parentDvSwitch = (ManagedObjectReference) propValues.get(VimPath.pg_config_distributedVirtualSwitch);
            if (parentDvSwitch != null) {
                dvsUuid = get.entityProp(parentDvSwitch, VimPath.dvs_uuid);
            }
        } catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg ignore) {
            getService().logWarning("Cannot retrieve dvPortGroup properties of [%s]: %s", VimUtils.convertMoRefToString(pg), ignore.getLocalizedMessage());
        }
        // store the moref as custom property
        CustomProperties.of(this.subnetState).put(CustomProperties.MOREF, pg).put(DvsProperties.PORT_GROUP_KEY, pgKey).put(DvsProperties.DVS_UUID, dvsUuid);
        OperationContext.setFrom(getOperationContext());
        Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getService().getHost(), this.subnetState.documentSelfLink)).setBody(this.subnetState).setCompletion((o, e) -> {
            if (e != null) {
                result.fail(e);
                return;
            }
            result.complete(null);
            getTaskManager().patchTask(TaskStage.FINISHED);
        }).sendWith(getService());
    };
}
Also used : VimPath(com.vmware.photon.controller.model.adapters.vsphere.util.VimPath) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) OperationContext(com.vmware.xenon.common.OperationContext) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) DistributedVirtualPortgroupPortgroupType(com.vmware.vim25.DistributedVirtualPortgroupPortgroupType) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) TaskInfo(com.vmware.vim25.TaskInfo) VimUtils(com.vmware.photon.controller.model.adapters.vsphere.VimUtils) ConnectionCallback(com.vmware.photon.controller.model.adapters.vsphere.VSphereIOThreadPool.ConnectionCallback) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) CustomProperties(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) SubnetInstanceRequest(com.vmware.photon.controller.model.adapterapi.SubnetInstanceRequest) DeferredResult(com.vmware.xenon.common.DeferredResult) DVPortgroupConfigSpec(com.vmware.vim25.DVPortgroupConfigSpec) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) TaskInfoState(com.vmware.vim25.TaskInfoState) TaskInfo(com.vmware.vim25.TaskInfo) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) Map(java.util.Map) DVPortgroupConfigSpec(com.vmware.vim25.DVPortgroupConfigSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 47 with DeferredResult

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

the class AWSComputeStateCreationAdapterService method updateTagLinks.

/**
 * Updates tag links of existing computes using TagsUtil.
 */
private DeferredResult<AWSComputeStateCreationContext> updateTagLinks(AWSComputeStateCreationContext context) {
    if (context.request.instancesToBeUpdated == null || context.request.instancesToBeUpdated.size() == 0) {
        logFine(() -> "No local compute states to be updated so there are no tags to update.");
        return DeferredResult.completed(context);
    } else {
        List<DeferredResult<Set<String>>> updateCSTagLinksOps = new ArrayList<>();
        for (String instanceId : context.request.instancesToBeUpdated.keySet()) {
            Instance instance = context.request.instancesToBeUpdated.get(instanceId);
            ComputeState existingComputeState = context.request.computeStatesToBeUpdated.get(instanceId);
            Map<String, String> remoteTags = new HashMap<>();
            for (Tag awsInstanceTag : instance.getTags()) {
                if (!awsInstanceTag.getKey().equals(AWSConstants.AWS_TAG_NAME)) {
                    remoteTags.put(awsInstanceTag.getKey(), awsInstanceTag.getValue());
                }
            }
            updateCSTagLinksOps.add(updateLocalTagStates(this, existingComputeState, remoteTags, null));
        }
        return DeferredResult.allOf(updateCSTagLinksOps).thenApply(gnore -> context);
    }
}
Also used : AWSEnumerationUtils.mapInstanceToComputeState(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.mapInstanceToComputeState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Instance(com.amazonaws.services.ec2.model.Instance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 48 with DeferredResult

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

the class AWSSecurityGroupService method getAWSClient.

private DeferredResult<AWSSecurityGroupContext> getAWSClient(AWSSecurityGroupContext context) {
    if (context.request.isMockRequest) {
        return DeferredResult.completed(context);
    }
    DeferredResult<AWSSecurityGroupContext> r = new DeferredResult<>();
    this.clientManager.getOrCreateEC2ClientAsync(context.credentials, context.securityGroup.regionId, this).whenComplete((client, t) -> {
        if (t != null) {
            r.fail(t);
            return;
        }
        context.client = new AWSSecurityGroupClient(this, client);
        r.complete(context);
    });
    return r;
}
Also used : AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 49 with DeferredResult

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

the class AWSSubnetService method getAWSClient.

private DeferredResult<AWSSubnetContext> getAWSClient(AWSSubnetContext context) {
    if (context.request.isMockRequest) {
        return DeferredResult.completed(context);
    }
    DeferredResult<AWSSubnetContext> r = new DeferredResult<>();
    this.clientManager.getOrCreateEC2ClientAsync(context.credentials, context.parentNetwork.regionId, this).whenComplete((client, t) -> {
        if (t != null) {
            r.fail(t);
            return;
        }
        context.client = new AWSNetworkClient(this, client);
        r.complete(context);
    });
    return r;
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 50 with DeferredResult

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

the class AWSS3StorageEnumerationAdapterService method enumerateTags.

/**
 * Calls getBucketTaggingConfiguration() on every bucket to enumerate bucket tags.
 * getBucketTaggingConfiguration() method is region aware and only returns valid results when we
 * call it with the client region same as the S3 bucket region. Since S3 bucket region is not
 * available through API, when a new bucket is discovered, we try to get tags for it by calling
 * getBucketTaggingConfiguration() with client in every AWS region. We then store the client region
 * for which we received successful response as region in DiskState for that S3 bucket. This region
 * in DiskState is then used for any subsequent calls for enumerating tags.
 */
private void enumerateTags(S3StorageEnumerationContext aws, S3StorageEnumerationSubStage next) {
    OperationContext operationContext = OperationContext.getOperationContext();
    this.executorService.submit(() -> {
        OperationContext.restoreOperationContext(operationContext);
        List<DeferredResult<S3ClientHandler>> s3ClientsEnumeratePreviousBuckets = new ArrayList<>();
        // Get a client for that region and make a call to enumerate tags.
        for (Map.Entry<String, DiskState> entry : aws.diskStatesToBeUpdatedByBucketName.entrySet()) {
            // with valid region in subsequent enumeration runs.
            if (entry.getValue().regionId != null) {
                aws.regionsByBucketName.put(entry.getValue().id, entry.getValue().regionId);
            } else {
                logWarning("Null region found in S3 diskState");
                Operation.createDelete(aws.service.getHost(), entry.getValue().documentSelfLink).setReferer(aws.service.getUri()).setBody(getDeletionState(Utils.getNowMicrosUtc())).setCompletion((o, e) -> {
                    if (e != null) {
                        logWarning("Exception deleting diskState with null " + "region [ex=%s]", e.getMessage());
                        return;
                    }
                    logWarning("Deleted diskState with null region [diskState=%s]", Utils.toJsonHtml(entry.getValue()));
                }).sendWith(aws.service);
                continue;
            }
            s3ClientsEnumeratePreviousBuckets.add(aws.clientManager.getOrCreateS3ClientAsync(aws.endpointAuth, entry.getValue().regionId, aws.service).thenApply(s3Client -> {
                S3ClientHandler s3ClientHandler = new S3ClientHandler();
                s3ClientHandler.s3Client = s3Client;
                s3ClientHandler.bucketName = entry.getKey();
                s3ClientHandler.diskState = entry.getValue();
                return s3ClientHandler;
            }));
        }
        // Handler to enumerate new buckets not previously enumerated.
        BiConsumer<List<S3ClientHandler>, Throwable> enumerateNewBuckets = (s3ClientHandlers, throwable) -> {
            OperationContext.restoreOperationContext(operationContext);
            for (Bucket bucket : aws.bucketsToBeCreated) {
                for (S3ClientHandler s3ClientHandler : s3ClientHandlers) {
                    try {
                        BucketTaggingConfiguration bucketTaggingConfiguration = s3ClientHandler.s3Client.getBucketTaggingConfiguration(bucket.getName());
                        aws.regionsByBucketName.put(bucket.getName(), s3ClientHandler.region.getName());
                        if (bucketTaggingConfiguration != null) {
                            aws.tagsByBucketName.put(bucket.getName(), new ConcurrentHashMap<>());
                            bucketTaggingConfiguration.getAllTagSets().forEach(tagSet -> aws.tagsByBucketName.get(bucket.getName()).putAll(tagSet.getAllTags()));
                        }
                        break;
                    } catch (Exception e) {
                        // getbucketTaggingConfiguration().
                        if (e instanceof AmazonS3Exception && (((AmazonS3Exception) e).getStatusCode() == Operation.STATUS_CODE_MOVED_PERM || ((AmazonS3Exception) e).getStatusCode() == Operation.STATUS_CODE_FORBIDDEN || ((AmazonS3Exception) e).getStatusCode() == Operation.STATUS_CODE_BAD_REQUEST)) {
                            continue;
                        } else {
                            logSevere("Exception enumerating tags for S3 bucket with unknown region " + "[endpoint=%s] [region=%s] [ex=%s]", aws.request.original.endpointLink, s3ClientHandler.region.getName(), e.getMessage());
                            continue;
                        }
                    }
                }
            }
            // Once completed, move to next stage.
            aws.subStage = next;
            handleReceivedEnumerationData(aws);
        };
        // Handler to enumerate tags for buckets already previously enumerated.
        BiConsumer<List<S3ClientHandler>, Throwable> enumeratePreviousBuckets = (s3ClientHandlers, ignored) -> {
            OperationContext.restoreOperationContext(operationContext);
            for (S3ClientHandler s3ClientHandler : s3ClientHandlers) {
                try {
                    BucketTaggingConfiguration bucketTaggingConfiguration = s3ClientHandler.s3Client.getBucketTaggingConfiguration(s3ClientHandler.bucketName);
                    if (bucketTaggingConfiguration != null) {
                        aws.tagsByBucketName.put(s3ClientHandler.bucketName, new ConcurrentHashMap<>());
                        bucketTaggingConfiguration.getAllTagSets().forEach(tagSet -> aws.tagsByBucketName.get(s3ClientHandler.bucketName).putAll(tagSet.getAllTags()));
                    }
                } catch (Exception e) {
                    logSevere("Exception enumerating tags for S3 bucket with known region " + "[endpoint=%s] [bucketName=%s - %s] [region=%s] [ex=%s]", aws.request.original.endpointLink, s3ClientHandler.bucketName, s3ClientHandler.diskState.id, s3ClientHandler.diskState.regionId, e.getMessage());
                }
            }
            // For remaining buckets, they have yet to be enumerated. Brute force and try to
            // enumerate tags for these buckets over every region until we find the correct
            // region and then store it in DiskState for future reference.
            List<DeferredResult<S3ClientHandler>> s3ClientBucketsToEnumerate = new ArrayList<>();
            for (Regions region : Regions.values()) {
                // Get an s3 client in the region asynchronously. Once all completed, these
                // clients will be used to test each bucket.
                s3ClientBucketsToEnumerate.add(aws.clientManager.getOrCreateS3ClientAsync(aws.endpointAuth, region.getName(), aws.service).thenApply(s3Client -> {
                    S3ClientHandler s3ClientHandler = new S3ClientHandler();
                    s3ClientHandler.s3Client = s3Client;
                    s3ClientHandler.region = region;
                    return s3ClientHandler;
                }));
            }
            // Once finished, attempt to enumerate each of the "new" buckets.
            DeferredResult.allOf(s3ClientBucketsToEnumerate).whenComplete(enumerateNewBuckets);
        };
        // Enumerate tags of previously enumerated buckets first
        DeferredResult.allOf(s3ClientsEnumeratePreviousBuckets).whenComplete(enumeratePreviousBuckets);
    });
}
Also used : OperationContext(com.vmware.xenon.common.OperationContext) BUCKET_OWNER_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.BUCKET_OWNER_NAME) BucketTaggingConfiguration(com.amazonaws.services.s3.model.BucketTaggingConfiguration) AWSResourceType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSResourceType) QueryTask(com.vmware.xenon.services.common.QueryTask) Utils(com.vmware.xenon.common.Utils) STORAGE_TYPE_S3(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.STORAGE_TYPE_S3) Map(java.util.Map) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) List(java.util.List) TagService(com.vmware.photon.controller.model.resources.TagService) DeferredResult(com.vmware.xenon.common.DeferredResult) TAG_KEY_TYPE(com.vmware.photon.controller.model.constants.PhotonModelConstants.TAG_KEY_TYPE) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationContext(com.vmware.xenon.common.OperationContext) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) HashSet(java.util.HashSet) AWSConstants(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AdapterUtils.createPostOperation(com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) ExecutorService(java.util.concurrent.ExecutorService) AWSConstants.getQueryResultLimit(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.getQueryResultLimit) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Bucket(com.amazonaws.services.s3.model.Bucket) TimeUnit(java.util.concurrent.TimeUnit) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) 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) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) BucketTaggingConfiguration(com.amazonaws.services.s3.model.BucketTaggingConfiguration) Bucket(com.amazonaws.services.s3.model.Bucket) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DeferredResult(com.vmware.xenon.common.DeferredResult)

Aggregations

DeferredResult (com.vmware.xenon.common.DeferredResult)77 ArrayList (java.util.ArrayList)57 Operation (com.vmware.xenon.common.Operation)52 List (java.util.List)49 Collectors (java.util.stream.Collectors)43 StatelessService (com.vmware.xenon.common.StatelessService)42 Utils (com.vmware.xenon.common.Utils)38 Map (java.util.Map)38 HashMap (java.util.HashMap)37 UriUtils (com.vmware.xenon.common.UriUtils)36 DiskService (com.vmware.photon.controller.model.resources.DiskService)33 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)33 URI (java.net.URI)33 Collection (java.util.Collection)32 Set (java.util.Set)31 HashSet (java.util.HashSet)30 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)29 QueryTask (com.vmware.xenon.services.common.QueryTask)28 Consumer (java.util.function.Consumer)28 OperationJoin (com.vmware.xenon.common.OperationJoin)26