Skip to content

Commit

Permalink
r/communication_service: adding a state migration to fix hashicorp#17532
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff authored Oct 12, 2022
1 parent 082f57e commit b3f38fa
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 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/communication/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/communication/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand All @@ -39,6 +40,11 @@ func resourceArmCommunicationService() *pluginsdk.Resource {
return err
}),

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

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Expand Down Expand Up @@ -94,10 +100,7 @@ func resourceArmCommunicationServiceCreateUpdate(d *pluginsdk.ResourceData, meta
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

id := communicationservice.NewCommunicationServiceID(subscriptionId, resourceGroup, name)
id := communicationservice.NewCommunicationServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id)
if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions internal/services/communication/migration/service_v0_to_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-sdk/resource-manager/communication/2020-08-20/communicationservice"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = ServiceV0ToV1{}

type ServiceV0ToV1 struct{}

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

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

"data_location": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "United States",
},

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

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

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

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

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

func (ServiceV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
parsedId, err := communicationservice.ParseCommunicationServiceIDInsensitively(oldId)
if err != nil {
return nil, err
}
newId := parsedId.ID()
log.Printf("[DEBUG] Updating ID from %q to %q..", oldId, newId)
rawState["id"] = newId
return rawState, nil
}
}

0 comments on commit b3f38fa

Please sign in to comment.