Search in sources :

Example 66 with DeferredResult

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

the class VSphereAdapterImageEnumerationService method collectAllEndpointImages.

private DeferredResult<Set<String>> collectAllEndpointImages(ImageEnumerateRequest req) {
    Builder builder = Query.Builder.create().addKindFieldClause(ImageState.class).addFieldClause(ImageState.FIELD_NAME_ENDPOINT_LINK, req.resourceLink());
    QueryTask task = QueryTask.Builder.createDirectTask().setQuery(builder.build()).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).build();
    DeferredResult<Set<String>> res = new DeferredResult<>();
    Set<String> imageLinks = new ConcurrentSkipListSet<>();
    QueryUtils.startInventoryQueryTask(this, task).whenComplete((result, e) -> {
        if (e != null) {
            res.complete(new HashSet<>());
            return;
        }
        if (result.results.nextPageLink == null) {
            res.complete(imageLinks);
            return;
        }
        Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), result.results.nextPageLink)).setCompletion(makeCompletion(imageLinks, res)).sendWith(this);
    });
    return res;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 67 with DeferredResult

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

the class DeletePortgroupFlow method deletePortgroup.

private DeferredResult<Void> deletePortgroup(Operation stateOp) {
    this.networkState = stateOp.getBody(NetworkState.class);
    DeferredResult<Void> res = new DeferredResult<>();
    ManagedObjectReference pgRef = CustomProperties.of(this.subnetState).getMoRef(CustomProperties.MOREF);
    if (pgRef == null) {
        // a provisioning has failed mid-flight, just proceed, skipping actual vc call
        res.complete(null);
        return res;
    }
    if (IAAS_API_ENABLED) {
        if (this.operation != null) {
            return DeferredResult.failed(new IllegalArgumentException("Unable to authenticate"));
        }
        SessionUtil.retrieveExternalToken(getService(), this.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
            if (throwable != null) {
                res.fail(throwable);
                return;
            }
            getVsphereIoPool().submit(this.networkState.adapterManagementReference, authCredentialsServiceState, deletePortgroupInVsphere(res));
        });
    } else {
        getVsphereIoPool().submit(getService(), this.networkState.adapterManagementReference, this.networkState.authCredentialsLink, deletePortgroupInVsphere(res));
    }
    return res;
}
Also used : NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) DeferredResult(com.vmware.xenon.common.DeferredResult) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 68 with DeferredResult

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

the class AWSInstanceContext method createSecurityGroup.

/**
 * For the provided SecurityGroupState, create corresponding SecurityGroup on AWS.
 */
private DeferredResult<Void> createSecurityGroup(AWSSecurityGroupClient client, AWSInstanceContext context, AWSNicContext nicCtx, SecurityGroupState missingSecurityGroupState) {
    // Once AWS security group creation is done PATCH SecurityGroupState.id {{
    Function<String, DeferredResult<SecurityGroupState>> patchSecurityGroupState = (ignore) -> {
        SecurityGroupState patchSecurityGroup = new SecurityGroupState();
        // updated after creating SG in AWS
        patchSecurityGroup.id = missingSecurityGroupState.id;
        Operation op = Operation.createPatch(context.service.getHost(), missingSecurityGroupState.documentSelfLink).setBody(patchSecurityGroup);
        return context.service.sendWithDeferredResult(op, SecurityGroupState.class);
    };
    // use state name for both group name and description
    return client.createSecurityGroupAsync(missingSecurityGroupState.name, missingSecurityGroupState.name, nicCtx.vpc.getVpcId()).thenCompose(sgId -> {
        nicCtx.securityGroupIds.add(sgId);
        // keep the new ID in order to patch the state after creation is done
        missingSecurityGroupState.id = sgId;
        return DeferredResult.completed(sgId);
    }).thenCompose(patchSecurityGroupState).thenApply(ignore -> (Void) null);
}
Also used : InstanceNetworkInterfaceSpecification(com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) CREATE_CONTEXT_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.CREATE_CONTEXT_PROP_NAME) Function(java.util.function.Function) CreateSubnetResult(com.amazonaws.services.ec2.model.CreateSubnetResult) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) URI_PARAM_INSTANCE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_INSTANCE_TYPE) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Filter(com.amazonaws.services.ec2.model.Filter) Collections.singletonMap(java.util.Collections.singletonMap) URI(java.net.URI) Subnet(com.amazonaws.services.ec2.model.Subnet) StatelessService(com.vmware.xenon.common.StatelessService) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Vpc(com.amazonaws.services.ec2.model.Vpc) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Collectors(java.util.stream.Collectors) AWS_VPC_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER) URI_PARAM_ENDPOINT(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_ENDPOINT) InstanceType(com.vmware.photon.controller.model.support.InstanceTypeList.InstanceType) List(java.util.List) BaseComputeInstanceContext(com.vmware.photon.controller.model.adapters.util.instance.BaseComputeInstanceContext) AWS_SUBNET_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_SUBNET_ID_FILTER) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) DiskService(com.vmware.photon.controller.model.resources.DiskService) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) CreateSubnetRequest(com.amazonaws.services.ec2.model.CreateSubnetRequest) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) Operation(com.vmware.xenon.common.Operation) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 69 with DeferredResult

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

the class AWSInstanceContext method createSecurityGroupsIfNotExist.

/**
 * When there are SecurityGroupStates for the new VM to be provisioned, for which there are no
 * corresponding existing SecurityGroups in AWS, the missing SecurityGroups are created
 */
