/
util.go
362 lines (343 loc) 路 11.8 KB
/
util.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package azure
import (
"fmt"
"regexp"
"strings"
"github.com/rs/zerolog/log"
"github.com/shopspring/decimal"
"github.com/infracost/infracost/internal/schema"
)
func strPtr(s string) *string {
return &s
}
func decimalPtr(d decimal.Decimal) *decimal.Decimal {
return &d
}
func regexPtr(regex string) *string {
return strPtr(fmt.Sprintf("/%s/i", regex))
}
var sReg = regexp.MustCompile(`\s+`)
func toAzureCLIName(location string) string {
return strings.ToLower(sReg.ReplaceAllString(location, ""))
}
func lookupRegion(d *schema.ResourceData, parentResourceKeys []string) string {
// First check for a location set directly on a resource
location := d.Get("location").String()
if location != "" && !strings.Contains(location, "mock") {
return toAzureCLIName(location)
}
// Then check for any parent resources with a location
for _, k := range parentResourceKeys {
parents := d.References(k)
for _, p := range parents {
location := p.Get("location").String()
if location != "" && !strings.Contains(location, "mock") {
return toAzureCLIName(location)
}
}
}
// When all else fails use the default region
defaultRegion := toAzureCLIName(d.Get("region").String())
log.Warn().Msgf("Using %s for resource %s as its 'location' property could not be found.", defaultRegion, d.Address)
return defaultRegion
}
func convertRegion(region string) string {
if strings.Contains(strings.ToLower(region), "usgov") {
return "US Gov"
} else if strings.Contains(strings.ToLower(region), "china") {
return "小hina"
} else {
return "Global"
}
}
// locationNameMapping returns a display name for a given location name.
// Up-to-date mapping can be found by running the following command:
//
// az account list-locations -o json | jq '.[] | .name + " " + .displayName'
func locationNameMapping(l string) string {
name := map[string]string{
"eastus": "East US",
"eastus2": "East US 2",
"southcentralus": "South Central US",
"westus2": "West US 2",
"westus3": "West US 3",
"australiaeast": "Australia East",
"southeastasia": "Southeast Asia",
"northeurope": "North Europe",
"swedencentral": "Sweden Central",
"uksouth": "UK South",
"westeurope": "West Europe",
"centralus": "Central US",
"southafricanorth": "South Africa North",
"centralindia": "Central India",
"eastasia": "East Asia",
"japaneast": "Japan East",
"koreacentral": "Korea Central",
"canadacentral": "Canada Central",
"francecentral": "France Central",
"germanywestcentral": "Germany West Central",
"italynorth": "Italy North",
"norwayeast": "Norway East",
"polandcentral": "Poland Central",
"switzerlandnorth": "Switzerland North",
"uaenorth": "UAE North",
"brazilsouth": "Brazil South",
"centraluseuap": "Central US EUAP",
"israelcentral": "Israel Central",
"qatarcentral": "Qatar Central",
"centralusstage": "Central US (Stage)",
"eastusstage": "East US (Stage)",
"eastus2stage": "East US 2 (Stage)",
"northcentralusstage": "North Central US (Stage)",
"southcentralusstage": "South Central US (Stage)",
"westusstage": "West US (Stage)",
"westus2stage": "West US 2 (Stage)",
"asia": "Asia",
"asiapacific": "Asia Pacific",
"australia": "Australia",
"brazil": "Brazil",
"canada": "Canada",
"europe": "Europe",
"france": "France",
"germany": "Germany",
"global": "Global",
"india": "India",
"japan": "Japan",
"korea": "Korea",
"norway": "Norway",
"singapore": "Singapore",
"southafrica": "South Africa",
"sweden": "Sweden",
"switzerland": "Switzerland",
"uae": "United Arab Emirates",
"uk": "United Kingdom",
"unitedstates": "United States",
"unitedstateseuap": "United States EUAP",
"eastasiastage": "East Asia (Stage)",
"southeastasiastage": "Southeast Asia (Stage)",
"brazilus": "Brazil US",
"eastusstg": "East US STG",
"northcentralus": "North Central US",
"westus": "West US",
"japanwest": "Japan West",
"jioindiawest": "Jio India West",
"eastus2euap": "East US 2 EUAP",
"westcentralus": "West Central US",
"southafricawest": "South Africa West",
"australiacentral": "Australia Central",
"australiacentral2": "Australia Central 2",
"australiasoutheast": "Australia Southeast",
"jioindiacentral": "Jio India Central",
"koreasouth": "Korea South",
"southindia": "South India",
"westindia": "West India",
"canadaeast": "Canada East",
"francesouth": "France South",
"germanynorth": "Germany North",
"norwaywest": "Norway West",
"switzerlandwest": "Switzerland West",
"ukwest": "UK West",
"uaecentral": "UAE Central",
"brazilsoutheast": "Brazil Southeast",
"usgovvirginia": "US Gov Virginia",
"usgovarizona": "US Gov Arizona",
"usgovtexas": "US Gov Texas",
}[l]
if name == "" {
return l
}
return name
}
// regionToVNETZone returns the VNET zone for a given region.
//
// Mapped based on the values here: https://azure.microsoft.com/en-us/pricing/details/virtual-network/#faq
func regionToVNETZone(region string) string {
return map[string]string{
"eastus": "Zone 1",
"eastus2": "Zone 1",
"southcentralus": "Zone 1",
"westus2": "Zone 1",
"westus3": "Zone 1",
"australiaeast": "Zone 2",
"southeastasia": "Zone 2",
"northeurope": "Zone 1",
"swedencentral": "Zone 1",
"uksouth": "Zone 1",
"westeurope": "Zone 1",
"centralus": "Zone 1",
"southafricanorth": "Zone 3",
"centralindia": "Zone 2",
"eastasia": "Zone 2",
"japaneast": "Zone 2",
"koreacentral": "Zone 2",
"canadacentral": "Zone 1",
"francecentral": "Zone 1",
"germanywestcentral": "Zone 1",
"italynorth": "Zone 1",
"norwayeast": "Zone 1",
"polandcentral": "Zone 1",
"switzerlandnorth": "Zone 1",
"uaenorth": "Zone 3",
"brazilsouth": "Zone 3",
"centraluseuap": "Zone 1",
"israelcentral": "Zone 1",
"qatarcentral": "Zone 1",
"centralusstage": "Zone 1",
"eastusstage": "Zone 1",
"eastus2stage": "Zone 1",
"northcentralusstage": "Zone 1",
"southcentralusstage": "Zone 1",
"westusstage": "Zone 1",
"westus2stage": "Zone 1",
"asia": "Zone 1",
"asiapacific": "Zone 1",
"australia": "Zone 1",
"brazil": "Zone 3",
"canada": "Zone 1",
"europe": "Zone 1",
"france": "Zone 1",
"germany": "Zone 1",
"india": "Zone 2",
"japan": "Zone 2",
"korea": "Zone 2",
"norway": "Zone 1",
"singapore": "Zone 1",
"southafrica": "Zone 3",
"sweden": "Zone 1",
"switzerland": "Zone 1",
"uae": "Zone 3",
"uk": "Zone 1",
"unitedstates": "Zone 1",
"unitedstateseuap": "Zone 1",
"eastasiastage": "Zone 2",
"southeastasiastage": "Zone 2",
"brazilus": "Zone 1",
"eastusstg": "Zone 1",
"northcentralus": "Zone 1",
"westus": "Zone 1",
"japanwest": "Zone 2",
"jioindiawest": "Zone 1",
"eastus2euap": "Zone 1",
"westcentralus": "Zone 1",
"southafricawest": "Zone 3",
"australiacentral": "Zone 1",
"australiacentral2": "Zone 1",
"australiasoutheast": "Zone 2",
"jioindiacentral": "Zone 2",
"koreasouth": "Zone 2",
"southindia": "Zone 2",
"westindia": "Zone 2",
"canadaeast": "Zone 1",
"francesouth": "Zone 1",
"germanynorth": "Zone 1",
"norwaywest": "Zone 1",
"switzerlandwest": "Zone 1",
"ukwest": "Zone 1",
"uaecentral": "Zone 3",
"brazilsoutheast": "Zone 3",
"usgovvirginia": "US Gov Zone 1",
"usgovarizona": "US Gov Zone 1",
"usgovtexas": "US Gov Zone 1",
}[region]
}
// https://learn.microsoft.com/en-us/azure/cdn/cdn-billing#what-is-a-billing-region
func regionToCDNZone(region string) string {
return map[string]string{
"eastus": "Zone 1",
"eastus2": "Zone 1",
"southcentralus": "Zone 1",
"westus2": "Zone 1",
"westus3": "Zone 1",
"australiaeast": "Zone 4",
"southeastasia": "Zone 2",
"northeurope": "Zone 1",
"swedencentral": "Zone 1",
"uksouth": "Zone 1",
"westeurope": "Zone 1",
"centralus": "Zone 1",
"southafricanorth": "Zone 1",
"centralindia": "Zone 5",
"eastasia": "Zone 2",
"japaneast": "Zone 2",
"koreacentral": "Zone 2",
"canadacentral": "Zone 1",
"francecentral": "Zone 1",
"germanywestcentral": "Zone 1",
"italynorth": "Zone 1",
"norwayeast": "Zone 1",
"polandcentral": "Zone 1",
"switzerlandnorth": "Zone 1",
"uaenorth": "Zone 1",
"brazilsouth": "Zone 3",
"centraluseuap": "Zone 1",
"israelcentral": "Zone 1",
"qatarcentral": "Zone 1",
"centralusstage": "Zone 1",
"eastusstage": "Zone 1",
"eastus2stage": "Zone 1",
"northcentralusstage": "Zone 1",
"southcentralusstage": "Zone 1",
"westusstage": "Zone 1",
"westus2stage": "Zone 1",
"asia": "Zone 2",
"asiapacific": "Zone 2",
"australia": "Zone 4",
"brazil": "Zone 3",
"canada": "Zone 1",
"europe": "Zone 1",
"france": "Zone 1",
"germany": "Zone 1",
"india": "Zone 5",
"japan": "Zone 2",
"korea": "Zone 2",
"norway": "Zone 1",
"singapore": "Zone 2",
"southafrica": "Zone 1",
"sweden": "Zone 1",
"switzerland": "Zone 1",
"uae": "Zone 1",
"uk": "Zone 1",
"unitedstates": "Zone 1",
"unitedstateseuap": "Zone 1",
"eastasiastage": "Zone 2",
"southeastasiastage": "Zone 2",
"brazilus": "Zone 3",
"eastusstg": "Zone 1",
"northcentralus": "Zone 1",
"westus": "Zone 1",
"japanwest": "Zone 2",
"jioindiawest": "Zone 5",
"eastus2euap": "Zone 1",
"westcentralus": "Zone 1",
"southafricawest": "Zone 1",
"australiacentral": "Zone 4",
"australiacentral2": "Zone 4",
"australiasoutheast": "Zone 4",
"jioindiacentral": "Zone 5",
"koreasouth": "Zone 2",
"southindia": "Zone 5",
"westindia": "Zone 5",
"canadaeast": "Zone 1",
"francesouth": "Zone 1",
"germanynorth": "Zone 1",
"norwaywest": "Zone 1",
"switzerlandwest": "Zone 1",
"ukwest": "Zone 1",
"uaecentral": "Zone 1",
"brazilsoutheast": "Zone 3",
"usgovvirginia": "US Gov Zone 1",
"usgovarizona": "US Gov Zone 1",
"usgovtexas": "US Gov Zone 1",
}[region]
}
func intPtr(i int64) *int64 {
return &i
}
func contains(arr []string, e string) bool {
for _, a := range arr {
if a == e {
return true
}
}
return false
}