/
aws.go
82 lines (66 loc) · 2.01 KB
/
aws.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
package aws
import (
"errors"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/costexplorer"
"github.com/shopspring/decimal"
"github.com/mrusme/cloudcash/lib"
)
type AWS struct {
c *costexplorer.CostExplorer
}
func New(config *lib.Config) (*AWS, error) {
if config.Service.AWS.AWSAccessKeyID == "" ||
config.Service.AWS.AWSSecretAccessKey == "" ||
config.Service.AWS.Region == "" {
return nil, errors.New("No API key")
}
s := new(AWS)
sess, err := session.NewSession(&aws.Config{
Region: aws.String(config.Service.AWS.Region),
Credentials: credentials.NewStaticCredentials(config.Service.AWS.AWSAccessKeyID, config.Service.AWS.AWSSecretAccessKey, ""),
})
if err != nil {
return nil, err
}
s.c = costexplorer.New(sess)
return s, nil
}
func (s *AWS) GetServiceStatus() (*lib.ServiceStatus, error) {
start := time.Now().Format("2006-01-02")
end := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
granularity := "MONTHLY"
metrics := []string{
"UnblendedCost",
"UsageQuantity",
}
result, err := s.c.GetCostAndUsage(&costexplorer.GetCostAndUsageInput{
TimePeriod: &costexplorer.DateInterval{
Start: aws.String(start),
End: aws.String(end),
},
Granularity: aws.String(granularity),
GroupBy: []*costexplorer.GroupDefinition{
&costexplorer.GroupDefinition{
Type: aws.String("DIMENSION"),
Key: aws.String("SERVICE"),
},
},
Metrics: aws.StringSlice(metrics),
})
if err != nil {
return nil, err
}
status := new(lib.ServiceStatus)
status.AccountBalance, _ = decimal.NewFromString("0.0")
if len(result.ResultsByTime) > 0 {
if unblendedCost, ok := result.ResultsByTime[0].Total["UnblendedCost"]; ok {
status.CurrentCharges, _ = decimal.NewFromString(*unblendedCost.Amount)
}
}
status.PreviousCharges, _ = decimal.NewFromString("0.0")
return status, nil
}