use of com.vmware.xenon.common.Operation 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.Operation 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.xenon.common.Operation in project photon-model by vmware.
the class AWSLoadBalancerService method provisionSecurityGroup.
private DeferredResult<AWSLoadBalancerContext> provisionSecurityGroup(AWSLoadBalancerContext context) {
SecurityGroupInstanceRequest req = buildSecurityGroupInstanceRequest(context.provisionedSecurityGroupState, InstanceRequestType.CREATE, context);
Operation operation = Operation.createPatch(this, AWSSecurityGroupService.SELF_LINK).setBody(req);
return sendWithDeferredResult(operation, SecurityGroupState.class).thenApply(sgs -> {
context.provisionedSecurityGroupState = sgs;
return context;
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSLoadBalancerService method updateLoadBalancerState.
private DeferredResult<AWSLoadBalancerContext> updateLoadBalancerState(AWSLoadBalancerContext context) {
LoadBalancerState loadBalancerState = new LoadBalancerState();
loadBalancerState.address = context.loadBalancerStateExpanded.address;
loadBalancerState.name = context.loadBalancerStateExpanded.name;
if (context.provisionedSecurityGroupState != null) {
loadBalancerState.securityGroupLinks = Collections.singletonList(context.provisionedSecurityGroupState.documentSelfLink);
}
Operation op = Operation.createPatch(this, context.loadBalancerStateExpanded.documentSelfLink);
op.setBody(loadBalancerState);
return this.sendWithDeferredResult(op).thenApply(ignore -> context);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method collectRegions.
private void collectRegions(EnumerationContext ctx, ComputeEnumerationSubStages next) {
Operation getEndpoint = Operation.createGet(this, ctx.request.endpointLink);
DeferredResult<EndpointState> getEndpointDR = sendWithDeferredResult(getEndpoint, EndpointState.class);
getEndpointDR.thenCompose(endpoint -> {
Operation getRegions = Operation.createPost(this, AzureRegionEnumerationAdapterService.SELF_LINK).setBody(endpoint);
return sendWithDeferredResult(getRegions, RegionEnumerationResponse.class);
}).whenComplete((regionsResponse, e) -> {
if (e != null) {
logWarning("Resource enumeration failed at stage %s with exception %s", ctx.stage, Utils.toString(e));
handleError(ctx, e);
} else {
ctx.regions.putAll(regionsResponse.regions.stream().collect(Collectors.toMap(r -> r.regionId, r -> r)));
ctx.regionIds.addAll(ctx.regions.keySet());
ctx.subStage = next;
handleSubStage(ctx);
}
});
}
Aggregations