Search in sources :

Example 36 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureCostStatsService method setCustomProperty.

private void setCustomProperty(Context context, String key, String value, Stages next) {
    context.computeHostDesc.customProperties.put(key, value);
    ComputeState accountState = new ComputeState();
    accountState.customProperties = new HashMap<>();
    accountState.customProperties.put(key, value);
    sendRequest(Operation.createPatch(UriUtils.extendUri(getInventoryServiceUri(), context.computeHostDesc.documentSelfLink)).setBody(accountState).setCompletion((operation, exception) -> {
        if (exception != null) {
            handleError(context, null, exception, false);
            return;
        }
        context.stage = next;
        handleRequest(context);
    }));
}
Also used : AzureCostConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants) AzureStatsNormalizer(com.vmware.photon.controller.model.adapters.azure.utils.AzureStatsNormalizer) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) StringUtils(org.apache.commons.lang3.StringUtils) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) BufferedSink(okio.BufferedSink) Map(java.util.Map) AzureService(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService) URI(java.net.URI) AzureSubscriptionsEnumerationRequest(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService.AzureSubscriptionsEnumerationRequest) Builder(com.vmware.xenon.services.common.QueryTask.Builder) Path(java.nio.file.Path) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Interceptor(okhttp3.Interceptor) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) AzureDetailedBillHandler(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureDetailedBillHandler) Request(okhttp3.Request) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OldEaSummarizedBillElement(com.vmware.photon.controller.model.adapters.azure.model.cost.OldEaSummarizedBillElement) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AzureSubscriptionsEnumerationService(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService) AzureCostHelper(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureCostHelper) List(java.util.List) Stream(java.util.stream.Stream) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) INTERNAL_REQUEST_TIMEOUT_SECONDS(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants.INTERNAL_REQUEST_TIMEOUT_SECONDS) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) Okio(okio.Okio) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) OperationContext(com.vmware.xenon.common.OperationContext) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) HashMap(java.util.HashMap) Function(java.util.function.Function) AzureResource(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) CollectionUtils(org.apache.commons.collections.CollectionUtils) EaBillLinkElement(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinkElement) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AUTO_DISCOVERED_ENTITY(com.vmware.photon.controller.model.constants.PhotonModelConstants.AUTO_DISCOVERED_ENTITY) EaBillLinks(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinks) BiConsumer(java.util.function.BiConsumer) Response(okhttp3.Response) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) Call(okhttp3.Call) Callback(okhttp3.Callback) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) ExecutorService(java.util.concurrent.ExecutorService) OldApi(com.vmware.photon.controller.model.adapters.azure.model.cost.OldApi) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Files(java.nio.file.Files) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) File(java.io.File) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) BillParsingStatus(com.vmware.photon.controller.model.adapters.azure.model.cost.BillParsingStatus) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) LocalDate(org.joda.time.LocalDate) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) OkHttpClient(okhttp3.OkHttpClient) Paths(java.nio.file.Paths) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) OperationJoin(com.vmware.xenon.common.OperationJoin) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 37 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureDiskEnumerationAdapterService method getManagedDisks.

