use of com.vmware.xenon.common.OperationContext 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.OperationContext in project photon-model by vmware.
the class AWSEBSStorageEnumerationAdapterService method validateClient.
/**
* Method to validate
* @param aws
* @param next
*/
private void validateClient(EBSStorageEnumerationContext aws, AWSEBSStorageEnumerationStages next) {
OperationContext opContext = OperationContext.getOperationContext();
AWSUtils.validateCredentials(aws.amazonEC2Client, this.clientManager, aws.endpointAuth, aws.request, this, (describeAvailabilityZonesResult) -> {
OperationContext.restoreOperationContext(opContext);
aws.stage = next;
handleEnumerationRequest(aws);
}, t -> {
OperationContext.restoreOperationContext(opContext);
aws.error = t;
aws.stage = AWSEBSStorageEnumerationStages.ERROR;
handleEnumerationRequest(aws);
}, // onUnaccessible: the region is not accessible so complete the enum Op
() -> {
OperationContext.restoreOperationContext(opContext);
aws.operation.complete();
});
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AWSEnumerationAndCreationAdapterService method getAWSAsyncClient.
/**
* Method to instantiate the AWS Async client for future use
*/
private void getAWSAsyncClient(EnumerationCreationContext aws, AWSEnumerationCreationStages next) {
this.clientManager.getOrCreateEC2ClientAsync(aws.endpointAuth, aws.request.regionId, this).whenComplete((ec2Client, error) -> {
if (error != null) {
aws.error = error;
aws.stage = AWSEnumerationCreationStages.ERROR;
handleEnumerationRequest(aws);
return;
}
aws.amazonEC2Client = ec2Client;
OperationContext opContext = OperationContext.getOperationContext();
AWSUtils.validateCredentials(aws.amazonEC2Client, this.clientManager, aws.endpointAuth, aws.request, this, describeAvailabilityZonesResult -> {
OperationContext.restoreOperationContext(opContext);
aws.stage = next;
handleEnumerationRequest(aws);
}, t -> {
OperationContext.restoreOperationContext(opContext);
aws.error = t;
aws.stage = AWSEnumerationCreationStages.ERROR;
handleEnumerationRequest(aws);
}, // onUnaccessible: the region is not accessible so complete the enum Op
() -> {
OperationContext.restoreOperationContext(opContext);
aws.operation.complete();
});
});
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class PhotonModelUtils method runInExecutor.
/**
* Executes given code in the specified executor.
*
* @param executor
* Executor in which code is to be executed.
* @param runnable
* Code to be executed in the executor.
* @param failure
* failure consumer.
*/
public static void runInExecutor(ExecutorService executor, Runnable runnable, Consumer<Throwable> failure) {
try {
OperationContext operationContext = OperationContext.getOperationContext();
executor.submit(() -> {
OperationContext.restoreOperationContext(operationContext);
try {
runnable.run();
} catch (Throwable runnableExc) {
failure.accept(runnableExc);
}
});
} catch (Throwable executorExc) {
failure.accept(executorExc);
}
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AWSPowerService method powerOn.
private void powerOn(AmazonEC2AsyncClient client, ComputePowerRequest pr, DefaultAdapterContext c) {
OperationContext opContext = OperationContext.getOperationContext();
StartInstancesRequest request = new StartInstancesRequest();
request.withInstanceIds(c.child.id);
client.startInstancesAsync(request, new AsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
public void onSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
OperationContext.restoreOperationContext(opContext);
if (e != null) {
onError(e);
return;
}
updateComputeState(pr, c);
});
}
@Override
public void onError(Exception e) {
OperationContext.restoreOperationContext(opContext);
c.taskManager.patchTaskToFailure(e);
}
});
}
Aggregations