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

postgres - add server name validation #5443

Merged
merged 3 commits into from
Jan 21, 2020
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 @@ -40,9 +40,10 @@ func resourceArmPostgreSQLConfiguration() *schema.Resource {
"resource_group_name": azure.SchemaResourceGroupName(),

"server_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidatePSQLServerName,
},

"value": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func resourceArmPostgreSQLDatabase() *schema.Resource {
"resource_group_name": azure.SchemaResourceGroupName(),

"server_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidatePSQLServerName,
},

"charset": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func resourceArmPostgreSQLFirewallRule() *schema.Resource {
"resource_group_name": azure.SchemaResourceGroupName(),

"server_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidatePSQLServerName,
},

"start_ip_address": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func ValidatePSQLServerName(i interface{}, k string) (_ []string, errors []error) {
if m, regexErrs := validate.RegExHelper(i, k, `^[0-9a-z]{2}[-0-9a-z]{0,60}[0-9a-z]$`); !m {
errors = append(regexErrs, fmt.Errorf("%q can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 63 characters.", k))
}

return nil, errors
}

func resourceArmPostgreSQLServer() *schema.Resource {
return &schema.Resource{
Create: resourceArmPostgreSQLServerCreate,
Expand All @@ -41,9 +49,10 @@ func resourceArmPostgreSQLServer() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidatePSQLServerName,
},

"location": azure.SchemaLocation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func resourceArmPostgreSQLVirtualNetworkRule() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.NoEmptyStrings,
ValidateFunc: ValidatePSQLServerName,
},

"subnet_id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ func TestAccDataSourceAzureRMPPostgreSqlServer_basic(t *testing.T) {
}

func testAccDataSourceAzureRMPostgreSqlServer_basic(data acceptance.TestData, version string) string {
template := testAccAzureRMPostgreSQLServer_basic(data, version)
return fmt.Sprintf(`
%s

data "azurerm_postgresql_server" "test" {
name = "${azurerm_postgresql_server.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, template)
`, testAccAzureRMPostgreSQLServer_basic(data, version))
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func testCheckAzureRMPostgreSQLConfigurationValueReset(rInt int, configurationNa
client := acceptance.AzureProvider.Meta().(*clients.Client).Postgres.ConfigurationsClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext

resourceGroup := fmt.Sprintf("acctestRG-%d", rInt)
serverName := fmt.Sprintf("acctestpsqlsvr-%d", rInt)
resourceGroup := fmt.Sprintf("acctestRG-psql-%d", rInt)
serverName := fmt.Sprintf("acctest-psql-server-%d", rInt)

resp, err := client.Get(ctx, resourceGroup, serverName, configurationName)
if err != nil {
Expand Down Expand Up @@ -188,7 +188,6 @@ func testAccAzureRMPostgreSQLConfiguration_deadlockTimeout(data acceptance.TestD
}

func testAccAzureRMPostgreSQLConfiguration_template(data acceptance.TestData, name string, value string) string {
server := testAccAzureRMPostgreSQLConfiguration_empty(data)
return fmt.Sprintf(`
%s

Expand All @@ -198,18 +197,18 @@ resource "azurerm_postgresql_configuration" "test" {
server_name = "${azurerm_postgresql_server.test.name}"
value = "%s"
}
`, server, name, value)
`, testAccAzureRMPostgreSQLConfiguration_empty(data), name, value)
}

func testAccAzureRMPostgreSQLConfiguration_empty(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ func testCheckAzureRMPostgreSQLDatabaseDestroy(s *terraform.State) error {
func testAccAzureRMPostgreSQLDatabase_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -199,7 +199,7 @@ resource "azurerm_postgresql_server" "test" {
}

resource "azurerm_postgresql_database" "test" {
name = "acctestdb_%d"
name = "acctest_PSQL_db_%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_postgresql_server.test.name}"
charset = "UTF8"
Expand All @@ -226,12 +226,12 @@ resource "azurerm_postgresql_database" "import" {
func testAccAzureRMPostgreSQLDatabase_collationWithHyphen(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -250,7 +250,7 @@ resource "azurerm_postgresql_server" "test" {
}

resource "azurerm_postgresql_database" "test" {
name = "acctestdb_%d"
name = "acctest_PSQL_db_%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_postgresql_server.test.name}"
charset = "UTF8"
Expand All @@ -262,12 +262,12 @@ resource "azurerm_postgresql_database" "test" {
func testAccAzureRMPostgreSQLDatabase_charsetLowercase(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -286,7 +286,7 @@ resource "azurerm_postgresql_server" "test" {
}

resource "azurerm_postgresql_database" "test" {
name = "acctestdb_%d"
name = "acctest_PSQL_db_%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_postgresql_server.test.name}"
charset = "utf8"
Expand All @@ -298,12 +298,12 @@ resource "azurerm_postgresql_database" "test" {
func testAccAzureRMPostgreSQLDatabase_charsetMixedcase(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -322,7 +322,7 @@ resource "azurerm_postgresql_server" "test" {
}

resource "azurerm_postgresql_database" "test" {
name = "acctestdb_%d"
name = "acctest_PSQL_db_%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_postgresql_server.test.name}"
charset = "Utf8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ func testCheckAzureRMPostgreSQLFirewallRuleDestroy(s *terraform.State) error {
func testAccAzureRMPostgreSQLFirewallRule_basic(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -144,7 +144,7 @@ resource "azurerm_postgresql_server" "test" {
}

resource "azurerm_postgresql_firewall_rule" "test" {
name = "acctestfwrule-%d"
name = "acctest-PSQL-fwrule-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
server_name = "${azurerm_postgresql_server.test.name}"
start_ip_address = "0.0.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ func testCheckAzureRMPostgreSQLServerDestroy(s *terraform.State) error {
func testAccAzureRMPostgreSQLServer_basic(data acceptance.TestData, version string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -384,12 +384,12 @@ resource "azurerm_postgresql_server" "test" {
func testAccAzureRMPostgreSQLServer_basicNinePointFiveOldSku(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand Down Expand Up @@ -455,12 +455,12 @@ resource "azurerm_postgresql_server" "import" {
func testAccAzureRMPostgreSQLServer_basicNinePointSixUpdatedPassword(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -483,12 +483,12 @@ resource "azurerm_postgresql_server" "test" {
func testAccAzureRMPostgreSQLServer_basicNinePointSixUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -511,12 +511,12 @@ resource "azurerm_postgresql_server" "test" {
func testAccAzureRMPostgreSQLServer_basicMaxStorage(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -540,12 +540,12 @@ resource "azurerm_postgresql_server" "test" {
func testAccAzureRMPostgreSQLServer_generalPurpose(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -568,12 +568,12 @@ resource "azurerm_postgresql_server" "test" {
func testAccAzureRMPostgreSQLServer_memoryOptimized(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand All @@ -596,12 +596,12 @@ resource "azurerm_postgresql_server" "test" {
func testAccAzureRMPostgreSQLServer_memoryOptimizedGeoRedundant(data acceptance.TestData) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
name = "acctestRG-psql-%d"
location = "%s"
}

resource "azurerm_postgresql_server" "test" {
name = "acctestpsqlsvr-%d"
name = "acctest-psql-server-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

Expand Down
Loading