Skip to content

Commit

Permalink
Merge pull request #323 from terraform-providers/b-sql-server
Browse files Browse the repository at this point in the history
Add validation to sql server name
  • Loading branch information
mbfrahry authored Sep 12, 2017
2 parents 6e3c569 + 000fb9b commit 36fd0ca
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 63 deletions.
19 changes: 1 addition & 18 deletions azurerm/resource_arm_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +28,7 @@ func resourceArmCosmosDBAccount() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRmCosmosDBAccountName,
ValidateFunc: validateDBAccountName,
},

"location": {
Expand Down Expand Up @@ -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
}
41 changes: 0 additions & 41 deletions azurerm/resource_arm_cosmos_db_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions azurerm/resource_arm_sql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,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: validateDBAccountName,
},

"location": locationSchema(),
Expand Down
17 changes: 17 additions & 0 deletions azurerm/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurerm

import (
"fmt"
"regexp"
"time"

"github.com/Azure/go-autorest/autorest/date"
Expand Down Expand Up @@ -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
}
47 changes: 46 additions & 1 deletion azurerm/validators_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package azurerm

import "testing"
import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
)

func TestValidateRFC3339Date(t *testing.T) {
cases := []struct {
Expand Down Expand Up @@ -85,3 +89,44 @@ 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)
}
}
}

0 comments on commit 36fd0ca

Please sign in to comment.