use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method getVirtualNetworks.
/**
* Retrieve a page of Virtual Network resources from azure, based on the provided compute host
* description.
*/
private void getVirtualNetworks(NetworkEnumContext context, NetworkEnumStages next) {
logFine(() -> "Enumerating Virtual Networks from Azure.");
URI uri;
if (context.enumNextPageLink == null) {
// First request to fetch Virtual Networks from Azure.
String uriStr = AdapterUriUtil.expandUriPathTemplate(LIST_VIRTUAL_NETWORKS_URI, context.endpointAuth.userLink);
uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(uriStr), QUERY_PARAM_API_VERSION, NETWORK_REST_API_VERSION);
} else {
// Request to fetch next page of Virtual Networks from Azure.
uri = UriUtils.buildUri(context.enumNextPageLink);
}
final Operation operation = Operation.createGet(uri);
operation.addRequestHeader(Operation.ACCEPT_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
operation.addRequestHeader(Operation.CONTENT_TYPE_HEADER, Operation.MEDIA_TYPE_APPLICATION_JSON);
try {
operation.addRequestHeader(Operation.AUTHORIZATION_HEADER, AUTH_HEADER_BEARER_PREFIX + context.credentials.getToken(AzureUtils.getAzureBaseUri()));
} catch (Exception ex) {
handleError(context, ex);
return;
}
operation.setCompletion((op, ex) -> {
op.complete();
if (ex != null) {
handleError(context, ex);
return;
}
VirtualNetworkListResult results = op.getBody(VirtualNetworkListResult.class);
List<VirtualNetwork> virtualNetworks = results.value;
// If there are no Virtual Networks in Azure we directly skip over to deletion phase.
if (virtualNetworks == null || virtualNetworks.size() == 0) {
handleSubStage(context, NetworkEnumStages.DELETE_NETWORK_STATES);
return;
}
// Store next page link.
context.enumNextPageLink = results.nextLink;
logFine(() -> String.format("Retrieved %d Virtual Networks from Azure", virtualNetworks.size()));
logFine(() -> String.format("Next page link %s", context.enumNextPageLink));
// Store virtual networks for further processing during the next stages.
virtualNetworks.forEach(virtualNetwork -> {
context.addVirtualNetwork(virtualNetwork);
});
logFine(() -> String.format("Processing %d virtual networks", context.virtualNetworks.size()));
handleSubStage(context, next);
});
sendRequest(operation);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method createNetworkInternalTagStates.
/**
* Create internal tag states for networks.
*/
private void createNetworkInternalTagStates(NetworkEnumContext context, NetworkEnumStages next) {
TagState internalTypeTag = newTagState(PhotonModelConstants.TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE, false, context.parentCompute.tenantLinks);
// operation to create tag "type" for subnets.
Operation.createPost(this, TagService.FACTORY_LINK).setBody(internalTypeTag).setCompletion((o, e) -> {
if (e != null) {
logSevere("Error creating internal type tag for networks %s", e.getMessage());
} else {
context.networkInternalTagsMap.put(PhotonModelConstants.TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE);
context.networkInternalTagLinksSet.add(internalTypeTag.documentSelfLink);
}
handleSubStage(context, next);
}).sendWith(this);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method disassociateResourceStates.
/**
* Generic disassociate resource helper.
*/
private void disassociateResourceStates(QueryByPages<? extends ResourceState> resourcesQuery, NetworkEnumContext context, NetworkEnumStages next) {
final String msg = resourcesQuery.documentClass.getSimpleName() + " disassociation";
logFine(() -> msg + ": STARTED");
resourcesQuery.queryDocuments(resourceState -> {
if (shouldDelete(context, resourceState)) {
Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, context.request.endpointLink, resourceState);
if (disassociateOp != null) {
sendRequest(disassociateOp);
}
}
}).thenRun(() -> logFine(() -> msg + ": SUCCESS")).whenComplete(thenHandleSubStage(context, next));
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class BaseTestCase method doOperation.
protected <T extends ServiceDocument> T doOperation(T inState, URI uri, Class<T> type, boolean expectFailure, Action action) throws Throwable {
this.host.log("Executing operation %s for resource: %s ...", action.name(), uri);
final List<T> doc = Arrays.asList((T) null);
final Throwable[] error = { null };
TestContext ctx = testCreate(1);
Operation op;
if (action == Action.POST) {
op = createForcedPost(uri);
} else {
// createPost sets the proper authorization context for the operation
op = Operation.createPost(uri);
// replace POST with the provided action
op.setAction(action);
}
op.addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY);
op.setBody(inState).setCompletion((o, e) -> {
if (e != null) {
if (expectFailure) {
error[0] = e;
ctx.completeIteration();
} else {
ctx.failIteration(e);
}
return;
}
if (!o.hasBody()) {
ctx.failIteration(new IllegalStateException("body was expected"));
return;
}
doc.set(0, o.getBody(type));
if (expectFailure) {
ctx.failIteration(new IllegalStateException("ERROR: operation completed successfully but exception excepted."));
} else {
ctx.completeIteration();
}
});
this.host.send(op);
ctx.await();
this.host.logThroughput();
if (expectFailure) {
Throwable ex = error[0];
throw ex;
}
return doc.get(0);
}
use of com.vmware.xenon.common.Operation in project photon-model by vmware.
the class BaseTestCase method getDocument.
@SuppressWarnings("unchecked")
protected <T> T getDocument(Class<T> type, URI uri) throws Throwable {
TestContext ctx = testCreate(1);
Object[] result = new Object[1];
Operation get = Operation.createGet(uri).addPragmaDirective(Operation.PRAGMA_DIRECTIVE_QUEUE_FOR_SERVICE_AVAILABILITY).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
if (e != null) {
this.host.log("Can't load document %s. Error: %s", uri, Utils.toString(e));
ctx.failIteration(e);
} else {
result[0] = o.getBody(type);
ctx.completeIteration();
}
});
this.host.send(get);
ctx.await();
return (T) result[0];
}
Aggregations