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..e7802217ae9d 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -18,13 +18,18 @@ azurerm_client_config -