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_managed_disk - support for PremiumV2_LRS #17671

Merged
merged 19 commits into from
Sep 22, 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 @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2021-11-01/dedicatedhosts"
"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/terraform-provider-azurerm/internal/common"
)

Expand All @@ -17,7 +18,7 @@ type Client struct {
CapacityReservationGroupsClient *compute.CapacityReservationGroupsClient
DedicatedHostsClient *dedicatedhosts.DedicatedHostsClient
DedicatedHostGroupsClient *dedicatedhostgroups.DedicatedHostGroupsClient
DisksClient *compute.DisksClient
DisksClient *disks.DisksClient
DiskAccessClient *compute.DiskAccessesClient
DiskEncryptionSetsClient *compute.DiskEncryptionSetsClient
GalleriesClient *compute.GalleriesClient
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewClient(o *common.ClientOptions) *Client {
dedicatedHostGroupsClient := dedicatedhostgroups.NewDedicatedHostGroupsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&dedicatedHostGroupsClient.Client, o.ResourceManagerAuthorizer)

disksClient := compute.NewDisksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
disksClient := disks.NewDisksClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&disksClient.Client, o.ResourceManagerAuthorizer)

diskAccessClient := compute.NewDiskAccessesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
Expand Down
7 changes: 4 additions & 3 deletions internal/services/compute/disk_encryption_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"

"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/terraform-provider-azurerm/internal/services/compute/parse"
)

