diff --git a/azurerm/internal/services/compute/managed_disk_resource.go b/azurerm/internal/services/compute/managed_disk_resource.go index 7647280f18ccc..6cc30a17eff98 100644 --- a/azurerm/internal/services/compute/managed_disk_resource.go +++ b/azurerm/internal/services/compute/managed_disk_resource.go @@ -25,7 +25,7 @@ import ( func resourceManagedDisk() *schema.Resource { return &schema.Resource{ - Create: resourceManagedDiskCreateUpdate, + Create: resourceManagedDiskCreate, Read: resourceManagedDiskRead, Update: resourceManagedDiskUpdate, Delete: resourceManagedDiskDelete, @@ -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.") @@ -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) @@ -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, @@ -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) @@ -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) diff --git a/azurerm/internal/services/compute/managed_disk_resource_test.go b/azurerm/internal/services/compute/managed_disk_resource_test.go index 1aa4878b3dfda..dc0c206714f80 100644 --- a/azurerm/internal/services/compute/managed_disk_resource_test.go +++ b/azurerm/internal/services/compute/managed_disk_resource_test.go @@ -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{} @@ -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" { diff --git a/website/docs/r/managed_disk.html.markdown b/website/docs/r/managed_disk.html.markdown index 2b8c0ec8e677b..c2ba45e09f504 100644 --- a/website/docs/r/managed_disk.html.markdown +++ b/website/docs/r/managed_disk.html.markdown @@ -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.