Skip to content

Commit

Permalink
add functionality to get a random region for a specific service (#1269)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoonMoon1919 authored Apr 11, 2023
1 parent 4e55865 commit e0c790c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
51 changes: 51 additions & 0 deletions modules/aws/region.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package aws

import (
"fmt"
"os"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/gruntwork-io/terratest/modules/collections"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/random"
Expand Down Expand Up @@ -156,3 +158,52 @@ func GetAvailabilityZonesE(t testing.TestingT, region string) ([]string, error)

return out, nil
}

// GetRegionsForService gets all AWS regions in which a service is available.
func GetRegionsForService(t testing.TestingT, serviceName string) []string {
out, err := GetRegionsForServiceE(t, serviceName)
if err != nil {
t.Fatal(err)
}
return out
}

// GetRegionsForService gets all AWS regions in which a service is available and returns errors.
// See https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters-global-infrastructure.html
func GetRegionsForServiceE(t testing.TestingT, serviceName string) ([]string, error) {
// These values are available in any region, defaulting to us-east-1 since it's the oldest
ssmClient, err := NewSsmClientE(t, "us-east-1")

if err != nil {
return nil, err
}

paramPath := "/aws/service/global-infrastructure/services/%s/regions"
req, resp := ssmClient.GetParametersByPathRequest(&ssm.GetParametersByPathInput{
Path: aws.String(fmt.Sprintf(paramPath, serviceName)),
})

ssmErr := req.Send()
if ssmErr != nil {
return nil, err
}

var availableRegions []string
for _, p := range resp.Parameters {
availableRegions = append(availableRegions, *p.Value)
}

return availableRegions, nil
}

// GetRandomRegionForService retrieves a list of AWS regions in which a service is available
// Then returns one region randomly from the list
func GetRandomRegionForService(t testing.TestingT, serviceName string) string {
availableRegions, err := GetRegionsForServiceE(t, serviceName)

if err != nil {
t.Fatal(err)
}

return GetRandomRegion(t, availableRegions, nil)
}
11 changes: 11 additions & 0 deletions modules/aws/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ func TestGetAvailabilityZones(t *testing.T) {
assert.Regexp(t, fmt.Sprintf("^%s[a-z]$", randomRegion), az)
}
}

func TestGetRandomRegionForService(t *testing.T) {
t.Parallel()

serviceName := "apigatewayv2"

regionsForService, _ := GetRegionsForServiceE(t, serviceName)
randomRegionForService := GetRandomRegionForService(t, serviceName)

assert.Contains(t, regionsForService, randomRegionForService)
}

0 comments on commit e0c790c

Please sign in to comment.