./resource-manager - the Resource Manager Services supported by this SDK.
./sdk - the base layer that's used in this repository.
In addition, a README.md file can be found within each Service / API Version in the ./resource-manager directory highlighting how that SDK can be used (example).
SDK Clients require authentication which is available from the auth package, see the README in the ./sdk/auth directory for information on how to authenticate and obtain an Authorizer.
Once you've got an Authorizer, you can use an SDK Client like so:
package main
import (
"context"
"log"
"time"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/resources/2022-09-01/resourcegroups"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
)
func main() {
ctx, cancel := context.WithTimeout(context.TODO(), 5 * time.Minute)
defer cancel()
id := commonids.NewResourceGroupID("12345678-1234-9876-4563-123456789012", "example-resource-group")
environment := environments.AzurePublic()
credentials := auth.Credentials{
Environment: *environment,
EnableAuthenticatingUsingAzureCLI: true,
// you can also authenticate using a Service Principal, via MSI, OIDC etc by turning on the flag
}
log.Printf("[DEBUG] Obtaining Credentials..")
authorizer, err := auth.NewAuthorizerFromCredentials(ctx, credentials, environment.ResourceManager)
if err != nil {
log.Fatalf("building authorizer from credentials: %+v", err)
}
log.Printf("[DEBUG] Building Resource Groups Client..")
client, err := resourcegroups.NewResourceGroupsClientWithBaseURI(environment.ResourceManager)
if err != nil {
log.Fatalf("building ResourceGroups client: %+v", err)
}
client.Client.Authorizer = authorizer
// NOTE: the Resource ID type can be used for logging too, it contains a human-friendly description
log.Printf("[DEBUG] Creating %s..", id)
payload := resourcegroups.ResourceGroup{
// ...
}
if _, err := client.CreateOrUpdate(ctx, id, payload); err != nil {
log.Fatalf("creating %s: %+v", id, err)
}
log.Printf("[DEBUG] %s created..", id)
}