/
costexplorer.go
50 lines (41 loc) · 1.29 KB
/
costexplorer.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
package costexplorer
import (
"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/aws/aws-sdk-go/service/costexplorer/costexploreriface"
log "github.com/sirupsen/logrus"
)
// CostExplorer is a wrapper around the aws costexplorer service with some default config info
type CostExplorer struct {
session *session.Session
Service costexploreriface.CostExplorerAPI
}
type CostExplorerOption func(*CostExplorer)
func New(opts ...CostExplorerOption) *CostExplorer {
client := CostExplorer{}
for _, opt := range opts {
opt(&client)
}
if client.session != nil {
client.Service = costexplorer.New(client.session)
}
return &client
}
func WithSession(sess *session.Session) CostExplorerOption {
return func(client *CostExplorer) {
log.Debug("using aws session")
client.session = sess
}
}
func WithCredentials(key, secret, token, region string) CostExplorerOption {
return func(client *CostExplorer) {
log.Debugf("creating new session with key id %s in region %s", key, region)
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(key, secret, token),
Region: aws.String(region),
}))
client.session = sess
}
}