use of com.amazonaws.services.ec2.model.CreateSubnetRequest in project photon-model by vmware.
the class AWSSubnetTaskServiceTest method createAwsSubnet.
private Subnet createAwsSubnet() {
if (this.isMock) {
Subnet subnet = new Subnet();
subnet.setSubnetId(UUID.randomUUID().toString());
return subnet;
}
CreateSubnetRequest createRequest = new CreateSubnetRequest((String) this.awsTestContext.get(TestAWSSetupUtils.VPC_KEY), AWS_NON_EXISTING_SUBNET_CIDR);
return this.client.createSubnet(createRequest).getSubnet();
}
use of com.amazonaws.services.ec2.model.CreateSubnetRequest 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;
});
}
use of com.amazonaws.services.ec2.model.CreateSubnetRequest in project photon-model by vmware.
the class AWSNetworkClient method createSubnetAsync.
/**
* Async create the subnet and return it.
*/
public DeferredResult<Subnet> createSubnetAsync(String subnetCidr, String vpcId, String zoneId) {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId).withAvailabilityZone(zoneId);
String message = "Create AWS Subnet with CIDR [" + subnetCidr + "] for vpc id [" + vpcId + "].";
AWSDeferredResultAsyncHandler<CreateSubnetRequest, CreateSubnetResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createSubnetAsync(req, handler);
return handler.toDeferredResult().thenApply(CreateSubnetResult::getSubnet);
}
use of com.amazonaws.services.ec2.model.CreateSubnetRequest in project photon-model by vmware.
the class TestAWSSetupUtils method createOrGetSubnet.
/**
* Creates a Subnet if not exist and return the Subnet id.
*/
public static Subnet createOrGetSubnet(AmazonEC2AsyncClient client, String subnetCidr, String vpcId, String zoneId) {
List<Filter> filters = new ArrayList<>();
Filter vpcFilter = new Filter();
vpcFilter.withName(VPC_KEY);
vpcFilter.withValues(vpcId);
filters.add(vpcFilter);
if (zoneId != null) {
Filter azFilter = new Filter();
azFilter.withName(AVAILABILITY_ZONE_FILTER);
azFilter.withValues(zoneId);
filters.add(azFilter);
}
DescribeSubnetsResult result = client.describeSubnets(new DescribeSubnetsRequest().withFilters(filters));
List<Subnet> defaultSubnets = new ArrayList<>();
result.getSubnets().stream().forEach(subnet -> {
subnet.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(TAG_KEY_FOR_TEST_RESOURCES) && tag.getValue().equalsIgnoreCase(AWS_DEFAULT_SUBNET_NAME)).forEach(tag -> defaultSubnets.add(subnet));
});
if (defaultSubnets != null && !defaultSubnets.isEmpty()) {
return defaultSubnets.get(0);
} else {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId);
if (zoneId != null) {
req.withAvailabilityZone(zoneId);
}
CreateSubnetResult res = client.createSubnet(req);
String subnetId = res.getSubnet().getSubnetId();
tagResources(client, Arrays.asList(subnetId), TAG_KEY_FOR_TEST_RESOURCES, AWS_DEFAULT_SUBNET_NAME);
return res.getSubnet();
}
}
use of com.amazonaws.services.ec2.model.CreateSubnetRequest in project photon-model by vmware.
the class AWSNetworkClient method createSubnet.
/**
* Creates the subnet and return it.
*/
public Subnet createSubnet(String subnetCidr, String vpcId) {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId);
CreateSubnetResult res = this.client.createSubnet(req);
return res.getSubnet();
}
Aggregations