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_snapshot - switch to go-azure-sdk #18957

Merged
merged 4 commits into from
Oct 26, 2022
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
5 changes: 3 additions & 2 deletions internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/proximityplacementgroups"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/sshpublickeys"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/snapshots"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

Expand All @@ -30,7 +31,7 @@ type Client struct {
MarketplaceAgreementsClient *marketplaceordering.MarketplaceAgreementsClient
ProximityPlacementGroupsClient *proximityplacementgroups.ProximityPlacementGroupsClient
SSHPublicKeysClient *sshpublickeys.SshPublicKeysClient
SnapshotsClient *compute.SnapshotsClient
SnapshotsClient *snapshots.SnapshotsClient
UsageClient *compute.UsageClient
VMExtensionImageClient *compute.VirtualMachineExtensionImagesClient
VMExtensionClient *compute.VirtualMachineExtensionsClient
Expand Down Expand Up @@ -91,7 +92,7 @@ func NewClient(o *common.ClientOptions) *Client {
proximityPlacementGroupsClient := proximityplacementgroups.NewProximityPlacementGroupsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&proximityPlacementGroupsClient.Client, o.ResourceManagerAuthorizer)

snapshotsClient := compute.NewSnapshotsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
snapshotsClient := snapshots.NewSnapshotsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&snapshotsClient.Client, o.ResourceManagerAuthorizer)

sshPublicKeysClient := sshpublickeys.NewSshPublicKeysClientWithBaseURI(o.ResourceManagerEndpoint)
Expand Down
52 changes: 26 additions & 26 deletions internal/services/compute/encryption_settings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package compute

import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/snapshots"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
Expand Down Expand Up @@ -114,50 +114,50 @@ func encryptionSettingsSchema() *pluginsdk.Schema {
}
}

func expandSnapshotDiskEncryptionSettings(settingsList []interface{}) *compute.EncryptionSettingsCollection {
func expandSnapshotDiskEncryptionSettings(settingsList []interface{}) *snapshots.EncryptionSettingsCollection {
if len(settingsList) == 0 {
return &compute.EncryptionSettingsCollection{}
return &snapshots.EncryptionSettingsCollection{}
}
settings := settingsList[0].(map[string]interface{})

config := &compute.EncryptionSettingsCollection{
Enabled: utils.Bool(true),
config := &snapshots.EncryptionSettingsCollection{
Enabled: true,
}

if !features.FourPointOhBeta() {
config.Enabled = utils.Bool(settings["enabled"].(bool))
config.Enabled = settings["enabled"].(bool)
}

var diskEncryptionKey *compute.KeyVaultAndSecretReference
var diskEncryptionKey *snapshots.KeyVaultAndSecretReference
if v := settings["disk_encryption_key"].([]interface{}); len(v) > 0 {
dek := v[0].(map[string]interface{})

secretURL := dek["secret_url"].(string)
sourceVaultId := dek["source_vault_id"].(string)
diskEncryptionKey = &compute.KeyVaultAndSecretReference{
SecretURL: utils.String(secretURL),
SourceVault: &compute.SourceVault{
ID: utils.String(sourceVaultId),
diskEncryptionKey = &snapshots.KeyVaultAndSecretReference{
SecretUrl: secretURL,
SourceVault: snapshots.SourceVault{
Id: utils.String(sourceVaultId),
},
}
}

var keyEncryptionKey *compute.KeyVaultAndKeyReference
var keyEncryptionKey *snapshots.KeyVaultAndKeyReference
if v := settings["key_encryption_key"].([]interface{}); len(v) > 0 {
kek := v[0].(map[string]interface{})

secretURL := kek["key_url"].(string)
sourceVaultId := kek["source_vault_id"].(string)
keyEncryptionKey = &compute.KeyVaultAndKeyReference{
KeyURL: utils.String(secretURL),
SourceVault: &compute.SourceVault{
ID: utils.String(sourceVaultId),
keyEncryptionKey = &snapshots.KeyVaultAndKeyReference{
KeyUrl: secretURL,
SourceVault: snapshots.SourceVault{
Id: utils.String(sourceVaultId),
},
}
}

// at this time we only support a single element
config.EncryptionSettings = &[]compute.EncryptionSettingsElement{
config.EncryptionSettings = &[]snapshots.EncryptionSettingsElement{
{
DiskEncryptionKey: diskEncryptionKey,
KeyEncryptionKey: keyEncryptionKey,
Expand All @@ -166,7 +166,7 @@ func expandSnapshotDiskEncryptionSettings(settingsList []interface{}) *compute.E
return config
}

func flattenSnapshotDiskEncryptionSettings(encryptionSettings *compute.EncryptionSettingsCollection) []interface{} {
func flattenSnapshotDiskEncryptionSettings(encryptionSettings *snapshots.EncryptionSettingsCollection) []interface{} {
if encryptionSettings == nil {
return []interface{}{}
}
Expand All @@ -179,13 +179,13 @@ func flattenSnapshotDiskEncryptionSettings(encryptionSettings *compute.Encryptio

if key := settings.DiskEncryptionKey; key != nil {
secretUrl := ""
if key.SecretURL != nil {
secretUrl = *key.SecretURL
if key.SecretUrl != "" {
secretUrl = key.SecretUrl
}

sourceVaultId := ""
if key.SourceVault != nil && key.SourceVault.ID != nil {
sourceVaultId = *key.SourceVault.ID
if key.SourceVault.Id != nil {
sourceVaultId = *key.SourceVault.Id
}

diskEncryptionKeys = append(diskEncryptionKeys, map[string]interface{}{
Expand All @@ -196,13 +196,13 @@ func flattenSnapshotDiskEncryptionSettings(encryptionSettings *compute.Encryptio

if key := settings.KeyEncryptionKey; key != nil {
keyUrl := ""
if key.KeyURL != nil {
keyUrl = *key.KeyURL
if key.KeyUrl != "" {
keyUrl = key.KeyUrl
}

sourceVaultId := ""
if key.SourceVault != nil && key.SourceVault.ID != nil {
sourceVaultId = *key.SourceVault.ID
if key.SourceVault.Id != nil {
sourceVaultId = *key.SourceVault.Id
}

keyEncryptionKeys = append(keyEncryptionKeys, map[string]interface{}{
Expand Down
134 changes: 134 additions & 0 deletions internal/services/compute/migration/snapshot_v0_to_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package migration

import (
"context"
"log"

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

var _ pluginsdk.StateUpgrade = SnapshotV0ToV1{}

type SnapshotV0ToV1 struct{}

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

newId := oldId.ID()
log.Printf("[DEBUG] Updating the ID from %q to %q", oldIdRaw, newId)
rawState["id"] = newId
return rawState, nil
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

can we also remove the old Resource ID Type from resourceids.go and the generated ID Parsers/Validators?


func (SnapshotV0ToV1) 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,
},

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

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

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

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

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

"encryption_settings": {
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,
},
},
},
},
},
},
},

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

"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
}
}
69 changes: 0 additions & 69 deletions internal/services/compute/parse/snapshot.go

This file was deleted.

Loading