Merge pull request #4722 from stack72/azurerm-resource-tagging

[WIP] provider/azurerm: tagging of resources
This commit is contained in:
James Nugent 2016-01-18 16:15:52 -05:00
commit 7897e08342
12 changed files with 402 additions and 0 deletions

View File

@ -139,6 +139,8 @@ func resourceArmNetworkInterface() *schema.Resource {
Optional: true, Optional: true,
Computed: true, Computed: true,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -152,6 +154,7 @@ func resourceArmNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
name := d.Get("name").(string) name := d.Get("name").(string)
location := d.Get("location").(string) location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string) resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
properties := network.InterfacePropertiesFormat{} properties := network.InterfacePropertiesFormat{}
@ -198,6 +201,7 @@ func resourceArmNetworkInterfaceCreate(d *schema.ResourceData, meta interface{})
Name: &name, Name: &name,
Location: &location, Location: &location,
Properties: &properties, Properties: &properties,
Tags: expandTags(tags),
} }
resp, err := ifaceClient.CreateOrUpdate(resGroup, name, iface) resp, err := ifaceClient.CreateOrUpdate(resGroup, name, iface)
@ -271,6 +275,8 @@ func resourceArmNetworkInterfaceRead(d *schema.ResourceData, meta interface{}) e
} }
} }
flattenAndSetTags(d, resp.Tags)
return nil return nil
} }

View File

@ -26,6 +26,40 @@ func TestAccAzureRMNetworkInterface_basic(t *testing.T) {
}) })
} }
func TestAccAzureRMNetworkInterface_withTags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkInterfaceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMNetworkInterface_withTags,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
resource.TestCheckResourceAttr(
"azurerm_network_interface.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_network_interface.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_network_interface.test", "tags.cost_center", "MSFT"),
),
},
resource.TestStep{
Config: testAccAzureRMNetworkInterface_withTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkInterfaceExists("azurerm_network_interface.test"),
resource.TestCheckResourceAttr(
"azurerm_network_interface.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_network_interface.test", "tags.environment", "staging"),
),
},
},
})
}
///TODO: Re-enable this test when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed ///TODO: Re-enable this test when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed
//func TestAccAzureRMNetworkInterface_addingIpConfigurations(t *testing.T) { //func TestAccAzureRMNetworkInterface_addingIpConfigurations(t *testing.T) {
// //
@ -142,6 +176,81 @@ resource "azurerm_network_interface" "test" {
} }
` `
var testAccAzureRMNetworkInterface_withTags = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_virtual_network" "test" {
name = "acceptanceTestVirtualNetwork1"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "testsubnet"
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 = "acceptanceTestNetworkInterface1"
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"
}
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMNetworkInterface_withTagsUpdate = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_virtual_network" "test" {
name = "acceptanceTestVirtualNetwork1"
address_space = ["10.0.0.0/16"]
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
name = "testsubnet"
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 = "acceptanceTestNetworkInterface1"
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"
}
tags {
environment = "staging"
}
}
`
//TODO: Re-enable this test when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed //TODO: Re-enable this test when https://github.com/Azure/azure-sdk-for-go/issues/259 is fixed
//var testAccAzureRMNetworkInterface_extraIpConfiguration = ` //var testAccAzureRMNetworkInterface_extraIpConfiguration = `
//resource "azurerm_resource_group" "test" { //resource "azurerm_resource_group" "test" {

View File

@ -118,6 +118,8 @@ func resourceArmNetworkSecurityGroup() *schema.Resource {
}, },
Set: resourceArmNetworkSecurityGroupRuleHash, Set: resourceArmNetworkSecurityGroupRuleHash,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -129,6 +131,7 @@ func resourceArmNetworkSecurityGroupCreate(d *schema.ResourceData, meta interfac
name := d.Get("name").(string) name := d.Get("name").(string)
location := d.Get("location").(string) location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string) resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
sgRules, sgErr := expandAzureRmSecurityRules(d) sgRules, sgErr := expandAzureRmSecurityRules(d)
if sgErr != nil { if sgErr != nil {
@ -141,6 +144,7 @@ func resourceArmNetworkSecurityGroupCreate(d *schema.ResourceData, meta interfac
Properties: &network.SecurityGroupPropertiesFormat{ Properties: &network.SecurityGroupPropertiesFormat{
SecurityRules: &sgRules, SecurityRules: &sgRules,
}, },
Tags: expandTags(tags),
} }
resp, err := secClient.CreateOrUpdate(resGroup, name, sg) resp, err := secClient.CreateOrUpdate(resGroup, name, sg)
@ -187,6 +191,8 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{
d.Set("security_rule", flattenNetworkSecurityRules(resp.Properties.SecurityRules)) d.Set("security_rule", flattenNetworkSecurityRules(resp.Properties.SecurityRules))
} }
flattenAndSetTags(d, resp.Tags)
return nil return nil
} }

