Skip to content

Commit

Permalink
azurerm_app_service_certificate_order - fix the incorrect ID issue (#…
Browse files Browse the repository at this point in the history
…25428)

* fix the incorrect app service certificate order issue

* remove unnecessary fields for state migration
  • Loading branch information
xiaxyi authored Apr 4, 2024
1 parent 53fdf2a commit 2990001
Show file tree
Hide file tree
Showing 10 changed files with 469 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/web/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/web/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -32,6 +33,11 @@ func resourceAppServiceCertificateOrder() *pluginsdk.Resource {
return err
}),

SchemaVersion: 1,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.AppServiceCertificateOrderResourceV0ToV1{},
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Expand Down
159 changes: 159 additions & 0 deletions internal/services/web/migration/app_service_certificate_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package migration

import (
"context"
"fmt"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/web/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = AppServiceCertificateOrderResourceV0ToV1{}

type AppServiceCertificateOrderResourceV0ToV1 struct{}

func (AppServiceCertificateOrderResourceV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"location": {
Type: pluginsdk.TypeString,
Required: true,
},

"resource_group_name": {
Type: pluginsdk.TypeString,
Required: true,
},

"auto_renew": {
Type: pluginsdk.TypeBool,
Optional: true,
},

"certificates": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"certificate_name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"key_vault_id": {
Type: pluginsdk.TypeString,
Computed: true,
},

"key_vault_secret_name": {
Type: pluginsdk.TypeString,
Computed: true,
},

"provisioning_state": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"csr": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
},

"distinguished_name": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
},

"key_size": {
Type: pluginsdk.TypeInt,
Optional: true,
},

"product_type": {
Type: pluginsdk.TypeString,
Optional: true,
},

"validity_in_years": {
Type: pluginsdk.TypeInt,
Optional: true,
},

"domain_verification_token": {
Type: pluginsdk.TypeString,
Computed: true,
},

"status": {
Type: pluginsdk.TypeString,
Computed: true,
},

"expiration_time": {
Type: pluginsdk.TypeString,
Computed: true,
},

"is_private_key_external": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"app_service_certificate_not_renewable_reasons": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"signed_certificate_thumbprint": {
Type: pluginsdk.TypeString,
Computed: true,
},

"root_thumbprint": {
Type: pluginsdk.TypeString,
Computed: true,
},

"intermediate_thumbprint": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

func (AppServiceCertificateOrderResourceV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldIdRaw := rawState["id"].(string)
oldId, err := parse.CertificateOrderOldID(oldIdRaw)
if err != nil {
return rawState, fmt.Errorf("parsing ID %q to upgrade: %+v", oldIdRaw, err)
}

appServiceCertOrderId := parse.NewCertificateOrderID(oldId.SubscriptionId, oldId.ResourceGroup, oldId.CertificateOrderName)
newId := appServiceCertOrderId.ID()

rawState["id"] = newId
return rawState, nil
}
}
2 changes: 1 addition & 1 deletion internal/services/web/parse/certificate_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (id CertificateOrderId) String() string {
}

func (id CertificateOrderId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Web/certificateOrders/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.CertificateRegistration/certificateOrders/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
}

Expand Down
72 changes: 72 additions & 0 deletions internal/services/web/parse/certificate_order_old.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"fmt"
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

type CertificateOrderOldId struct {
SubscriptionId string
ResourceGroup string
CertificateOrderName string
}

func NewCertificateOrderOldID(subscriptionId, resourceGroup, certificateOrderName string) CertificateOrderOldId {
return CertificateOrderOldId{
SubscriptionId: subscriptionId,
ResourceGroup: resourceGroup,
CertificateOrderName: certificateOrderName,
}
}

func (id CertificateOrderOldId) String() string {
segments := []string{
fmt.Sprintf("Certificate Order Name %q", id.CertificateOrderName),
fmt.Sprintf("Resource Group %q", id.ResourceGroup),
}
segmentsStr := strings.Join(segments, " / ")
return fmt.Sprintf("%s: (%s)", "Certificate Order Old", segmentsStr)
}

func (id CertificateOrderOldId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Web/certificateOrders/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.CertificateOrderName)
}

// CertificateOrderOldID parses a CertificateOrderOld ID into an CertificateOrderOldId struct
func CertificateOrderOldID(input string) (*CertificateOrderOldId, error) {
id, err := resourceids.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing %q as an CertificateOrderOld ID: %+v", input, err)
}

resourceId := CertificateOrderOldId{
SubscriptionId: id.SubscriptionID,
ResourceGroup: id.ResourceGroup,
}

if resourceId.SubscriptionId == "" {
return nil, fmt.Errorf("ID was missing the 'subscriptions' element")
}

if resourceId.ResourceGroup == "" {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.CertificateOrderName, err = id.PopSegment("certificateOrders"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &resourceId, nil
}
115 changes: 115 additions & 0 deletions internal/services/web/parse/certificate_order_old_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package parse

// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten

import (
"testing"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
)

var _ resourceids.Id = CertificateOrderOldId{}

func TestCertificateOrderOldIDFormatter(t *testing.T) {
actual := NewCertificateOrderOldID("12345678-1234-9876-4563-123456789012", "resGroup1", "order1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Web/certificateOrders/order1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
}

func TestCertificateOrderOldID(t *testing.T) {
testData := []struct {
Input string
Error bool
Expected *CertificateOrderOldId
}{

{
// empty
Input: "",
Error: true,
},

{
// missing SubscriptionId
Input: "/",
Error: true,
},

{
// missing value for SubscriptionId
Input: "/subscriptions/",
Error: true,
},

{
// missing ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/",
Error: true,
},

{
// missing value for ResourceGroup
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/",
Error: true,
},

{
// missing CertificateOrderName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Web/",
Error: true,
},

{
// missing value for CertificateOrderName
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Web/certificateOrders/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Web/certificateOrders/order1",
Expected: &CertificateOrderOldId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
CertificateOrderName: "order1",
},
},

{
// upper-cased
Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.WEB/CERTIFICATEORDERS/ORDER1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Input)

actual, err := CertificateOrderOldID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get one")
}

if actual.SubscriptionId != v.Expected.SubscriptionId {
t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId)
}
if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}
if actual.CertificateOrderName != v.Expected.CertificateOrderName {
t.Fatalf("Expected %q but got %q for CertificateOrderName", v.Expected.CertificateOrderName, actual.CertificateOrderName)
}
}
}
Loading

0 comments on commit 2990001

Please sign in to comment.