diff --git a/internal/services/resource/client/client.go b/internal/services/resource/client/client.go index 884790aa02d0..7724c0e38eaf 100644 --- a/internal/services/resource/client/client.go +++ b/internal/services/resource/client/client.go @@ -17,7 +17,10 @@ type Client struct { ProvidersClient *providers.ProvidersClient ResourceProvidersClient *resources.ProvidersClient ResourcesClient *resources.Client + TagsClient *resources.TagsClient TemplateSpecsVersionsClient *templatespecs.VersionsClient + + options *common.ClientOptions } func NewClient(o *common.ClientOptions) *Client { @@ -47,6 +50,9 @@ func NewClient(o *common.ClientOptions) *Client { templatespecsVersionsClient := templatespecs.NewVersionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&templatespecsVersionsClient.Client, o.ResourceManagerAuthorizer) + tagsClient := resources.NewTagsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&tagsClient.Client, o.ResourceManagerAuthorizer) + return &Client{ GroupsClient: &groupsClient, DeploymentsClient: &deploymentsClient, @@ -55,6 +61,15 @@ func NewClient(o *common.ClientOptions) *Client { ProvidersClient: &providersClient, ResourceProvidersClient: &resourceProvidersClient, ResourcesClient: &resourcesClient, + TagsClient: &tagsClient, TemplateSpecsVersionsClient: &templatespecsVersionsClient, + + options: o, } } + +func (c Client) TagsClientForSubscription(subscriptionID string) *resources.TagsClient { + tagsClient := resources.NewTagsClientWithBaseURI(c.options.ResourceManagerEndpoint, subscriptionID) + c.options.ConfigureClient(&tagsClient.Client, c.options.ResourceManagerAuthorizer) + return &tagsClient +} diff --git a/internal/services/subscription/subscription_resource.go b/internal/services/subscription/subscription_resource.go index 8ce8a70e688d..3deeb37048e2 100644 --- a/internal/services/subscription/subscription_resource.go +++ b/internal/services/subscription/subscription_resource.go @@ -7,6 +7,7 @@ import ( "time" "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-11-01/subscriptions" + "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" subscriptionAlias "github.com/Azure/azure-sdk-for-go/services/subscription/mgmt/2020-09-01/subscription" "github.com/google/uuid" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" @@ -110,13 +111,7 @@ func resourceSubscription() *pluginsdk.Resource { Computed: true, }, - "tags": { - Type: pluginsdk.TypeMap, - Computed: true, - Elem: &pluginsdk.Schema{ - Type: pluginsdk.TypeString, - }, - }, + "tags": tags.Schema(), }, } } @@ -226,7 +221,21 @@ func resourceSubscriptionCreate(d *pluginsdk.ResourceData, meta interface{}) err createDeadline := time.Until(deadline) if err := waitForSubscriptionStateToSettle(ctx, meta.(*clients.Client), *alias.Properties.SubscriptionID, "Active", createDeadline); err != nil { - return fmt.Errorf("failed waiting for Subscription %q (Alias %q) to enter %q state: %+v", subscriptionId, id.Name, "Active", err) + return fmt.Errorf("failed waiting for Subscription %q (Alias %q) to enter %q state: %+v", *alias.Properties.SubscriptionID, id.Name, "Active", err) + } + + if d.HasChange("tags") { + tagsClient := meta.(*clients.Client).Resource.TagsClientForSubscription(*alias.Properties.SubscriptionID) + t := tags.Expand(d.Get("tags").(map[string]interface{})) + scope := fmt.Sprintf("subscriptions/%s", *alias.Properties.SubscriptionID) + tagsResource := resources.TagsResource{ + Properties: &resources.Tags{ + Tags: t, + }, + } + if _, err = tagsClient.CreateOrUpdateAtScope(ctx, scope, tagsResource); err != nil { + return fmt.Errorf("setting tags on %s: %+v", id, err) + } } d.SetId(id.ID()) @@ -269,6 +278,20 @@ func resourceSubscriptionUpdate(d *pluginsdk.ResourceData, meta interface{}) err } } + if d.HasChange("tags") { + tagsClient := meta.(*clients.Client).Resource.TagsClientForSubscription(*subscriptionId) + t := tags.Expand(d.Get("tags").(map[string]interface{})) + scope := fmt.Sprintf("subscriptions/%s", *subscriptionId) + tagsResource := resources.TagsResource{ + Properties: &resources.Tags{ + Tags: t, + }, + } + if _, err = tagsClient.CreateOrUpdateAtScope(ctx, scope, tagsResource); err != nil { + return fmt.Errorf("setting tags on %s: %+v", *id, err) + } + } + return nil } diff --git a/website/docs/r/subscription.html.markdown b/website/docs/r/subscription.html.markdown index 8df253793cc5..10e93340000f 100644 --- a/website/docs/r/subscription.html.markdown +++ b/website/docs/r/subscription.html.markdown @@ -89,6 +89,8 @@ The following arguments are supported: * `workload` - (Optional) The workload type of the Subscription. Possible values are `Production` (default) and `DevTest`. Changing this forces a new Subscription to be created. +* `tags` - (Optional) A mapping of tags to assign to the Subscription. + ## Attributes Reference In addition to the Arguments listed above - the following Attributes are exported: