diff --git a/build.gradle b/build.gradle index 163c680..133fd54 100644 --- a/build.gradle +++ b/build.gradle @@ -30,6 +30,7 @@ dependencies { implementation 'software.amazon.awssdk:ec2' implementation 'software.amazon.awssdk:iam' implementation 'software.amazon.awssdk:costexplorer' + implementation 'software.amazon.awssdk:costandusagereport' implementation 'com.google.code.gson:gson:2.8.5' implementation "javax.annotation:javax.annotation-api:1.3.2" diff --git a/src/main/java/com/tmax/cmp/controller/aws/AWSInstanceBillingController.java b/src/main/java/com/tmax/cmp/controller/aws/AWSInstanceBillingController.java index 5261b99..e36c44e 100644 --- a/src/main/java/com/tmax/cmp/controller/aws/AWSInstanceBillingController.java +++ b/src/main/java/com/tmax/cmp/controller/aws/AWSInstanceBillingController.java @@ -14,9 +14,22 @@ public class AWSInstanceBillingController { AWSInstanceBillingService awsInstanceBillingService; @GetMapping("/annual") - public void getAnnualCost(String startDate, String endDate){ + public void getMetricAndBillings(String startDate, String endDate, String metrics, String granularity, + String group, String dimensionKey, String dimensionValue){ - awsInstanceBillingService.getCostOfInstance(startDate, endDate); + awsInstanceBillingService.getCostOfInstance(startDate, endDate, metrics, granularity, group, dimensionKey, dimensionValue); + } + + @GetMapping("/dimensionValue") + public void getDimensionValue(String startDate, String endDate, String dimensionContext, String dimensionKey){ + + awsInstanceBillingService.getDimensionValues(dimensionContext, dimensionKey, startDate, endDate); + } + + @GetMapping("/costAndUsageReport") + public void getCostAndUsageReport(){ + + awsInstanceBillingService.getCostAndUsageReport(); } } diff --git a/src/main/java/com/tmax/cmp/entity/common/client/awsClient.java b/src/main/java/com/tmax/cmp/entity/common/client/awsClient.java index 03a8f63..fb25e50 100644 --- a/src/main/java/com/tmax/cmp/entity/common/client/awsClient.java +++ b/src/main/java/com/tmax/cmp/entity/common/client/awsClient.java @@ -4,6 +4,7 @@ import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.costandusagereport.CostAndUsageReportClient; import software.amazon.awssdk.services.costexplorer.CostExplorerClient; import software.amazon.awssdk.services.ec2.Ec2Client; @@ -19,15 +20,20 @@ public class awsClient implements Client{ Region.EU_WEST_1, Region.AP_NORTHEAST_3, Region.AP_NORTHEAST_2, Region.AP_NORTHEAST_1, Region.SA_EAST_1, Region.AP_EAST_1, Region.CN_NORTH_1, Region.AP_SOUTHEAST_1, Region.AP_SOUTHEAST_2, Region.US_EAST_1, Region.US_EAST_2)); private Ec2Client ec2Client; - private CostExplorerClient costClient; + private CostExplorerClient costExplorerClient; + private CostAndUsageReportClient costAndUsageReportClient; + @Getter - private List costClients; + private List costExplorerClients; @Getter private List ec2Clients; + @Getter + private List costAndUsageReportClients; public awsClient(String accessKey, String secretKey) { AwsBasicCredentials awsCredential = AwsBasicCredentials.create(accessKey, secretKey); - this.costClients = new ArrayList(); + this.costExplorerClients = new ArrayList(); + this.costAndUsageReportClients = new ArrayList(); this.ec2Clients = new ArrayList(); for(Region region : REGIONS) { @@ -35,11 +41,17 @@ public awsClient(String accessKey, String secretKey) { .credentialsProvider(StaticCredentialsProvider.create(awsCredential)) .region(region) .build(); - costClient = CostExplorerClient.builder() - .region(region) - .build(); ec2Clients.add(ec2Client); - costClients.add(costClient); } + + costExplorerClient = CostExplorerClient.builder().credentialsProvider(StaticCredentialsProvider.create(awsCredential)) + .region(Region.US_EAST_1) + .build(); + + costAndUsageReportClient = CostAndUsageReportClient.builder().credentialsProvider(StaticCredentialsProvider.create(awsCredential)) + .region(Region.US_EAST_1).build(); + + costExplorerClients.add(costExplorerClient); + costAndUsageReportClients.add(costAndUsageReportClient); } } diff --git a/src/main/java/com/tmax/cmp/svc/aws/AWSInstanceBillingService.java b/src/main/java/com/tmax/cmp/svc/aws/AWSInstanceBillingService.java index af6ce3e..92c399c 100644 --- a/src/main/java/com/tmax/cmp/svc/aws/AWSInstanceBillingService.java +++ b/src/main/java/com/tmax/cmp/svc/aws/AWSInstanceBillingService.java @@ -1,50 +1,127 @@ package com.tmax.cmp.svc.aws; +import com.tmax.cmp.configuration.ClientConfig; +import com.tmax.cmp.entity.common.client.awsClient; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Service; -import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.costandusagereport.CostAndUsageReportClient; +import software.amazon.awssdk.services.costandusagereport.model.DescribeReportDefinitionsRequest; +import software.amazon.awssdk.services.costandusagereport.model.DescribeReportDefinitionsResponse; import software.amazon.awssdk.services.costexplorer.CostExplorerClient; import software.amazon.awssdk.services.costexplorer.model.*; +import java.util.List; + @Service public class AWSInstanceBillingService { - public void getCostOfInstance(String startDate, String endDate){ + public void getCostOfInstance(String startDate, String endDate, String metrics, String granularity, + String group, String dimensionKey, String dimensionValue){ - CostExplorerClient costClient = CostExplorerClient.builder().region(Region.US_EAST_1).build(); - GroupDefinition groupDefinition = GroupDefinition.builder().type("DIMENSION").key("AZ").build(); - DateInterval dateInterval = DateInterval.builder().start(startDate).end(endDate).build(); - String metrics = "BlendedCost"; - GetCostAndUsageRequest request = GetCostAndUsageRequest - .builder().timePeriod(dateInterval).granularity("MONTHLY").metrics(metrics) - .groupBy(groupDefinition).build(); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ClientConfig.class); + List awsClientList = (List)context.getBean("awsClients"); + + for(awsClient awsClient : awsClientList){ + for(CostExplorerClient costExplorerClient : awsClient.getCostExplorerClients()){ + + GroupDefinition groupDefinition = GroupDefinition.builder().type("DIMENSION").key(group).build(); + + DateInterval dateInterval = DateInterval.builder().start(startDate).end(endDate).build(); + + Expression exp = null; + + + if(dimensionKey != null || dimensionValue != null){ + exp = Expression.builder() + .dimensions(DimensionValues.builder() + .key(dimensionKey).values(dimensionValue) + .build()) + .build(); + } + + GetCostAndUsageRequest request = GetCostAndUsageRequest + .builder().timePeriod(dateInterval).granularity(granularity).metrics(metrics) + .groupBy(groupDefinition) + .filter(exp) + .build(); - GetCostAndUsageResponse response = costClient.getCostAndUsage(request); + System.out.println("==============costAndUsage================="); + GetCostAndUsageResponse response = costExplorerClient.getCostAndUsage(request); - Object[] costResponse = response.resultsByTime().toArray(); + Object[] costResponse = response.resultsByTime().toArray(); - int time = 1; - for(Object obj : costResponse){ - ResultByTime resultByTime = (ResultByTime)obj; + int time = 1; + for(Object obj : costResponse){ - System.out.println("=== Time" + time + ": " + resultByTime.timePeriod().start() + " ~ " + resultByTime.timePeriod().end() + " ==="); - String cost; - String unit; - String region; + ResultByTime resultByTime = (ResultByTime)obj; - for(Group group : resultByTime.groups()){ + System.out.println("=== Time" + time + ": " + resultByTime.timePeriod().start() + " ~ " + resultByTime.timePeriod().end() + " ==="); + String cost; + String unit; + String groupResponse; + + for(Group groupResult : resultByTime.groups()){ + + cost = groupResult.metrics().get(metrics).amount(); + unit = groupResult.metrics().get(metrics).unit(); + groupResponse = groupResult.keys().toString(); + System.out.println("Group: " + groupResponse.substring(1,groupResponse.length()-1).toUpperCase() + " | Cost: " + cost + " " + unit); + } + System.out.println(); + + time++; + } -// System.out.println(group.toString()); - cost = group.metrics().get(metrics).amount(); - unit = group.metrics().get(metrics).unit(); - region = group.keys().toString(); - System.out.println("Region: " + region.substring(1,region.length()-1).toUpperCase() + " | Cost: " + cost + " " + unit); } - System.out.println(); + } + + } + + public void getCostAndUsageReport(){ + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ClientConfig.class); + List awsClientList = (List)context.getBean("awsClients"); + + for(awsClient awsClient : awsClientList) { + for (CostAndUsageReportClient costAndUsageReportClient : awsClient.getCostAndUsageReportClients()) { - time++; + DescribeReportDefinitionsRequest request = DescribeReportDefinitionsRequest + .builder().maxResults(5).build(); + + DescribeReportDefinitionsResponse response = costAndUsageReportClient.describeReportDefinitions(request); + System.out.println("=========Report========="); + System.out.println(response.toString()); + } } } + + public void getDimensionValues(String dimensionContext, String dimensionKey, String startDate, String endDate) { + + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ClientConfig.class); + List awsClientList = (List) context.getBean("awsClients"); + + DateInterval dateInterval = DateInterval.builder().start(startDate).end(endDate).build(); + + for (awsClient awsClient : awsClientList) { + for (CostExplorerClient costExplorerClient : awsClient.getCostExplorerClients()) { + + + GetDimensionValuesRequest request1 = GetDimensionValuesRequest + .builder().context(dimensionContext).dimension(Dimension.fromValue(dimensionKey)).timePeriod(dateInterval).build(); + + + GetDimensionValuesResponse response1 = costExplorerClient.getDimensionValues(request1); + + System.out.println("============= DimensionValuesWith Attributes ============="); + for (DimensionValuesWithAttributes dimensionValuesWithAttributes : response1.dimensionValues()) { + System.out.println(dimensionValuesWithAttributes.value() + " : " + dimensionValuesWithAttributes.attributes()); + } + } + + } + } + }