github.com/mponton/terratest@v0.44.0/modules/azure/region.go (about)

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