Last active
June 8, 2023 11:06
-
-
Save ljtill/6e6b5108b686e14c6a545b1a452d5a32 to your computer and use it in GitHub Desktop.
Provides the ability to retrieve all Resource Groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"log" | |
"os" | |
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" | |
"github.com/Azure/go-autorest/autorest" | |
"github.com/Azure/go-autorest/autorest/azure" | |
"github.com/Azure/go-autorest/autorest/azure/auth" | |
) | |
const () | |
var ( | |
authorizer autorest.Authorizer | |
ctx = context.Background() | |
err error | |
subscriptionID = getSubscriptionID() | |
) | |
func main() { | |
for groups, err := getResourceGroups(subscriptionID); groups.NotDone(); err = groups.Next() { | |
if err != nil { | |
log.Fatalf("Error: %v", err) | |
} | |
groupName := *groups.Value().Name | |
log.Printf("Resource Group: %v", groupName) | |
} | |
} | |
func init() {} | |
func getResourceGroups(subscriptionID string) (resources.GroupListResultIterator, error) { | |
c := resources.NewGroupsClient(subscriptionID) | |
a, err := auth.NewAuthorizerFromFile(azure.PublicCloud.ResourceManagerEndpoint) | |
if err != nil { | |
log.Fatalf("Error: %v", err) | |
} | |
c.Authorizer = a | |
return c.ListComplete(ctx, "", nil) | |
} | |
func getSubscriptionID() string { | |
id := os.Getenv("AZURE_SUBSCRIPTION_ID") | |
if id == "" { | |
log.Fatalf("Error: environment variable AZURE_SUBSCRIPTION_ID is not set") | |
} | |
return id | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment