Skip to content

Commit

Permalink
azurerm_subscription - Replace Track1 with Pandora SDK (#18813)
Browse files Browse the repository at this point in the history
Co-authored-by: Tom Bamford <[email protected]>
Co-authored-by: jackofallops <[email protected]>
  • Loading branch information
3 people authored Mar 30, 2023
1 parent 44656fc commit b7f8502
Show file tree
Hide file tree
Showing 46 changed files with 2,072 additions and 92 deletions.
9 changes: 5 additions & 4 deletions internal/services/subscription/client/client.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
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
"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 *subscriptionAlias.AliasClient
AliasClient *subscriptionAliasPandora.SubscriptionsClient
SubscriptionClient *subscriptionAlias.Client
}

func NewClient(o *common.ClientOptions) *Client {
client := subscriptions.NewClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&client.Client, o.ResourceManagerAuthorizer)

aliasClient := subscriptionAlias.NewAliasClientWithBaseURI(o.ResourceManagerEndpoint)
aliasClient := subscriptionAliasPandora.NewSubscriptionsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&aliasClient.Client, o.ResourceManagerAuthorizer)

subscriptionClient := subscriptionAlias.NewClientWithBaseURI(o.ResourceManagerEndpoint)
Expand Down
10 changes: 5 additions & 5 deletions internal/services/subscription/subscription_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2021-01-01/subscriptions" // nolint: staticcheck
subscriptionAliasPandora "github.com/hashicorp/go-azure-sdk/resource-manager/subscription/2021-10-01/subscriptions"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/subscription/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

func importSubscriptionByAlias() pluginsdk.ImporterFunc {
return func(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) (data []*pluginsdk.ResourceData, err error) {
aliasClient := meta.(*clients.Client).Subscription.AliasClient
client := meta.(*clients.Client).Subscription.Client
aliasId, err := parse.SubscriptionAliasID(d.Id())
aliasId, err := subscriptionAliasPandora.ParseAliasID(d.Id())
if err != nil {
return []*pluginsdk.ResourceData{}, fmt.Errorf("failed parsing Subscription Alias ID for import")
}
alias, err := aliasClient.Get(ctx, aliasId.Name)
alias, err := aliasClient.AliasGet(ctx, *aliasId)
if err != nil {
return []*pluginsdk.ResourceData{}, fmt.Errorf("failed reading Subscription Alias: %+v", err)
}
if alias.Properties == nil || alias.Properties.SubscriptionID == nil {
if alias.Model == nil || alias.Model.Properties == nil || alias.Model.Properties.SubscriptionId == nil {
return []*pluginsdk.ResourceData{}, fmt.Errorf("failed reading Subscription Alias Properties, empty response or missing Subscription ID")
}
subscription, err := client.Get(ctx, *alias.Properties.SubscriptionID)
subscription, err := client.Get(ctx, *alias.Model.Properties.SubscriptionId)
if err != nil {
return []*pluginsdk.ResourceData{}, fmt.Errorf("failed parsing Subscription details for import: %+v", err)
}
Expand Down
143 changes: 73 additions & 70 deletions internal/services/subscription/subscription_resource.go

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions internal/services/subscription/subscription_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"os"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/subscription/2021-10-01/subscriptions"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/subscription/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -94,16 +95,16 @@ func TestAccSubscriptionResource_devTest(t *testing.T) {
}

func (SubscriptionResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.SubscriptionAliasID(state.ID)
id, err := subscriptions.ParseAliasID(state.ID)
if err != nil {
return nil, err
}
resp, err := client.Subscription.AliasClient.Get(ctx, id.Name)
resp, err := client.Subscription.AliasClient.AliasGet(ctx, *id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving Subscription Alias %q: %+v", id.Name, err)
return nil, fmt.Errorf("retrieving Subscription Alias %q: %+v", id.AliasName, err)
}

return utils.Bool(true), nil
Expand All @@ -112,23 +113,25 @@ func (SubscriptionResource) Exists(ctx context.Context, client *clients.Client,
// TODO - Need Env vars in CI for Billing Account and Enrollment Account - Testing disabled for now
func (SubscriptionResource) basicEnrollmentAccount(data acceptance.TestData) string {
billingAccount := os.Getenv("ARM_BILLING_ACCOUNT")
enrollmentAccount := os.Getenv("ARM_BILLING_ENROLLMENT_ACCOUNT")
billingProfile := os.Getenv("ARM_BILLING_PROFILE")
invoiceSection := os.Getenv("ARM_INVOICE_SECTION")
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
data "azurerm_billing_enrollment_account_scope" "test" {
billing_account = "%s"
enrollment_account = "%s"
data "azurerm_billing_mca_account_scope" "test" {
billing_account_name = "%[1]s"
billing_profile_name = "%[2]s"
invoice_section_name = "%[3]s"
}
resource "azurerm_subscription" "test" {
alias = "testAcc-%[3]d"
subscription_name = "testAccSubscription %[3]d"
billing_scope_id = data.azurerm_billing_enrollment_account_scope.test.id
alias = "testAcc-%[4]d"
subscription_name = "testAccSubscription %[4]d"
billing_scope_id = data.azurerm_billing_mca_account_scope.test.id
}
`, billingAccount, enrollmentAccount, data.RandomInteger)
`, billingAccount, billingProfile, invoiceSection, data.RandomInteger)
}

func (SubscriptionResource) basicEnrollmentAccountUpdate(data acceptance.TestData) string {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b7f8502

Please sign in to comment.