From dbbdc01b68a85468aab716ab81dbd154780b2871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Wed, 5 Dec 2018 16:56:05 -0300 Subject: [PATCH 1/6] Add simple virtual machine data source It currently only returns the ID of the Virtual Machine and only allows searching by exact name. --- azurerm/data_source_arm_virtual_machine.go | 45 +++++++ .../data_source_arm_virtual_machine_test.go | 117 ++++++++++++++++++ azurerm/provider.go | 1 + website/docs/d/virtual_machine.html.markdown | 33 +++++ 4 files changed, 196 insertions(+) create mode 100644 azurerm/data_source_arm_virtual_machine.go create mode 100644 azurerm/data_source_arm_virtual_machine_test.go create mode 100644 website/docs/d/virtual_machine.html.markdown diff --git a/azurerm/data_source_arm_virtual_machine.go b/azurerm/data_source_arm_virtual_machine.go new file mode 100644 index 000000000000..f57ec723fb72 --- /dev/null +++ b/azurerm/data_source_arm_virtual_machine.go @@ -0,0 +1,45 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmVirtualMachine() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmVMRead, + Schema: map[string]*schema.Schema{ + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + }, + } +} + +func dataSourceArmVMRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).vmClient + ctx := meta.(*ArmClient).StopContext + + resGroup := d.Get("resource_group_name").(string) + name := d.Get("name").(string) + + resp, err := client.Get(ctx, resGroup, name, "") + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Virtual Machine %q (Resource Group %q) was not found", name, resGroup) + } + + return fmt.Errorf("Error making Read request on Virtual Machine %q (Resource Group %q): %+v", name, resGroup, err) + } + + d.SetId(*resp.ID) + + return nil +} diff --git a/azurerm/data_source_arm_virtual_machine_test.go b/azurerm/data_source_arm_virtual_machine_test.go new file mode 100644 index 000000000000..e93b8cb57774 --- /dev/null +++ b/azurerm/data_source_arm_virtual_machine_test.go @@ -0,0 +1,117 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + + "github.com/hashicorp/terraform/helper/acctest" +) + +func TestAccDataSourceVirtualMachine_basic(t *testing.T) { + dataSourceName := "data.azurerm_virtual_machine.test" + ri := acctest.RandInt() + + name := fmt.Sprintf("acctvm-%d", ri) + config := testAccDataSourceVirtualMachine_basic(ri, testLocation()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.TestCheckResourceAttr(dataSourceName, "name", name), + }, + }, + }) +} + +func testAccDataSourceVirtualMachine_basic(rInt int, location string) string { + return fmt.Sprintf(` + resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" + } + + resource "azurerm_virtual_network" "test" { + name = "acctvn-%[1]d" + address_space = ["10.0.0.0/16"] + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + } + + resource "azurerm_subnet" "test" { + name = "acctsub-%[1]d" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" + } + + resource "azurerm_network_interface" "test" { + name = "acctni-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + ip_configuration { + name = "testconfiguration1" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" + } + } + + resource "azurerm_storage_account" "test" { + name = "accsa%[1]d" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + account_tier = "Standard" + account_replication_type = "LRS" + } + + resource "azurerm_storage_container" "test" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.test.name}" + storage_account_name = "${azurerm_storage_account.test.name}" + container_access_type = "private" + } + + resource "azurerm_virtual_machine" "test" { + name = "acctvm-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + network_interface_ids = ["${azurerm_network_interface.test.id}"] + vm_size = "Standard_D1_v2" + + storage_image_reference { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2012-Datacenter" + version = "latest" + } + + storage_os_disk { + name = "myosdisk1" + vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" + caching = "ReadWrite" + create_option = "FromImage" + } + + os_profile { + computer_name = "winhost01" + admin_username = "testadmin" + admin_password = "Password1234!" + } + + os_profile_windows_config { + timezone = "Pacific Standard Time" + } + } + + data "azurerm_virtual_machine" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_virtual_machine.test.name}" + } + + `, rInt, location) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 16cd01be3e96..dca830016953 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -122,6 +122,7 @@ func Provider() terraform.ResourceProvider { "azurerm_subscription": dataSourceArmSubscription(), "azurerm_subscriptions": dataSourceArmSubscriptions(), "azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(), + "azurerm_virtual_machine": dataSourceArmVirtualMachine(), "azurerm_virtual_network": dataSourceArmVirtualNetwork(), "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), }, diff --git a/website/docs/d/virtual_machine.html.markdown b/website/docs/d/virtual_machine.html.markdown new file mode 100644 index 000000000000..3d1a9bd57c17 --- /dev/null +++ b/website/docs/d/virtual_machine.html.markdown @@ -0,0 +1,33 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_virtual_machine" +sidebar_current: "docs-azurerm-datasource-virtual-machine-x" +description: |- + Gets information about an existing Virtual Machine. +--- + +# Data Source: azurerm_virtual_machine + +Use this data source to access information about an existing Virtual Machine. + +## Example Usage + +```hcl +data "azurerm_virtual_machine" "test" { + name = "production" + resource_group_name = "networking" +} + +output "virtual_machine_id" { + value = "${data.azurerm_virtual_machine.test.id}" +} +``` + +## Argument Reference + +* `name` - (Required) Specifies the name of the Virtual Machine. +* `resource_group_name` - (Required) Specifies the name of the resource group the Virtual Machine is located in. + +## Attributes Reference + +* `id` - The ID of the Virtual Machine. From 2c343a255043f5cf889ba54d881ef7765bf28573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Thu, 6 Dec 2018 14:26:12 -0300 Subject: [PATCH 2/6] Add data source for virtual machines IDs and names --- azurerm/data_source_arm_virtual_machines.go | 71 +++++++++++++++++++ azurerm/provider.go | 1 + website/docs/d/virtual_machines.html.markdown | 38 ++++++++++ 3 files changed, 110 insertions(+) create mode 100644 azurerm/data_source_arm_virtual_machines.go create mode 100644 website/docs/d/virtual_machines.html.markdown diff --git a/azurerm/data_source_arm_virtual_machines.go b/azurerm/data_source_arm_virtual_machines.go new file mode 100644 index 000000000000..b1fc25ee4f1e --- /dev/null +++ b/azurerm/data_source_arm_virtual_machines.go @@ -0,0 +1,71 @@ +package azurerm + +import ( + "fmt" + "strings" + "time" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" +) + +func dataSourceArmVirtualMachines() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmVMsRead, + Schema: map[string]*schema.Schema{ + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "name_prefix": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.NoZeroValues, + }, + + "ids": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "names": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceArmVMsRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).vmClient + ctx := meta.(*ArmClient).StopContext + + resGroup := d.Get("resource_group_name").(string) + namePrefix := d.Get("name_prefix").(string) + + resp, err := client.List(ctx, resGroup) + if err != nil { + return fmt.Errorf("Error listing Virtual Machines in the Resource Group %q: %v", resGroup, err) + } + + var ids []string + var names []string + + for _, vm := range resp.Values() { + if strings.HasPrefix(*vm.Name, namePrefix) { + ids = append(ids, *vm.ID) + names = append(names, *vm.Name) + } + } + + d.SetId(time.Now().UTC().String()) + + if err := d.Set("ids", ids); err != nil { + return fmt.Errorf("Error setting `ids`: %+v", err) + } + if err := d.Set("names", names); err != nil { + return fmt.Errorf("Error setting `names`: %+v", err) + } + + return nil +} diff --git a/azurerm/provider.go b/azurerm/provider.go index dca830016953..a2d40bc9f21e 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -123,6 +123,7 @@ func Provider() terraform.ResourceProvider { "azurerm_subscriptions": dataSourceArmSubscriptions(), "azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(), "azurerm_virtual_machine": dataSourceArmVirtualMachine(), + "azurerm_virtual_machines": dataSourceArmVirtualMachines(), "azurerm_virtual_network": dataSourceArmVirtualNetwork(), "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), }, diff --git a/website/docs/d/virtual_machines.html.markdown b/website/docs/d/virtual_machines.html.markdown new file mode 100644 index 000000000000..34bf8da2f567 --- /dev/null +++ b/website/docs/d/virtual_machines.html.markdown @@ -0,0 +1,38 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_virtual_machines" +sidebar_current: "docs-azurerm-datasource-virtual-machines-x" +description: |- + Gets information about existing Virtual Machines. +--- + +# Data Source: azurerm_virtual_machines + +Use this data source to access information about existing Virtual Machines. + +## Example Usage + +```hcl +data "azurerm_virtual_machines" "test" { + name_prefix = "prod-" + resource_group_name = "networking" +} + +output "virtual_machine_ids" { + value = "${join(", ", data.azurerm_virtual_machine.test.ids)}" +} + +output "virtual_machine_names" { + value = "${join(", ", data.azurerm_virtual_machine.test.names)}" +} +``` + +## Argument Reference + +* `name_prefix` - (Optional) Specifies the prefix to match the name of the Virtual Machines. +* `resource_group_name` - (Required) Specifies the name of the resource group the Virtual Machines are located in. + +## Attributes Reference + +* `ids` - List of IDs of the Virtual Machines. +* `names` - List of names of the Virtual Machines. From 7b883b294560179af1e2b797c0c79d6b69b18c13 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Thu, 13 Dec 2018 13:21:41 -0300 Subject: [PATCH 3/6] Rename virtual machine data source reading function Co-Authored-By: inkel --- azurerm/data_source_arm_virtual_machine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/azurerm/data_source_arm_virtual_machine.go b/azurerm/data_source_arm_virtual_machine.go index f57ec723fb72..bd4e096f055d 100644 --- a/azurerm/data_source_arm_virtual_machine.go +++ b/azurerm/data_source_arm_virtual_machine.go @@ -10,7 +10,7 @@ import ( func dataSourceArmVirtualMachine() *schema.Resource { return &schema.Resource{ - Read: dataSourceArmVMRead, + Read: dataSourceArmVirtualMachineRead, Schema: map[string]*schema.Schema{ "resource_group_name": resourceGroupNameForDataSourceSchema(), @@ -23,7 +23,7 @@ func dataSourceArmVirtualMachine() *schema.Resource { } } -func dataSourceArmVMRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*ArmClient).vmClient ctx := meta.(*ArmClient).StopContext From c0239728d2b7e7a4fd9d450d7f45be70754ec687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Thu, 13 Dec 2018 13:24:21 -0300 Subject: [PATCH 4/6] Update documentation's sidebar for azurerm_virtual_machine --- website/azurerm.erb | 4 ++++ website/docs/d/virtual_machine.html.markdown | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/website/azurerm.erb b/website/azurerm.erb index 9902d35acce8..18d27b12ef69 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -228,6 +228,10 @@ azurerm_traffic_manager_geographical_location + > + azurerm_virtual_machine + + > azurerm_virtual_network diff --git a/website/docs/d/virtual_machine.html.markdown b/website/docs/d/virtual_machine.html.markdown index 3d1a9bd57c17..8284777ed14d 100644 --- a/website/docs/d/virtual_machine.html.markdown +++ b/website/docs/d/virtual_machine.html.markdown @@ -1,7 +1,7 @@ --- layout: "azurerm" page_title: "Azure Resource Manager: azurerm_virtual_machine" -sidebar_current: "docs-azurerm-datasource-virtual-machine-x" +sidebar_current: "docs-azurerm-datasource-virtual-machine" description: |- Gets information about an existing Virtual Machine. --- From 0ea20e3fa65466228b2ca8cac63dbbb819143cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Thu, 13 Dec 2018 13:25:36 -0300 Subject: [PATCH 5/6] Revert "Add data source for virtual machines IDs and names" This reverts commit 2c343a255043f5cf889ba54d881ef7765bf28573. --- azurerm/data_source_arm_virtual_machines.go | 71 ------------------- azurerm/provider.go | 1 - website/docs/d/virtual_machines.html.markdown | 38 ---------- 3 files changed, 110 deletions(-) delete mode 100644 azurerm/data_source_arm_virtual_machines.go delete mode 100644 website/docs/d/virtual_machines.html.markdown diff --git a/azurerm/data_source_arm_virtual_machines.go b/azurerm/data_source_arm_virtual_machines.go deleted file mode 100644 index b1fc25ee4f1e..000000000000 --- a/azurerm/data_source_arm_virtual_machines.go +++ /dev/null @@ -1,71 +0,0 @@ -package azurerm - -import ( - "fmt" - "strings" - "time" - - "github.com/hashicorp/terraform/helper/schema" - "github.com/hashicorp/terraform/helper/validation" -) - -func dataSourceArmVirtualMachines() *schema.Resource { - return &schema.Resource{ - Read: dataSourceArmVMsRead, - Schema: map[string]*schema.Schema{ - "resource_group_name": resourceGroupNameForDataSourceSchema(), - - "name_prefix": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.NoZeroValues, - }, - - "ids": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - - "names": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - }, - } -} - -func dataSourceArmVMsRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient).vmClient - ctx := meta.(*ArmClient).StopContext - - resGroup := d.Get("resource_group_name").(string) - namePrefix := d.Get("name_prefix").(string) - - resp, err := client.List(ctx, resGroup) - if err != nil { - return fmt.Errorf("Error listing Virtual Machines in the Resource Group %q: %v", resGroup, err) - } - - var ids []string - var names []string - - for _, vm := range resp.Values() { - if strings.HasPrefix(*vm.Name, namePrefix) { - ids = append(ids, *vm.ID) - names = append(names, *vm.Name) - } - } - - d.SetId(time.Now().UTC().String()) - - if err := d.Set("ids", ids); err != nil { - return fmt.Errorf("Error setting `ids`: %+v", err) - } - if err := d.Set("names", names); err != nil { - return fmt.Errorf("Error setting `names`: %+v", err) - } - - return nil -} diff --git a/azurerm/provider.go b/azurerm/provider.go index a2d40bc9f21e..dca830016953 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -123,7 +123,6 @@ func Provider() terraform.ResourceProvider { "azurerm_subscriptions": dataSourceArmSubscriptions(), "azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(), "azurerm_virtual_machine": dataSourceArmVirtualMachine(), - "azurerm_virtual_machines": dataSourceArmVirtualMachines(), "azurerm_virtual_network": dataSourceArmVirtualNetwork(), "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), }, diff --git a/website/docs/d/virtual_machines.html.markdown b/website/docs/d/virtual_machines.html.markdown deleted file mode 100644 index 34bf8da2f567..000000000000 --- a/website/docs/d/virtual_machines.html.markdown +++ /dev/null @@ -1,38 +0,0 @@ ---- -layout: "azurerm" -page_title: "Azure Resource Manager: azurerm_virtual_machines" -sidebar_current: "docs-azurerm-datasource-virtual-machines-x" -description: |- - Gets information about existing Virtual Machines. ---- - -# Data Source: azurerm_virtual_machines - -Use this data source to access information about existing Virtual Machines. - -## Example Usage - -```hcl -data "azurerm_virtual_machines" "test" { - name_prefix = "prod-" - resource_group_name = "networking" -} - -output "virtual_machine_ids" { - value = "${join(", ", data.azurerm_virtual_machine.test.ids)}" -} - -output "virtual_machine_names" { - value = "${join(", ", data.azurerm_virtual_machine.test.names)}" -} -``` - -## Argument Reference - -* `name_prefix` - (Optional) Specifies the prefix to match the name of the Virtual Machines. -* `resource_group_name` - (Required) Specifies the name of the resource group the Virtual Machines are located in. - -## Attributes Reference - -* `ids` - List of IDs of the Virtual Machines. -* `names` - List of names of the Virtual Machines. From 82d74a53bab9f79e7be328b7b948d259014df85d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez?= Date: Thu, 13 Dec 2018 13:43:00 -0300 Subject: [PATCH 6/6] Update virtual machine data source test configuration As suggested by @tombuildsstuff the test VM was changed to a Linux one, which tends to boot faster. I've also properly formattd the test using `terraform fmt`. --- .../data_source_arm_virtual_machine_test.go | 61 +++++++++---------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/azurerm/data_source_arm_virtual_machine_test.go b/azurerm/data_source_arm_virtual_machine_test.go index e93b8cb57774..842cafa12e7c 100644 --- a/azurerm/data_source_arm_virtual_machine_test.go +++ b/azurerm/data_source_arm_virtual_machine_test.go @@ -33,50 +33,50 @@ func testAccDataSourceVirtualMachine_basic(rInt int, location string) string { resource "azurerm_resource_group" "test" { name = "acctestRG-%[1]d" location = "%[2]s" - } + } - resource "azurerm_virtual_network" "test" { + resource "azurerm_virtual_network" "test" { name = "acctvn-%[1]d" address_space = ["10.0.0.0/16"] location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" - } + } - resource "azurerm_subnet" "test" { + resource "azurerm_subnet" "test" { name = "acctsub-%[1]d" resource_group_name = "${azurerm_resource_group.test.name}" virtual_network_name = "${azurerm_virtual_network.test.name}" address_prefix = "10.0.2.0/24" - } + } - resource "azurerm_network_interface" "test" { + resource "azurerm_network_interface" "test" { name = "acctni-%[1]d" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" ip_configuration { - name = "testconfiguration1" - subnet_id = "${azurerm_subnet.test.id}" - private_ip_address_allocation = "dynamic" + name = "testconfiguration1" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" } - } + } - resource "azurerm_storage_account" "test" { + resource "azurerm_storage_account" "test" { name = "accsa%[1]d" resource_group_name = "${azurerm_resource_group.test.name}" location = "${azurerm_resource_group.test.location}" account_tier = "Standard" account_replication_type = "LRS" - } + } - resource "azurerm_storage_container" "test" { + resource "azurerm_storage_container" "test" { name = "vhds" resource_group_name = "${azurerm_resource_group.test.name}" storage_account_name = "${azurerm_storage_account.test.name}" container_access_type = "private" - } + } - resource "azurerm_virtual_machine" "test" { + resource "azurerm_virtual_machine" "test" { name = "acctvm-%[1]d" location = "${azurerm_resource_group.test.location}" resource_group_name = "${azurerm_resource_group.test.name}" @@ -84,34 +84,33 @@ func testAccDataSourceVirtualMachine_basic(rInt int, location string) string { vm_size = "Standard_D1_v2" storage_image_reference { - publisher = "MicrosoftWindowsServer" - offer = "WindowsServer" - sku = "2012-Datacenter" + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" version = "latest" - } + } - storage_os_disk { + storage_os_disk { name = "myosdisk1" vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd" caching = "ReadWrite" create_option = "FromImage" - } + } - os_profile { - computer_name = "winhost01" + os_profile { + computer_name = "linuxhost01" admin_username = "testadmin" admin_password = "Password1234!" - } + } - os_profile_windows_config { - timezone = "Pacific Standard Time" + os_profile_linux_config { + disable_password_authentication = false } - } + } - data "azurerm_virtual_machine" "test" { + data "azurerm_virtual_machine" "test" { resource_group_name = "${azurerm_resource_group.test.name}" name = "${azurerm_virtual_machine.test.name}" - } - - `, rInt, location) + } +`, rInt, location) }