forked from gruntwork-io/terratest
/
region.go
168 lines (145 loc) · 4.97 KB
/
region.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
package azure
import (
"context"
"testing"
"github.com/gruntwork-io/terratest/modules/collections"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
)
// Reference for region list: https://azure.microsoft.com/en-us/global-infrastructure/locations/
var stableRegions = []string{
// Americas
"centralus",
"eastus",
"eastus2",
"northcentralus",
"southcentralus",
"westcentralus",
"westus",
"westus2",
"canadacentral",
"canadaeast",
"brazilsouth",
// Europe
"northeurope",
"westeurope",
"francecentral",
"francesouth",
"uksouth",
"ukwest",
// "germanycentral", // Shows as active on Azure website, but not from API
// "germanynortheast", // Shows as active on Azure website, but not from API
// Asia Pacific
"eastasia",
"southeastasia",
"australiacentral",
"australiacentral2",
"australiaeast",
"australiasoutheast",
"chinaeast",
"chinaeast2",
"chinanorth",
"chinanorth2",
"centralindia",
"southindia",
"westindia",
"japaneast",
"japanwest",
"koreacentral",
"koreasouth",
// Middle East and Africa
"southafricanorth",
"southfricawest",
"uaecentral",
"uaenorth",
}
// GetStableRandomRegion gets a randomly chosen Azure region that is considered stable. Like GetRandomRegion, you can
// further restrict the stable region list using approvedRegions and forbiddenRegions. We consider stable regions to be
// those that have been around for at least 1 year.
// Note that regions in the approvedRegions list that are not considered stable are ignored.
func GetRandomStableRegion(t *testing.T, approvedRegions []string, forbiddenRegions []string, subscriptionID string) string {
regionsToPickFrom := stableRegions
if len(approvedRegions) > 0 {
regionsToPickFrom = collections.ListIntersection(regionsToPickFrom, approvedRegions)
}
if len(forbiddenRegions) > 0 {
regionsToPickFrom = collections.ListSubtract(regionsToPickFrom, forbiddenRegions)
}
return GetRandomRegion(t, regionsToPickFrom, nil, subscriptionID)
}
// GetRandomRegion gets a randomly chosen Azure region. If approvedRegions is not empty, this will be a region from the approvedRegions
// list; otherwise, this method will fetch the latest list of regions from the Azure APIs and pick one of those. If
// forbiddenRegions is not empty, this method will make sure the returned region is not in the forbiddenRegions list.
func GetRandomRegion(t *testing.T, approvedRegions []string, forbiddenRegions []string, subscriptionID string) string {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
t.Fatal(err)
}
region, err := GetRandomRegionE(t, approvedRegions, forbiddenRegions, subscriptionID)
if err != nil {
t.Fatal(err)
}
return region
}
// GetRandomRegionE gets a randomly chosen Azure region. If approvedRegions is not empty, this will be a region from the approvedRegions
// list; otherwise, this method will fetch the latest list of regions from the Azure APIs and pick one of those. If
// forbiddenRegions is not empty, this method will make sure the returned region is not in the forbiddenRegions list.
func GetRandomRegionE(t *testing.T, approvedRegions []string, forbiddenRegions []string, subscriptionID string) (string, error) {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return "", err
}
regionsToPickFrom := approvedRegions
if len(regionsToPickFrom) == 0 {
allRegions, err := GetAllAzureRegionsE(t, subscriptionID)
if err != nil {
return "", err
}
regionsToPickFrom = allRegions
}
regionsToPickFrom = collections.ListSubtract(regionsToPickFrom, forbiddenRegions)
region := random.RandomString(regionsToPickFrom)
logger.Logf(t, "Using region %s", region)
return region, nil
}
// GetAllAzureRegions gets the list of Azure regions available in this subscription.
func GetAllAzureRegions(t *testing.T, subscriptionID string) []string {
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
t.Fatal(err)
}
// Get list of Azure locations
out, err := GetAllAzureRegionsE(t, subscriptionID)
if err != nil {
t.Fatal(err)
}
return out
}
// GetAllAzureRegionsE gets the list of Azure regions available in this subscription.
func GetAllAzureRegionsE(t *testing.T, subscriptionID string) ([]string, error) {
logger.Log(t, "Looking up all Azure regions available in this account")
// Validate Azure subscription ID
subscriptionID, err := getTargetAzureSubscription(subscriptionID)
if err != nil {
return nil, err
}
// Setup Subscription client
subscriptionClient, err := GetSubscriptionClient()
if err != nil {
return nil, err
}
// Get list of Azure locations
out, err := subscriptionClient.ListLocations(context.Background(), subscriptionID)
if err != nil {
return nil, err
}
// Populate a return slice
regions := []string{}
for _, region := range *out.Value {
regions = append(regions, *region.Name)
}
return regions, nil
}