From 0dd44be308cb054b5ead9bfe6d9794628d569ca0 Mon Sep 17 00:00:00 2001 From: Eugene Chuvyrov Date: Wed, 21 Jun 2017 23:02:24 -0700 Subject: [PATCH 1/2] Added Managed Disk as a Data Source --- azurerm/data_source_managed_disk.go | 82 ++++++++++++++++ azurerm/data_source_managed_disk_test.go | 64 +++++++++++++ azurerm/provider.go | 1 + website/azurerm.erb | 5 + website/docs/d/managed_disk.html.markdown | 112 ++++++++++++++++++++++ 5 files changed, 264 insertions(+) create mode 100644 azurerm/data_source_managed_disk.go create mode 100644 azurerm/data_source_managed_disk_test.go create mode 100644 website/docs/d/managed_disk.html.markdown diff --git a/azurerm/data_source_managed_disk.go b/azurerm/data_source_managed_disk.go new file mode 100644 index 000000000000..3034d6b8d65e --- /dev/null +++ b/azurerm/data_source_managed_disk.go @@ -0,0 +1,82 @@ +package azurerm + +import ( + "fmt" + "net/http" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceArmManagedDisk() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmManagedDiskRead, + Schema: map[string]*schema.Schema{ + + "name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_group_name": { + Type: schema.TypeString, + Required: true, + }, + + "storage_account_type": { + Type: schema.TypeString, + Computed: true, + }, + + "source_uri": { + Type: schema.TypeString, + Computed: true, + }, + + "source_resource_id": { + Type: schema.TypeString, + Computed: true, + }, + + "os_type": { + Type: schema.TypeString, + Computed: true, + }, + + "disk_size_gb": { + Type: schema.TypeInt, + Computed: true, + }, + + "tags": tagsSchema(), + }, + } +} + +func dataSourceArmManagedDiskRead(d *schema.ResourceData, meta interface{}) error { + diskClient := meta.(*ArmClient).diskClient + + resGroup := d.Get("resource_group_name").(string) + name := d.Get("name").(string) + + resp, err := diskClient.Get(resGroup, name) + if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return nil + } + return fmt.Errorf("[ERROR] Error making Read request on Azure Managed Disk %s (resource group %s): %s", name, resGroup, err) + } + + d.SetId(*resp.ID) + if resp.Properties != nil { + flattenAzureRmManagedDiskProperties(d, resp.Properties) + } + + if resp.CreationData != nil { + flattenAzureRmManagedDiskCreationData(d, resp.CreationData) + } + + flattenAndSetTags(d, resp.Tags) + + return nil +} diff --git a/azurerm/data_source_managed_disk_test.go b/azurerm/data_source_managed_disk_test.go new file mode 100644 index 000000000000..0237fcc5526f --- /dev/null +++ b/azurerm/data_source_managed_disk_test.go @@ -0,0 +1,64 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAzureRMManagedDisk_basic(t *testing.T) { + ri := acctest.RandInt() + + name := fmt.Sprintf("acctestmanageddisk-%d", ri) + resourceGroupName := fmt.Sprintf("acctestRG-%d", ri) + + config := testAccDatSourceAzureRMManagedDiskBasic(name, resourceGroupName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.azurerm_managed_disk.test", "name", name), + resource.TestCheckResourceAttr("data.azurerm_managed_disk.test", "resource_group_name", resourceGroupName), + resource.TestCheckResourceAttr("data.azurerm_managed_disk.test", "storage_account_type", "Premium_LRS"), + resource.TestCheckResourceAttr("data.azurerm_managed_disk.test", "disk_size_gb", "10"), + resource.TestCheckResourceAttr("data.azurerm_managed_disk.test", "tags.%", "1"), + resource.TestCheckResourceAttr("data.azurerm_managed_disk.test", "tags.environment", "acctest"), + ), + }, + }, + }) +} + +func testAccDatSourceAzureRMManagedDiskBasic(name string, resourceGroupName string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "%s" + location = "West US" +} + +resource "azurerm_managed_disk" "test" { + name = "%s" + location = "West US" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_type = "Premium_LRS" + create_option = "Empty" + disk_size_gb = "10" + + tags { + environment = "acctest" + } +} + +data "azurerm_managed_disk" "test" { + name = "${azurerm_managed_disk.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, resourceGroupName, name) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 05c10505cfd9..d88f792f65dd 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -65,6 +65,7 @@ func Provider() terraform.ResourceProvider { "azurerm_client_config": dataSourceArmClientConfig(), "azurerm_resource_group": dataSourceArmResourceGroup(), "azurerm_public_ip": dataSourceArmPublicIP(), + "azurerm_managed_disk": dataSourceArmManagedDisk(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/azurerm.erb b/website/azurerm.erb index 95c5b39791cf..963928c99d81 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -25,6 +25,11 @@ > azurerm_public_ip + + > + azurerm_managed_disk + + diff --git a/website/docs/d/managed_disk.html.markdown b/website/docs/d/managed_disk.html.markdown new file mode 100644 index 000000000000..03172224d15e --- /dev/null +++ b/website/docs/d/managed_disk.html.markdown @@ -0,0 +1,112 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_managed_disk" +sidebar_current: "docs-azurerm-datasource-managed-disk" +description: |- + Get information about the specified managed disk. +--- + +# azurerm\_managed\_disk + +Use this data source to access the properties of an existing Azure Managed Disk. + +## Example Usage + +```hcl +data "azurerm_managed_disk" "datasourcemd" { + name = "testManagedDisk" + resource_group_name = "acctestRG" +} + +resource "azurerm_virtual_network" "test" { + name = "acctvn" + address_space = ["10.0.0.0/16"] + location = "West US 2" + resource_group_name = "acctestRG" +} + +resource "azurerm_subnet" "test" { + name = "acctsub" + resource_group_name = "acctestRG" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" +} + +resource "azurerm_network_interface" "test" { + name = "acctni" + location = "West US 2" + resource_group_name = "acctestRG" + + ip_configuration { + name = "testconfiguration1" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" + } +} + +resource "azurerm_virtual_machine" "test" { + name = "acctvm" + location = "West US 2" + resource_group_name = "acctestRG" + network_interface_ids = ["${azurerm_network_interface.test.id}"] + vm_size = "Standard_DS1_v2" + + storage_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "14.04.2-LTS" + version = "latest" + } + + storage_os_disk { + name = "myosdisk1" + caching = "ReadWrite" + create_option = "FromImage" + managed_disk_type = "Standard_LRS" + } + + storage_data_disk { + name = "datadisk_new" + managed_disk_type = "Standard_LRS" + create_option = "Empty" + lun = 0 + disk_size_gb = "1023" + } + + storage_data_disk { + name = "${data.azurerm_managed_disk.datasourcemd.name}" + managed_disk_id = "${data.azurerm_managed_disk.datasourcemd.id}" + create_option = "Attach" + lun = 1 + disk_size_gb = "${data.azurerm_managed_disk.datasourcemd.disk_size_gb}" + } + + os_profile { + computer_name = "hostname" + admin_username = "testadmin" + admin_password = "Password1234!" + } + + os_profile_linux_config { + disable_password_authentication = false + } + + tags { + environment = "staging" + } +} +``` + +## Argument Reference + +* `name` - (Required) Specifies the name of the Managed Disk. +* `resource_group_name` - (Required) Specifies the name of the resource group. + + +## Attributes Reference + +* `storage_account_type` - The storage account type for the managed disk. +* `source_uri` - The source URI for the managed disk +* `source_resource_id` - ID of an existing managed disk that the current resource was created from. +* `os_type` - The operating system for managed disk. Valid values are `Linux` or `Windows` +* `disk_size_gb` - The size of the managed disk in gigabytes. From 99a9091ec25f593a6547de92641036787f413e95 Mon Sep 17 00:00:00 2001 From: Eugene Chuvyrov Date: Thu, 22 Jun 2017 10:46:43 -0700 Subject: [PATCH 2/2] Added tags to managed disk doc and sorted data sources alphabetically --- website/azurerm.erb | 12 ++++++------ website/docs/d/managed_disk.html.markdown | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/website/azurerm.erb b/website/azurerm.erb index 963928c99d81..e7802217ae9d 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -18,18 +18,18 @@ azurerm_client_config - > - azurerm_resource_group + > + azurerm_managed_disk - + > azurerm_public_ip - > - azurerm_managed_disk + > + azurerm_resource_group - + diff --git a/website/docs/d/managed_disk.html.markdown b/website/docs/d/managed_disk.html.markdown index 03172224d15e..e154438fa0be 100644 --- a/website/docs/d/managed_disk.html.markdown +++ b/website/docs/d/managed_disk.html.markdown @@ -110,3 +110,4 @@ resource "azurerm_virtual_machine" "test" { * `source_resource_id` - ID of an existing managed disk that the current resource was created from. * `os_type` - The operating system for managed disk. Valid values are `Linux` or `Windows` * `disk_size_gb` - The size of the managed disk in gigabytes. +* `tags` - A mapping of tags assigned to the resource.