use of com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_LINKED_ACCOUNT_IDS in project photon-model by vmware.
the class AWSCostStatsService method createMissingResourcesComputes.
private void createMissingResourcesComputes(AWSCostStatsCreationContext context, AWSCostStatsCreationStages next) {
String linkedAccountsCsv = context.computeDesc.customProperties.get(AWS_LINKED_ACCOUNT_IDS);
if (context.isSecondPass || linkedAccountsCsv == null || linkedAccountsCsv.isEmpty() || !isAutoDiscoveryEnabled(context)) {
context.stage = next;
handleCostStatsCreationRequest(context);
return;
}
// Following function tells whether the computes of a particular account contain an
// auto-discovered compute under the primary account's endpoint.
Function<List<ComputeState>, Boolean> hasAutoDiscoveredCompute = (computes) -> {
if (CollectionUtils.isEmpty(computes)) {
return false;
}
return computes.stream().filter(c -> c.endpointLinks.contains(context.computeDesc.endpointLink) && Boolean.valueOf(c.customProperties.get(ACCOUNT_IS_AUTO_DISCOVERED))).count() != 0;
};
Set<String> existingLinkedAccountIds = context.awsAccountIdToComputeStates.entrySet().stream().filter(e -> hasAutoDiscoveredCompute.apply(e.getValue())).map(e -> e.getKey()).collect(Collectors.toCollection(HashSet::new));
List<String> missingLinkedAccountIds = Stream.of(linkedAccountsCsv.split(",")).filter(id -> !existingLinkedAccountIds.contains(id)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(missingLinkedAccountIds)) {
context.stage = next;
handleCostStatsCreationRequest(context);
return;
}
AWSMissingResourcesEnumerationService.Request request = new AWSMissingResourcesEnumerationService.Request();
request.missingLinkedAccountIds = missingLinkedAccountIds;
request.primaryAccountCompute = context.computeDesc;
Operation op = Operation.createPost(UriUtils.buildUri(getHost(), AWSMissingResourcesEnumerationService.SELF_LINK)).setBody(request).setCompletion((o, e) -> {
// Even if the operation fails, we continue with the second pass.
// The exception might indicate failure for creation of few linked account
// computes but not all. So we execute the second pass to model the cost of
// atleast the ones which were created.
context.stage = AWSCostStatsCreationStages.QUERY_LINKED_ACCOUNTS;
context.isSecondPass = true;
context.billMonthToDownload = null;
context.awsResourceLinksById.clear();
handleCostStatsCreationRequest(context);
});
sendRequest(op);
}
Aggregations