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

Fix - MariaDB firewall rule name restrictions does not match official #10579

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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package mariadb
import (
"fmt"
"log"
"regexp"
"time"

"github.com/Azure/azure-sdk-for-go/services/mariadb/mgmt/2018-06-01/mariadb"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
azValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
Expand Down Expand Up @@ -37,13 +35,10 @@ func resourceArmMariaDBFirewallRule() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[-a-z0-9]{1,128}$"),
"name must be 1-128 characters long and contain only letters, numbers and hyphens",
),
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.FirewallRuleName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ resource "azurerm_mariadb_server" "test" {
}

resource "azurerm_mariadb_firewall_rule" "test" {
name = "acctestfwrule-%d"
name = "acctestFWRule_01-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_mariadb_server.test.name}"
start_ip_address = "0.0.0.0"
Expand Down
25 changes: 25 additions & 0 deletions azurerm/internal/services/mariadb/validate/firewall_rule_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package validate

import (
"fmt"
"regexp"
)

func FirewallRuleName(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
}

// The name attribute rules are :
// 1. can contain only letters, numbers, underscore and hyphen.
// 2. The value must be between 1 and 128 characters long

if !regexp.MustCompile(`^[a-zA-Z\d-_]{1,128}$`).MatchString(v) {
errors = append(errors, fmt.Errorf("%s can contain only letters, numbers, underscore and hyphen, and be between 1 and 128 characters long", k))
return
}

return warnings, errors
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package validate

import (
"testing"
)

func TestFirewallRuleName(t *testing.T) {
testData := []struct {
input string
expected bool
}{
{
// empty
input: "",
expected: false,
},
{
// basic example
input: "abc123",
expected: true,
},
{
// can contain underscore
input: "aBc_123",
expected: true,
},
{
// can contain hyphen
input: "ab-c",
expected: true,
},
{
// can't contain `*`
input: "abcon*demand",
expected: false,
},
{
// 128 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef",
expected: true,
},
{
// 129 chars
input: "abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefg",
expected: false,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q..", v.input)

_, errors := FirewallRuleName(v.input, "name")
actual := len(errors) == 0
if v.expected != actual {
t.Fatalf("Expected %t but got %t", v.expected, actual)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ func FirewallRuleName(i interface{}, k string) (warnings []string, errors []erro
}

// The name attribute rules are :
// 1. can contain only letters, numbers, underscore and hythen.
// 1. can contain only letters, numbers, underscore and hyphen.
// 2. The value must be between 1 and 128 characters long

if !regexp.MustCompile(`^[a-zA-Z\d-_]{1,128}$`).MatchString(v) {
errors = append(errors, fmt.Errorf("%s can contain only letters, numbers, underscore and hythen, and be between 1 and 128 characters long", k))
errors = append(errors, fmt.Errorf("%s can contain only letters, numbers, underscore and hyphen, and be between 1 and 128 characters long", k))
return
}

Expand Down