github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/azure/region.go (about)

     1  package azure
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/gruntwork-io/terratest/modules/collections"
     7  	"github.com/gruntwork-io/terratest/modules/logger"
     8  	"github.com/gruntwork-io/terratest/modules/random"
     9  	"github.com/gruntwork-io/terratest/modules/testing"
    10  )
    11  
    12  // Reference for region list: https://azure.microsoft.com/en-us/global-infrastructure/locations/
    13  var stableRegions = []string{
    14  	// Americas
    15  	"centralus",
    16  	"eastus",
    17  	"eastus2",
    18  	"northcentralus",
    19  	"southcentralus",
    20  	"westcentralus",
    21  	"westus",
    22  	"westus2",
    23  	"canadacentral",
    24  	"canadaeast",
    25  	"brazilsouth",
    26  
    27  	// Europe
    28  	"northeurope",
    29  	"westeurope",
    30  	"francecentral",
    31  	"francesouth",
    32  	"uksouth",
    33  	"ukwest",
    34  	// "germanycentral", // Shows as active on Azure website, but not from API
    35  	// "germanynortheast", // Shows as active on Azure website, but not from API
    36  
    37  	// Asia Pacific
    38  	"eastasia",
    39  	"southeastasia",
    40  	"australiacentral",
    41  	"australiacentral2",
    42  	"australiaeast",
    43  	"australiasoutheast",
    44  	"chinaeast",
    45  	"chinaeast2",
    46  	"chinanorth",
    47  	"chinanorth2",
    48  	"centralindia",
    49  	"southindia",
    50  	"westindia",
    51  	"japaneast",
    52  	"japanwest",
    53  	"koreacentral",
    54  	"koreasouth",
    55  
    56  	// Middle East and Africa
    57  	"southafricanorth",
    58  	"southafricawest",
    59  	"uaecentral",
    60  	"uaenorth",
    61  }
    62  
    63  // GetRandomStableRegion gets a randomly chosen Azure region that is considered stable. Like GetRandomRegion, you can
    64  // further restrict the stable region list using approvedRegions and forbiddenRegions. We consider stable regions to be
    65  // those that have been around for at least 1 year.
    66  // Note that regions in the approvedRegions list that are not considered stable are ignored.
    67  func GetRandomStableRegion(t testing.TestingT, approvedRegions []string, forbiddenRegions []string, subscriptionID string) string {
    68  	regionsToPickFrom := stableRegions
    69  	if len(approvedRegions) > 0 {
    70  		regionsToPickFrom = collections.ListIntersection(regionsToPickFrom, approvedRegions)
    71  	}
    72  	if len(forbiddenRegions) > 0 {
    73  		regionsToPickFrom = collections.ListSubtract(regionsToPickFrom, forbiddenRegions)
    74  	}
    75  	return GetRandomRegion(t, regionsToPickFrom, nil, subscriptionID)
    76  }
    77  
    78  // GetRandomRegion gets a randomly chosen Azure region. If approvedRegions is not empty, this will be a region from the approvedRegions
    79  // list; otherwise, this method will fetch the latest list of regions from the Azure APIs and pick one of those. If
    80  // forbiddenRegions is not empty, this method will make sure the returned region is not in the forbiddenRegions list.
    81  func GetRandomRegion(t testing.TestingT, approvedRegions []string, forbiddenRegions []string, subscriptionID string) string {
    82  	// Validate Azure subscription ID
    83  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	region, err := GetRandomRegionE(t, approvedRegions, forbiddenRegions, subscriptionID)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	return region
    93  }
    94  
    95  // GetRandomRegionE gets a randomly chosen Azure region. If approvedRegions is not empty, this will be a region from the approvedRegions
    96  // list; otherwise, this method will fetch the latest list of regions from the Azure APIs and pick one of those. If
    97  // forbiddenRegions is not empty, this method will make sure the returned region is not in the forbiddenRegions list
    98  func GetRandomRegionE(t testing.TestingT, approvedRegions []string, forbiddenRegions []string, subscriptionID string) (string, error) {
    99  	// Validate Azure subscription ID
   100  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   101  	if err != nil {
   102  		return "", err
   103  	}
   104  
   105  	regionsToPickFrom := approvedRegions
   106  
   107  	if len(regionsToPickFrom) == 0 {
   108  		allRegions, err := GetAllAzureRegionsE(t, subscriptionID)
   109  		if err != nil {
   110  			return "", err
   111  		}
   112  		regionsToPickFrom = allRegions
   113  	}
   114  
   115  	regionsToPickFrom = collections.ListSubtract(regionsToPickFrom, forbiddenRegions)
   116  	region := random.RandomString(regionsToPickFrom)
   117  
   118  	logger.Logf(t, "Using region %s", region)
   119  	return region, nil
   120  }
   121  
   122  // GetAllAzureRegions gets the list of Azure regions available in this subscription.
   123  func GetAllAzureRegions(t testing.TestingT, subscriptionID string) []string {
   124  	// Validate Azure subscription ID
   125  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	// Get list of Azure locations
   131  	out, err := GetAllAzureRegionsE(t, subscriptionID)
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  
   136  	return out
   137  }
   138  
   139  // GetAllAzureRegionsE gets the list of Azure regions available in this subscription
   140  func GetAllAzureRegionsE(t testing.TestingT, subscriptionID string) ([]string, error) {
   141  	logger.Log(t, "Looking up all Azure regions available in this account")
   142  
   143  	// Validate Azure subscription ID
   144  	subscriptionID, err := getTargetAzureSubscription(subscriptionID)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	// Setup Subscription client
   150  	subscriptionClient, err := GetSubscriptionClientE()
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	// Get list of Azure locations
   156  	out, err := subscriptionClient.ListLocations(context.Background(), subscriptionID)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	// Populate a return slice
   162  	regions := []string{}
   163  	for _, region := range *out.Value {
   164  		regions = append(regions, *region.Name)
   165  	}
   166  
   167  	return regions, nil
   168  }