use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class VSphereAdapterImageEnumerationService method collectAllEndpointImages.
private DeferredResult<Set<String>> collectAllEndpointImages(ImageEnumerateRequest req) {
Builder builder = Query.Builder.create().addKindFieldClause(ImageState.class).addFieldClause(ImageState.FIELD_NAME_ENDPOINT_LINK, req.resourceLink());
QueryTask task = QueryTask.Builder.createDirectTask().setQuery(builder.build()).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).build();
DeferredResult<Set<String>> res = new DeferredResult<>();
Set<String> imageLinks = new ConcurrentSkipListSet<>();
QueryUtils.startInventoryQueryTask(this, task).whenComplete((result, e) -> {
if (e != null) {
res.complete(new HashSet<>());
return;
}
if (result.results.nextPageLink == null) {
res.complete(imageLinks);
return;
}
Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), result.results.nextPageLink)).setCompletion(makeCompletion(imageLinks, res)).sendWith(this);
});
return res;
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class DeletePortgroupFlow method deletePortgroup.
private DeferredResult<Void> deletePortgroup(Operation stateOp) {
this.networkState = stateOp.getBody(NetworkState.class);
DeferredResult<Void> res = new DeferredResult<>();
ManagedObjectReference pgRef = CustomProperties.of(this.subnetState).getMoRef(CustomProperties.MOREF);
if (pgRef == null) {
// a provisioning has failed mid-flight, just proceed, skipping actual vc call
res.complete(null);
return res;
}
if (IAAS_API_ENABLED) {
if (this.operation != null) {
return DeferredResult.failed(new IllegalArgumentException("Unable to authenticate"));
}
SessionUtil.retrieveExternalToken(getService(), this.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
if (throwable != null) {
res.fail(throwable);
return;
}
getVsphereIoPool().submit(this.networkState.adapterManagementReference, authCredentialsServiceState, deletePortgroupInVsphere(res));
});
} else {
getVsphereIoPool().submit(getService(), this.networkState.adapterManagementReference, this.networkState.authCredentialsLink, deletePortgroupInVsphere(res));
}
return res;
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSInstanceContext method createSecurityGroup.
/**
* For the provided SecurityGroupState, create corresponding SecurityGroup on AWS.
*/
private DeferredResult<Void> createSecurityGroup(AWSSecurityGroupClient client, AWSInstanceContext context, AWSNicContext nicCtx, SecurityGroupState missingSecurityGroupState) {
// Once AWS security group creation is done PATCH SecurityGroupState.id {{
Function<String, DeferredResult<SecurityGroupState>> patchSecurityGroupState = (ignore) -> {
SecurityGroupState patchSecurityGroup = new SecurityGroupState();
// updated after creating SG in AWS
patchSecurityGroup.id = missingSecurityGroupState.id;
Operation op = Operation.createPatch(context.service.getHost(), missingSecurityGroupState.documentSelfLink).setBody(patchSecurityGroup);
return context.service.sendWithDeferredResult(op, SecurityGroupState.class);
};
// use state name for both group name and description
return client.createSecurityGroupAsync(missingSecurityGroupState.name, missingSecurityGroupState.name, nicCtx.vpc.getVpcId()).thenCompose(sgId -> {
nicCtx.securityGroupIds.add(sgId);
// keep the new ID in order to patch the state after creation is done
missingSecurityGroupState.id = sgId;
return DeferredResult.completed(sgId);
}).thenCompose(patchSecurityGroupState).thenApply(ignore -> (Void) null);
}
use of com.vmware.xenon.common.DeferredResult in project photon-model by vmware.
the class AWSInstanceContext method createSecurityGroupsIfNotExist.
/**
* When there are SecurityGroupStates for the new VM to be provisioned, for which there are no
* corresponding existing SecurityGroups in AWS, the missing SecurityGroups are created
*/
private DeferredResult<AWSInstanceContext> createSecurityGroupsIfNotExist(AWSInstanceContext context) {
if (context.nics.isEmpty()) {
return DeferredResult.completed(context);
}
List<DeferredResult<Void>> createSecurityGroupsDRs = new ArrayList<>();
AWSSecurityGroupClient sgClient = new AWSSecurityGroupClient(context.amazonEC2Client);
for (AWSNicContext nicCtx : context.nics) {
if (nicCtx.securityGroupStates == null) {
continue;
}
Collection<String> foundIds = nicCtx.securityGroupIds;
List<SecurityGroupState> missingSecurityGroupStates = nicCtx.securityGroupStates.stream().filter(sgState -> !foundIds.contains(sgState.id)).collect(Collectors.toList());
for (SecurityGroupState missingSGState : missingSecurityGroupStates) {
DeferredResult<Void> createSGWithRulesDR = createSecurityGroup(sgClient, context, nicCtx, missingSGState).thenCompose(ignore -> createIngressRules(context, nicCtx, missingSGState, sgClient)).thenCompose(ignore -> createEgressRules(context, nicCtx, missingSGState, sgClient)).thenApply(ignore -> (Void) null);
createSecurityGroupsDRs.add(createSGWithRulesDR);
}
}
return DeferredResult.allOf(createSecurityGroupsDRs).handle((all, exc) -> {
if (exc != null) {
String msg = String.format("Error creating SecurityGroups in 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 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