Search in sources :

Example 1 with CREATE_CONTEXT_PROP_NAME

use of com.vmware.photon.controller.model.ComputeProperties.CREATE_CONTEXT_PROP_NAME in project photon-model by vmware.

the class AWSInstanceContext method createSubnetsIfNotExist.

/**
 * For every NIC create AWS Subnet (as specified by {@code AWSNicContext.subnetState}) if it
 * does not exist.
 *
 * @see #getSubnets(AWSInstanceContext)
 */
private DeferredResult<AWSInstanceContext> createSubnetsIfNotExist(AWSInstanceContext context) {
    if (context.nics.isEmpty()) {
        return DeferredResult.completed(context);
    }
    List<DeferredResult<Void>> createSubnetDRs = new ArrayList<>();
    for (AWSNicContext nicCtx : context.nics) {
        if (nicCtx.subnet != null) {
            // No need to create
            continue;
        }
        // Create AWS subnet and set it to nicCtx.subnet {{
        CreateSubnetRequest subnetRequest = new CreateSubnetRequest().withVpcId(nicCtx.vpc.getVpcId()).withCidrBlock(nicCtx.subnetState.subnetCIDR);
        if (nicCtx.subnetState.zoneId != null) {
            subnetRequest.withAvailabilityZone(nicCtx.subnetState.zoneId);
        }
        String msg = "Create AWS subnet + [" + nicCtx.subnetState.name + "]";
        AWSDeferredResultAsyncHandler<CreateSubnetRequest, CreateSubnetResult> createAWSSubnet = new AWSDeferredResultAsyncHandler<CreateSubnetRequest, CreateSubnetResult>(this.service, msg) {

            @Override
            protected DeferredResult<CreateSubnetResult> consumeSuccess(CreateSubnetRequest request, CreateSubnetResult result) {
                nicCtx.subnet = result.getSubnet();
                AWSUtils.tagResourcesWithName(context.amazonEC2Client, nicCtx.subnetState.name, nicCtx.subnet.getSubnetId());
                return DeferredResult.completed(result);
            }
        };
        context.amazonEC2Client.createSubnetAsync(subnetRequest, createAWSSubnet);
        // }}
        // Once AWS subnet creation is done PATCH SubnetState.id {{
        Function<CreateSubnetResult, DeferredResult<Void>> patchSubnetState = (ignore) -> {
            SubnetState patchSubnet = new SubnetState();
            patchSubnet.id = nicCtx.subnet.getSubnetId();
            patchSubnet.documentSelfLink = nicCtx.subnetState.documentSelfLink;
            patchSubnet.customProperties = singletonMap(CREATE_CONTEXT_PROP_NAME, context.computeRequest.resourceLink());
            Operation op = Operation.createPatch(context.service.getHost(), patchSubnet.documentSelfLink).setBody(patchSubnet);
            return context.service.sendWithDeferredResult(op, SubnetState.class).thenAccept(patchedSubnet -> nicCtx.subnetState = patchedSubnet);
        };
        // }}
        // Chain AWS subnet creation with SubnetState patching
        createSubnetDRs.add(createAWSSubnet.toDeferredResult().thenCompose(patchSubnetState));
    }
    return DeferredResult.allOf(createSubnetDRs).handle((all, exc) -> {
        if (exc != null) {
            String msg = String.format("Error creating Subnets 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) CreateSubnetResult(com.amazonaws.services.ec2.model.CreateSubnetResult) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) CreateSubnetRequest(com.amazonaws.services.ec2.model.CreateSubnetRequest) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) AWSDeferredResultAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler) DeferredResult(com.vmware.xenon.common.DeferredResult)

Aggregations

AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 CreateSubnetRequest (com.amazonaws.services.ec2.model.CreateSubnetRequest)1 CreateSubnetResult (com.amazonaws.services.ec2.model.CreateSubnetResult)1 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)1 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)1 DescribeVpcsRequest (com.amazonaws.services.ec2.model.DescribeVpcsRequest)1 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)1 Filter (com.amazonaws.services.ec2.model.Filter)1 InstanceNetworkInterfaceSpecification (com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification)1 Subnet (com.amazonaws.services.ec2.model.Subnet)1 Tag (com.amazonaws.services.ec2.model.Tag)1 Vpc (com.amazonaws.services.ec2.model.Vpc)1 CREATE_CONTEXT_PROP_NAME (com.vmware.photon.controller.model.ComputeProperties.CREATE_CONTEXT_PROP_NAME)1 ComputeInstanceRequest (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)1 AWS_SUBNET_ID_FILTER (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_SUBNET_ID_FILTER)1 AWS_TAG_NAME (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_TAG_NAME)1 AWS_VPC_ID_FILTER (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_VPC_ID_FILTER)1 URI_PARAM_ENDPOINT (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_ENDPOINT)1 URI_PARAM_INSTANCE_TYPE (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.URI_PARAM_INSTANCE_TYPE)1 AWSDeferredResultAsyncHandler (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler)1