-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from echuvyrov/manageddiskdatasource
[MS] Adding Managed Disk as a Data Source
- Loading branch information
Showing
5 changed files
with
268 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
--- | ||
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. | ||
* `tags` - A mapping of tags assigned to the resource. |