|
| 1 | +package azure |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources" |
| 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 ResourceGroupExistsV2(t *testing.T, resourceGroupName string, subscriptionID string) bool { |
| 15 | + result, err := ResourceGroupExistsV2E(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 ResourceGroupExistsV2E(resourceGroupName, subscriptionID string) (bool, error) { |
| 22 | + exists, err := GetResourceGroupV2E(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 GetResourceGroupV2E(resourceGroupName, subscriptionID string) (bool, error) { |
| 35 | + rg, err := GetAResourceGroupV2E(resourceGroupName, subscriptionID) |
| 36 | + if err != nil { |
| 37 | + return false, err |
| 38 | + } |
| 39 | + return (resourceGroupName == *rg.Name), nil |
| 40 | +} |
| 41 | + |
| 42 | +// GetAResourceGroup returns a resource group within a subscription |
| 43 | +// This function would fail the test if there is an error. |
| 44 | +func GetAResourceGroupV2(t *testing.T, resourceGroupName string, subscriptionID string) *armresources.ResourceGroup { |
| 45 | + rg, err := GetAResourceGroupV2E(resourceGroupName, subscriptionID) |
| 46 | + require.NoError(t, err) |
| 47 | + return rg |
| 48 | +} |
| 49 | + |
| 50 | +// GetAResourceGroupE gets a resource group within a subscription |
| 51 | +func GetAResourceGroupV2E(resourceGroupName, subscriptionID string) (*armresources.ResourceGroup, error) { |
| 52 | + client, err := CreateResourceGroupClientV2E(subscriptionID) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + rg, err := client.Get(context.Background(), resourceGroupName, &armresources.ResourceGroupsClientGetOptions{}) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + return &rg.ResourceGroup, nil |
| 62 | +} |
| 63 | + |
| 64 | +// ListResourceGroupsByTag returns a resource group list within a subscription based on a tag key |
| 65 | +// This function would fail the test if there is an error. |
| 66 | +func ListResourceGroupsByTagV2(t *testing.T, tag, subscriptionID string) []*armresources.ResourceGroup { |
| 67 | + rg, err := ListResourceGroupsByTagV2E(tag, subscriptionID) |
| 68 | + require.NoError(t, err) |
| 69 | + return rg |
| 70 | +} |
| 71 | + |
| 72 | +// ListResourceGroupsByTagE returns a resource group list within a subscription based on a tag key |
| 73 | +func ListResourceGroupsByTagV2E(tag string, subscriptionID string) (rg []*armresources.ResourceGroup, err error) { |
| 74 | + client, err := CreateResourceGroupClientV2E(subscriptionID) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + filter := fmt.Sprintf("tagName eq '%s'", tag) |
| 80 | + pager := client.NewListPager(&armresources.ResourceGroupsClientListOptions{ |
| 81 | + Filter: &filter, |
| 82 | + }) |
| 83 | + ctx := context.Background() |
| 84 | + for pager.More() { |
| 85 | + page, err := pager.NextPage(ctx) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + rg = append(rg, page.ResourceGroupListResult.Value...) |
| 90 | + } |
| 91 | + |
| 92 | + return |
| 93 | +} |
0 commit comments