use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AWSUtils method setDeleteOnTerminateAttribute.
public static DeferredResult<DiskService.DiskState> setDeleteOnTerminateAttribute(AmazonEC2AsyncClient client, String instanceId, Map<String, Pair<String, Boolean>> deleteDiskMapByDeviceName, OperationContext opCtx) {
List<InstanceBlockDeviceMappingSpecification> instanceBlockDeviceMappingSpecificationList = deleteDiskMapByDeviceName.entrySet().stream().map(entry -> new InstanceBlockDeviceMappingSpecification().withDeviceName(entry.getKey()).withEbs(new EbsInstanceBlockDeviceSpecification().withDeleteOnTermination(entry.getValue().right).withVolumeId(entry.getValue().left))).collect(Collectors.toList());
DeferredResult<DiskService.DiskState> modifyInstanceAttrDr = new DeferredResult();
ModifyInstanceAttributeRequest modifyInstanceAttrReq = new ModifyInstanceAttributeRequest().withInstanceId(instanceId).withAttribute(InstanceAttributeName.BlockDeviceMapping).withBlockDeviceMappings(instanceBlockDeviceMappingSpecificationList);
AWSAsyncHandler<ModifyInstanceAttributeRequest, ModifyInstanceAttributeResult> modifyInstanceAttrHandler = new AWSAsyncHandler<ModifyInstanceAttributeRequest, ModifyInstanceAttributeResult>() {
@Override
protected void handleError(Exception exception) {
OperationContext.restoreOperationContext(opCtx);
modifyInstanceAttrDr.fail(exception);
}
@Override
protected void handleSuccess(ModifyInstanceAttributeRequest request, ModifyInstanceAttributeResult result) {
OperationContext.restoreOperationContext(opCtx);
modifyInstanceAttrDr.complete(new DiskService.DiskState());
}
};
client.modifyInstanceAttributeAsync(modifyInstanceAttrReq, modifyInstanceAttrHandler);
return modifyInstanceAttrDr;
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AWSComputeDiskDay2Service method startInstance.
/**
* start the instance and on success updates the disk and compute state to reflect the detach information.
*/
private void startInstance(AmazonEC2AsyncClient client, DiskContext c, DeferredResult<DiskContext> dr, OperationContext opCtx) {
StartInstancesRequest startRequest = new StartInstancesRequest();
startRequest.withInstanceIds(c.baseAdapterContext.child.id);
client.startInstancesAsync(startRequest, new AWSAsyncHandler<StartInstancesRequest, StartInstancesResult>() {
@Override
protected void handleError(Exception e) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Failed to start the instance %s. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
}
@Override
protected void handleSuccess(StartInstancesRequest request, StartInstancesResult result) {
AWSUtils.waitForTransitionCompletion(getHost(), result.getStartingInstances(), "running", client, (is, e) -> {
if (e != null) {
service.logSevere(() -> String.format("[AWSComputeDiskDay2Service] Instance %s failed to reach " + "running state. %s", c.baseAdapterContext.child.id, Utils.toString(e)));
OperationContext.restoreOperationContext(opCtx);
c.error = e;
dr.complete(c);
return;
}
logInfo(() -> String.format("[AWSComputeDiskDay2Service] Successfully started the " + "instance %s", result.getStartingInstances().get(0).getInstanceId()));
updateComputeAndDiskState(dr, c, opCtx);
});
}
});
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AWSClientManager method getArnCredentialsFromCache.
/**
* Common client manager method to retrieve (or update) credentials from the
* {@link #arnCredentialsCache} if ARN-based, or just return the current credentials set if not.
* If expired, the credentials will be regenerated. Otherwise, they will continue to be
* retrieved as normal.
*
* @param credentials The auth credentials to be used for the client creation
* @param service A service to issue requests
*/
public synchronized DeferredResult<AuthCredentialsServiceState> getArnCredentialsFromCache(AuthCredentialsServiceState credentials, StatelessService service) {
if (!isArnCredentials(credentials)) {
return DeferredResult.completed(credentials);
}
String arn = credentials.customProperties.get(ARN_KEY);
String arnCacheKey = Utils.computeHash(arn);
// if they are expired. If not expired, continue to use them.
if (this.arnCredentialsCache.containsKey(arnCacheKey)) {
// cache reference.
if (!this.arnCredentialsCache.get(arnCacheKey).isDone()) {
return this.arnCredentialsCache.get(arnCacheKey);
}
// Check if the credentials are expired. If not, return. If so, refresh.
AuthCredentialsServiceState arnCredentials = this.arnCredentialsCache.get(arnCacheKey).getNow(new AuthCredentialsServiceState());
if (!AWSUtils.isExpiredCredentials(arnCredentials)) {
return this.arnCredentialsCache.get(arnCacheKey);
}
service.logInfo("Refreshing session credentials for arn: '%s'", arn);
}
this.arnCredentialsCache.put(arnCacheKey, new DeferredResult<>());
// If unavailable in the cache, or expired, generate a new set of session credentials.
OperationContext operationContext = OperationContext.getOperationContext();
getArnSessionCredentialsAsync(arn, credentials.customProperties.get(EXTERNAL_ID_KEY), getExecutor()).whenComplete((awsSessionCredentials, t) -> {
OperationContext.restoreOperationContext(operationContext);
if (t != null) {
this.arnCredentialsCache.get(arnCacheKey).fail(t);
return;
}
service.logInfo("Generated session credentials for arn: '%s'", arn);
AuthCredentialsServiceState arnCredentials = awsSessionCredentialsToAuthCredentialsState(awsSessionCredentials);
// Update the cache with the new credentials.
this.arnCredentialsCache.get(arnCacheKey).complete(arnCredentials);
});
return this.arnCredentialsCache.get(arnCacheKey);
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AzureUtils method injectOperationContext.
public static <T> Action1<T> injectOperationContext(Action1<T> original) {
OperationContext operationContext = OperationContext.getOperationContext();
return new Action1<T>() {
@Override
public void call(T t) {
OperationContext.restoreOperationContext(operationContext);
original.call(t);
}
};
}
use of com.vmware.xenon.common.OperationContext in project photon-model by vmware.
the class AzureUtils method injectOperationContext.
public static Action0 injectOperationContext(Action0 original) {
OperationContext operationContext = OperationContext.getOperationContext();
return new Action0() {
@Override
public void call() {
OperationContext.restoreOperationContext(operationContext);
original.call();
}
};
}
Aggregations