use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class IPAddressAllocationTaskService method deallocateIpAddress.
/**
* De-allocates an IP address, based on IP address resource link.
*
* @param state IP Address allocation task state.
*/
private void deallocateIpAddress(IPAddressAllocationTaskState state) {
IPAddressState addressState = new IPAddressState();
addressState.connectedResourceLink = state.connectedResourceLink;
addressState.ipAddressStatus = IPAddressState.IPAddressStatus.RELEASED;
List<DeferredResult<Operation>> deferredResults = new ArrayList<>();
for (int i = 0; i < state.ipAddressLinks.size(); i++) {
String ipAddressResourceLink = state.ipAddressLinks.get(i);
Operation patchOp = Operation.createPatch(this, ipAddressResourceLink).setBody(addressState);
deferredResults.add(this.sendWithDeferredResult(patchOp));
}
DeferredResult.allOf(deferredResults).thenAccept(dr -> proceedTo(IPAddressAllocationTaskState.SubStage.FINISHED, null)).exceptionally(e -> {
if (e != null) {
failTask(e, "Failed to de-allocate IP addresses due to failure %s", e.getMessage());
}
return null;
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class IPAddressAllocationTaskService method createNewIpAddressResource.
/**
* Creates new IP address resource with the specified IP address and moves the task state to completed,
* once it is done. Then it creates IP Address resource, it first creates it with AVAILABLE state. It then
* changes the state to ALLOCATED. This is done in two steps, due to concurrency issues. When multiple allocation
* requests are invoked at the same time, they end up creating single IP Address resource. Only one of them
* succeeds in their PATCH and the other one will retry the allocation operation.
*
* @param ipAddress IP address to use for the new IP address resource.
* @param subnetRangeResourceLink Subnet range resource link to use for the new IP address resource.
* @param connectedResourceLink Link to the resource this IP is assigned to.
*/
private DeferredResult<IPAddressState> createNewIpAddressResource(String ipAddress, String subnetRangeResourceLink, String connectedResourceLink, IPAddressAllocationContext context) {
IPAddressState ipAddressState = new IPAddressState();
ipAddressState.ipAddressStatus = IPAddressState.IPAddressStatus.AVAILABLE;
ipAddressState.ipAddress = ipAddress;
ipAddressState.subnetRangeLink = subnetRangeResourceLink;
ipAddressState.documentSelfLink = generateIPAddressDocumentSelfLink(subnetRangeResourceLink, ipAddress);
logInfo("Creating IPAddressState with IP %s, subnet %s, for connected resource " + "%s", ipAddress, subnetRangeResourceLink, connectedResourceLink);
return sendWithDeferredResult(Operation.createPost(this, IPAddressService.FACTORY_LINK).setBody(ipAddressState)).thenApply((out) -> {
IPAddressState availableIPAddress = out.getBody(IPAddressState.class);
availableIPAddress.ipAddressStatus = IPAddressState.IPAddressStatus.ALLOCATED;
availableIPAddress.connectedResourceLink = connectedResourceLink;
return availableIPAddress;
}).thenCompose((availableIPAddress) -> (updateExistingIpAddressResource(availableIPAddress, context)));
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ImageEnumerationTaskService method getImageEnumerationAdapterReference.
/**
* Go to {@link PhotonModelAdaptersRegistryService Service Registry} and get the
* 'image-enumeration' URI for passed end-point.
*
* @return <code>null</code> is returned if 'image-enumeration' adapter is not registered by
* passed end-point.
*
* @see PhotonModelAdapterConfig
* @see AdapterTypePath#IMAGE_ENUMERATION_ADAPTER
*/
private DeferredResult<SendImageEnumerationAdapterContext> getImageEnumerationAdapterReference(SendImageEnumerationAdapterContext ctx) {
// Use 'endpointType' (such as aws, azure) as AdapterConfig id/selfLink!
String configLink = buildUriPath(PhotonModelAdaptersConfigAccessService.SELF_LINK, ctx.endpointState.endpointType);
Operation getConfigOp = Operation.createGet(this, configLink);
return sendWithDeferredResult(getConfigOp, PhotonModelAdapterConfig.class).thenApply(endpointConfig -> {
// Lookup the 'image-enumeration' URI for passed end-point
if (endpointConfig.adapterEndpoints != null) {
String uriStr = endpointConfig.adapterEndpoints.get(AdapterTypePath.IMAGE_ENUMERATION_ADAPTER.key);
if (uriStr != null && !uriStr.isEmpty()) {
ctx.adapterRef = URI.create(uriStr);
}
}
logInfo(() -> String.format("[getImageEnumerationAdapterReference] adapterRef = %s: SUCCESS", ctx.adapterRef));
return ctx;
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ImageEnumerationTaskService method deletePublicImagesByEndpointType.
private DeferredResult<SendImageEnumerationAdapterContext> deletePublicImagesByEndpointType(SendImageEnumerationAdapterContext ctx) {
QueryByPages<ImageState> queryAll = new QueryByPages<ImageState>(getHost(), publicImagesByEndpointTypeQuery(ctx).build(), ImageState.class, null).setQueryTaskTenantLinks(ctx.taskState.tenantLinks);
queryAll.setMaxPageSize(QueryUtils.DEFAULT_MAX_RESULT_LIMIT);
queryAll.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
final List<DeferredResult<Operation>> deleteDRs = new ArrayList<>();
queryAll.queryLinks(imageLink -> {
Operation delOp = Operation.createDelete(UriUtils.buildUri(getHost(), imageLink));
DeferredResult<Operation> delDR = sendWithDeferredResult(delOp).whenComplete((op, e) -> {
final String msg = "Deleting '%s' public image state [%s]";
if (e != null) {
// Be tolerant on individual images delete
log(Level.WARNING, () -> String.format(msg + ": FAILED with %s", ctx.taskState.endpointType, imageLink, Utils.toString(e)));
} else {
log(Level.FINEST, () -> String.format(msg + ": SUCCESS", ctx.taskState.endpointType, imageLink));
}
});
deleteDRs.add(delDR);
});
return DeferredResult.allOf(deleteDRs).thenApply(ignore -> ctx);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class ImageEnumerationTaskService method callImageEnumerationAdapter.
/**
* Call 'image-enumeration' adapter if registered by the end-point OR fail if not registered.
*/
private DeferredResult<SendImageEnumerationAdapterContext> callImageEnumerationAdapter(SendImageEnumerationAdapterContext ctx) {
if (ctx.adapterRef == null) {
// No 'image-enumeration' URI registered for passed end-point
return DeferredResult.failed(new IllegalStateException(String.format("No '%s' URI registered by '%s' end-point.", AdapterTypePath.IMAGE_ENUMERATION_ADAPTER.key, ctx.taskState.endpointType)));
}
// Create 'image-enumeration' adapter request
final ImageEnumerateRequest adapterReq = new ImageEnumerateRequest();
// Set ImageEnumerateRequest specific params
adapterReq.enumerationAction = ctx.taskState.enumerationAction;
if (ctx.taskState.enumeratePrivateImages()) {
adapterReq.requestType = ImageEnumerateRequestType.PRIVATE;
} else if (ctx.taskState.enumeratePublicImages()) {
adapterReq.requestType = ImageEnumerateRequestType.PUBLIC;
}
// Set generic ResourceRequest params
// The end-point is ALWAYS set regardless of Private/Public enum type
// In case of Public, end-point credentials are used to run the enumeration
adapterReq.resourceReference = createInventoryUri(this.getHost(), ctx.endpointState.documentSelfLink);
adapterReq.regionId = ctx.taskState.regionId;
adapterReq.taskReference = buildUri(getHost(), ctx.taskState.documentSelfLink);
adapterReq.isMockRequest = ctx.taskState.options.contains(TaskOption.IS_MOCK);
Operation callAdapterOp = Operation.createPatch(ctx.adapterRef).setBody(adapterReq);
return sendWithDeferredResult(callAdapterOp).thenApply(op -> {
logInfo(() -> String.format("[callImageEnumerationAdapter] adpterRef = %s: SUCCESS", op.getUri()));
return ctx;
});
}
Aggregations