Skip to content

Commit f0b3431

Browse files
authored
managed_disk_resource: Add disk_iops_read_only, disk_mbps_read_only (hashicorp#14025)
Add support for DiskIOPSReadOnly and DiskMBpsReadOnly These two properties are the read-only version of DiskIOPSReadWrite and DiskMBpsReadWrite, which specify the read-only IOps and MBps to the disk These two properties are only available for Ultra disk, and only available if it is a shared disk.
1 parent e999081 commit f0b3431

File tree

3 files changed

+147
-9
lines changed

3 files changed

+147
-9
lines changed

internal/services/compute/managed_disk_resource.go

+60-9
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ func resourceManagedDisk() *pluginsdk.Resource {
147147
ValidateFunc: validation.IntAtLeast(1),
148148
},
149149

150+
"disk_iops_read_only": {
151+
Type: pluginsdk.TypeInt,
152+
Optional: true,
153+
Computed: true,
154+
ValidateFunc: validation.IntAtLeast(1),
155+
},
156+
157+
"disk_mbps_read_only": {
158+
Type: pluginsdk.TypeInt,
159+
Optional: true,
160+
Computed: true,
161+
ValidateFunc: validation.IntAtLeast(1),
162+
},
163+
150164
"disk_encryption_set_id": {
151165
Type: pluginsdk.TypeString,
152166
Optional: true,
@@ -229,6 +243,7 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro
229243
createOption := compute.DiskCreateOption(d.Get("create_option").(string))
230244
storageAccountType := d.Get("storage_account_type").(string)
231245
osType := d.Get("os_type").(string)
246+
maxShares := d.Get("max_shares").(int)
232247

233248
t := d.Get("tags").(map[string]interface{})
234249
zones := azure.ExpandZones(d.Get("zones").([]interface{}))
@@ -249,8 +264,8 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro
249264
props.DiskSizeGB = &diskSize
250265
}
251266

252-
if v, ok := d.GetOk("max_shares"); ok {
253-
props.MaxShares = utils.Int32(int32(v.(int)))
267+
if maxShares != 0 {
268+
props.MaxShares = utils.Int32(int32(maxShares))
254269
}
255270

256271
if storageAccountType == string(compute.StorageAccountTypesUltraSSDLRS) {
@@ -266,11 +281,27 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro
266281
props.DiskMBpsReadWrite = &diskMBps
267282
}
268283

284+
if v, ok := d.GetOk("disk_iops_read_only"); ok {
285+
if maxShares == 0 {
286+
return fmt.Errorf("[ERROR] disk_iops_read_only is only available for UltraSSD disks with shared disk enabled")
287+
}
288+
289+
props.DiskIOPSReadOnly = utils.Int64(int64(v.(int)))
290+
}
291+
292+
if v, ok := d.GetOk("disk_mbps_read_only"); ok {
293+
if maxShares == 0 {
294+
return fmt.Errorf("[ERROR] disk_mbps_read_only is only available for UltraSSD disks with shared disk enabled")
295+
}
296+
297+
props.DiskMBpsReadOnly = utils.Int64(int64(v.(int)))
298+
}
299+
269300
if v, ok := d.GetOk("logical_sector_size"); ok {
270301
props.CreationData.LogicalSectorSize = utils.Int32(int32(v.(int)))
271302
}
272-
} else if d.HasChange("disk_iops_read_write") || d.HasChange("disk_mbps_read_write") || d.HasChange("logical_sector_size") {
273-
return fmt.Errorf("[ERROR] disk_iops_read_write, disk_mbps_read_write and logical_sector_size are only available for UltraSSD disks")
303+
} else if d.HasChange("disk_iops_read_write") || d.HasChange("disk_mbps_read_write") || d.HasChange("disk_iops_read_only") || d.HasChange("disk_mbps_read_only") || d.HasChange("logical_sector_size") {
304+
return fmt.Errorf("[ERROR] disk_iops_read_write, disk_mbps_read_write, disk_iops_read_only, disk_mbps_read_only and logical_sector_size are only available for UltraSSD disks")
274305
}
275306

276307
if createOption == compute.DiskCreateOptionImport {
@@ -398,6 +429,7 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
398429

399430
name := d.Get("name").(string)
400431
resourceGroup := d.Get("resource_group_name").(string)
432+
maxShares := d.Get("max_shares").(int)
401433
storageAccountType := d.Get("storage_account_type").(string)
402434
shouldShutDown := false
403435

@@ -415,9 +447,7 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
415447
}
416448

417449
if d.HasChange("max_shares") {
418-
v := d.Get("max_shares")
419-
maxShares := int32(v.(int))
420-
diskUpdate.MaxShares = &maxShares
450+
diskUpdate.MaxShares = utils.Int32(int32(maxShares))
421451
var skuName compute.DiskStorageAccountTypes
422452
for _, v := range compute.PossibleDiskStorageAccountTypesValues() {
423453
if strings.EqualFold(storageAccountType, string(v)) {
@@ -468,8 +498,27 @@ func resourceManagedDiskUpdate(d *pluginsdk.ResourceData, meta interface{}) erro
468498
diskMBps := int64(v.(int))
469499
diskUpdate.DiskMBpsReadWrite = &diskMBps
470500
}
471-
} else if d.HasChange("disk_iops_read_write") || d.HasChange("disk_mbps_read_write") {
472-
return fmt.Errorf("[ERROR] disk_iops_read_write and disk_mbps_read_write are only available for UltraSSD disks")
501+
502+
if d.HasChange("disk_iops_read_only") {
503+
if maxShares == 0 {
504+
return fmt.Errorf("[ERROR] disk_iops_read_only is only available for UltraSSD disks with shared disk enabled")
505+
}
506+
507+
v := d.Get("disk_iops_read_only")
508+
diskUpdate.DiskIOPSReadOnly = utils.Int64(int64(v.(int)))
509+
}
510+
511+
if d.HasChange("disk_mbps_read_only") {
512+
if maxShares == 0 {
513+
return fmt.Errorf("[ERROR] disk_mbps_read_only is only available for UltraSSD disks with shared disk enabled")
514+
}
515+
516+
v := d.Get("disk_mbps_read_only")
517+
diskUpdate.DiskMBpsReadOnly = utils.Int64(int64(v.(int)))
518+
}
519+
520+
} else if d.HasChange("disk_iops_read_write") || d.HasChange("disk_mbps_read_write") || d.HasChange("disk_iops_read_only") || d.HasChange("disk_mbps_read_only") {
521+
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")
473522
}
474523

475524
if d.HasChange("os_type") {
@@ -687,6 +736,8 @@ func resourceManagedDiskRead(d *pluginsdk.ResourceData, meta interface{}) error
687736
d.Set("disk_size_gb", props.DiskSizeGB)
688737
d.Set("disk_iops_read_write", props.DiskIOPSReadWrite)
689738
d.Set("disk_mbps_read_write", props.DiskMBpsReadWrite)
739+
d.Set("disk_iops_read_only", props.DiskIOPSReadOnly)
740+
d.Set("disk_mbps_read_only", props.DiskMBpsReadOnly)
690741
d.Set("os_type", props.OsType)
691742
d.Set("tier", props.Tier)
692743
d.Set("max_shares", props.MaxShares)

internal/services/compute/managed_disk_resource_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,27 @@ func TestAccAzureRMManagedDisk_create_withTrustedLaunchEnabled(t *testing.T) {
475475
})
476476
}
477477

478+
func TestAccAzureRMManagedDisk_update_withIOpsReadOnlyAndMBpsReadOnly(t *testing.T) {
479+
data := acceptance.BuildTestData(t, "azurerm_managed_disk", "test")
480+
r := ManagedDiskResource{}
481+
482+
data.ResourceTest(t, r, []acceptance.TestStep{
483+
{
484+
Config: r.create_withIOpsReadOnlyAndMBpsReadOnly(data),
485+
Check: resource.ComposeTestCheckFunc(
486+
check.That(data.ResourceName).ExistsInAzure(r),
487+
),
488+
},
489+
data.ImportStep(),
490+
{
491+
Config: r.update_withIOpsReadOnlyAndMBpsReadOnly(data),
492+
Check: resource.ComposeTestCheckFunc(
493+
check.That(data.ResourceName).ExistsInAzure(r),
494+
),
495+
},
496+
})
497+
}
498+
478499
func (ManagedDiskResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
479500
id, err := parse.ManagedDiskID(state.ID)
480501
if err != nil {
@@ -1469,3 +1490,65 @@ resource "azurerm_managed_disk" "test" {
14691490
}
14701491
`, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
14711492
}
1493+
1494+
func (ManagedDiskResource) create_withIOpsReadOnlyAndMBpsReadOnly(data acceptance.TestData) string {
1495+
return fmt.Sprintf(`
1496+
provider "azurerm" {
1497+
features {}
1498+
}
1499+
1500+
resource "azurerm_resource_group" "test" {
1501+
name = "acctestRG-%d"
1502+
location = "%s"
1503+
}
1504+
1505+
resource "azurerm_managed_disk" "test" {
1506+
name = "acctestd-%d"
1507+
location = azurerm_resource_group.test.location
1508+
resource_group_name = azurerm_resource_group.test.name
1509+
storage_account_type = "UltraSSD_LRS"
1510+
create_option = "Empty"
1511+
disk_size_gb = "4"
1512+
disk_iops_read_only = "101"
1513+
disk_mbps_read_only = "10"
1514+
max_shares = "2"
1515+
zones = ["1"]
1516+
1517+
tags = {
1518+
environment = "acctest"
1519+
cost-center = "ops"
1520+
}
1521+
}
1522+
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
1523+
}
1524+
1525+
func (ManagedDiskResource) update_withIOpsReadOnlyAndMBpsReadOnly(data acceptance.TestData) string {
1526+
return fmt.Sprintf(`
1527+
provider "azurerm" {
1528+
features {}
1529+
}
1530+
1531+
resource "azurerm_resource_group" "test" {
1532+
name = "acctestRG-%d"
1533+
location = "%s"
1534+
}
1535+
1536+
resource "azurerm_managed_disk" "test" {
1537+
name = "acctestd-%d"
1538+
location = azurerm_resource_group.test.location
1539+
resource_group_name = azurerm_resource_group.test.name
1540+
storage_account_type = "UltraSSD_LRS"
1541+
create_option = "Empty"
1542+
disk_size_gb = "4"
1543+
disk_iops_read_only = "102"
1544+
disk_mbps_read_only = "11"
1545+
max_shares = "2"
1546+
zones = ["1"]
1547+
1548+
tags = {
1549+
environment = "acctest"
1550+
cost-center = "ops"
1551+
}
1552+
}
1553+
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
1554+
}

website/docs/r/managed_disk.html.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ The following arguments are supported:
101101

102102
* `disk_mbps_read_write` - (Optional) The bandwidth allowed for this disk; only settable for UltraSSD disks. MBps means millions of bytes per second.
103103

104+
* `disk_iops_read_only` - (Optional) The number of IOPS allowed across all VMs mounting the shared disk as read-only; only settable for UltraSSD disks with shared disk enabled. One operation can transfer between 4k and 256k bytes.
105+
106+
* `disk_mbps_read_only` - (Optional) The bandwidth allowed across all VMs mounting the shared disk as read-only; only settable for UltraSSD disks with shared disk enabled. MBps means millions of bytes per second.
107+
104108
* `disk_size_gb` - (Optional, Required for a new managed disk) Specifies the size of the managed disk to create in gigabytes. If `create_option` is `Copy` or `FromImage`, then the value must be equal to or greater than the source's size. The size can only be increased.
105109

106110
~> **NOTE:** Changing this value is disruptive if the disk is attached to a Virtual Machine. The VM will be shut down and de-allocated as required by Azure to action the change. Terraform will attempt to start the machine again after the update if it was in a `running` state when the apply was started.

0 commit comments

Comments
 (0)