private void getManagedDisks(DiskEnumContext ctx, DiskEnumStages nextStage) {
    logInfo(() -> "Enumerating Azure Managed disks.");
    URI uri;
    if (ctx.enumNextPageLink == null) {
        // TODO- change to use sdk to fetch disks
        String uriStr = AdapterUriUtil.expandUriPathTemplate(LIST_DISKS_URI, ctx.endpointAuth.userLink);
        uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(uriStr), QUERY_PARAM_API_VERSION, DISK_REST_API_VERSION);
    } else {
        uri = UriUtils.buildUri(ctx.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 + ctx.credentials.getToken(AzureUtils.getAzureBaseUri()));
    } catch (Exception ex) {
        handleError(ctx, ex);
        return;
    }
    operation.setCompletion((op, th) -> {
        if (th != null) {
            handleError(ctx, th);
            return;
        }
        ManagedDiskList results = op.getBody(ManagedDiskList.class);
        // Store next page link
        ctx.enumNextPageLink = results.nextLink;
        logInfo(() -> String.format("Next page link %s", ctx.enumNextPageLink));
        List<Disk> diskList = results.value;
        if (diskList == null || diskList.size() == 0) {
            ctx.subStage = DiskEnumStages.DISASSOCIATE_ENDPOINT_LINKS;
            handleSubStage(ctx);
            return;
        }
        logInfo(() -> String.format("Retrieved %d managed disks from Azure", diskList.size()));
        // save all disks from Azure to process further
        diskList.forEach(disk -> ctx.managedDisks.put(disk.id, disk));
        // filter all un-attached disks from diskList
        List<Disk> unattachedDisks = diskList.stream().filter(dk -> dk.properties.diskState.equals(DISK_STATUS_UNATTACHED)).collect(Collectors.toList());
        // TODO - Remove toLowerCase() after https://github.com/Azure/azure-sdk-for-java/issues/2014 is fixed.
        // save all unattached disks in managedDisks map for further processing
        unattachedDisks.forEach(disk -> ctx.unattachedDisks.put(disk.id.toLowerCase(), disk));
        logInfo(() -> String.format("Processing %d independent disks", ctx.managedDisks.size()));
        if (ctx.enumNextPageLink != null) {
            ctx.subStage = DiskEnumStages.GET_DISKS;
            logFine(() -> String.format("Transition to same stage" + ctx.subStage));
        } else {
            ctx.subStage = nextStage;
            logFine(() -> String.format("Transition to " + nextStage));
        }
        handleSubStage(ctx);
    });
    sendRequest(operation);
}
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) DISK_STATUS_UNATTACHED(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DISK_STATUS_UNATTACHED) QueryTask(com.vmware.xenon.services.common.QueryTask) DISK_CONTROLLER_NUMBER(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DISK_CONTROLLER_NUMBER) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) AZURE_MANAGED_DISK_TYPE(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.AZURE_MANAGED_DISK_TYPE) Utils(com.vmware.xenon.common.Utils) Map(java.util.Map) Disk(com.vmware.photon.controller.model.adapters.azure.model.storage.Disk) URI(java.net.URI) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) UUID(java.util.UUID) AdapterUtils.getDeletionState(com.vmware.photon.controller.model.adapters.util.AdapterUtils.getDeletionState) Collectors(java.util.stream.Collectors) List(java.util.List) AzureUtils(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils) TagService(com.vmware.photon.controller.model.resources.TagService) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) AzureUtils.getAzureConfig(com.vmware.photon.controller.model.adapters.azure.utils.AzureUtils.getAzureConfig) DiskService(com.vmware.photon.controller.model.resources.DiskService) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) LIST_DISKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_DISKS_URI) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) TagsUtil(com.vmware.photon.controller.model.adapters.util.TagsUtil) ArrayList(java.util.ArrayList) DISK_REST_API_VERSION(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.DISK_REST_API_VERSION) HashSet(java.util.HashSet) 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) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) DiskStatus(com.vmware.photon.controller.model.resources.DiskService.DiskStatus) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ManagedDiskList(com.vmware.photon.controller.model.adapters.azure.model.storage.ManagedDiskList) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ComputeEnumerateAdapterRequest(com.vmware.photon.controller.model.adapters.util.ComputeEnumerateAdapterRequest) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) Operation(com.vmware.xenon.common.Operation) ManagedDiskList(com.vmware.photon.controller.model.adapters.azure.model.storage.ManagedDiskList) URI(java.net.URI) LIST_DISKS_URI(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants.LIST_DISKS_URI) Disk(com.vmware.photon.controller.model.adapters.azure.model.storage.Disk)

Example 38 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureDiskEnumerationAdapterService method disassociateEndpointLinksFromDiskStates.

