-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudability.go
318 lines (292 loc) · 10 KB
/
cloudability.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
"net/url"
"strconv"
"time"
)
type CloudabilityCostData struct {
Limit int `json:"limit"`
Meta MetaSection `json:"meta"`
Offset int `json:"offset"`
Pagination struct {
Next string `json:"next"`
Previous string `json:"previous"`
} `json:"pagination"`
Results []ResultsEntry `json:"results"`
TotalResults int `json:"total_results"`
}
type ResultsEntry struct {
AccountID string `json:"vendor_account_identifier"`
AccountName string `json:"vendor_account_name"`
CloudProvider string `json:"vendor"`
Cost string `json:"unblended_cost"`
CostCenter string `json:"category4"`
PayerAccountId string `json:"account_identifier"`
UsageFamily string `json:"usage_family"`
}
type MetaSection struct {
Aggregates []AggregatesEntry `json:"aggregates"`
Dates struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
} `json:"dates"`
Dimensions []Measure `json:"dimensions"`
Filters []FiltersEntry `json:"filters"`
Metrics []Measure `json:"metrics"`
}
type FiltersEntry struct {
Comparator string `json:"comparator"`
Value string `json:"value"`
Measure `json:"measure"`
}
type AggregatesEntry struct {
Value string `json:"value"`
Element
}
type Measure struct {
Element
//Group struct {
// ID int `json:"ID"`
// Key string `json:"Key"`
// Name string `json:"Name"`
//} `json:"group"`
//SubGroup struct {
// ID int `json:"ID"`
// Key string `json:"Key"`
// Name string `json:"Name"`
//} `json:"sub_group"`
}
type Element struct {
Description string `json:"description"`
Label string `json:"label"`
Name string `json:"name"`
//DataType string `json:"data_type"`
//Type string `json:"type"`
}
func getCloudabilityData(configMap Configuration, options CommandLineOptions) *CloudabilityCostData {
uri := "/v3/reporting/cost/run"
cUrl, err := url.Parse(getMapKeyString(configMap, "api", "cloudability"))
if err != nil {
log.Fatalf("Error in Cloudability \"api_host\" value (%q): %v", configMap["api"], err)
}
now := time.Now()
var startString, endString string
if inTime, err := time.Parse("2006-01", *options.monthPtr); err == nil {
if inTime.After(now) {
log.Fatalf(
"Error: specified month, %q, is in the future.",
*options.monthPtr,
)
}
startString = inTime.Format("2006-01-02")
endTime := inTime.AddDate(0, 1, -1)
if endTime.After(now) {
log.Printf(
"Warning: specified month, %q, extends into the future.",
*options.monthPtr,
)
endTime = now
}
endString = endTime.Format("2006-01-02")
} else {
log.Fatalf("Error in Cloudability \"month\" value (%q): %v", *options.monthPtr, err)
}
costType := *options.costTypePtr
if costType == "UnblendedCost" {
costType = "unblended_cost"
}
qParams := cUrl.Query()
qParams.Set("start_date", startString)
qParams.Set("end_date", endString)
qParams.Set("dimensions", "vendor,category4,account_identifier,vendor_account_name,vendor_account_identifier,usage_family")
qParams.Set("metrics", costType)
filtersAny := getMapKeyValue(configMap, "filters", "")
if filters, ok := filtersAny.(map[any]any); ok {
for filterAny, expAny := range filters {
filter := getStringFromAny(filterAny, "Cloudability filter name")
if expAny == nil {
log.Fatalf("Missing value(s) for Cloudability filter %q", filter)
}
exp, ok := expAny.([]any)
if !ok {
log.Fatalf(
"Unexpected value (%v) for Cloudability filter values for filter %q, expected an array of strings",
expAny,
filter,
)
}
for _, valAny := range exp {
val := getStringFromAny(valAny, "Cloudability filter value")
qParams.Add("filters", filter+"=="+val)
}
}
} else if filtersAny != nil {
log.Fatalf("Error in Cloudability \"filters\" value (%q), type is %T, expected a mapping",
filtersAny, filtersAny)
}
//qParams.Add("filters", "unblended_cost>0")
qParams.Set("view_id", "0")
qParams.Set("limit", "0")
path, err := url.JoinPath(cUrl.Path, uri)
if err != nil {
log.Fatalf("Error composing Cloudability API path, joining %q to %q: %v", cUrl.Path, uri, err)
}
cUrl = &url.URL{
Scheme: "https",
Host: cUrl.Host,
Path: path,
RawQuery: qParams.Encode(),
}
client := http.Client{Timeout: time.Second * 180}
request, err := http.NewRequest("GET", cUrl.String(), http.NoBody)
if err != nil {
log.Fatalf("Error creating Cloudability request: %v", err)
}
if _, ok := configMap["api_key"]; ok {
apiKey := getMapKeyString(configMap, "api_key", "cloudability")
request.SetBasicAuth(apiKey, "")
} else {
request.Header.Add("apptio-opentoken", getApptioOpentoken(configMap, client))
environmentId := getMapKeyString(configMap, "environmentId", "cloudability")
request.Header.Add("apptio-environmentid", environmentId)
}
request.Header.Add("Accept", "application/json")
log.Println("[getCloudabilityData] Sending request for data")
response, err := client.Do(request)
if err != nil {
log.Fatalf("Error sending request to Cloudability: %v", err)
}
if response.StatusCode != http.StatusOK {
log.Fatalf("Error getting data from Cloudability: %d, %q", response.StatusCode, response.Status)
}
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
log.Fatalf("Ignoring error closing Cloudability body: %v", err)
}
}(response.Body)
responseBytes, err := io.ReadAll(response.Body)
if err != nil {
log.Fatalf("Error reading Cloudability response body: %v", err)
}
log.Println("[getCloudabilityData] Processing results")
responseData := new(CloudabilityCostData)
err = json.Unmarshal(responseBytes, responseData)
if err != nil {
log.Fatalf("Error unmarshalling the Cloudability response body: %v\n", err)
}
if responseData.Pagination.Next != "" {
log.Fatal("Cloudability result is unexpectedly paginated")
}
return responseData
}
func getApptioOpentoken(configMap Configuration, client http.Client) string {
apiKeyPairAny := getMapKeyValue(configMap, "api_key_pair", "cloudability")
apiKeyPair, ok := apiKeyPairAny.([]any)
if !ok {
log.Fatalf("Error reading Cloudability API keypair, expected an array, found %T",
apiKeyPairAny)
}
if len(apiKeyPair) != 2 {
log.Fatalf("Error reading Cloudability API keypair, expected 2 items, found %d",
len(apiKeyPair))
}
apiAccessKey, ok1 := apiKeyPair[0].(string)
apiSecret, ok2 := apiKeyPair[1].(string)
if !ok1 || !ok2 {
log.Fatalf(
"Error reading Cloudability API keypair, expected entries to be strings, found %T and %T",
apiKeyPair[0], apiKeyPair[1])
}
body := bytes.NewBufferString(`{"keyAccess":"` + apiAccessKey + `","keySecret":"` + apiSecret + `"}`)
authRequest, err := http.NewRequest("POST", "https://frontdoor.apptio.com/service/apikeylogin", body)
if err != nil {
log.Fatalf("Error creating Cloudability authorization request: %v", err)
}
authRequest.Header.Add("Accept", "application/json")
authRequest.Header.Add("content-type", "application/json")
log.Println("[getCloudabilityData] Sending request for authorization")
authResponse, err := client.Do(authRequest)
if err != nil {
log.Fatalf("Error sending authorization request to Cloudability: %v", err)
}
if authResponse.StatusCode != http.StatusOK {
log.Fatalf("Error getting authorization data from Cloudability: %d, %q",
authResponse.StatusCode, authResponse.Status)
}
defer func(Body io.ReadCloser) {
if err := Body.Close(); err != nil {
log.Fatalf("Ignoring error closing Cloudability body: %v", err)
}
}(authResponse.Body)
return authResponse.Header.Get("apptio-opentoken")
}
func getSheetDataFromCloudability(
cldy *CloudabilityCostData,
accountsMetadata map[string]*AccountMetadata,
configMap Configuration,
costCells map[string]map[string]float64,
columnHeadsSet map[string]struct{},
metadata map[string]providerAccountMetadata,
) {
// Build a two-dimensional map in which the first key is the account ID,
// the second key is the usage family, and the value is the corresponding
// cost -- this amounts to a sparse sheet grid. While we're at it, collect
// the column headers for the grid (using a map "trick" where we only care
// about the keys), and collect some metadata for each account.
ignored := make(map[string]struct{}) // Suppress multiple warnings
for _, entry := range cldy.Results {
// Skip accounts that we're not looking for, but keep a list of them so
// that we don't issue multiple warnings for them; warn about accounts
// attributed to our cost center that we're not currently tracking.
if skipAccountEntry(
accountsMetadata[entry.AccountID],
entry.AccountID,
entry.CostCenter,
entry.CloudProvider,
entry.AccountName,
ignored,
configMap,
"Cloudability",
) {
continue
}
// Note the current entry's usage family so that we can use it as a
// column header; and, if this is the first time we've seen this
// account, note its account-specific metadata.
columnHeadsSet[entry.UsageFamily] = struct{}{}
if _, exists := metadata[entry.AccountID]; !exists {
metadata[entry.AccountID] = providerAccountMetadata{
AccountName: entry.AccountName,
CloudProvider: entry.CloudProvider,
CostCenter: entry.CostCenter,
Date: cldy.Meta.Dates.Start.Format("2006-01"),
PayerAccountId: entry.PayerAccountId,
}
}
// Capture the cost data. If this is the first data for this account,
// create its "row". If the cell has already been written, exit with
// an error.
cost, err := strconv.ParseFloat(entry.Cost, 64)
if err != nil {
log.Fatalf("Error parsing %s:%s Cost value (%v) as a float: %v",
entry.AccountID, entry.UsageFamily, entry.Cost, err)
}
if _, exists := costCells[entry.AccountID]; !exists {
costCells[entry.AccountID] = make(map[string]float64)
}
if _, exists := costCells[entry.AccountID][entry.UsageFamily]; exists {
log.Fatalf(
"Duplicate entry for %s:%s, values %f and %f",
entry.AccountID,
entry.UsageFamily,
costCells[entry.AccountID][entry.UsageFamily],
cost)
}
costCells[entry.AccountID][entry.UsageFamily] = cost
}
}