Skip to content
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

azurerm_public_ip - Add support for ip_tags #11270

Merged
merged 4 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/public_ip_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func dataSourcePublicIPRead(d *schema.ResourceData, meta interface{}) error {
d.Set("ip_address", props.IPAddress)
d.Set("ip_version", string(props.PublicIPAddressVersion))
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)

iptags := flattenPublicIpPropsIpTags(*props.IPTags)
if iptags != nil {
d.Set("ip_tags", iptags)
}
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestAccDataSourcePublicIP_static(t *testing.T) {
check.That(data.ResourceName).Key("ip_version").HasValue("IPv4"),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.environment").HasValue("test"),
check.That(data.ResourceName).Key("ip_tags.RoutingPreference").HasValue("Internet"),
),
},
})
Expand Down Expand Up @@ -79,6 +80,11 @@ resource "azurerm_public_ip" "test" {
allocation_method = "Static"
domain_name_label = "acctest-%d"
idle_timeout_in_minutes = 30
sku = "Standard"

ip_tags = {
RoutingPreference = "Internet"
}

tags = {
environment = "test"
Expand Down
40 changes: 40 additions & 0 deletions azurerm/internal/services/network/public_ip_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func resourcePublicIp() *schema.Resource {
ValidateFunc: azure.ValidateResourceID,
},

"ip_tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"zones": azure.SchemaSingleZone(),

"tags": tags.Schema(),
Expand Down Expand Up @@ -209,6 +218,21 @@ func resourcePublicIpCreateUpdate(d *schema.ResourceData, meta interface{}) erro
publicIp.PublicIPAddressPropertiesFormat.DNSSettings = &dnsSettings
}

if v, ok := d.GetOk("ip_tags"); ok {
ipTags := v.(map[string]interface{})
newIpTags := []network.IPTag{}

for key, val := range ipTags {
ipTag := network.IPTag{
IPTagType: utils.String(key),
Tag: utils.String(val.(string)),
}
newIpTags = append(newIpTags, ipTag)
}

publicIp.PublicIPAddressPropertiesFormat.IPTags = &newIpTags
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.Name, publicIp)
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
Expand Down Expand Up @@ -267,6 +291,11 @@ func resourcePublicIpRead(d *schema.ResourceData, meta interface{}) error {
d.Set("domain_name_label", settings.DomainNameLabel)
}

iptags := flattenPublicIpPropsIpTags(*props.IPTags)
if iptags != nil {
d.Set("ip_tags", iptags)
}

d.Set("ip_address", props.IPAddress)
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)
}
Expand Down Expand Up @@ -295,3 +324,14 @@ func resourcePublicIpDelete(d *schema.ResourceData, meta interface{}) error {

return nil
}

func flattenPublicIpPropsIpTags(ipTags []network.IPTag) map[string]interface{} {
mapIpTags := make(map[string]interface{})

for _, tag := range ipTags {
if tag.IPTagType != nil {
mapIpTags[*tag.IPTagType] = tag.Tag
}
}
return mapIpTags
}
41 changes: 41 additions & 0 deletions azurerm/internal/services/network/public_ip_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,22 @@ func TestAccPublicIpStatic_canLabelBe63(t *testing.T) {
})
}

func TestAccPublicIpStatic_ipTags(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_public_ip", "test")
r := PublicIPResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.standard_IpTags(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("ip_tags.RoutingPreference").HasValue("Internet"),
),
},
data.ImportStep(),
})
}

func (t PublicIPResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.PublicIpAddressID(state.ID)
if err != nil {
Expand Down Expand Up @@ -760,3 +776,28 @@ resource "azurerm_public_ip" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (PublicIPResource) standard_IpTags(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-network-%d"
location = "%s"
}

resource "azurerm_public_ip" "test" {
name = "acctestpublicip-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
allocation_method = "Static"
sku = "Standard"

ip_tags = {
RoutingPreference = "Internet"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}
1 change: 1 addition & 0 deletions website/docs/d/public_ip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ output "public_ip_address" {
* `ip_address` - The IP address value that was allocated.
* `ip_version` - The IP version being used, for example `IPv4` or `IPv6`.
* `sku` - The SKU of the Public IP.
* `ip_tags` - A mapping of tags to assigned to the resource.
* `tags` - A mapping of tags to assigned to the resource.

## Timeouts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ Managements can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_management_group_subscription_association.example /managementGroup/MyManagementGroup/subscription/12345678-1234-1234-1234-123456789012
```
```
4 changes: 4 additions & 0 deletions website/docs/r/public_ip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ The following arguments are supported:

* `public_ip_prefix_id` - (Optional) If specified then public IP address allocated will be provided from the public IP prefix resource.

* `ip_tags` - (Optional) A mapping of IP tags to assign to the public IP.

-> **Note** IP Tag `RoutingPreference` requires multiple `zones` and `Standard` SKU to be set.

* `tags` - (Optional) A mapping of tags to assign to the resource.

* `zones` - (Optional) A collection containing the availability zone to allocate the Public IP in.
Expand Down