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

r/subscription: consolidating the SDK clients onto hashicorp/go-azure-sdk #24003

Merged
merged 2 commits into from
Dec 7, 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: 4 additions & 10 deletions internal/services/subscription/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2021-01-01/subscriptions" // nolint: staticcheck
subscriptionAlias "github.com/Azure/azure-sdk-for-go/services/subscription/mgmt/2020-09-01/subscription" // nolint: staticcheck
subscriptionAliasPandora "github.com/hashicorp/go-azure-sdk/resource-manager/subscription/2021-10-01/subscriptions" // nolint: staticcheck
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
Client *subscriptions.Client
AliasClient *subscriptionAliasPandora.SubscriptionsClient
SubscriptionClient *subscriptionAlias.Client
Client *subscriptions.Client
AliasClient *subscriptionAliasPandora.SubscriptionsClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -23,12 +21,8 @@ func NewClient(o *common.ClientOptions) *Client {
aliasClient := subscriptionAliasPandora.NewSubscriptionsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&aliasClient.Client, o.ResourceManagerAuthorizer)

subscriptionClient := subscriptionAlias.NewClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&subscriptionClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AliasClient: &aliasClient,
Client: &client,
SubscriptionClient: &subscriptionClient,
AliasClient: &aliasClient,
Client: &client,
}
}
19 changes: 9 additions & 10 deletions internal/services/subscription/subscription_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ func resourceSubscription() *pluginsdk.Resource {

func resourceSubscriptionCreate(d *pluginsdk.ResourceData, meta interface{}) error {
aliasClient := meta.(*clients.Client).Subscription.AliasClient
subscriptionClient := meta.(*clients.Client).Subscription.SubscriptionClient
client := meta.(*clients.Client).Subscription.Client
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand All @@ -136,7 +135,6 @@ func resourceSubscriptionCreate(d *pluginsdk.ResourceData, meta interface{}) err
}

id := subscriptionAlias.NewAliasID(aliasName)

existing, err := aliasClient.AliasGet(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
Expand Down Expand Up @@ -168,6 +166,7 @@ func resourceSubscriptionCreate(d *pluginsdk.ResourceData, meta interface{}) err
// Check if we're adding alias management for an existing subscription
if subscriptionIdRaw, ok := d.GetOk("subscription_id"); ok {
subscriptionId = subscriptionIdRaw.(string)
subscriptionResourceId := commonids.NewSubscriptionID(subscriptionId)

locks.ByID(subscriptionId)
defer locks.UnlockByID(subscriptionId)
Expand All @@ -192,12 +191,12 @@ func resourceSubscriptionCreate(d *pluginsdk.ResourceData, meta interface{}) err
// Disabled and Warned are both "effectively" cancelled states,
if existingSub.State == subscriptions.StateDisabled || existingSub.State == subscriptions.StateWarned {
log.Printf("[DEBUG] Existing subscription in Disabled/Cancelled state Terraform will attempt to re-activate it")
if _, err := subscriptionClient.Enable(ctx, subscriptionId); err != nil {
if _, err := aliasClient.SubscriptionEnable(ctx, subscriptionResourceId); err != nil {
return fmt.Errorf("enabling Subscription %q: %+v", subscriptionId, err)
}
deadline, _ := ctx.Deadline()
createDeadline := time.Until(deadline)
if err := waitForSubscriptionStateToSettle(ctx, meta.(*clients.Client), subscriptionId, "Active", createDeadline); err != nil {
if err := waitForSubscriptionStateToSettle(ctx, client, subscriptionId, "Active", createDeadline); err != nil {
return fmt.Errorf("failed waiting for Subscription %q (Alias %q) to enter %q state: %+v", subscriptionId, id.AliasName, "Active", err)
}
}
Expand All @@ -224,7 +223,7 @@ func resourceSubscriptionCreate(d *pluginsdk.ResourceData, meta interface{}) err
deadline, _ := ctx.Deadline()
createDeadline := time.Until(deadline)

if err := waitForSubscriptionStateToSettle(ctx, meta.(*clients.Client), *alias.Model.Properties.SubscriptionId, "Active", createDeadline); err != nil {
if err := waitForSubscriptionStateToSettle(ctx, client, *alias.Model.Properties.SubscriptionId, "Active", createDeadline); err != nil {
return fmt.Errorf("failed waiting for Subscription %q (Alias %q) to enter %q state: %+v", *alias.Model.Properties.SubscriptionId, id.AliasName, "Active", err)
}

Expand Down Expand Up @@ -358,7 +357,6 @@ func resourceSubscriptionRead(d *pluginsdk.ResourceData, meta interface{}) error
// Alias assignments, `Warned` for Cancelled with "something" associated with it.
func resourceSubscriptionDelete(d *pluginsdk.ResourceData, meta interface{}) error {
aliasClient := meta.(*clients.Client).Subscription.AliasClient
subscriptionClient := meta.(*clients.Client).Subscription.SubscriptionClient
client := meta.(*clients.Client).Subscription.Client
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand Down Expand Up @@ -414,14 +412,15 @@ func resourceSubscriptionDelete(d *pluginsdk.ResourceData, meta interface{}) err
if !meta.(*clients.Client).Features.Subscription.PreventCancellationOnDestroy {
log.Printf("[DEBUG] Cancelling subscription %s", subscriptionId)

if _, err := subscriptionClient.Cancel(ctx, subscriptionId); err != nil {
subscriptionResourceId := commonids.NewSubscriptionID(subscriptionId)
if _, err := aliasClient.SubscriptionCancel(ctx, subscriptionResourceId); err != nil {
return fmt.Errorf("failed to cancel Subscription: %+v", err)
}

deadline, _ := ctx.Deadline()
deleteDeadline := time.Until(deadline)

if err := waitForSubscriptionStateToSettle(ctx, meta.(*clients.Client), subscriptionId, "Cancelled", deleteDeadline); err != nil {
if err := waitForSubscriptionStateToSettle(ctx, client, subscriptionId, "Cancelled", deleteDeadline); err != nil {
return fmt.Errorf("failed to cancel Subscription %q (Alias %q): %+v", subscriptionId, id.AliasName, err)
}
} else {
Expand All @@ -431,10 +430,10 @@ func resourceSubscriptionDelete(d *pluginsdk.ResourceData, meta interface{}) err
return nil
}

func waitForSubscriptionStateToSettle(ctx context.Context, clients *clients.Client, subscriptionId string, targetState string, timeout time.Duration) error {
func waitForSubscriptionStateToSettle(ctx context.Context, client *subscriptions.Client, subscriptionId string, targetState string, timeout time.Duration) error {
stateConf := &pluginsdk.StateChangeConf{
Refresh: func() (result interface{}, state string, err error) {
status, err := clients.Subscription.Client.Get(ctx, subscriptionId)
status, err := client.Get(ctx, subscriptionId)
return status, string(status.State), err
},
PollInterval: 10 * time.Second,
Expand Down

This file was deleted.

This file was deleted.

Loading
Loading