/**
 * Disassociate EndpointLinks from Disk States (of local store) that are no longer existing in Azure.
 */
private void disassociateEndpointLinksFromDiskStates(DiskEnumContext ctx, DiskEnumStages nextStage) {
    Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_STATUS, DiskStatus.AVAILABLE).addRangeClause(DiskState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(ctx.enumerationStartTimeInMicros));
    QueryTop<DiskState> queryLocalDiskStates = new QueryTop<>(getHost(), qBuilder.build(), DiskState.class, ctx.parentCompute.tenantLinks, null, ctx.parentCompute.documentSelfLink).setClusterType(ClusterUtil.ServiceTypeCluster.INVENTORY_SERVICE);
    final String msg = queryLocalDiskStates.documentClass.getSimpleName() + " - Disassociation of endpoint links from disk states ";
    logInfo(() -> msg + " STARTED");
    queryLocalDiskStates.queryDocuments(ds -> {
        // check for diskState which is managed disk type
        if (ds.customProperties != null && ds.customProperties.containsKey(AZURE_MANAGED_DISK_TYPE)) {
            if (!ctx.unattachedDisks.containsKey(ds.id.toLowerCase())) {
                Operation disassociateOp = PhotonModelUtils.createRemoveEndpointLinksOperation(this, ctx.request.endpointLink, ds);
                if (disassociateOp != null) {
                    sendRequest(disassociateOp);
                }
            }
        }
    }).thenRun(() -> logInfo(() -> "Disassociation of endpoint link from disk states which " + "are not present in Azure.")).whenComplete((aVoid, th) -> {
        if (th != null) {
            handleError(ctx, th);
        }
        logFine(() -> msg + ": SUCCESS");
        logFine(() -> String.format("Transition to " + nextStage));
        ctx.subStage = nextStage;
        handleSubStage(ctx);
    });
}
Also used : Query(com.vmware.xenon.services.common.QueryTask.Query) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Operation(com.vmware.xenon.common.Operation)

Example 39 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method createUpdateNetworkStates.

/**
 * Create new network states or update matching network states with the actual state in Azure.
 */
private void createUpdateNetworkStates(NetworkEnumContext context, NetworkEnumStages next) {
    logFine(() -> "Create or update Network States with the actual state in Azure.");
    if (context.virtualNetworks.size() == 0) {
        logFine(() -> "No virtual networks found to create/update.");
        handleSubStage(context, next);
        return;
    }
    Stream<Operation> operations = context.virtualNetworks.values().stream().map(virtualNetwork -> {
        NetworkState existingNetworkState = context.networkStates.get(virtualNetwork.id);
        NetworkState networkState = buildNetworkState(context, virtualNetwork, existingNetworkState);
        setTagLinksToResourceState(networkState, virtualNetwork.tags, true);
        if (existingNetworkState == null) {
            // set internal tags as tagLinks for networks to be newly created.
            setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
        } else {
            // for already existing networks, add internal tags only if missing
            if (networkState.tagLinks == null || networkState.tagLinks.isEmpty()) {
                setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
            } else {
                context.networkInternalTagLinksSet.stream().filter(tagLink -> !networkState.tagLinks.contains(tagLink)).map(tagLink -> networkState.tagLinks.add(tagLink)).collect(Collectors.toSet());
            }
        }
        CompletionHandler handler = (completedOp, failure) -> {
            if (failure != null) {
                // Process successful operations only.
                logWarning(() -> String.format("Error: %s", failure.getMessage()));
                return;
            }
            NetworkState result;
            if (completedOp.getStatusCode() == Operation.STATUS_CODE_NOT_MODIFIED) {
                // Use the original networkState as result
                result = networkState;
            } else {
                result = completedOp.getBody(NetworkState.class);
            }
            context.networkStates.put(result.id, result);
        };
        return existingNetworkState != null ? // Update case.
        Operation.createPatch(this, networkState.documentSelfLink).setBody(networkState).setCompletion(handler) : // Create case.
        Operation.createPost(getHost(), NetworkService.FACTORY_LINK).setBody(networkState).setCompletion(handler);
    });
    OperationJoin.create(operations).setCompletion((ops, failures) -> {
        // We don't want to fail the whole data collection if some of the so we don't care of
        // any potential operation failures. They are already logged at individual operation
        // level.
        logFine(() -> "Finished updating network states.");
        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) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) Operation(com.vmware.xenon.common.Operation) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState)

