use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSEnumerationAndCreationAdapterService method refreshLoadBalancerInformation.
private void refreshLoadBalancerInformation(EnumerationCreationContext aws, AWSEnumerationRefreshSubStage next) {
AWSLoadBalancerEnumerationRequest awsLoadBalancerEnumerationRequest = new AWSLoadBalancerEnumerationRequest();
awsLoadBalancerEnumerationRequest.computeRequest = aws.request;
awsLoadBalancerEnumerationRequest.enumeratedNetworks = aws.enumeratedNetworks;
awsLoadBalancerEnumerationRequest.enumeratedSecurityGroups = aws.enumeratedSecurityGroups;
Operation patchLoadBalancerOperation = Operation.createPatch(this, AWSLoadBalancerEnumerationAdapterService.SELF_LINK).setBody(awsLoadBalancerEnumerationRequest).setReferer(UriUtils.buildUri(getHost().getPublicUri(), getSelfLink()));
this.getHost().sendWithDeferredResult(patchLoadBalancerOperation).thenAccept(ignore -> {
logFine(() -> "Successfully enumerated load balancer states");
aws.refreshSubStage = next;
processRefreshSubStages(aws);
}).exceptionally(throwable -> {
logWarning("Failed to enumerate load balancer states: %s ", throwable.getLocalizedMessage());
aws.error = throwable;
aws.refreshSubStage = next;
processRefreshSubStages(aws);
return null;
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSEnumerationAndDeletionAdapterService method retireComputeStates.
/**
* Creates operations to retire all the compute states in the local system for which the AWS
* instance has been terminated/missing from the remote instance.
*/
private void retireComputeStates(EnumerationDeletionContext context) {
List<Operation> operations = new ArrayList<>();
// Disks.
for (ComputeState cs : context.instancesToBeDeleted) {
ComputeState cps = new ComputeState();
cps.powerState = PowerState.OFF;
cps.lifecycleState = LifecycleState.RETIRED;
Operation operation = Operation.createPatch(this.getHost(), cs.documentSelfLink).setBody(cps).setReferer(getHost().getUri());
operations.add(operation);
}
// Kick off patch operations with a join handler.
if (operations == null || operations.size() == 0) {
logFine(() -> "No local compute states to be deleted.");
deleteResourcesInLocalSystem(context);
return;
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Failure retiring local compute states: %s ", Utils.toString(exc)));
deleteResourcesInLocalSystem(context);
return;
}
logFine(() -> "Successfully retired local compute states.");
deleteResourcesInLocalSystem(context);
return;
};
OperationJoin joinOp = OperationJoin.create(operations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(getHost());
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSMissingResourcesEnumerationService method createLinkedComputeStates.
/**
* creates new linked account endpoint states of a primary account
* @param context
*/
private void createLinkedComputeStates(AwsMissingResourcesEnumContext context) {
AtomicInteger completionCounter = new AtomicInteger(context.request.missingLinkedAccountIds.size());
AtomicBoolean isSuccessful = new AtomicBoolean(true);
context.request.missingLinkedAccountIds.forEach(linkedAccountId -> {
ComputeDescription computeDescription = populateComputeDescription(context, linkedAccountId);
ComputeState computeState = populateComputeState(context, linkedAccountId);
computeState.descriptionLink = computeDescription.documentSelfLink;
Operation csOp = Operation.createPost(this, ComputeService.FACTORY_LINK).setBody(computeState);
Operation cdOp = Operation.createPost(this, ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
JoinedCompletionHandler completionHandler = (ops, exs) -> {
if (exs != null && !exs.isEmpty()) {
logWarning(() -> String.format("Error creating missing resources for" + " account with ID %s.", linkedAccountId));
isSuccessful.set(false);
} else {
logInfo(() -> String.format("Created missing resources for account " + "with ID %s.", linkedAccountId));
}
if (completionCounter.decrementAndGet() == 0) {
if (isSuccessful.get()) {
context.requestOp.complete();
} else {
context.requestOp.fail(new Exception("Failed to create missing resources " + "for atleast one linked account."));
}
}
};
Operation.createGet(this, computeDescription.documentSelfLink).setReferer(this.getUri()).setCompletion((o, e) -> {
if (e != null && (e instanceof ServiceNotFoundException || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND)) {
OperationSequence.create(cdOp).next(csOp).setCompletion(completionHandler).sendWith(this);
} else {
OperationSequence.create(csOp).setCompletion(completionHandler).sendWith(this);
}
}).sendWith(this);
});
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AWSRegionEnumerationAdapterService method handlePost.
@Override
public void handlePost(Operation post) {
List<RegionInfo> regions = Arrays.stream(Regions.values()).map(r -> new RegionInfo(r.getName(), r.getName())).collect(Collectors.toList());
RegionEnumerationResponse result = new RegionEnumerationResponse();
result.regions = regions;
post.setBody(result);
post.complete();
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class TestAWSEnumerationDocumentCountInLongRun method storeDocumentLinksFromNetworkInterfaceStates.
/**
* Gets and stores network interface ids from network interface links and security group links, subnet
* links by querying network interface ids.
*/
private void storeDocumentLinksFromNetworkInterfaceStates() {
// If there are no network interface links, return.
if (this.networkInterfaceLinks.isEmpty()) {
return;
}
// Get network interface IDs from network interface links.
for (String s : this.networkInterfaceLinks) {
Operation op = Operation.createGet(UriUtils.buildUri(this.host.getUri(), s)).setReferer(this.host.getUri());
Operation response = this.host.waitForResponse(op);
Assert.assertTrue("Error retrieving network interface IDs", response.getStatusCode() == 200);
NetworkInterfaceState state = response.getBody(NetworkInterfaceState.class);
this.networkInterfaceIds.add(state.id);
}
// Query all network interface documents associated with list of network interface IDs.
QueryTask.Query networkInterfaceQuery = QueryTask.Query.Builder.create().addKindFieldClause(NetworkInterfaceState.class).addInClause(NetworkInterfaceState.FIELD_NAME_ID, this.networkInterfaceIds).build();
QueryTask q = QueryTask.Builder.createDirectTask().setQuery(networkInterfaceQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).build();
Operation queryNetworkInterface = QueryUtils.createQueryTaskOperation(this.host, q, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
Operation queryResponse = this.host.waitForResponse(queryNetworkInterface);
Assert.assertTrue("Error retrieving network interface states", queryResponse.getStatusCode() == 200);
QueryTask qt = queryResponse.getBody(QueryTask.class);
// Store security group links and subnet links.
for (String documentLink : qt.results.documentLinks) {
NetworkInterfaceState nis = Utils.fromJson(qt.results.documents.get(documentLink), NetworkInterfaceState.class);
this.securityGroupLinks.addAll(nis.securityGroupLinks);
this.subnetLinks.add(nis.subnetLink);
}
}
Aggregations