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());
}
}
}
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);
}
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);
});
}
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);
}
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;
}
Aggregations