use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler in project photon-model by vmware.
the class AWSEndpointAdapterService method validateCredentials.
private DeferredResult<Void> validateCredentials(AmazonEC2AsyncClient client) {
AWSDeferredResultAsyncHandler<DescribeAvailabilityZonesRequest, DescribeAvailabilityZonesResult> asyncHandler = new AWSDeferredResultAsyncHandler<>(this, "Validate Credentials");
client.describeAvailabilityZonesAsync(asyncHandler);
return asyncHandler.toDeferredResult().handle((describeAvailabilityZonesResult, e) -> {
if (e instanceof AmazonServiceException) {
AmazonServiceException ase = (AmazonServiceException) e;
if (ase.getStatusCode() == STATUS_CODE_UNAUTHORIZED) {
throw new LocalizableValidationException(e, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE, PHOTON_MODEL_ADAPTER_UNAUTHORIZED_MESSAGE_CODE);
}
}
return null;
});
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler in project photon-model by vmware.
the class AWSInstanceContext method getSubnets.
/**
* For every NIC lookup associated AWS Subnet as specified by
* {@code AWSNicContext.subnetState.id}. If any of the subnets is not found then
* {@code AWSNicContext.subnet} is not populated. That's an indicator the subnet should be
* created.
*/
private DeferredResult<AWSInstanceContext> getSubnets(AWSInstanceContext context) {
if (context.nics.isEmpty()) {
return DeferredResult.completed(context);
}
List<DeferredResult<DescribeSubnetsResult>> getSubnetDRs = new ArrayList<>();
for (AWSNicContext nicCtx : context.nics) {
DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest().withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList(nicCtx.networkState.id))).withFilters(new Filter(AWS_SUBNET_ID_FILTER, singletonList(nicCtx.subnetState.id)));
String msg = "Getting AWS Subnet [" + nicCtx.networkState.id + "/" + nicCtx.subnetState.id + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.child.name + "] VM";
AWSDeferredResultAsyncHandler<DescribeSubnetsRequest, DescribeSubnetsResult> subnetHandler = new AWSDeferredResultAsyncHandler<DescribeSubnetsRequest, DescribeSubnetsResult>(this.service, msg) {
@Override
protected DeferredResult<DescribeSubnetsResult> consumeSuccess(DescribeSubnetsRequest request, DescribeSubnetsResult result) {
// The subnet specified might not exist. It's OK cause it will be created.
if (!result.getSubnets().isEmpty()) {
nicCtx.subnet = result.getSubnets().get(0);
}
return DeferredResult.completed(result);
}
};
context.amazonEC2Client.describeSubnetsAsync(subnetRequest, subnetHandler);
getSubnetDRs.add(subnetHandler.toDeferredResult());
}
return DeferredResult.allOf(getSubnetDRs).handle((all, exc) -> {
if (exc != null) {
String msg = String.format("Error getting Subnets from AWS for [%s] VM.", context.child.name);
throw new IllegalStateException(msg, exc);
}
return context;
});
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler 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.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler in project photon-model by vmware.
the class AWSLoadBalancerService method registerInstances.
private DeferredResult<AWSLoadBalancerContext> registerInstances(AWSLoadBalancerContext context) {
// Do not try to assign instances if there aren't any
if (context.instanceIdsToRegister.isEmpty()) {
return DeferredResult.completed(context);
}
RegisterInstancesWithLoadBalancerRequest request = buildInstanceRegistrationRequest(context);
String message = "Registering instances to AWS Load Balancer with name [" + context.loadBalancerStateExpanded.name + "]";
AWSDeferredResultAsyncHandler<RegisterInstancesWithLoadBalancerRequest, RegisterInstancesWithLoadBalancerResult> handler = new AWSDeferredResultAsyncHandler<>(this, message);
context.client.registerInstancesWithLoadBalancerAsync(request, handler);
return handler.toDeferredResult().thenApply(ignore -> context);
}
use of com.vmware.photon.controller.model.adapters.awsadapter.util.AWSDeferredResultAsyncHandler 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;
});
}
Aggregations