// retrieveDiskEncryptionSetEncryptionType returns encryption type of the disk encryption set
func retrieveDiskEncryptionSetEncryptionType(ctx context.Context, client *compute.DiskEncryptionSetsClient, diskEncryptionSetId string) (*compute.EncryptionType, error) {
func retrieveDiskEncryptionSetEncryptionType(ctx context.Context, client *compute.DiskEncryptionSetsClient, diskEncryptionSetId string) (*disks.EncryptionType, error) {
diskEncryptionSet, err := parse.DiskEncryptionSetID(diskEncryptionSetId)
if err != nil {
return nil, err
Expand All @@ -20,9 +21,9 @@ func retrieveDiskEncryptionSetEncryptionType(ctx context.Context, client *comput
return nil, fmt.Errorf("retrieving %s: %+v", *diskEncryptionSet, err)
}

var encryptionType *compute.EncryptionType
var encryptionType *disks.EncryptionType
if props := resp.EncryptionSetProperties; props != nil && string(props.EncryptionType) != "" {
v := compute.EncryptionType(props.EncryptionType)
v := disks.EncryptionType(props.EncryptionType)
encryptionType = &v
}

Expand Down
105 changes: 64 additions & 41 deletions internal/services/compute/disk_sas_token_resource.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package compute

import (
"bytes"
"encoding/json"
"fmt"
"log"
"time"

"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/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
Expand All @@ -14,6 +16,18 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

type Result struct {
Properties Properties `tfschema:"properties"`
}

type Properties struct {
Output Output `tfschema:"output"`
}

type Output struct {
AccessSAS string `tfschema:"accessSAS"`
}

func resourceManagedDiskSasToken() *pluginsdk.Resource {

return &pluginsdk.Resource{
Expand Down Expand Up @@ -53,8 +67,8 @@ func resourceManagedDiskSasToken() *pluginsdk.Resource {
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.AccessLevelRead),
string(compute.AccessLevelWrite),
string(disks.AccessLevelRead),
string(disks.AccessLevelWrite),
}, false),
},

Expand All @@ -74,48 +88,61 @@ func resourceManagedDiskSasTokenCreate(d *pluginsdk.ResourceData, meta interface
defer cancel()

log.Printf("[INFO] preparing arguments for AzureRM Disk Export.")
durationInSeconds := int32(d.Get("duration_in_seconds").(int))
access := compute.AccessLevel(d.Get("access_level").(string))
durationInSeconds := int64(d.Get("duration_in_seconds").(int))
access := disks.AccessLevel(d.Get("access_level").(string))

diskId, err := parse.ManagedDiskID(d.Get("managed_disk_id").(string))
diskId, err := disks.ParseDiskID(d.Get("managed_disk_id").(string))
if err != nil {
return err
}

grantAccessData := compute.GrantAccessData{
grantAccessData := disks.GrantAccessData{
Access: access,
DurationInSeconds: &durationInSeconds,
DurationInSeconds: durationInSeconds,
}

resp, err := client.Get(ctx, diskId.ResourceGroup, diskId.DiskName)
resp, err := client.Get(ctx, *diskId)
if err != nil {
return fmt.Errorf("error retrieving Disk %s: %+v", *diskId, err)
}

// checking whether disk export SAS URL is active already before creating. If yes, we raise an error
if resp.DiskState == "ActiveSAS" {
return fmt.Errorf("active SAS Token for Disk Export already exists, cannot create another one %s: %+v", *diskId, err)
}

future, err := client.GrantAccess(ctx, diskId.ResourceGroup, diskId.DiskName, grantAccessData)
if err != nil {
return fmt.Errorf("granting access to %s: %+v", *diskId, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for access to be granted to %s: %+v", *diskId, err)
}
read, err := future.Result(*client)
if err != nil {
return fmt.Errorf("retrieving SAS Token for Disk Access %s: %+v", *diskId, err)
}
if read.AccessSAS == nil {
return fmt.Errorf("retrieving SAS Token for Disk Access %s: SAS was nil", *diskId)
if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
// checking whether disk export SAS URL is active already before creating. If yes, we raise an error
if string(*props.DiskState) == "ActiveSAS" {
return fmt.Errorf("active SAS Token for Disk Export already exists, cannot create another one %s: %+v", *diskId, err)
}

future, err := client.GrantAccess(ctx, *diskId, grantAccessData)
if err != nil {
return fmt.Errorf("granting access to %s: %+v", *diskId, err)
}

if err := future.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("waiting for access to be granted to %s: %+v", *diskId, err)
}

buf := new(bytes.Buffer)
_, err = buf.ReadFrom(future.Poller.HttpResponse.Body)
if err != nil {
return err
}

var result Result
err = json.Unmarshal([]byte(buf.String()), &result)
if err != nil {
return fmt.Errorf("retrieving SAS Token for Disk Access %s: %+v", *diskId, err)
}
if result.Properties.Output.AccessSAS == "" {
return fmt.Errorf("retrieving SAS Token for Disk Access %s: SAS was nil", *diskId)
}

d.SetId(diskId.ID())
sasToken := result.Properties.Output.AccessSAS
d.Set("sas_url", sasToken)
}
}

d.SetId(diskId.ID())
sasToken := *read.AccessSAS
d.Set("sas_url", sasToken)

return resourceManagedDiskSasTokenRead(d, meta)

}
Expand All @@ -125,20 +152,20 @@ func resourceManagedDiskSasTokenRead(d *pluginsdk.ResourceData, meta interface{}
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

diskId, err := parse.ManagedDiskID(d.Id())
diskId, err := disks.ParseDiskID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, diskId.ResourceGroup, diskId.DiskName)
resp, err := client.Get(ctx, *diskId)
if err != nil {
// checking whether disk export SAS URL is active post creation. If no, we raise an error
if resp.DiskState != "ActiveSAS" {
if string(*resp.Model.Properties.DiskState) != "ActiveSAS" {
log.Printf("[INFO] Disk SAS token %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on Azure Disk Export for SAS Token %s (resource group %s): %s", diskId.DiskName, diskId.ResourceGroup, err)
return fmt.Errorf("making Read request on Azure Disk Export for SAS Token %s (resource group %s): %s", diskId.DiskName, diskId.ResourceGroupName, err)
}

d.SetId(diskId.ID())
Expand All @@ -151,19 +178,15 @@ func resourceManagedDiskSasTokenDelete(d *pluginsdk.ResourceData, meta interface
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ManagedDiskID(d.Id())
id, err := disks.ParseDiskID(d.Id())
if err != nil {
return err
}

future, err := client.RevokeAccess(ctx, id.ResourceGroup, id.DiskName)
err = client.RevokeAccessThenPoll(ctx, *id)
if err != nil {
return fmt.Errorf("revoking access to %s: %+v", *id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for revocation of access to %s: %+v", *id, err)
}

return nil
}
12 changes: 6 additions & 6 deletions internal/services/compute/disk_sas_token_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-02/disks"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"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/utils"
)
Expand All @@ -30,21 +30,21 @@ func TestAccManagedDiskSASToken_basic(t *testing.T) {
}

func (t ManagedDiskSASTokenResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ManagedDiskID(state.ID)
id, err := disks.ParseDiskID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.Compute.DisksClient.Get(ctx, id.ResourceGroup, id.DiskName)
resp, err := clients.Compute.DisksClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Compute Disk Export status %q", id.String())
}

if resp.DiskState != "ActiveSAS" {
return nil, fmt.Errorf("Disk SAS token %s (resource group %s): %s", id.DiskName, id.ResourceGroup, err)
if string(*resp.Model.Properties.DiskState) != "ActiveSAS" {
return nil, fmt.Errorf("Disk SAS token %s (resource group %s): %s", id.DiskName, id.ResourceGroupName, err)
}

return utils.Bool(resp.ID != nil), nil
return utils.Bool(resp.Model != nil), nil
}

func (r ManagedDiskSASTokenResource) basic(data acceptance.TestData) string {
Expand Down
18 changes: 18 additions & 0 deletions internal/services/compute/edge_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,21 @@ func flattenEdgeZone(input *compute.ExtendedLocation) string {
}
return edgezones.NormalizeNilable(input.Name)
}

func expandManagedDiskEdgeZone(input string) *edgezones.Model {
normalized := edgezones.Normalize(input)
if normalized == "" {
return nil
}

return &edgezones.Model{
Name: normalized,
}
}

func flattenManagedDiskEdgeZone(input *edgezones.Model) string {
if input == nil || input.Name == "" {
return ""
}
return edgezones.NormalizeNilable(&input.Name)
}
Loading