|
1 | 1 | package aws |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 |
|
6 | 7 | "github.com/aws/aws-sdk-go/aws" |
7 | 8 | "github.com/aws/aws-sdk-go/service/ec2" |
| 9 | + "github.com/aws/aws-sdk-go/service/ssm" |
8 | 10 | "github.com/gruntwork-io/terratest/modules/collections" |
9 | 11 | "github.com/gruntwork-io/terratest/modules/logger" |
10 | 12 | "github.com/gruntwork-io/terratest/modules/random" |
@@ -156,3 +158,52 @@ func GetAvailabilityZonesE(t testing.TestingT, region string) ([]string, error) |
156 | 158 |
|
157 | 159 | return out, nil |
158 | 160 | } |
| 161 | + |
| 162 | +// GetRegionsForService gets all AWS regions in which a service is available. |
| 163 | +func GetRegionsForService(t testing.TestingT, serviceName string) []string { |
| 164 | + out, err := GetRegionsForServiceE(t, serviceName) |
| 165 | + if err != nil { |
| 166 | + t.Fatal(err) |
| 167 | + } |
| 168 | + return out |
| 169 | +} |
| 170 | + |
| 171 | +// GetRegionsForService gets all AWS regions in which a service is available and returns errors. |
| 172 | +// See https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters-global-infrastructure.html |
| 173 | +func GetRegionsForServiceE(t testing.TestingT, serviceName string) ([]string, error) { |
| 174 | + // These values are available in any region, defaulting to us-east-1 since it's the oldest |
| 175 | + ssmClient, err := NewSsmClientE(t, "us-east-1") |
| 176 | + |
| 177 | + if err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + |
| 181 | + paramPath := "/aws/service/global-infrastructure/services/%s/regions" |
| 182 | + req, resp := ssmClient.GetParametersByPathRequest(&ssm.GetParametersByPathInput{ |
| 183 | + Path: aws.String(fmt.Sprintf(paramPath, serviceName)), |
| 184 | + }) |
| 185 | + |
| 186 | + ssmErr := req.Send() |
| 187 | + if ssmErr != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + |
| 191 | + var availableRegions []string |
| 192 | + for _, p := range resp.Parameters { |
| 193 | + availableRegions = append(availableRegions, *p.Value) |
| 194 | + } |
| 195 | + |
| 196 | + return availableRegions, nil |
| 197 | +} |
| 198 | + |
| 199 | +// GetRandomRegionForService retrieves a list of AWS regions in which a service is available |
| 200 | +// Then returns one region randomly from the list |
| 201 | +func GetRandomRegionForService(t testing.TestingT, serviceName string) string { |
| 202 | + availableRegions, err := GetRegionsForServiceE(t, serviceName) |
| 203 | + |
| 204 | + if err != nil { |
| 205 | + t.Fatal(err) |
| 206 | + } |
| 207 | + |
| 208 | + return GetRandomRegion(t, availableRegions, nil) |
| 209 | +} |
0 commit comments