Search in sources :

Example 41 with Operation

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);
}
Also used : VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI)

Example 42 with 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);
}
Also used : QUERY_PARAM_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.QUERY_PARAM_API_VERSION) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) LifecycleState(com.vmware.photon.controller.model.support.LifecycleState) StringUtils(org.apache.commons.lang3.StringUtils) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) MatchType(com.vmware.xenon.services.common.QueryTask.QueryTerm.MatchType) Utils(com.vmware.xenon.common.Utils) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) VirtualNetworkListResult(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetworkListResult) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_INSTANCE_ADAPTER_REFERENCE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DEFAULT_INSTANCE_ADAPTER_REFERENCE) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) NETWORK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.NETWORK_REST_API_VERSION) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) Stream(java.util.stream.Stream) TagService(com.vmware.photon.controller.model.resources.TagService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) AddressSpace(com.vmware.photon.controller.model.adapters.azure.model.network.AddressSpace) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) TagsUtil.setTagLinksToResourceState(com.vmware.photon.controller.model.adapters.util.TagsUtil.setTagLinksToResourceState) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) LIST_VIRTUAL_NETWORKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_VIRTUAL_NETWORKS_URI) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceGroupStateType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.ResourceGroupStateType) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TagState(com.vmware.photon.controller.model.resources.TagService.TagState) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AUTH_HEADER_BEARER_PREFIX) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) TagsUtil.updateLocalTagStates(com.vmware.photon.controller.model.adapters.util.TagsUtil.updateLocalTagStates) VirtualNetwork(com.vmware.photon.controller.model.adapters.azure.model.network.VirtualNetwork) BiConsumer(java.util.function.BiConsumer) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AzureResourceType(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AzureResourceType) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Subnet(com.vmware.photon.controller.model.adapters.azure.model.network.Subnet) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) TagsUtil.newTagState(com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState) TagState(com.vmware.photon.controller.model.resources.TagService.TagState)

Example 43 with Operation

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));
}
Also used : Operation(com.vmware.xenon.common.Operation)

Example 44 with Operation

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);
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 45 with Operation

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];
}
Also used : TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Aggregations

Operation (com.vmware.xenon.common.Operation)391 URI (java.net.URI)142 ArrayList (java.util.ArrayList)132 QueryTask (com.vmware.xenon.services.common.QueryTask)118 List (java.util.List)118 Utils (com.vmware.xenon.common.Utils)111 StatelessService (com.vmware.xenon.common.StatelessService)108 UriUtils (com.vmware.xenon.common.UriUtils)106 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)98 Map (java.util.Map)92 HashMap (java.util.HashMap)90 OperationJoin (com.vmware.xenon.common.OperationJoin)86 Query (com.vmware.xenon.services.common.QueryTask.Query)86 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)82 Collectors (java.util.stream.Collectors)79 HashSet (java.util.HashSet)78 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)73 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)70 DeferredResult (com.vmware.xenon.common.DeferredResult)69 Consumer (java.util.function.Consumer)69