/
get_cost_forecast.go
104 lines (85 loc) · 2.26 KB
/
get_cost_forecast.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package awsservice
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
"github.com/aws/aws-sdk-go-v2/service/costexplorer/types"
types2 "github.com/cduggn/ccexplorer/internal/types"
)
func (srv *Service) GetCostForecast(ctx context.Context,
req types2.GetCostForecastRequest) (
*costexplorer.
GetCostForecastOutput, error) {
result, err := srv.Client.GetCostForecast(context.TODO(),
&costexplorer.GetCostForecastInput{
Granularity: types.Granularity(req.Granularity),
Metric: types.Metric(req.Metric),
TimePeriod: &types.DateInterval{
Start: aws.String(req.Time.Start),
End: aws.String(req.Time.End),
},
PredictionIntervalLevel: aws.Int32(req.PredictionIntervalLevel),
Filter: CostForecastFilterGenerator(req),
})
if err != nil {
return nil, types2.APIError{
Msg: err.Error(),
}
}
return result, nil
}
func CostForecastFilterGenerator(req types2.GetCostForecastRequest) *types.
Expression {
var filterExpression types.Expression
var expList []types.Expression
var exp types.Expression
if req.Filter.Dimensions == nil && req.Filter.Tags == nil {
return nil
}
var isMultiFilter bool
if len(req.Filter.Dimensions) > 1 {
isMultiFilter = true
}
for _, dimension := range req.Filter.Dimensions {
temp := &types.DimensionValues{
Key: types.Dimension(dimension.Key),
Values: dimension.Value,
}
if len(req.Filter.Dimensions) == 1 {
expList = append(expList, types.Expression{
Dimensions: temp,
})
} else if len(req.Filter.Dimensions) > 1 {
exp.And = append(exp.And, types.Expression{
Dimensions: temp,
})
}
}
if isMultiFilter {
expList = append(expList, exp)
}
filterExpression = expList[0]
return &filterExpression
}
func ExtractForecastFilters(d map[string]string) types2.Filter {
if len(d) == 0 {
return types2.Filter{}
}
dimensions := CreateForecastDimensionFilter(d)
return types2.Filter{
Dimensions: dimensions,
}
}
func CreateForecastDimensionFilter(m map[string]string) []types2.Dimension {
if len(m) == 0 {
return nil
}
var dimensions []types2.Dimension
for k, v := range m {
dimensions = append(dimensions, types2.Dimension{
Key: k,
Value: []string{v},
})
}
return dimensions
}