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: ensure all fields have a value #2448

Merged
merged 2 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 11 additions & 18 deletions azurerm/data_source_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,21 @@ 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", "")
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
d.Set("idle_timeout_in_minutes", 0)

if props := resp.PublicIPAddressPropertiesFormat; props != nil {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
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)
}