Search in sources :

Example 1 with AzureSubscription

use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.

the class AzureCostStatsService method populateMonthlySubscriptionCost.

/**
 * Populate the total cost of each subscription for a given month.
 * @param context the data holder for the current run.
 * @param newMonthlyBillBatch new batch of rows of the bill.
 */
private void populateMonthlySubscriptionCost(Context context, Map<String, AzureSubscription> newMonthlyBillBatch) {
    for (AzureSubscription subscription : newMonthlyBillBatch.values()) {
        String subscriptionGuid = subscription.entityId;
        AzureSubscription existingSubscription = context.allSubscriptionsCost.get(subscriptionGuid);
        if (existingSubscription == null) {
            existingSubscription = new AzureSubscription(subscriptionGuid, subscription.entityName, subscription.parentEntityId, subscription.parentEntityName);
            context.allSubscriptionsCost.put(subscriptionGuid, existingSubscription);
        }
        for (Entry<Long, Double> cost : subscription.cost.entrySet()) {
            existingSubscription.addToDailyCosts(cost.getKey(), cost.getValue());
        }
    }
}
Also used : AtomicDouble(com.google.common.util.concurrent.AtomicDouble) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

Example 2 with AzureSubscription

use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.

the class AzureCostStatsService method createAzureSubscriptionStats.

// Create Azure account stats
private void createAzureSubscriptionStats(Context context, AzureSubscription subscription) {
    // convert the subscription daily costs to cumulative
    AtomicDouble cumulativeValue = new AtomicDouble(0.0);
    subscription.cost = subscription.cost.entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).collect(Collectors.toMap(Entry::getKey, e -> cumulativeValue.addAndGet(e.getValue())));
    Consumer<List<ComputeState>> subscriptionStatsProcessor = (subscriptionComputeStates) -> subscriptionComputeStates.forEach(subscriptionComputeState -> {
        String statName = AzureStatsNormalizer.getNormalizedStatKeyValue(AzureCostConstants.COST);
        String costUnit = AzureStatsNormalizer.getNormalizedUnitValue(AzureCostConstants.DEFAULT_CURRENCY_VALUE);
        ComputeStats subscriptionStats = new ComputeStats();
        subscriptionStats.computeLink = subscriptionComputeState.documentSelfLink;
        subscriptionStats.statValues = new ConcurrentSkipListMap<>();
        List<ServiceStat> costStats = new ArrayList<>();
        for (Entry<Long, Double> cost : subscription.cost.entrySet()) {
            ServiceStat azureAccountStat = AzureCostHelper.createServiceStat(statName, cost.getValue(), costUnit, cost.getKey());
            costStats.add(azureAccountStat);
        }
        subscriptionStats.statValues.put(statName, costStats);
        context.statsResponse.statsList.add(subscriptionStats);
    });
    processSubscriptionStats(context, subscription, subscriptionStatsProcessor);
}
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) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ArrayList(java.util.ArrayList) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) Entry(java.util.Map.Entry) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with AzureSubscription

use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.

the class AzureSubscriptionsEnumerationServiceTest method testAddFirstAzureSubscription.

