github.com/mponton/terratest@v0.44.0/modules/azure/resourcegroup.go (about) 1 package azure 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources" 9 "github.com/stretchr/testify/require" 10 ) 11 12 // ResourceGroupExists indicates whether a resource group exists within a subscription; otherwise false 13 // This function would fail the test if there is an error. 14 func ResourceGroupExists(t *testing.T, resourceGroupName string, subscriptionID string) bool { 15 result, err := ResourceGroupExistsE(resourceGroupName, subscriptionID) 16 require.NoError(t, err) 17 return result 18 } 19 20 // ResourceGroupExistsE indicates whether a resource group exists within a subscription 21 func ResourceGroupExistsE(resourceGroupName, subscriptionID string) (bool, error) { 22 exists, err := GetResourceGroupE(resourceGroupName, subscriptionID) 23 if err != nil { 24 if ResourceNotFoundErrorExists(err) { 25 return false, nil 26 } 27 return false, err 28 } 29 return exists, nil 30 31 } 32 33 // GetResourceGroupE gets a resource group within a subscription 34 func GetResourceGroupE(resourceGroupName, subscriptionID string) (bool, error) { 35 36 rg, err := GetAResourceGroupE(resourceGroupName, subscriptionID) 37 if err != nil { 38 return false, err 39 } 40 return (resourceGroupName == *rg.Name), nil 41 } 42 43 // GetResourceGroupClientE gets a resource group client in a subscription 44 // TODO: remove in next version 45 func GetResourceGroupClientE(subscriptionID string) (*resources.GroupsClient, error) { 46 subscriptionID, err := getTargetAzureSubscription(subscriptionID) 47 if err != nil { 48 return nil, err 49 } 50 resourceGroupClient := resources.NewGroupsClient(subscriptionID) 51 authorizer, err := NewAuthorizer() 52 if err != nil { 53 return nil, err 54 } 55 resourceGroupClient.Authorizer = *authorizer 56 return &resourceGroupClient, nil 57 } 58 59 // GetAResourceGroup returns a resource group within a subscription 60 // This function would fail the test if there is an error. 61 func GetAResourceGroup(t *testing.T, resourceGroupName string, subscriptionID string) *resources.Group { 62 rg, err := GetAResourceGroupE(resourceGroupName, subscriptionID) 63 require.NoError(t, err) 64 return rg 65 } 66 67 // GetAResourceGroupE gets a resource group within a subscription 68 func GetAResourceGroupE(resourceGroupName, subscriptionID string) (*resources.Group, error) { 69 client, err := CreateResourceGroupClientE(subscriptionID) 70 if err != nil { 71 return nil, err 72 } 73 74 rg, err := client.Get(context.Background(), resourceGroupName) 75 if err != nil { 76 return nil, err 77 } 78 return &rg, nil 79 } 80 81 // ListResourceGroupsByTag returns a resource group list within a subscription based on a tag key 82 // This function would fail the test if there is an error. 83 func ListResourceGroupsByTag(t *testing.T, tag, subscriptionID string) []resources.Group { 84 rg, err := ListResourceGroupsByTagE(tag, subscriptionID) 85 require.NoError(t, err) 86 return rg 87 } 88 89 // ListResourceGroupsByTagE returns a resource group list within a subscription based on a tag key 90 func ListResourceGroupsByTagE(tag string, subscriptionID string) ([]resources.Group, error) { 91 client, err := CreateResourceGroupClientE(subscriptionID) 92 if err != nil { 93 return nil, err 94 } 95 96 rg, err := client.List(context.Background(), fmt.Sprintf("tagName eq '%s'", tag), nil) 97 if err != nil { 98 return nil, err 99 } 100 return rg.Values(), nil 101 }