-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
tombuildsstuff
merged 2 commits into
hashicorp:master
from
echuvyrov:manageddiskdatasource
Jun 23, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done