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 - Replace Track1 with Pandora SDK #18813

Merged
merged 8 commits into from
Mar 30, 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
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