View File

@ -26,6 +26,40 @@ func TestAccAzureRMNetworkSecurityGroup_basic(t *testing.T) {
}) })
} }
func TestAccAzureRMNetworkSecurityGroup_withTags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkSecurityGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMNetworkSecurityGroup_withTags,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
resource.TestCheckResourceAttr(
"azurerm_network_security_group.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_network_security_group.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_network_security_group.test", "tags.cost_center", "MSFT"),
),
},
resource.TestStep{
Config: testAccAzureRMNetworkSecurityGroup_withTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMNetworkSecurityGroupExists("azurerm_network_security_group.test"),
resource.TestCheckResourceAttr(
"azurerm_network_security_group.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_network_security_group.test", "tags.environment", "staging"),
),
},
},
})
}
func TestAccAzureRMNetworkSecurityGroup_addingExtraRules(t *testing.T) { func TestAccAzureRMNetworkSecurityGroup_addingExtraRules(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -169,3 +203,63 @@ resource "azurerm_network_security_group" "test" {
} }
} }
` `
var testAccAzureRMNetworkSecurityGroup_withTags = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_network_security_group" "test" {
name = "acceptanceTestSecurityGroup1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
security_rule {
name = "test123"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMNetworkSecurityGroup_withTagsUpdate = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_network_security_group" "test" {
name = "acceptanceTestSecurityGroup1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
security_rule {
name = "test123"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "*"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags {
environment = "staging"
}
}
`

View File

@ -79,6 +79,8 @@ func resourceArmPublicIp() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -92,6 +94,7 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string) name := d.Get("name").(string)
location := d.Get("location").(string) location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string) resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
properties := network.PublicIPAddressPropertiesFormat{ properties := network.PublicIPAddressPropertiesFormat{
PublicIPAllocationMethod: network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)), PublicIPAllocationMethod: network.IPAllocationMethod(d.Get("public_ip_address_allocation").(string)),
@ -126,6 +129,7 @@ func resourceArmPublicIpCreate(d *schema.ResourceData, meta interface{}) error {
Name: &name, Name: &name,
Location: &location, Location: &location,
Properties: &properties, Properties: &properties,
Tags: expandTags(tags),
} }
resp, err := publicIPClient.CreateOrUpdate(resGroup, name, publicIp) resp, err := publicIPClient.CreateOrUpdate(resGroup, name, publicIp)
@ -176,6 +180,8 @@ func resourceArmPublicIpRead(d *schema.ResourceData, meta interface{}) error {
d.Set("ip_address", resp.Properties.IPAddress) d.Set("ip_address", resp.Properties.IPAddress)
} }
flattenAndSetTags(d, resp.Tags)
return nil return nil
} }

View File

@ -96,6 +96,40 @@ func TestAccAzureRMPublicIpStatic_basic(t *testing.T) {
}) })
} }
func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMVPublicIpStatic_withTags,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.cost_center", "MSFT"),
),
},
resource.TestStep{
Config: testAccAzureRMVPublicIpStatic_withTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists("azurerm_public_ip.test"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_public_ip.test", "tags.environment", "staging"),
),
},
},
})
}
func TestAccAzureRMPublicIpStatic_update(t *testing.T) { func TestAccAzureRMPublicIpStatic_update(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -242,3 +276,38 @@ resource "azurerm_public_ip" "test" {
public_ip_address_allocation = "dynamic" public_ip_address_allocation = "dynamic"
} }
` `
var testAccAzureRMVPublicIpStatic_withTags = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_public_ip" "test" {
name = "acceptanceTestPublicIp1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
public_ip_address_allocation = "static"
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMVPublicIpStatic_withTagsUpdate = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_public_ip" "test" {
name = "acceptanceTestPublicIp1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
public_ip_address_allocation = "static"
tags {
environment = "staging"
}
}
`

View File

@ -80,6 +80,8 @@ func resourceArmRouteTable() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString, Set: schema.HashString,
}, },
"tags": tagsSchema(),
}, },
} }
} }
@ -93,10 +95,12 @@ func resourceArmRouteTableCreate(d *schema.ResourceData, meta interface{}) error
name := d.Get("name").(string) name := d.Get("name").(string)
location := d.Get("location").(string) location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string) resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
routeSet := network.RouteTable{ routeSet := network.RouteTable{
Name: &name, Name: &name,
Location: &location, Location: &location,
Tags: expandTags(tags),
} }
if _, ok := d.GetOk("route"); ok { if _, ok := d.GetOk("route"); ok {
@ -165,6 +169,8 @@ func resourceArmRouteTableRead(d *schema.ResourceData, meta interface{}) error {
} }
} }
flattenAndSetTags(d, resp.Tags)
return nil return nil
} }

View File

