use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSUtils method getArnSessionCredentialsAsync.
/**
* Authenticates and returns a DeferredResult set of session credentials for a valid ARN that
* authorizes this system's account ID (validated through
* {@link #AWS_MASTER_ACCOUNT_ACCESS_KEY_PROPERTY} and
* {@link #AWS_MASTER_ACCOUNT_SECRET_KEY_PROPERTY}) and the externalId parameter.
*
* If the system properties are unset, then this call will automatically fail.
*
* @param arn The Amazon Resource Name to validate.
* @param externalId The external ID this ARN has authorized.
* @param region The region to validate within.
* @param executorService The executor service to issue the request.
*/
public static DeferredResult<Credentials> getArnSessionCredentialsAsync(String arn, String externalId, String region, ExecutorService executorService) {
AWSCredentialsProvider serviceAwsCredentials;
try {
serviceAwsCredentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials(AWS_MASTER_ACCOUNT_ACCESS_KEY, AWS_MASTER_ACCOUNT_SECRET_KEY));
} catch (Throwable t) {
return DeferredResult.failed(t);
}
AWSSecurityTokenServiceAsync awsSecurityTokenServiceAsync = AWSSecurityTokenServiceAsyncClientBuilder.standard().withRegion(region).withCredentials(serviceAwsCredentials).withExecutorFactory(() -> executorService).build();
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withRoleArn(arn).withRoleSessionName(UUID.randomUUID().toString()).withDurationSeconds(getArnSessionDurationSeconds()).withExternalId(externalId);
DeferredResult<AssumeRoleResult> r = new DeferredResult<>();
OperationContext operationContext = OperationContext.getOperationContext();
awsSecurityTokenServiceAsync.assumeRoleAsync(assumeRoleRequest, new AsyncHandler<AssumeRoleRequest, AssumeRoleResult>() {
@Override
public void onSuccess(AssumeRoleRequest request, AssumeRoleResult result) {
OperationContext.restoreOperationContext(operationContext);
r.complete(result);
}
@Override
public void onError(Exception ex) {
OperationContext.restoreOperationContext(operationContext);
r.fail(ex);
}
});
return r.thenApply(AssumeRoleResult::getCredentials);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSEndpointAdapterService method validateCredentialsWithRegions.
private DeferredResult<Void> validateCredentialsWithRegions(AuthCredentialsServiceState credentials, String endpointRegion) {
AtomicInteger index = new AtomicInteger(0);
Regions[] regions = Regions.values();
int epRegionIndex = Arrays.stream(regions).map(Regions::getName).collect(Collectors.toList()).indexOf(endpointRegion);
// if found, swap defaultRegion with the first region to optimize
if (epRegionIndex != -1) {
Regions temp = regions[0];
regions[0] = regions[epRegionIndex];
regions[epRegionIndex] = temp;
}
DeferredResult<Void> deferredResult = new DeferredResult<>();
validateCredentialsWithRegions(credentials, index, regions, deferredResult);
return deferredResult;
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSInstanceContext method getInstanceTypeInfo.
private DeferredResult<AWSInstanceContext> getInstanceTypeInfo(AWSInstanceContext context) {
String instanceType = context.child.description.instanceType;
if (instanceType == null) {
instanceType = context.child.description.name;
}
if (instanceType == null) {
String msg = String.format("AWS Instance type not specified for [%s] VM.", context.child.name);
return DeferredResult.failed(new IllegalStateException(msg));
}
URI instanceTypeServiceURI = UriUtils.buildUri(context.service.getHost(), AWSInstanceTypeService.SELF_LINK);
instanceTypeServiceURI = UriUtils.appendQueryParam(instanceTypeServiceURI, URI_PARAM_ENDPOINT, context.child.endpointLink);
instanceTypeServiceURI = UriUtils.appendQueryParam(instanceTypeServiceURI, URI_PARAM_INSTANCE_TYPE, instanceType);
Operation op = Operation.createGet(instanceTypeServiceURI).setReferer(context.service.getHost().getUri());
DeferredResult<InstanceType> dr = context.service.sendWithDeferredResult(op, InstanceType.class);
return dr.thenAccept(type -> {
context.instanceTypeInfo = type;
}).handle((all, err) -> {
if (err != null) {
String msg = String.format("Error getting instance-type info for [%s] VM. Reason [%s]", context.child.name, err.getMessage());
throw new IllegalStateException(msg, err);
}
return context;
});
}
use of com.vmware.xenon.common.DeferredResult 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.xenon.common.DeferredResult 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;
});
}
Aggregations