use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.
the class AzureSubscriptionEndpointsEnumerationServiceTest method testAddSameAzureSubscriptions.
private void testAddSameAzureSubscriptions() throws Throwable {
// Request for creating computes for existing Azure Subscriptions
AzureSubscription subscription1 = getAzureSubscription(SUBSCRIPTION_ID_1, ACCOUNT_ID_1);
AzureSubscription subscription2 = getAzureSubscription(SUBSCRIPTION_ID_2, ACCOUNT_ID_2);
Collection<AzureSubscription> subscriptions = new ArrayList<>();
subscriptions.add(subscription1);
subscriptions.add(subscription2);
createAzureEndpointsForSubscriptions(subscriptions);
// Query the Endpoints to assert
ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, EndpointService.FACTORY_LINK));
Assert.assertEquals(3, result.documents.size());
}
use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.
the class AzureSubscriptionEndpointsEnumerationServiceTest method testAddFirstAzureSubscription.
private void testAddFirstAzureSubscription() throws Throwable {
// Request for creating computes for 1 Azure Subscriptions
AzureSubscription subscription = getAzureSubscription(SUBSCRIPTION_ID_1, ACCOUNT_ID_1);
createAzureEndpointsForSubscriptions(Collections.singletonList(subscription));
// Query the Endpoints to assert
ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, EndpointService.FACTORY_LINK));
Assert.assertEquals(2, result.documents.size());
// Assert the created Endpoint and other resources
result.documents.remove(this.endpointLink);
EndpointState endpointStateCreated = Utils.fromJson(result.documents.values().iterator().next(), EndpointState.class);
assertCreatedEndpoint(endpointStateCreated, SUBSCRIPTION_ID_1);
this.createdEndpointLinks.add(endpointStateCreated.documentSelfLink);
// Assert the root compute under the endpoint
ComputeState computeStateCreated = getServiceSynchronously(endpointStateCreated.computeLink, ComputeState.class);
assertCreatedComputeState(computeStateCreated, SUBSCRIPTION_ID_1, ACCOUNT_ID_1);
// Assert the partial AuthCredentialsState
AuthCredentialsServiceState authCreated = getServiceSynchronously(endpointStateCreated.authCredentialsLink, AuthCredentialsServiceState.class);
assertAuthCredentialState(authCreated, SUBSCRIPTION_ID_1);
}
use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.
the class AzureCostStatsService method createMissingComputeStates.
/**
* The user adds the EA account as the end-point. By parsing the bill, we understand there are
* one or more subscriptions, resources whose compute states need to be created in order
* to store cost information for those entities.
*/
private void createMissingComputeStates(Context context, Stages next, List<AzureSubscription> newSubscriptions) {
logInfo(() -> String.format("Creating compute states for the following subscriptions: %s for " + "endpoint %s ", newSubscriptions.toString(), context.computeHostDesc.endpointLink));
AzureSubscriptionsEnumerationRequest request = new AzureSubscriptionsEnumerationRequest();
request.resourceReference = UriUtils.extendUri(getInventoryServiceUri(), context.computeHostDesc.documentSelfLink);
request.azureSubscriptions = newSubscriptions;
Operation.createPatch(getHost(), AzureSubscriptionsEnumerationService.SELF_LINK).setBody(request).setCompletion((operation, exception) -> {
logInfo(() -> String.format("Finished creating compute states for " + "subscriptions under endpoint %s.", context.computeHostDesc.documentSelfLink));
context.stage = next;
handleRequest(context);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.
the class AzureCostStatsService method createServiceStatsForSubscription.
private void createServiceStatsForSubscription(Context context, AzureSubscription subscription) {
Consumer<List<ComputeState>> serviceStatsProcessor = (subscriptionComputeStates) -> subscriptionComputeStates.forEach(subscriptionComputeState -> {
List<ComputeStats> resourceStatsList = new ArrayList<>();
ComputeStats subscriptionStats = new ComputeStats();
subscriptionStats.statValues = new ConcurrentHashMap<>();
subscriptionStats.computeLink = subscriptionComputeState.documentSelfLink;
subscriptionStats.addCustomProperty(PhotonModelConstants.DOES_CONTAIN_SERVICE_STATS, Boolean.TRUE.toString());
for (AzureService service : subscription.getServices().values()) {
List<ComputeStats> resourcesCostStats = new ArrayList<>();
Map<String, List<ServiceStat>> statsForAzureService = createStatsForAzureService(context, service, resourcesCostStats);
subscriptionStats.statValues.putAll(statsForAzureService);
resourceStatsList.addAll(resourcesCostStats);
}
if (!subscriptionStats.statValues.isEmpty()) {
context.statsResponse.statsList.add(subscriptionStats);
}
if (!resourceStatsList.isEmpty()) {
context.statsResponse.statsList.addAll(resourceStatsList);
}
});
processSubscriptionStats(context, subscription, serviceStatsProcessor);
}
use of com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription in project photon-model by vmware.
the class AzureDetailedBillHandler method parseDetailedCsv.
public BillParsingStatus parseDetailedCsv(File billFile, Set<String> newSubscriptions, BillParsingStatus status, long billProcessedTimeMillis, String currency, BiConsumer<Map<String, AzureSubscription>, Long> dailyStatsConsumer) throws IOException {
logger.fine(() -> "Beginning to parse CSV file.");
try (CSVReader csvReader = new CSVReader(new FileReader(billFile), AzureCostConstants.DEFAULT_COLUMN_SEPARATOR, AzureCostConstants.DEFAULT_QUOTE_CHARACTER, AzureCostConstants.DEFAULT_ESCAPE_CHARACTER, (int) status.getNoLinesRead())) {
HeaderColumnNameMappingStrategy<EaDetailedBillElement> strategy = new HeaderColumnNameMappingStrategy<>();
strategy.setType(EaDetailedBillElement.class);
long timeToStartBillProcessing = getTimeToStartBillProcessing(billProcessedTimeMillis);
// This map will contain daily subscription, service & resource cost. The subscription
// GUID is the key and the subscription details is the value. This map is maintained
// since daily-level stats are needed for services and resources.
Map<String, AzureSubscription> monthlyBill = new HashMap<>();
String[] nextRow;
Long prevRowEpoch = null;
while ((nextRow = csvReader.readNext()) != null) {
final String[] finalNextRow = nextRow;
if (nextRow.length != BillHeaders.values().length) {
// Skip any blank or malformed rows
logger.warning(() -> String.format("Skipping malformed row: %s", Arrays.toString(finalNextRow)));
continue;
}
logger.fine(() -> String.format("Beginning to process row: %s", Arrays.toString(finalNextRow)));
EaDetailedBillElement detailedBillElement = AzureCostHelper.sanitizeDetailedBillElement(nextRow, currency);
AzureSubscription subscription = populateSubscriptionCost(monthlyBill, detailedBillElement);
long curRowEpoch = detailedBillElement.epochDate;
if (shouldCreateServiceAndResourceCost(detailedBillElement, newSubscriptions, timeToStartBillProcessing)) {
AzureService service = populateServiceCost(subscription, detailedBillElement);
populateResourceCost(service, detailedBillElement);
}
billProcessedTimeMillis = billProcessedTimeMillis < curRowEpoch ? curRowEpoch : billProcessedTimeMillis;
monthlyBill.put(detailedBillElement.subscriptionGuid, subscription);
if (prevRowEpoch != null && !prevRowEpoch.equals(curRowEpoch)) {
// This indicates that we have processed all rows belonging to a
// corresponding day in the current month's bill.
// Consume the batch
// Subtract 1, to account for detecting date change line.
status.setNoLinesRead(csvReader.getLinesRead() - 1);
dailyStatsConsumer.accept(monthlyBill, null);
break;
}
prevRowEpoch = curRowEpoch;
}
if ((nextRow == null && monthlyBill.size() > 0) || (nextRow == null && csvReader.getLinesRead() == AzureCostConstants.DEFAULT_LINES_TO_SKIP)) {
status.setParsingComplete(true);
dailyStatsConsumer.accept(monthlyBill, billProcessedTimeMillis);
logger.fine(() -> "Finished parsing CSV bill.");
}
return status;
}
}
Aggregations