private void testAddFirstAzureSubscription() throws Throwable {
    // Fail if this.computeLink is null
    Assert.assertNotNull("Root computeLink of Azure EA account is null", this.computeLink);
    // Fail if this.compute is null
    Assert.assertNotNull("Root compute of Azure EA account is null", this.compute);
    // Request for creating computes for 1 Azure Subscriptions
    AzureSubscription subscription = getAzureSubscription(SUBSCRIPTION_ID_1, ACCOUNT_EMAIL_ID_1);
    AzureSubscription existingSubscription1 = getAzureSubscription(SUBSCRIPTION_EXISTING_1, ACCOUNT_EMAIL_ID_1);
    createAzureCostComputesForSubscriptions(Arrays.asList(subscription, existingSubscription1));
    // Query for Azure Computes created with CLIENT_ID as enrollment Number
    QueryTask task = createQueryTaskForAzureComputes(ENROLLMENT_NUMNBER, Collections.singletonList(TENANT_ID));
    QueryTask queryTaskResponse = executQuerySynchronously(task);
    assertQueryTaskResponse(queryTaskResponse, 3);
    // Remove compute for existing subscription
    ComputeState existingSubsCs = Utils.fromJson(queryTaskResponse.results.documents.remove(this.existingSubsComputeLink1), ComputeState.class);
    Assert.assertNotNull(existingSubsCs);
    Assert.assertNull(existingSubsCs.customProperties.get(PhotonModelConstants.AUTO_DISCOVERED_ENTITY));
    // Get and assert the returned compute
    queryTaskResponse.results.documents.values().forEach(resultCompute -> {
        ComputeState cs = Utils.fromJson(resultCompute, ComputeState.class);
        assertPropertiesOfComputeState(cs, ENROLLMENT_NUMNBER, cs.customProperties.get(AzureConstants.AZURE_SUBSCRIPTION_ID_KEY), ACCOUNT_EMAIL_ID_1, this.compute.endpointLink, this.compute.tenantLinks);
        this.createdComputeLinks.add(cs.documentSelfLink);
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

Example 4 with AzureSubscription

use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.

the class AzureSubscriptionsEnumerationServiceTest method testAddSameAzureSubscriptions.

private void testAddSameAzureSubscriptions() throws Throwable {
    // Request for creating computes for existing Azure Subscriptions
    AzureSubscription subscription1 = getAzureSubscription(SUBSCRIPTION_ID_1, ACCOUNT_EMAIL_ID_1);
    AzureSubscription subscription2 = getAzureSubscription(SUBSCRIPTION_ID_2, ACCOUNT_EMAIL_ID_2);
    AzureSubscription existingSubscription2 = getAzureSubscription(SUBSCRIPTION_EXISTING_2, ACCOUNT_EMAIL_ID_2);
    createAzureCostComputesForSubscriptions(Arrays.asList(subscription1, subscription2, existingSubscription2));
    // Query for Azure Computes created with CLIENT_ID as enrollment Number
    QueryTask task = createQueryTaskForAzureComputes(ENROLLMENT_NUMNBER, Collections.singletonList(TENANT_ID));
    QueryTask queryTaskResponse = executQuerySynchronously(task);
    assertQueryTaskResponse(queryTaskResponse, 6);
    // Remove compute for existing subscription
    ComputeState existingSubsCs1 = Utils.fromJson(queryTaskResponse.results.documents.remove(this.existingSubsComputeLink1), ComputeState.class);
    Assert.assertNotNull(existingSubsCs1);
    Assert.assertNull(existingSubsCs1.customProperties.get(PhotonModelConstants.AUTO_DISCOVERED_ENTITY));
    ComputeState existingSubsCs2 = Utils.fromJson(queryTaskResponse.results.documents.remove(this.existingSubsComputeLink2), ComputeState.class);
    Assert.assertNotNull(existingSubsCs2);
    Assert.assertNull(existingSubsCs1.customProperties.get(PhotonModelConstants.AUTO_DISCOVERED_ENTITY));
    // Remove the already asserted computes
    this.createdComputeLinks.stream().forEach(computeLnk -> {
        queryTaskResponse.results.documents.remove(computeLnk);
    });
    // Get and assert the returned compute, which is created newly
    ComputeState cs = Utils.fromJson(queryTaskResponse.results.documents.values().iterator().next(), ComputeState.class);
    assertPropertiesOfComputeState(cs, ENROLLMENT_NUMNBER, SUBSCRIPTION_EXISTING_2, ACCOUNT_EMAIL_ID_2, this.compute.endpointLink, this.compute.tenantLinks);
    this.createdComputeLinks.add(cs.documentSelfLink);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

Example 5 with AzureSubscription

use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.

the class AzureSubscriptionEndpointsEnumerationServiceTest method getAzureSubscription.

private AzureSubscription getAzureSubscription(String subscriptionId, String accountId) {
    AzureSubscription subscription = new AzureSubscription();
    subscription.entityId = subscriptionId;
    subscription.parentEntityId = accountId;
    return subscription;
}
Also used : AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

Aggregations

AzureSubscription (com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)15 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)8 QueryTask (com.vmware.xenon.services.common.QueryTask)7 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)6 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)5 AtomicDouble (com.google.common.util.concurrent.AtomicDouble)4 AzureService (com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService)4 Sets (com.google.common.collect.Sets)3 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)3 ComputeStatsResponse (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse)3 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)3 EndpointConfigRequest (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest)3 AzureUriPaths (com.vmware.photon.controller.model.adapters.azure.AzureUriPaths)3 AzureCostConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants)3 INTERNAL_REQUEST_TIMEOUT_SECONDS (com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants.INTERNAL_REQUEST_TIMEOUT_SECONDS)3 AzureSubscriptionsEnumerationService (com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService)3 AzureSubscriptionsEnumerationRequest (com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService.AzureSubscriptionsEnumerationRequest)3 AzureCostHelper (com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureCostHelper)3 AzureDetailedBillHandler (com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureDetailedBillHandler)3 AzureResource (com.vmware.photon.controller.model.adapters.azure.model.cost.AzureResource)3