Skip to content

Commit

Permalink
azurerm_snapshot - switch to go-azure-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
myc2h6o committed Oct 25, 2022
1 parent eb92f00 commit 6cad386
Show file tree
Hide file tree
Showing 40 changed files with 2,058 additions and 117 deletions.
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
85 changes: 85 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,85 @@
package migration

import (
"context"

"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) {
oldId, err := snapshots.ParseSnapshotIDInsensitively(rawState["id"].(string))
if err != nil {
return rawState, err
}

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

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": encryptionSettingsSchema(),

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

"tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
}
}
68 changes: 41 additions & 27 deletions internal/services/compute/snapshot_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/snapshots"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceSnapshot() *pluginsdk.Resource {
Expand Down Expand Up @@ -120,44 +119,59 @@ func dataSourceSnapshotRead(d *pluginsdk.ResourceData, meta interface{}) error {
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewSnapshotID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := snapshots.NewSnapshotID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
resp, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("loading %s: %+v", id, err)
}

d.SetId(id.ID())

if props := resp.SnapshotProperties; props != nil {
d.Set("os_type", string(props.OsType))
d.Set("time_created", props.TimeCreated.String())
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
if props.OsType != nil {
d.Set("os_type", string(*props.OsType))
}

if props.DiskSizeGB != nil {
d.Set("disk_size_gb", int(*props.DiskSizeGB))
}
if props.TimeCreated != nil {
d.Set("time_created", *props.TimeCreated)
}

if err := d.Set("encryption_settings", flattenSnapshotDiskEncryptionSettings(props.EncryptionSettingsCollection)); err != nil {
return fmt.Errorf("setting `encryption_settings`: %+v", err)
}
if props.DiskSizeGB != nil {
d.Set("disk_size_gb", int(*props.DiskSizeGB))
}

trustedLaunchEnabled := false
if securityProfile := props.SecurityProfile; securityProfile != nil {
if securityProfile.SecurityType == compute.DiskSecurityTypesTrustedLaunch {
trustedLaunchEnabled = true
if err := d.Set("encryption_settings", flattenSnapshotDiskEncryptionSettings(props.EncryptionSettingsCollection)); err != nil {
return fmt.Errorf("setting `encryption_settings`: %+v", err)
}

trustedLaunchEnabled := false
if securityProfile := props.SecurityProfile; securityProfile != nil && securityProfile.SecurityType != nil {
if *securityProfile.SecurityType == snapshots.DiskSecurityTypesTrustedLaunch {
trustedLaunchEnabled = true
}
}
d.Set("trusted_launch_enabled", trustedLaunchEnabled)

data := props.CreationData
d.Set("creation_option", string(data.CreateOption))

if data.SourceUri != nil {
d.Set("source_uri", data.SourceUri)
}
}
d.Set("trusted_launch_enabled", trustedLaunchEnabled)
}

if data := resp.CreationData; data != nil {
d.Set("creation_option", string(data.CreateOption))
d.Set("source_uri", data.SourceURI)
d.Set("source_resource_id", data.SourceResourceID)
d.Set("storage_account_id", data.StorageAccountID)
if data.SourceResourceId != nil {
d.Set("source_resource_id", data.SourceResourceId)
}

if data.StorageAccountId != nil {
d.Set("storage_account_id", data.StorageAccountId)
}
}
}

return nil
Expand Down
Loading

0 comments on commit 6cad386

Please sign in to comment.