Skip to content

Commit

Permalink
azurerm_managed_disk - add state upgrader from V0 to V1 to fix ID i…
Browse files Browse the repository at this point in the history
…ssue (#18885)

* `azurerm_managed_disk` - add state upgrader from V0 to V1 to fix ID issue

* Remove uncessary meta-argument
  • Loading branch information
myc2h6o committed Oct 20, 2022
1 parent 188a7b9 commit 607f923
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/services/compute/managed_disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -34,6 +35,11 @@ func resourceManagedDisk() *pluginsdk.Resource {
Update: resourceManagedDiskUpdate,
Delete: resourceManagedDiskDelete,

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

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ManagedDiskID(id)
return err
Expand Down
292 changes: 292 additions & 0 deletions internal/services/compute/migration/managed_disk_v0_to_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
package migration

import (
"context"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = ManagedDiskV0ToV1{}

type ManagedDiskV0ToV1 struct{}

func (ManagedDiskV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId, err := disks.ParseDiskIDInsensitively(rawState["id"].(string))
if err != nil {
return rawState, err
}

rawState["id"] = oldId.ID()
return rawState, nil
}
}

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

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

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

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

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

"edge_zone": {
Type: schema.TypeString,
Optional: true,
},

"logical_sector_size": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
},

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

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

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

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

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

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

"disk_size_gb": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
},

"disk_iops_read_write": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
},

"disk_mbps_read_write": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
},

"disk_iops_read_only": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
},

"disk_mbps_read_only": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
},

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

"encryption_settings": encryptionSettingsSchema(),

"network_access_policy": {
Type: pluginsdk.TypeString,
Optional: true,
},
"disk_access_id": {
Type: pluginsdk.TypeString,
Optional: true,
},

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

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

"max_shares": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},

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

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

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

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

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

"zone": {
Type: schema.TypeString,
Optional: true,
},

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

func encryptionSettingsSchema() *pluginsdk.Schema {
if !features.FourPointOhBeta() {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"disk_encryption_key": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"secret_url": {
Type: pluginsdk.TypeString,
Required: true,
},

"source_vault_id": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},
"key_encryption_key": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"key_url": {
Type: pluginsdk.TypeString,
Required: true,
},

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

return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"disk_encryption_key": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"secret_url": {
Type: pluginsdk.TypeString,
Required: true,
},

"source_vault_id": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},
"key_encryption_key": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"key_url": {
Type: pluginsdk.TypeString,
Required: true,
},

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

0 comments on commit 607f923

Please sign in to comment.