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: fixed domain name label validation #2122

Merged
merged 3 commits into from
Oct 21, 2018
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
14 changes: 14 additions & 0 deletions azurerm/helpers/validate/public_ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package validate

import (
"fmt"
"regexp"
)

func PublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-z][a-z0-9-]{1,61}[a-z0-9]$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%s must contain only lowercase alphanumeric characters, numbers and hyphens. It must start with a letter and end only with a number or letter", k))
}
return
}
41 changes: 41 additions & 0 deletions azurerm/helpers/validate/public_ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package validate

import (
"testing"
)

func TestPublicIpDomainNameLabel(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "tEsting123",
ErrCount: 1,
},
{
Value: "testing123!",
ErrCount: 1,
},
{
Value: "testing123-",
ErrCount: 1,
},
{
Value: "k2345678-1-2345678-2-2345678-3-2345678-4-2345678-5-2345678-6-23",
ErrCount: 0,
},
{
Value: "k2345678-1-2345678-2-2345678-3-2345678-4-2345678-5-2345678-6-234",
ErrCount: 1,
},
}

for _, tc := range cases {
_, errors := PublicIpDomainNameLabel(tc.Value, "azurerm_public_ip")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM Public IP Domain Name Label %s to trigger a validation error", tc.Value)
}
}
}
29 changes: 2 additions & 27 deletions azurerm/resource_arm_public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package azurerm

import (
"fmt"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"log"
"regexp"
"strings"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
Expand Down Expand Up @@ -95,7 +95,7 @@ func resourceArmPublicIp() *schema.Resource {
"domain_name_label": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validatePublicIpDomainNameLabel,
ValidateFunc: validate.PublicIpDomainNameLabel,
},

"reverse_fqdn": {
Expand Down Expand Up @@ -277,28 +277,3 @@ func resourceArmPublicIpDelete(d *schema.ResourceData, meta interface{}) error {

return nil
}

func validatePublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-z0-9-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q: %q",
k, value))
}

if len(value) > 61 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 61 characters: %q", k, value))
}

if len(value) == 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be an empty string: %q", k, value))
}
if regexp.MustCompile(`-$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot end with a hyphen: %q", k, value))
}

return
}
76 changes: 44 additions & 32 deletions azurerm/resource_arm_public_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,6 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestResourceAzureRMPublicIpDomainNameLabel_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "tEsting123",
ErrCount: 1,
},
{
Value: "testing123!",
ErrCount: 1,
},
{
Value: "testing123-",
ErrCount: 1,
},
{
Value: acctest.RandString(80),
ErrCount: 1,
},
}

for _, tc := range cases {
_, errors := validatePublicIpDomainNameLabel(tc.Value, "azurerm_public_ip")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected the Azure RM Public IP Domain Name Label to trigger a validation error")
}
}
}

func TestAccAzureRMPublicIpStatic_basic(t *testing.T) {
resourceName := "azurerm_public_ip.test"
ri := acctest.RandInt()
Expand Down Expand Up @@ -414,6 +382,32 @@ func TestAccAzureRMPublicIpStatic_importIdError(t *testing.T) {
})
}

func TestAccAzureRMPublicIpStatic_canLabelBe63(t *testing.T) {
resourceName := "azurerm_public_ip.test"
ri := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMPublicIpDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMPublicIPStatic_canLabelBe63(ri, testLocation()),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPublicIpExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
resource.TestCheckResourceAttr(resourceName, "public_ip_address_allocation", "static"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure we have enough information in state to look up in API
Expand Down Expand Up @@ -714,3 +708,21 @@ resource "azurerm_public_ip" "test" {
}
`, rInt, location, rInt)
}

func testAccAzureRMPublicIPStatic_canLabelBe63(rInt int, location 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 = "static"
domain_name_label = "k2345678-1-2345678-2-2345678-3-2345678-4-2345678-5-2345678-6-23"
}
`, rInt, location, rInt)
}