Example 40 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureNetworkEnumerationAdapterService method createUpdateSubnetStates.

/**
 * Create new subnet states or updates matching subnet states with the actual state in Azure.
 */
private void createUpdateSubnetStates(NetworkEnumContext context, NetworkEnumStages next) {
    if (context.subnets.size() == 0) {
        logFine(() -> "No network states available for update.");
        handleSubStage(context, next);
        return;
    }
    Stream<Operation> operations = context.subnets.keySet().stream().map(subnetId -> {
        SubnetStateWithParentVNetId subnetStateWithParentVNetId = context.subnets.get(subnetId);
        SubnetState subnetState = subnetStateWithParentVNetId.subnetState;
        if (!context.subnetStates.containsKey(subnetId)) {
            // set internal tags as tagLinks for subnets to be newly created.
            setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
        } else {
            // for already existing subnets, add internal tags only if missing
            if (subnetState.tagLinks == null || subnetState.tagLinks.isEmpty()) {
                setTagLinksToResourceState(subnetState, context.subnetInternalTagsMap, false);
            } else {
                context.subnetInternalTagLinksSet.stream().filter(tagLink -> !subnetState.tagLinks.contains(tagLink)).map(tagLink -> subnetState.tagLinks.add(tagLink)).collect(Collectors.toSet());
            }
        }
        // Update networkLink with "latest" (either created or updated)
        // NetworkState.documentSelfLink
        NetworkState networkState = context.networkStates.get(subnetStateWithParentVNetId.parentVNetId);
        if (networkState != null) {
            subnetState.networkLink = networkState.documentSelfLink;
        } else {
            logWarning(() -> String.format("Network state corresponding to subnet with" + " name [%s] was not found. Network Link is left empty.", subnetState.name));
        }
        if (StringUtils.isEmpty(subnetState.endpointLink)) {
            subnetState.endpointLink = context.request.endpointLink;
        }
        subnetState.computeHostLink = context.parentCompute.documentSelfLink;
        AdapterUtils.addToEndpointLinks(subnetState, context.request.endpointLink);
        return context.subnetStates.containsKey(subnetId) ? // Update case
        Operation.createPatch(this, context.subnetStates.get(subnetId)).setBody(subnetState) : // Create case.
        Operation.createPost(getHost(), SubnetService.FACTORY_LINK).setBody(subnetState);
    });
    OperationJoin.create(operations).setCompletion((ops, failures) -> {
        if (failures != null) {
            // We don't want to fail the whole data collection if some of the
            // operation fails.
            failures.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
        }
        // Process successful operations.
        ops.values().stream().filter(operation -> failures != null && !failures.containsKey(operation.getId())).filter(operation -> operation.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
            SubnetState subnetState = operation.getBody(SubnetState.class);
            context.subnets.get(subnetState.id).subnetState = subnetState;
        });
        if (context.enumNextPageLink != null) {
            logFine(() -> "Fetch next page of Virtual Networks from Azure.");
            handleSubStage(context, NetworkEnumStages.GET_VNETS);
            return;
        }
        logFine(() -> "Finished updating network states");
        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) SubnetStateWithParentVNetId(com.vmware.photon.controller.model.adapters.azure.enumeration.AzureNetworkEnumerationAdapterService.NetworkEnumContext.SubnetStateWithParentVNetId) Operation(com.vmware.xenon.common.Operation) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

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