-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2398 from terraform-providers/b-mssql-validation
Updated MS SQL resource name validation
- Loading branch information
Showing
14 changed files
with
335 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package azure | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" | ||
) | ||
|
||
//Your server name can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 63 characters. | ||
func ValidateMsSqlServerName(i interface{}, k string) (_ []string, errors []error) { | ||
if m, regexErrs := validate.RegExHelper(i, k, `^[0-9a-z]([-0-9a-z]{0,61}[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 | ||
} | ||
|
||
//Your database name can't end with '.' or ' ', can't contain '<,>,*,%,&,:,\,/,?' or control characters, and can't have more than 128 characters. | ||
func ValidateMsSqlDatabaseName(i interface{}, k string) (_ []string, errors []error) { | ||
if m, regexErrs := validate.RegExHelper(i, k, `^[^<>*%&:\\\/?]{0,127}[^\s.<>*%&:\\\/?]$`); !m { | ||
errors = append(regexErrs, fmt.Errorf(`%q can't end with '.' or ' ', can't contain '<,>,*,%%,&,:,\,/,?' or control characters, and can't have more than 128 characters.`, k)) | ||
} | ||
|
||
return nil, errors | ||
} | ||
|
||
//Following characters and any control characters are not allowed for resource name '%,&,\\\\,?,/'.\" | ||
//The name can not end with characters: '. ' | ||
//TODO: unsure about length, was able to deploy one at 120 | ||
func ValidateMsSqlElasticPoolName(i interface{}, k string) (_ []string, errors []error) { | ||
if m, regexErrs := validate.RegExHelper(i, k, `^[^&%\\\/?]{0,127}[^\s.&%\\\/?]$`); !m { | ||
errors = append(regexErrs, fmt.Errorf(`%q can't end with '.' or ' ', can't contain '%%,&,\,/,?' or control characters, and can't have more than 128 characters.`, k)) | ||
} | ||
|
||
return nil, errors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package azure | ||
|
||
import "testing" | ||
|
||
//Your server name can contain only lowercase letters, numbers, and '-', but can't start or end with '-' or have more than 63 characters. | ||
func TestValidateMsSqlServerName(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
Errors bool | ||
}{ | ||
{ | ||
Value: "", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "k", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "K", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "k-", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "k-t", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "K-T", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "validname", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "invalid_name", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "123456789112345678921234567893123456789412345678951234567896123", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "01234567891123456789212345678931234567894123456789512345678961234", | ||
Errors: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := ValidateMsSqlServerName(tc.Value, "name") | ||
|
||
if len(errors) > 0 != tc.Errors { | ||
if tc.Errors { | ||
t.Fatalf("Expected ValidateMsSqlServerName to have errors for '%s', got %d ", tc.Value, len(errors)) | ||
} else { | ||
t.Fatalf("Expected ValidateMsSqlServerName to not have errors for '%s', got %d ", tc.Value, len(errors)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
//Your database name can't end with '.' or ' ', can't contain '<,>,*,%,&,:,\,/,?' or control characters, and can't have more than 128 characters. | ||
func TestValidateMsSqlDatabaseName(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
Errors bool | ||
}{ | ||
{ | ||
Value: "", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "k", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "K", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "space ", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "dot.", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "data_base-name", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "ends.with.dash-", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "ends.with.underscore_", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "fail:semicolon", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "fail?question", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "fail&ersand", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "fail%percent", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "12345678911234567892123456789312345678941234567895123456789612345678971234567898123456789912345678901234567891123456789212345678", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "123456789112345678921234567893123456789412345678951234567896123456789712345678981234567899123456789012345678911234567892123456789", | ||
Errors: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := ValidateMsSqlDatabaseName(tc.Value, "name") | ||
|
||
if len(errors) > 0 != tc.Errors { | ||
if tc.Errors { | ||
t.Fatalf("Expected ValidateMsSqlDatabaseName to have errors for '%s', got %d ", tc.Value, len(errors)) | ||
} else { | ||
t.Fatalf("Expected ValidateMsSqlDatabaseName to not have errors for '%s', got %d ", tc.Value, len(errors)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
//Following characters and any control characters are not allowed for resource name '%,&,\\\\,?,/'.\" | ||
//The name can not end with characters: '. ' | ||
func TestValidateMsSqlElasticPoolName(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
Errors bool | ||
}{ | ||
{ | ||
Value: "", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "k", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "K", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "space ", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "dot.", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "data_base-name", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "ends.with.dash-", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "ends.with.underscore_", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "fail?question", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "fail&ersand", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "fail%percent", | ||
Errors: true, | ||
}, | ||
{ | ||
Value: "12345678911234567892123456789312345678941234567895123456789612345678971234567898123456789912345678901234567891123456789212345678", | ||
Errors: false, | ||
}, | ||
{ | ||
Value: "123456789112345678921234567893123456789412345678951234567896123456789712345678981234567899123456789012345678911234567892123456789", | ||
Errors: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := ValidateMsSqlElasticPoolName(tc.Value, "name") | ||
|
||
if len(errors) > 0 != tc.Errors { | ||
if tc.Errors { | ||
t.Fatalf("Expected ValidateMsSqlServerName to have errors for '%s', got %d ", tc.Value, len(errors)) | ||
} else { | ||
t.Fatalf("Expected ValidateMsSqlServerName to not have errors for '%s', got %d ", tc.Value, len(errors)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.