-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_cost.go
56 lines (51 loc) · 1.61 KB
/
get_cost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package aws
import (
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/costexplorer"
"github.com/stangirard/pricy/internal/format"
)
func getCostUsageByService(costExplorer *costexplorer.CostExplorer, start, end string, granularity string) *costexplorer.GetCostAndUsageOutput {
costAndUsageOutput, err := costExplorer.GetCostAndUsage(&costexplorer.GetCostAndUsageInput{
TimePeriod: &costexplorer.DateInterval{
Start: aws.String(start),
End: aws.String(end),
},
Granularity: aws.String(granularity),
Metrics: []*string{
aws.String("UNBLENDED_COST"),
},
GroupBy: []*costexplorer.GroupDefinition{
{
Type: aws.String("DIMENSION"),
Key: aws.String("SERVICE"),
},
},
Filter: &costexplorer.Expression{
Not: &costexplorer.Expression{
Dimensions: &costexplorer.DimensionValues{
Key: aws.String("RECORD_TYPE"),
Values: []*string{aws.String("Refund"), aws.String("Credit"), aws.String("SavingsPlanNegation"), aws.String("Tax")},
},
},
},
})
if err != nil {
panic(err)
}
return costAndUsageOutput
}
func formatCostUsagebyService(cost *costexplorer.GetCostAndUsageOutput) []format.Service {
var services []format.Service
for _, result := range cost.ResultsByTime {
for _, group := range result.Groups {
for index, metric := range group.Metrics {
if index == "UnblendedCost" {
cost, _ := strconv.ParseFloat(*metric.Amount, 64)
services = format.AddServices(services, *group.Keys[0], *metric.Unit, format.DateInterval{Start: *result.TimePeriod.Start, End: *result.TimePeriod.End}, cost)
}
}
}
}
return services
}