Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_subscription - Fix error for subscription update #21255

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions internal/services/subscription/subscription_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,11 @@ func resourceSubscriptionUpdate(d *pluginsdk.ResourceData, meta interface{}) err
locks.ByName(id.AliasName, SubscriptionResourceName)
defer locks.UnlockByName(id.AliasName, SubscriptionResourceName)
resp, err := aliasClient.AliasGet(ctx, *id)
if err != nil || resp.Model == nil || resp.Model.Properties == nil {
if err != nil || resp.Model == nil || resp.Model.Properties == nil || resp.Model.Properties.SubscriptionId == nil {
return fmt.Errorf("could not read Subscription Alias for update: %+v", err)
}

subscriptionId, err := commonids.ParseSubscriptionID(*resp.Model.Properties.SubscriptionId)
if err != nil {
return fmt.Errorf("could not parse Subscription ID from Alias: %+v", err)
}
subscriptionId := commonids.NewSubscriptionID(*resp.Model.Properties.SubscriptionId)

if d.HasChange("subscription_name") {
locks.ByID(subscriptionId.ID())
Expand All @@ -273,21 +270,20 @@ func resourceSubscriptionUpdate(d *pluginsdk.ResourceData, meta interface{}) err
displayName := subscriptionAlias.SubscriptionName{
SubscriptionName: utils.String(d.Get("subscription_name").(string)),
}
if _, err := aliasClient.SubscriptionRename(ctx, *subscriptionId, displayName); err != nil {
return fmt.Errorf("could not update Display Name of Subscription %q: %+v", *subscriptionId, err)
if _, err := aliasClient.SubscriptionRename(ctx, subscriptionId, displayName); err != nil {
return fmt.Errorf("could not update Display Name of Subscription %q: %+v", subscriptionId, err)
}
}

if d.HasChange("tags") {
tagsClient := meta.(*clients.Client).Resource.TagsClientForSubscription(subscriptionId.ID())
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 {
if _, err = tagsClient.CreateOrUpdateAtScope(ctx, subscriptionId.ID(), tagsResource); err != nil {
ms-zhenhua marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("setting tags on %s: %+v", *id, err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ resource "azurerm_subscription" "test" {
alias = "testAcc-%[3]d"
subscription_name = "testAccSubscription Renamed %[3]d"
billing_scope_id = data.azurerm_billing_enrollment_account_scope.test.id

tags = {
key = "value"
}
}
`, billingAccount, enrollmentAccount, data.RandomInteger)
}
Expand Down