Skip to content

Commit

Permalink
azurerm_subscription - add support for setting and updating tags (#…
Browse files Browse the repository at this point in the history
…14445)

Supersedes #12287

Thanks to @crkg for the initial work.
  • Loading branch information
jackofallops authored Dec 2, 2021
1 parent 429ecda commit 661037e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
15 changes: 15 additions & 0 deletions internal/services/resource/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
39 changes: 31 additions & 8 deletions internal/services/subscription/subscription_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
},
}
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/subscription.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 661037e

Please sign in to comment.