Search in sources :

Example 6 with AwsAccountDetailDto

use of com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto in project photon-model by vmware.

the class AWSCsvBillParser method createOrGetAccountDetailObject.

private AwsAccountDetailDto createOrGetAccountDetailObject(Map<String, AwsAccountDetailDto> monthlyBill, final String linkedAccountId) {
    AwsAccountDetailDto accountDetails = monthlyBill.get(linkedAccountId);
    if (accountDetails == null) {
        accountDetails = new AwsAccountDetailDto();
        accountDetails.id = linkedAccountId;
        monthlyBill.put(linkedAccountId, accountDetails);
    }
    return accountDetails;
}
Also used : AwsAccountDetailDto(com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto)

Example 7 with AwsAccountDetailDto

use of com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto in project photon-model by vmware.

the class AWSCsvBillParser method readRow.

/**
 * This method reads each row of the AWS bill file, ignores the values that
 * are not required, creates or updates the corresponding entry in
 * monthlyBill Map.
 */
private void readRow(Map<String, Object> rowMap, Map<String, AwsAccountDetailDto> monthlyBill, List<String> tagHeaders, Collection<String> ignorableInvoiceCharge, Set<String> configuredAccounts) {
    final String linkedAccountId = getStringFieldValue(rowMap, DetailedCsvHeaders.LINKED_ACCOUNT_ID);
    String serviceName = getStringFieldValue(rowMap, DetailedCsvHeaders.PRODUCT_NAME);
    String subscriptionId = getStringFieldValue(rowMap, DetailedCsvHeaders.SUBSCRIPTION_ID);
    // Summary Row: For all rows except summary rows this is not null.
    if (subscriptionId == null || subscriptionId.length() == 0 || serviceName == null || serviceName.length() == 0) {
        // Reads the summary lines in bill file, which consists of the
        // account cost and puts it in the monthly bill map
        readSummaryRow(rowMap, linkedAccountId, serviceName, monthlyBill, ignorableInvoiceCharge);
        return;
    }
    // ------------------------------------------------------------------------------------
    // Check if the account corresponding to this row is configured in the system.
    // If not, we skip holding related information in memory.
    AwsAccountDetailDto accountDetails = createOrGetAccountDetailObject(monthlyBill, linkedAccountId);
    if (!configuredAccounts.contains(linkedAccountId)) {
        return;
    }
    // Non-summary rows.
    if (this.billInvoiceId == null) {
        this.billInvoiceId = getStringFieldValue(rowMap, DetailedCsvHeaders.INVOICE_ID);
    }
    LocalDateTime usageStartTimeFromCsv = (LocalDateTime) rowMap.get(DetailedCsvHeaders.USAGE_START_DATE);
    Long millisForBillHour = getMillisForHour(usageStartTimeFromCsv);
    boolean isRowMarkedAsReserved = isRowReservedInstanceRecurringCost(rowMap);
    // for previous months.
    if (millisForBillHour == null || (millisForBillHour >= this.monthStartMillis)) {
        String resourceId = getStringFieldValue(rowMap, DetailedCsvHeaders.RESOURCE_ID);
        AwsServiceDetailDto serviceDetail = createOrGetServiceDetailObject(accountDetails, serviceName, resourceId);
        Double resourceCost = getResourceCost(rowMap);
        // (recurring charges for reserved instance)or sign up charges(which we have to ignore)}
        if (resourceId == null || resourceId.length() == 0) {
            // day, otherwise set it as common for month, can divide later for all days
            if (millisForBillHour != null) {
                if (isRowMarkedAsReserved && matchFieldValue(rowMap, DetailedCsvHeaders.OPERATION, RUN_INSTANCES)) {
                    serviceDetail.addToReservedRecurringCosts(millisForBillHour, resourceCost);
                } else {
                    serviceDetail.addToOtherCosts(millisForBillHour, resourceCost);
                    // Adding zero as direct cost for this entity to allow
                    // populating this as a resource while getting services
                    serviceDetail.addToDirectCosts(millisForBillHour, 0d);
                }
            }
        } else {
            if (millisForBillHour != null) {
                serviceDetail.addToDirectCosts(millisForBillHour, resourceCost);
                // currently we need only EC2 instances,volumes and s3 buckets
                if (resourceId.startsWith(AWS_INSTANCE_ID_PREFIX) || resourceId.startsWith(AWS_VOLUME_ID_PREFIX) || AwsServices.S3.getName().equalsIgnoreCase(serviceName)) {
                    AwsResourceDetailDto resourceDetail = createOrGetResourceDetailObject(rowMap, serviceDetail, resourceId);
                    resourceDetail.addToDirectCosts(millisForBillHour, resourceCost);
                    setLatestResourceValues(rowMap, tagHeaders, resourceDetail);
                }
            }
        }
    }
    if (millisForBillHour != null && millisForBillHour > accountDetails.billProcessedTimeMillis && millisForBillHour < System.currentTimeMillis()) {
        accountDetails.billProcessedTimeMillis = millisForBillHour;
    }
    // update the line count of the account to whom this row belongs to
    if (millisForBillHour != null) {
        LocalDateTime usageEndTimeFromCsv = (LocalDateTime) rowMap.get(DetailedCsvHeaders.USAGE_END_DATE);
        Long endMillisForBillHour = getMillisForHour(usageEndTimeFromCsv);
        String interval = createInterval(millisForBillHour, endMillisForBillHour);
        Integer currentLineCount = accountDetails.lineCountPerInterval.getOrDefault(interval, 0);
        accountDetails.lineCountPerInterval.put(interval, currentLineCount + 1);
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) AwsResourceDetailDto(com.vmware.photon.controller.model.adapters.aws.dto.AwsResourceDetailDto) AwsServiceDetailDto(com.vmware.photon.controller.model.adapters.aws.dto.AwsServiceDetailDto) AwsAccountDetailDto(com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto)

Aggregations

AwsAccountDetailDto (com.vmware.photon.controller.model.adapters.aws.dto.AwsAccountDetailDto)7 AwsServiceDetailDto (com.vmware.photon.controller.model.adapters.aws.dto.AwsServiceDetailDto)5 AwsResourceDetailDto (com.vmware.photon.controller.model.adapters.aws.dto.AwsResourceDetailDto)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ProgressEvent (com.amazonaws.event.ProgressEvent)3 ProgressEventType (com.amazonaws.event.ProgressEventType)3 ProgressListener (com.amazonaws.event.ProgressListener)3 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)3 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)3 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)3 UriPaths (com.vmware.photon.controller.model.UriPaths)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 ACCOUNT_IS_AUTO_DISCOVERED (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.ACCOUNT_IS_AUTO_DISCOVERED)3 AWS_ACCOUNT_BILL_PROCESSED_TIME_MILLIS (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_ACCOUNT_BILL_PROCESSED_TIME_MILLIS)3 AWS_ACCOUNT_ID_KEY (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_ACCOUNT_ID_KEY)3 AWS_LINKED_ACCOUNT_IDS (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWS_LINKED_ACCOUNT_IDS)3