Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MS] Adding Managed Disk as a Data Source #121

Merged
merged 2 commits into from
Jun 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions azurerm/data_source_managed_disk.go
Original file line number Diff line number Diff line change
@@ -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
}
64 changes: 64 additions & 0 deletions azurerm/data_source_managed_disk_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
5 changes: 5 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<li<%= sidebar_current("docs-azurerm-datasource-public-ip") %>>
<a href="/docs/providers/azurerm/d/public_ip.html">azurerm_public_ip</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-managed-disk") %>>
<a href="/docs/providers/azurerm/d/managed_disk.html">azurerm_managed_disk</a>
</li>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: can we sort this alphabetically in the list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


</ul>
</li>

Expand Down
112 changes: 112 additions & 0 deletions website/docs/d/managed_disk.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add in the missing tags field too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done