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_app_service_certificate_order - fix the incorrect ID issue #25428

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
166 changes: 166 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,166 @@
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a guide on state migrations in the contributor docs which explains which attributes are necessary and which can be omitted in the schema (point 3.) Can you please update the schema here and remove attributes that are unnecessary e.g. ForceNew, Default etc.

Copy link
Contributor Author

@xiaxyi xiaxyi Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the unnecessary fields. As for the docs, the ID was formatted in the correct way:
terraform import azurerm_app_service_certificate_order.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.CertificateRegistration/certificateOrders/certificateorder1
https://github.com/hashicorp/terraform-provider-azurerm/blob/main/website/docs/r/app_service_certificate_order.html.markdown?plain=1#L107

return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

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

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

"auto_renew": {
Type: pluginsdk.TypeBool,
Default: true,
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,
Default: 2048,
},

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

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

"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
Loading