Skip to content

Commit

Permalink
Merge pull request #2448 from terraform-providers/publicip/datasource
Browse files Browse the repository at this point in the history
azurerm_public_ip: ensure all fields have a value
  • Loading branch information
katbyte committed Dec 7, 2018
2 parents b5889d2 + f196a63 commit f454afa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 23 deletions.
30 changes: 12 additions & 18 deletions azurerm/data_source_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,22 @@ func dataSourceArmPublicIPRead(d *schema.ResourceData, meta interface{}) error {

d.SetId(*resp.ID)

//ensure values are at least set to "", d.Set() is a noop on a nil
d.Set("fqdn", "")
d.Set("domain_name_label", "")
d.Set("ip_address", "")
d.Set("ip_version", "")
d.Set("idle_timeout_in_minutes", 0)

if props := resp.PublicIPAddressPropertiesFormat; props != nil {
if dnsSettings := props.DNSSettings; dnsSettings != nil {
if v := dnsSettings.Fqdn; v != nil && *v != "" {
d.Set("fqdn", v)
}

if v := dnsSettings.DomainNameLabel; v != nil && *v != "" {
d.Set("domain_name_label", v)
}
}

if ipVersion := props.PublicIPAddressVersion; string(ipVersion) != "" {
d.Set("ip_version", string(ipVersion))
d.Set("fqdn", dnsSettings.Fqdn)
d.Set("domain_name_label", dnsSettings.DomainNameLabel)
}

if v := props.IPAddress; v != nil && *v != "" {
d.Set("ip_address", v)
}

if v := props.IdleTimeoutInMinutes; v != nil {
d.Set("idle_timeout_in_minutes", *resp.PublicIPAddressPropertiesFormat.IdleTimeoutInMinutes)
}
d.Set("ip_address", props.IPAddress)
d.Set("ip_version", string(props.PublicIPAddressVersion))
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)
}

flattenAndSetTags(d, resp.Tags)
Expand Down
64 changes: 59 additions & 5 deletions azurerm/data_source_public_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ import (
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMPublicIP_basic(t *testing.T) {
func TestAccDataSourceAzureRMPublicIP_static(t *testing.T) {
dataSourceName := "data.azurerm_public_ip.test"
ri := acctest.RandInt()

name := fmt.Sprintf("acctestpublicip-%d", ri)
resourceGroupName := fmt.Sprintf("acctestRG-%d", ri)

config := testAccDataSourceAzureRMPublicIPBasic(name, resourceGroupName, ri, testLocation())

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: testAccDataSourceAzureRMPublicIP_static(name, resourceGroupName, ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", name),
resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName),
Expand All @@ -40,7 +38,36 @@ func TestAccDataSourceAzureRMPublicIP_basic(t *testing.T) {
})
}

func testAccDataSourceAzureRMPublicIPBasic(name string, resourceGroupName string, rInt int, location string) string {
func TestAccDataSourceAzureRMPublicIP_dynamic(t *testing.T) {
dataSourceName := "data.azurerm_public_ip.test"
ri := acctest.RandInt()

name := fmt.Sprintf("acctestpublicip-%d", ri)
resourceGroupName := fmt.Sprintf("acctestRG-%d", ri)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAzureRMPublicIP_dynamic(ri, testLocation(), "Ipv4"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "name", name),
resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName),
resource.TestCheckResourceAttr(dataSourceName, "domain_name_label", ""),
resource.TestCheckResourceAttr(dataSourceName, "fqdn", ""),
resource.TestCheckResourceAttr(dataSourceName, "ip_address", ""),
resource.TestCheckResourceAttr(dataSourceName, "ip_version", "IPv4"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.environment", "test"),
),
},
},
})
}

func testAccDataSourceAzureRMPublicIP_static(name string, resourceGroupName string, rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "%s"
Expand All @@ -66,3 +93,30 @@ data "azurerm_public_ip" "test" {
}
`, resourceGroupName, location, name, rInt)
}

func testAccDataSourceAzureRMPublicIP_dynamic(rInt int, location string, ipVersion string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%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}"
public_ip_address_allocation = "dynamic"
ip_version = "%s"
tags {
environment = "test"
}
}
data "azurerm_public_ip" "test" {
name = "${azurerm_public_ip.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt, ipVersion)
}

0 comments on commit f454afa

Please sign in to comment.