Skip to content

Commit

Permalink
provider/azurerm: Adding support for tags to `azurerm_virtual_machi…
Browse files Browse the repository at this point in the history
…ne` (#6556)

provider/azurerm: Adding support for `tags` to `azurerm_virtual_machine`
  • Loading branch information
stack72 committed May 9, 2016
1 parent 4d66f1c commit d3939db
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 8 deletions.
7 changes: 7 additions & 0 deletions builtin/providers/azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ func resourceArmVirtualMachine() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"tags": tagsSchema(),
},
}
}
Expand All @@ -352,6 +354,8 @@ func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) e
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)

osDisk, err := expandAzureRmVirtualMachineOsDisk(d)
if err != nil {
Expand Down Expand Up @@ -406,6 +410,7 @@ func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) e
Name: &name,
Location: &location,
Properties: &properties,
Tags: expandedTags,
}

if _, ok := d.GetOk("plan"); ok {
Expand Down Expand Up @@ -514,6 +519,8 @@ func resourceArmVirtualMachineRead(d *schema.ResourceData, meta interface{}) err
}
}

flattenAndSetTags(d, resp.Tags)

return nil
}

Expand Down
128 changes: 128 additions & 0 deletions builtin/providers/azurerm/resource_arm_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,42 @@ func TestAccAzureRMVirtualMachine_basicLinuxMachine(t *testing.T) {
})
}

func TestAccAzureRMVirtualMachine_tags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine, ri, ri, ri, ri, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachineUpdated, ri, ri, ri, ri, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"),
resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "tags.cost_center", "Ops"),
),
},

resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test"),
resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "tags.environment", "Production"),
),
},
},
})
}

//This is a regression test around https://github.com/hashicorp/terraform/issues/6517
//Because we use CreateOrUpdate, we were sending an empty password on update requests
func TestAccAzureRMVirtualMachine_updateMachineSize(t *testing.T) {
Expand Down Expand Up @@ -211,6 +247,98 @@ resource "azurerm_virtual_machine" "test" {
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "Production"
cost-center = "Ops"
}
}
`

var testAccAzureRMVirtualMachine_basicLinuxMachineUpdated = `
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "West US"
}
resource "azurerm_virtual_network" "test" {
name = "acctvn-%d"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "acctsub-%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-%d"
location = "West US"
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%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "westus"
account_type = "Standard_LRS"
tags {
environment = "staging"
}
}
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-%d"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
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 = "hostname%d"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "Production"
}
}
`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ resource "azurerm_virtual_machine" "test" {
vm_size = "Standard_A0"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
version = "latest"
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
version = "latest"
}
storage_os_disk {
Expand All @@ -84,13 +84,17 @@ resource "azurerm_virtual_machine" "test" {
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
disable_password_authentication = false
}
tags {
environment = "staging"
}
}
```
Expand All @@ -115,6 +119,7 @@ The following arguments are supported:
* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below.
* `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below.
* `network_interface_ids` - (Required) Specifies the list of resource IDs for the network interfaces associated with the virtual machine.
* `tags` - (Optional) A mapping of tags to assign to the resource.

For more information on the different example configurations, please check out the [azure documentation](https://msdn.microsoft.com/en-us/library/mt163591.aspx#Anchor_2)

Expand Down

0 comments on commit d3939db

Please sign in to comment.