private DeferredResult<AWSInstanceContext> createSecurityGroupsIfNotExist(AWSInstanceContext context) {
    if (context.nics.isEmpty()) {
        return DeferredResult.completed(context);
    }
    List<DeferredResult<Void>> createSecurityGroupsDRs = new ArrayList<>();
    AWSSecurityGroupClient sgClient = new AWSSecurityGroupClient(context.amazonEC2Client);
    for (AWSNicContext nicCtx : context.nics) {
        if (nicCtx.securityGroupStates == null) {
            continue;
        }
        Collection<String> foundIds = nicCtx.securityGroupIds;
        List<SecurityGroupState> missingSecurityGroupStates = nicCtx.securityGroupStates.stream().filter(sgState -> !foundIds.contains(sgState.id)).collect(Collectors.toList());
        for (SecurityGroupState missingSGState : missingSecurityGroupStates) {
            DeferredResult<Void> createSGWithRulesDR = createSecurityGroup(sgClient, context, nicCtx, missingSGState).thenCompose(ignore -> createIngressRules(context, nicCtx, missingSGState, sgClient)).thenCompose(ignore -> createEgressRules(context, nicCtx, missingSGState, sgClient)).thenApply(ignore -> (Void) null);
            createSecurityGroupsDRs.add(createSGWithRulesDR);
        }
    }
    return DeferredResult.allOf(createSecurityGroupsDRs).handle((all, exc) -> {
        if (exc != null) {
            String msg = String.format("Error creating SecurityGroups in AWS for [%s] VM.", context.child.name);
            throw new IllegalStateException(msg, exc);
        }
        return context;
    });
}
Also used : InstanceNetworkInterfaceSpecification(com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) CREATE_CONTEXT_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.CREATE_CONTEXT_PROP_NAME) Function(java.util.function.Function) CreateSubnetResult(com.amazonaws.services.ec2.model.CreateSubnetResult) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) HashSet(java.util.HashSet) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) URI_PARAM_INSTANCE_TYPE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_INSTANCE_TYPE) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Filter(com.amazonaws.services.ec2.model.Filter) Collections.singletonMap(java.util.Collections.singletonMap) URI(java.net.URI) Subnet(com.amazonaws.services.ec2.model.Subnet) StatelessService(com.vmware.xenon.common.StatelessService) AWS_TAG_NAME(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME) Vpc(com.amazonaws.services.ec2.model.Vpc) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Collectors(java.util.stream.Collectors) AWS_VPC_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER) URI_PARAM_ENDPOINT(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_ENDPOINT) InstanceType(com.vmware.photon.controller.model.support.InstanceTypeList.InstanceType) List(java.util.List) BaseComputeInstanceContext(com.vmware.photon.controller.model.adapters.util.instance.BaseComputeInstanceContext) AWS_SUBNET_ID_FILTER(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_SUBNET_ID_FILTER) Tag(com.amazonaws.services.ec2.model.Tag) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) DiskService(com.vmware.photon.controller.model.resources.DiskService) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) CreateSubnetRequest(com.amazonaws.services.ec2.model.CreateSubnetRequest) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ArrayList(java.util.ArrayList) AWSSecurityGroupClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 70 with DeferredResult

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

the class AWSInstanceContext method getVPCs.

/**
 * For every NIC lookup associated AWS VPC as specified by
 * {@code AWSNicContext.networkState.id}. If any of the VPCs is not found then complete with an
 * exception.
 */
private DeferredResult<AWSInstanceContext> getVPCs(AWSInstanceContext context) {
    if (context.nics.isEmpty()) {
        return DeferredResult.completed(context);
    }
    List<DeferredResult<DescribeVpcsResult>> getVpcDRs = new ArrayList<>();
    for (AWSNicContext nicCtx : context.nics) {
        DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList(nicCtx.networkState.id)));
        String msg = "Getting AWS VPC [" + nicCtx.networkState.id + "/" + nicCtx.networkState.name + "/" + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.child.name + "] VM";
        AWSDeferredResultAsyncHandler<DescribeVpcsRequest, DescribeVpcsResult> handler = new AWSDeferredResultAsyncHandler<DescribeVpcsRequest, DescribeVpcsResult>(this.service, msg) {

            @Override
            protected DeferredResult<DescribeVpcsResult> consumeSuccess(DescribeVpcsRequest request, DescribeVpcsResult result) {
                if (result.getVpcs().isEmpty()) {
                    String msg = String.format("VPC with [%s] id is not found in AWS for [%s] NIC of [%s] VM.", nicCtx.networkState.id, nicCtx.nicStateWithDesc.name, context.child.name);
                    return DeferredResult.failed(new IllegalStateException(msg));
                }
                nicCtx.vpc = result.getVpcs().get(0);
                return DeferredResult.completed(result);
            }
        };
        context.amazonEC2Client.describeVpcsAsync(vpcRequest, handler);
        getVpcDRs.add(handler.toDeferredResult());
    }
    return DeferredResult.allOf(getVpcDRs).handle((all, exc) -> {
        if (exc != null) {
            String msg = String.format("Error getting VPCs from AWS for [%s] VM.", context.child.name);
            throw new IllegalStateException(msg, exc);
        }
        return context;
    });
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Filter(com.amazonaws.services.ec2.model.Filter) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) ArrayList(java.util.ArrayList) 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