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

Add validation to sql server name #323

Merged
merged 5 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
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
}
53 changes: 52 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,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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this secondary validation function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do!

}
}