@ -74,6 +74,40 @@ func TestAccAzureRMRouteTable_basic(t *testing.T) {
}) })
} }
func TestAccAzureRMRouteTable_withTags(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMRouteTableDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMRouteTable_withTags,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
resource.TestCheckResourceAttr(
"azurerm_route_table.test", "tags.#", "2"),
resource.TestCheckResourceAttr(
"azurerm_route_table.test", "tags.environment", "Production"),
resource.TestCheckResourceAttr(
"azurerm_route_table.test", "tags.cost_center", "MSFT"),
),
},
resource.TestStep{
Config: testAccAzureRMRouteTable_withTagsUpdate,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMRouteTableExists("azurerm_route_table.test"),
resource.TestCheckResourceAttr(
"azurerm_route_table.test", "tags.#", "1"),
resource.TestCheckResourceAttr(
"azurerm_route_table.test", "tags.environment", "staging"),
),
},
},
})
}
func TestAccAzureRMRouteTable_multipleRoutes(t *testing.T) { func TestAccAzureRMRouteTable_multipleRoutes(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
@ -199,3 +233,50 @@ resource "azurerm_route_table" "test" {
} }
} }
` `
var testAccAzureRMRouteTable_withTags = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_route_table" "test" {
name = "acceptanceTestSecurityGroup1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
route {
name = "route1"
address_prefix = "*"
next_hop_type = "internet"
}
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMRouteTable_withTagsUpdate = `
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_route_table" "test" {
name = "acceptanceTestSecurityGroup1"
location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}"
route {
name = "route1"
address_prefix = "*"
next_hop_type = "internet"
}
tags {
environment = "staging"
}
}
`

View File

@ -34,6 +34,10 @@ resource "azurerm_virtual_network" "test" {
name = "subnet3" name = "subnet3"
address_prefix = "10.0.3.0/24" address_prefix = "10.0.3.0/24"
} }
tags {
environment = "Production"
}
} }
``` ```
@ -59,6 +63,8 @@ The following arguments are supported:
* `ip_configuration` - (Optional) Collection of ipConfigurations associated with this NIC. Each `ip_configuration` block supports fields documented below. * `ip_configuration` - (Optional) Collection of ipConfigurations associated with this NIC. Each `ip_configuration` block supports fields documented below.
* `tags` - (Optional) A mapping of tags to assign to the resource.
The `ip_configuration` block supports: The `ip_configuration` block supports:
* `name` - (Required) User-defined name of the IP. * `name` - (Required) User-defined name of the IP.

View File

@ -34,7 +34,12 @@ resource "azurerm_network_security_group" "test" {
source_address_prefix = "*" source_address_prefix = "*"
destination_address_prefix = "*" destination_address_prefix = "*"
} }
tags {
environment = "Production"
}
} }
``` ```
## Argument Reference ## Argument Reference
@ -52,6 +57,8 @@ The following arguments are supported:
* `security_rule` - (Optional) Can be specified multiple times to define multiple * `security_rule` - (Optional) Can be specified multiple times to define multiple
security rules. Each `security_rule` block supports fields documented below. security rules. Each `security_rule` block supports fields documented below.
* `tags` - (Optional) A mapping of tags to assign to the resource.
The `security_rule` block supports: The `security_rule` block supports:

View File

@ -23,6 +23,10 @@ resource "azurerm_public_ip" "test" {
location = "West US" location = "West US"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
public_ip_address_allocation = "static" public_ip_address_allocation = "static"
tags {
environment = "Production"
}
} }
``` ```
@ -46,6 +50,8 @@ The following arguments are supported:
* `reverse_fqdn` - (Optional) A fully qualified domain name that resolves to this public IP address. If the reverseFqdn is specified, then a PTR DNS record is created pointing from the IP address in the in-addr.arpa domain to the reverse FQDN. * `reverse_fqdn` - (Optional) A fully qualified domain name that resolves to this public IP address. If the reverseFqdn is specified, then a PTR DNS record is created pointing from the IP address in the in-addr.arpa domain to the reverse FQDN.
* `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference ## Attributes Reference
The following attributes are exported: The following attributes are exported:

View File

@ -28,6 +28,10 @@ resource "azurerm_route_table" "test" {
address_prefix = "*" address_prefix = "*"
next_hop_type = "internet" next_hop_type = "internet"
} }
tags {
environment = "Production"
}
} }
``` ```
@ -46,6 +50,8 @@ The following arguments are supported:
* `route` - (Optional) Can be specified multiple times to define multiple * `route` - (Optional) Can be specified multiple times to define multiple
routes. Each `route` block supports fields documented below. routes. Each `route` block supports fields documented below.
* `tags` - (Optional) A mapping of tags to assign to the resource.
The `route` block supports: The `route` block supports:
* `name` - (Required) The name of the route. * `name` - (Required) The name of the route.