Skip to content

Commit

Permalink
managed_disk: Option to configure tier on managed disks (#11634)
Browse files Browse the repository at this point in the history
Fixes #11585

Tasks:
  • Loading branch information
aristosvo authored May 9, 2021
1 parent d861b03 commit f731aa7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
30 changes: 27 additions & 3 deletions azurerm/internal/services/compute/managed_disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func resourceManagedDisk() *schema.Resource {
return &schema.Resource{
Create: resourceManagedDiskCreateUpdate,
Create: resourceManagedDiskCreate,
Read: resourceManagedDiskRead,
Update: resourceManagedDiskUpdate,
Delete: resourceManagedDiskDelete,
Expand Down Expand Up @@ -163,15 +163,21 @@ func resourceManagedDisk() *schema.Resource {
ValidateFunc: azure.ValidateResourceID,
},

"tier": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"tags": tags.Schema(),
},
}
}

func resourceManagedDiskCreateUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceManagedDiskCreate(d *schema.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).Compute.DisksClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

log.Printf("[INFO] preparing arguments for Azure ARM Managed Disk creation.")
Expand All @@ -197,6 +203,7 @@ func resourceManagedDiskCreateUpdate(d *schema.ResourceData, meta interface{}) e
createOption := compute.DiskCreateOption(d.Get("create_option").(string))
storageAccountType := d.Get("storage_account_type").(string)
osType := d.Get("os_type").(string)

t := d.Get("tags").(map[string]interface{})
zones := azure.ExpandZones(d.Get("zones").([]interface{}))
skuName := compute.DiskStorageAccountTypes(storageAccountType)
Expand Down Expand Up @@ -295,6 +302,13 @@ func resourceManagedDiskCreateUpdate(d *schema.ResourceData, meta interface{}) e
}
}

if tier := d.Get("tier").(string); tier != "" {
if storageAccountType != string(compute.PremiumZRS) && storageAccountType != string(compute.PremiumLRS) {
return fmt.Errorf("`tier` can only be specified when `storage_account_type` is set to `Premium_LRS` or `Premium_ZRS`")
}
props.Tier = &tier
}

createDisk := compute.Disk{
Name: &name,
Location: &location,
Expand Down Expand Up @@ -353,6 +367,15 @@ func resourceManagedDiskUpdate(d *schema.ResourceData, meta interface{}) error {
DiskUpdateProperties: &compute.DiskUpdateProperties{},
}

if d.HasChange("tier") {
if storageAccountType != string(compute.PremiumZRS) && storageAccountType != string(compute.PremiumLRS) {
return fmt.Errorf("`tier` can only be specified when `storage_account_type` is set to `Premium_LRS` or `Premium_ZRS`")
}
shouldShutDown = true
tier := d.Get("tier").(string)
diskUpdate.Tier = &tier
}

if d.HasChange("tags") {
t := d.Get("tags").(map[string]interface{})
diskUpdate.Tags = tags.Expand(t)
Expand Down Expand Up @@ -600,6 +623,7 @@ func resourceManagedDiskRead(d *schema.ResourceData, meta interface{}) error {
d.Set("disk_iops_read_write", props.DiskIOPSReadWrite)
d.Set("disk_mbps_read_write", props.DiskMBpsReadWrite)
d.Set("os_type", props.OsType)
d.Set("tier", props.Tier)

if networkAccessPolicy := props.NetworkAccessPolicy; networkAccessPolicy != compute.AllowAll {
d.Set("network_access_policy", props.NetworkAccessPolicy)
Expand Down
51 changes: 51 additions & 0 deletions azurerm/internal/services/compute/managed_disk_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,30 @@ func TestAccManagedDisk_attachedStorageTypeUpdate(t *testing.T) {
})
}

func TestAccManagedDisk_attachedTierUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_managed_disk", "test")
r := ManagedDiskResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.tierUpdateWhileAttached(data, "P10"),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("tier").HasValue("P10"),
),
},
data.ImportStep(),
{
Config: r.tierUpdateWhileAttached(data, "P20"),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("tier").HasValue("P20"),
),
},
data.ImportStep(),
})
}

func TestAccAzureRMManagedDisk_networkPolicy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_managed_disk", "test")
r := ManagedDiskResource{}
Expand Down Expand Up @@ -1023,6 +1047,33 @@ resource "azurerm_virtual_machine_data_disk_attachment" "test" {
`, r.templateAttached(data), data.RandomInteger, diskSize)
}

func (r ManagedDiskResource) tierUpdateWhileAttached(data acceptance.TestData, tier string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
%s
resource "azurerm_managed_disk" "test" {
name = "%d-disk1"
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 = 10
tier = "%s"
}
resource "azurerm_virtual_machine_data_disk_attachment" "test" {
managed_disk_id = azurerm_managed_disk.test.id
virtual_machine_id = azurerm_linux_virtual_machine.test.id
lun = "0"
caching = "None"
}
`, r.templateAttached(data), data.RandomInteger, tier)
}

func (r ManagedDiskResource) storageTypeUpdateWhilstAttached(data acceptance.TestData, storageAccountType string) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/managed_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ The following arguments are supported:

* `storage_account_id` - (Optional) The ID of the Storage Account where the `source_uri` is located. Required when `create_option` is set to `Import`. Changing this forces a new resource to be created.

* `tier` - (Optional) The disk performance tier to use. Possible values are documented [here](https://docs.microsoft.com/en-us/azure/virtual-machines/disks-change-performance). This feature is currently supported only for premium SSDs.

~> **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.


* `tags` - (Optional) A mapping of tags to assign to the resource.

* `zones` - (Optional) A collection containing the availability zone to allocate the Managed Disk in.
Expand Down

0 comments on commit f731aa7

Please sign in to comment.