From 3fc6838b6240540b803a624636f3ec6c9707fbe3 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 11 Sep 2017 17:38:37 -0600 Subject: [PATCH 1/5] Add validation to sql server name --- azurerm/resource_arm_sql_server.go | 24 ++++++++++++++--- azurerm/resource_arm_sql_server_test.go | 36 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_sql_server.go b/azurerm/resource_arm_sql_server.go index e35a4719f6ea..ab761e5f21a9 100644 --- a/azurerm/resource_arm_sql_server.go +++ b/azurerm/resource_arm_sql_server.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "regexp" "github.com/Azure/azure-sdk-for-go/arm/sql" "github.com/hashicorp/terraform/helper/schema" @@ -22,9 +23,10 @@ func resourceArmSqlServer() *schema.Resource { Schema: map[string]*schema.Schema{ "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateAzureRmSQLDBAccountName, }, "location": locationSchema(), @@ -168,3 +170,19 @@ func resourceArmSqlServerDelete(d *schema.ResourceData, meta interface{}) error return nil } + +func validateAzureRmSQLDBAccountName(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + + r, _ := regexp.Compile("^[a-z0-9\\-]+$") + if !r.MatchString(value) { + errors = append(errors, fmt.Errorf("SQLDB Account Name can only contain lower-case characters, numbers and the `-` character.")) + } + + length := len(value) + if length > 50 || 3 > length { + errors = append(errors, fmt.Errorf("SQLDB Account Name can only be between 3 and 50 seconds.")) + } + + return +} diff --git a/azurerm/resource_arm_sql_server_test.go b/azurerm/resource_arm_sql_server_test.go index a5c2085ade89..edb89c25c395 100644 --- a/azurerm/resource_arm_sql_server_test.go +++ b/azurerm/resource_arm_sql_server_test.go @@ -3,6 +3,7 @@ package azurerm import ( "fmt" "log" + "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -74,6 +75,23 @@ func TestAccAzureRMSqlServer_basic(t *testing.T) { }) } +func TestAccAzureRMSqlServer_upperCaseName(t *testing.T) { + ri := acctest.RandInt() + config := testAccAzureRMSqlServer_upperCaseName(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMSqlServerDestroy, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("SQLDB Account Name can only contain lower-case characters"), + }, + }, + }) +} + func TestAccAzureRMSqlServer_disappears(t *testing.T) { resourceName := "azurerm_sql_server.test" ri := acctest.RandInt() @@ -225,6 +243,24 @@ resource "azurerm_sql_server" "test" { `, rInt, location, rInt) } +func testAccAzureRMSqlServer_upperCaseName(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG_%d" + location = "%s" +} + +resource "azurerm_sql_server" "test" { + name = "AccTestSQLserver%d" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + version = "12.0" + administrator_login = "mradministrator" + administrator_login_password = "thisIsDog11" +} +`, rInt, location, rInt) +} + func testAccAzureRMSqlServer_withTags(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From 1a979a79df0300b6c6d1dce24a4f248168e32113 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 12 Sep 2017 09:45:05 -0600 Subject: [PATCH 2/5] Consolidate db name validators and add unit test --- azurerm/resource_arm_cosmos_db_account.go | 19 +------ .../resource_arm_cosmos_db_account_test.go | 41 -------------- azurerm/resource_arm_sql_server.go | 2 +- azurerm/resource_arm_sql_server_test.go | 18 ------- azurerm/validators.go | 17 ++++++ azurerm/validators_test.go | 53 ++++++++++++++++++- 6 files changed, 71 insertions(+), 79 deletions(-) diff --git a/azurerm/resource_arm_cosmos_db_account.go b/azurerm/resource_arm_cosmos_db_account.go index 08081a16484b..17d6959a6869 100644 --- a/azurerm/resource_arm_cosmos_db_account.go +++ b/azurerm/resource_arm_cosmos_db_account.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "net/http" - "regexp" "github.com/Azure/azure-sdk-for-go/arm/cosmos-db" "github.com/hashicorp/terraform/helper/hashcode" @@ -29,7 +28,7 @@ func resourceArmCosmosDBAccount() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validateAzureRmCosmosDBAccountName, + ValidateFunc: validateDBAccountName, }, "location": { @@ -410,19 +409,3 @@ func resourceAzureRMCosmosDBAccountFailoverPolicyHash(v interface{}) int { return hashcode.String(buf.String()) } - -func validateAzureRmCosmosDBAccountName(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - - r, _ := regexp.Compile("^[a-z0-9\\-]+$") - if !r.MatchString(value) { - errors = append(errors, fmt.Errorf("CosmosDB Account Name can only contain lower-case characters, numbers and the `-` character.")) - } - - length := len(value) - if length > 50 || 3 > length { - errors = append(errors, fmt.Errorf("CosmosDB Account Name can only be between 3 and 50 seconds.")) - } - - return -} diff --git a/azurerm/resource_arm_cosmos_db_account_test.go b/azurerm/resource_arm_cosmos_db_account_test.go index a4b3a902bd82..2da692c91a4b 100644 --- a/azurerm/resource_arm_cosmos_db_account_test.go +++ b/azurerm/resource_arm_cosmos_db_account_test.go @@ -60,47 +60,6 @@ func testSweepCosmosDBAccount(region string) error { return nil } -func TestAccAzureRMCosmosDBAccountName_validation(t *testing.T) { - str := acctest.RandString(50) - cases := []struct { - Value string - ErrCount int - }{ - { - Value: "ab", - ErrCount: 1, - }, - { - Value: "abc", - ErrCount: 0, - }, - { - Value: "cosmosDBAccount1", - ErrCount: 1, - }, - { - Value: "hello-world", - ErrCount: 0, - }, - { - Value: str, - ErrCount: 0, - }, - { - Value: str + "a", - ErrCount: 1, - }, - } - - for _, tc := range cases { - _, errors := validateAzureRmCosmosDBAccountName(tc.Value, "azurerm_cosmosdb_account") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AzureRM CosmosDB Name to trigger a validation error for '%s'", tc.Value) - } - } -} - func TestAccAzureRMCosmosDBAccount_boundedStaleness(t *testing.T) { resourceName := "azurerm_cosmosdb_account.test" ri := acctest.RandInt() diff --git a/azurerm/resource_arm_sql_server.go b/azurerm/resource_arm_sql_server.go index ab761e5f21a9..eaf66134fadc 100644 --- a/azurerm/resource_arm_sql_server.go +++ b/azurerm/resource_arm_sql_server.go @@ -26,7 +26,7 @@ func resourceArmSqlServer() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validateAzureRmSQLDBAccountName, + ValidateFunc: validateDBAccountName, }, "location": locationSchema(), diff --git a/azurerm/resource_arm_sql_server_test.go b/azurerm/resource_arm_sql_server_test.go index edb89c25c395..a868366cbf7a 100644 --- a/azurerm/resource_arm_sql_server_test.go +++ b/azurerm/resource_arm_sql_server_test.go @@ -3,7 +3,6 @@ package azurerm import ( "fmt" "log" - "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -75,23 +74,6 @@ func TestAccAzureRMSqlServer_basic(t *testing.T) { }) } -func TestAccAzureRMSqlServer_upperCaseName(t *testing.T) { - ri := acctest.RandInt() - config := testAccAzureRMSqlServer_upperCaseName(ri, testLocation()) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testCheckAzureRMSqlServerDestroy, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile("SQLDB Account Name can only contain lower-case characters"), - }, - }, - }) -} - func TestAccAzureRMSqlServer_disappears(t *testing.T) { resourceName := "azurerm_sql_server.test" ri := acctest.RandInt() diff --git a/azurerm/validators.go b/azurerm/validators.go index 88ae3116a08f..f224c8a4eb2b 100644 --- a/azurerm/validators.go +++ b/azurerm/validators.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "regexp" "time" "github.com/Azure/go-autorest/autorest/date" @@ -46,3 +47,19 @@ func validateUUID(v interface{}, k string) (ws []string, errors []error) { } return } + +func validateDBAccountName(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + + r, _ := regexp.Compile("^[a-z0-9\\-]+$") + if !r.MatchString(value) { + errors = append(errors, fmt.Errorf("Account Name can only contain lower-case characters, numbers and the `-` character.")) + } + + length := len(value) + if length > 50 || 3 > length { + errors = append(errors, fmt.Errorf("Account Name can only be between 3 and 50 seconds.")) + } + + return +} diff --git a/azurerm/validators_test.go b/azurerm/validators_test.go index 97f53f78df88..c803bbc85ac8 100644 --- a/azurerm/validators_test.go +++ b/azurerm/validators_test.go @@ -1,6 +1,10 @@ package azurerm -import "testing" +import ( + "testing" + + "github.com/hashicorp/terraform/helper/acctest" +) func TestValidateRFC3339Date(t *testing.T) { cases := []struct { @@ -85,3 +89,50 @@ func TestValidateIntInSlice(t *testing.T) { } } + +func TestDBAccountName_validation(t *testing.T) { + str := acctest.RandString(50) + cases := []struct { + Value string + ErrCount int + }{ + { + Value: "ab", + ErrCount: 1, + }, + { + Value: "abc", + ErrCount: 0, + }, + { + Value: "cosmosDBAccount1", + ErrCount: 1, + }, + { + Value: "hello-world", + ErrCount: 0, + }, + { + Value: str, + ErrCount: 0, + }, + { + Value: str + "a", + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateDBAccountName(tc.Value, "azurerm_cosmosdb_account") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the AzureRM CosmosDB Name to trigger a validation error for '%s'", tc.Value) + } + + _, errors = validateDBAccountName(tc.Value, "azurerm_sql_server") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected the AzureRM SQL Server Name to trigger a validation error for '%s'", tc.Value) + } + } +} From 75086429a06b6bbf1b66834d7966f96f9f7af064 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 12 Sep 2017 09:51:25 -0600 Subject: [PATCH 3/5] remove old validator code --- azurerm/resource_arm_sql_server.go | 16 ---------------- azurerm/resource_arm_sql_server_test.go | 18 ------------------ 2 files changed, 34 deletions(-) diff --git a/azurerm/resource_arm_sql_server.go b/azurerm/resource_arm_sql_server.go index eaf66134fadc..f743215d2c95 100644 --- a/azurerm/resource_arm_sql_server.go +++ b/azurerm/resource_arm_sql_server.go @@ -170,19 +170,3 @@ func resourceArmSqlServerDelete(d *schema.ResourceData, meta interface{}) error return nil } - -func validateAzureRmSQLDBAccountName(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - - r, _ := regexp.Compile("^[a-z0-9\\-]+$") - if !r.MatchString(value) { - errors = append(errors, fmt.Errorf("SQLDB Account Name can only contain lower-case characters, numbers and the `-` character.")) - } - - length := len(value) - if length > 50 || 3 > length { - errors = append(errors, fmt.Errorf("SQLDB Account Name can only be between 3 and 50 seconds.")) - } - - return -} diff --git a/azurerm/resource_arm_sql_server_test.go b/azurerm/resource_arm_sql_server_test.go index a868366cbf7a..a5c2085ade89 100644 --- a/azurerm/resource_arm_sql_server_test.go +++ b/azurerm/resource_arm_sql_server_test.go @@ -225,24 +225,6 @@ resource "azurerm_sql_server" "test" { `, rInt, location, rInt) } -func testAccAzureRMSqlServer_upperCaseName(rInt int, location string) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG_%d" - location = "%s" -} - -resource "azurerm_sql_server" "test" { - name = "AccTestSQLserver%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - version = "12.0" - administrator_login = "mradministrator" - administrator_login_password = "thisIsDog11" -} -`, rInt, location, rInt) -} - func testAccAzureRMSqlServer_withTags(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From b4b1d3cbb8597396cd692df7493993c3d533e564 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 12 Sep 2017 09:54:03 -0600 Subject: [PATCH 4/5] Remove unused improt --- azurerm/resource_arm_sql_server.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/resource_arm_sql_server.go b/azurerm/resource_arm_sql_server.go index f743215d2c95..4405b94a02cc 100644 --- a/azurerm/resource_arm_sql_server.go +++ b/azurerm/resource_arm_sql_server.go @@ -2,7 +2,6 @@ package azurerm import ( "fmt" - "regexp" "github.com/Azure/azure-sdk-for-go/arm/sql" "github.com/hashicorp/terraform/helper/schema" From 000fb9bed8fa3d826bdd96d65a5874da6a75ed87 Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Tue, 12 Sep 2017 14:23:08 -0600 Subject: [PATCH 5/5] Update validators_test.go --- azurerm/validators_test.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/azurerm/validators_test.go b/azurerm/validators_test.go index c803bbc85ac8..7294ad2f821d 100644 --- a/azurerm/validators_test.go +++ b/azurerm/validators_test.go @@ -128,11 +128,5 @@ func TestDBAccountName_validation(t *testing.T) { if len(errors) != tc.ErrCount { t.Fatalf("Expected the AzureRM CosmosDB Name to trigger a validation error for '%s'", tc.Value) } - - _, errors = validateDBAccountName(tc.Value, "azurerm_sql_server") - - if len(errors) != tc.ErrCount { - t.Fatalf("Expected the AzureRM SQL Server Name to trigger a validation error for '%s'", tc.Value) - } } }