From 16ef9a9637d4f61d91f830a8ea357e1d778b121a Mon Sep 17 00:00:00 2001 From: Zhenhua Hu Date: Tue, 12 Sep 2023 10:19:51 +0800 Subject: [PATCH 1/2] add code --- .../services/compute/managed_disk_resource.go | 25 +++++- .../compute/managed_disk_resource_test.go | 89 +++++++++++++++++++ website/docs/r/managed_disk.html.markdown | 4 + 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/internal/services/compute/managed_disk_resource.go b/internal/services/compute/managed_disk_resource.go index 4ad87811a1fb..49f67cc25a67 100644 --- a/internal/services/compute/managed_disk_resource.go +++ b/internal/services/compute/managed_disk_resource.go @@ -113,6 +113,19 @@ func resourceManagedDisk() *pluginsdk.Resource { Computed: true, }, + "optimized_frequent_attach_enabled": { + Type: pluginsdk.TypeBool, + Optional: true, + Default: false, + }, + + "performance_plus_enabled": { + Type: pluginsdk.TypeBool, + Optional: true, + ForceNew: true, + Default: false, + }, + "source_uri": { Type: pluginsdk.TypeString, Optional: true, @@ -342,9 +355,11 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro props := &disks.DiskProperties{ CreationData: disks.CreationData{ - CreateOption: createOption, + CreateOption: createOption, + PerformancePlus: pointer.To(d.Get("performance_plus_enabled").(bool)), }, - OsType: &osType, + OptimizedForFrequentAttach: pointer.To(d.Get("optimized_frequent_attach_enabled").(bool)), + OsType: &osType, Encryption: &disks.Encryption{ Type: &encryptionTypePlatformKey, }, @@ -702,6 +717,10 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro return fmt.Errorf("[ERROR] disk_iops_read_write, disk_mbps_read_write, disk_iops_read_only and disk_mbps_read_only are only available for UltraSSD disks and PremiumV2 disks") } + if d.HasChange("optimized_frequent_attach_enabled") { + diskUpdate.Properties.OptimizedForFrequentAttach = pointer.To(d.Get("optimized_frequent_attach_enabled").(bool)) + } + if d.HasChange("os_type") { operatingSystemType := disks.OperatingSystemTypes(d.Get("os_type").(string)) diskUpdate.Properties.OsType = &operatingSystemType @@ -1038,6 +1057,7 @@ func resourceManagedDiskRead(d *pluginsdk.ResourceData, meta interface{}) error d.Set("gallery_image_reference_id", galleryImageReferenceId) d.Set("image_reference_id", imageReferenceId) + d.Set("performance_plus_enabled", creationData.PerformancePlus) d.Set("source_resource_id", creationData.SourceResourceId) d.Set("source_uri", creationData.SourceUri) d.Set("storage_account_id", creationData.StorageAccountId) @@ -1048,6 +1068,7 @@ func resourceManagedDiskRead(d *pluginsdk.ResourceData, meta interface{}) error d.Set("disk_mbps_read_write", props.DiskMBpsReadWrite) d.Set("disk_iops_read_only", props.DiskIOPSReadOnly) d.Set("disk_mbps_read_only", props.DiskMBpsReadOnly) + d.Set("optimized_frequent_attach_enabled", props.OptimizedForFrequentAttach) d.Set("os_type", string(pointer.From(props.OsType))) d.Set("tier", props.Tier) d.Set("max_shares", props.MaxShares) diff --git a/internal/services/compute/managed_disk_resource_test.go b/internal/services/compute/managed_disk_resource_test.go index 9604dacc4a82..fcebe0b14b74 100644 --- a/internal/services/compute/managed_disk_resource_test.go +++ b/internal/services/compute/managed_disk_resource_test.go @@ -170,6 +170,40 @@ func TestAccManagedDisk_update(t *testing.T) { }) } +func TestAccManagedDisk_optimizedFrequentAttach(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_managed_disk", "test") + r := ManagedDiskResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.optimizedFrequentAttach(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + { + Config: r.empty(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + }) +} + +func TestAccManagedDisk_performancePlus(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_managed_disk", "test") + r := ManagedDiskResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.performancePlus(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + }) +} + func TestAccManagedDisk_encryption(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_managed_disk", "test") r := ManagedDiskResource{} @@ -822,6 +856,61 @@ resource "azurerm_managed_disk" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomInteger) } +func (ManagedDiskResource) optimizedFrequentAttach(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_managed_disk" "test" { + name = "acctestd-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + storage_account_type = "Standard_LRS" + create_option = "Empty" + disk_size_gb = "1" + optimized_frequent_attach_enabled = true + + tags = { + environment = "acctest" + cost-center = "ops" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) +} + +func (ManagedDiskResource) performancePlus(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_managed_disk" "test" { + name = "acctestd-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + storage_account_type = "Premium_LRS" + create_option = "Empty" + disk_size_gb = "1024" + performance_plus_enabled = true + tags = { + environment = "acctest" + cost-center = "ops" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) +} + func (ManagedDiskResource) requiresImport(data acceptance.TestData) string { template := ManagedDiskResource{}.empty(data) return fmt.Sprintf(` diff --git a/website/docs/r/managed_disk.html.markdown b/website/docs/r/managed_disk.html.markdown index 0bc0732669d8..e7580d9e70f0 100644 --- a/website/docs/r/managed_disk.html.markdown +++ b/website/docs/r/managed_disk.html.markdown @@ -133,6 +133,10 @@ The following arguments are supported: ~> **NOTE:** Setting logical sector size is supported only with `UltraSSD_LRS` disks and `PremiumV2_LRS` disks. +* `optimized_frequent_attach_enabled` - (Optional) Whether the feature of improving reliability and performance of data disks that are frequently (more than 5 times a day) detached from one virtual machine and attached to another is enabled. This property should not be set for disks that are not detached and attached frequently as it causes the disks to not align with the fault domain of the virtual machine. Defaults to `false`. + +* `performance_plus_enabled` - (Optional) Whether performance plus is enabled for this disk. Performance plus is only supported for UltraSSD disks. Defaults to `false`. Changing this forces a new resource to be created. + * `os_type` - (Optional) Specify a value when the source of an `Import`, `ImportSecure` or `Copy` operation targets a source that contains an operating system. Valid values are `Linux` or `Windows`. * `source_resource_id` - (Optional) The ID of an existing Managed Disk or Snapshot to copy when `create_option` is `Copy` or the recovery point to restore when `create_option` is `Restore`. Changing this forces a new resource to be created. From 85e4c1950248c9a4443e3ed9267fc07a4f19af97 Mon Sep 17 00:00:00 2001 From: Zhenhua Hu Date: Wed, 13 Sep 2023 09:59:25 +0800 Subject: [PATCH 2/2] resolve comments 0913 --- website/docs/r/managed_disk.html.markdown | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/website/docs/r/managed_disk.html.markdown b/website/docs/r/managed_disk.html.markdown index e7580d9e70f0..ab6e0c8a2279 100644 --- a/website/docs/r/managed_disk.html.markdown +++ b/website/docs/r/managed_disk.html.markdown @@ -133,9 +133,13 @@ The following arguments are supported: ~> **NOTE:** Setting logical sector size is supported only with `UltraSSD_LRS` disks and `PremiumV2_LRS` disks. -* `optimized_frequent_attach_enabled` - (Optional) Whether the feature of improving reliability and performance of data disks that are frequently (more than 5 times a day) detached from one virtual machine and attached to another is enabled. This property should not be set for disks that are not detached and attached frequently as it causes the disks to not align with the fault domain of the virtual machine. Defaults to `false`. +* `optimized_frequent_attach_enabled` - (Optional) Specifies whether this Managed Disk should be optimized for frequent disk attachments (where a disk is attached/detached more than 5 times in a day). Defaults to `false`. -* `performance_plus_enabled` - (Optional) Whether performance plus is enabled for this disk. Performance plus is only supported for UltraSSD disks. Defaults to `false`. Changing this forces a new resource to be created. +-> **Note:** Setting `optimized_frequent_attach_enabled` to `true` causes the disks to not align with the fault domain of the Virtual Machine, which can have operational implications. + +* `performance_plus_enabled` - (Optional) Specifies whether Performance Plus is enabled for this Managed Disk. Defaults to `false`. Changing this forces a new resource to be created. + +-> **Note:** `performance_plus_enabled` can only be set to `true` when using a Managed Disk with an Ultra SSD. * `os_type` - (Optional) Specify a value when the source of an `Import`, `ImportSecure` or `Copy` operation targets a source that contains an operating system. Valid values are `Linux` or `Windows`.