-
Notifications
You must be signed in to change notification settings - Fork 6
/
client.go
159 lines (141 loc) · 4.18 KB
/
client.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
Copyright 2021 Adevinta
*/
package cgcatalogue
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/adevinta/vulcan-api/pkg/awscatalogue"
)
const (
awsProvider = "AWS"
apiVersion = "v1"
slash = "/"
)
var (
errAccounts = errors.New("error getting accounts data")
errAccount = errors.New("error getting account data")
)
// account represents the administrative information
// related with a provider account from CG API.
type account struct {
ID int64 `json:"id"`
AccountName string `json:"account_name"`
Administrators []string `json:"administrators"`
Asset string `json:"asset"`
CostCenter string `json:"cost_center"`
IsProduction bool `json:"is_production"`
Payer string `json:"payer"`
Provider string `json:"provider"`
ProviderID string `json:"provider_id"`
Status string `json:"status"`
}
func (a account) toAWSAccount() awscatalogue.Account {
return awscatalogue.Account{
ID: a.ProviderID,
AccountName: a.AccountName,
Status: a.Status,
}
}
type operationError struct {
opErr error
innerErr error
}
func (op *operationError) Unwrap() error {
return op.opErr
}
func (op *operationError) Error() string {
return fmt.Sprintf("%v:%v", op.opErr, op.innerErr)
}
// unexpectedStatusError is returned when a call the the
// catalogue API does not return the expected status code.
type unexpectedStatusError struct {
Expected int
Received int
}
func (e *unexpectedStatusError) Error() string {
return fmt.Sprintf("expected status %d, received status %d", e.Expected, e.Received)
}
// Client implements functions exposed by the CG Catalogue API.
type Client struct {
rt http.RoundTripper
baseURL string
}
type addAuthRoundTripper struct {
APIKey string
Next http.RoundTripper
}
func (a addAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
auth := fmt.Sprintf("apiKey %s", a.APIKey)
req.Header.Add("Authorization", auth)
return a.Next.RoundTrip(req)
}
// New returns a new Client given an API KEY, a baseURL and round tripper if the
// it is nil it will be set to the default http round tripper.
func New(APIKey string, baseURL string, rt http.RoundTripper) *Client {
if rt == nil {
rt = http.DefaultTransport
}
rt = addAuthRoundTripper{APIKey: APIKey, Next: rt}
if !strings.HasSuffix(baseURL, slash) {
baseURL = baseURL + slash
}
return &Client{rt, fmt.Sprint(baseURL, apiVersion, slash)}
}
// Accounts returns AWS accounts from CG catalogue.
func (c *Client) Accounts() ([]awscatalogue.Account, error) {
url := fmt.Sprintf("%saccounts/%s", c.baseURL, awsProvider)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, &operationError{errAccounts, err}
}
resp, err := c.rt.RoundTrip(req)
if err != nil {
return nil, &operationError{errAccounts, err}
}
if resp.StatusCode != http.StatusOK {
uerr := &unexpectedStatusError{http.StatusOK, resp.StatusCode}
return nil, &operationError{errAccounts, uerr}
}
accs := []account{}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, &operationError{errAccounts, err}
}
json.Unmarshal(content, &accs)
if err != nil {
return nil, &operationError{errAccounts, err}
}
awsAccs := []awscatalogue.Account{}
for _, a := range accs {
awsAccs = append(awsAccs, a.toAWSAccount())
}
return awsAccs, nil
}
// Account returns account information for the given AWS account ID.
func (c *Client) Account(ID string) (awscatalogue.Account, error) {
var acc account
url := fmt.Sprintf("%saccounts/%s/%s", c.baseURL, awsProvider, ID)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return awscatalogue.Account{}, &operationError{errAccount, err}
}
resp, err := c.rt.RoundTrip(req)
if err != nil {
return awscatalogue.Account{}, &operationError{errAccount, err}
}
if resp.StatusCode != http.StatusOK {
uerr := &unexpectedStatusError{http.StatusOK, resp.StatusCode}
return awscatalogue.Account{}, &operationError{uerr, err}
}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&acc)
if err != nil {
return awscatalogue.Account{}, &operationError{errAccount, err}
}
return acc.toAWSAccount(), nil
}