Skip to content

Commit

Permalink
azurerm_subscription - Fix error for subscription update (#21255)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-zhenhua authored Apr 13, 2023
1 parent bbe4878 commit fb8e284
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
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 {
return fmt.Errorf("setting tags on %s: %+v", *id, err)
}
}
Expand Down
4 changes: 4 additions & 0 deletions internal/services/subscription/subscription_resource_test.go
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

0 comments on commit fb8e284

Please sign in to comment.