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

Fixed value length check #2221

Merged
merged 6 commits into from
Nov 6, 2018
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
34 changes: 18 additions & 16 deletions azurerm/resource_arm_databricks_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,30 @@ func resourceArmDatabricksWorkspaceDelete(d *schema.ResourceData, meta interface
return nil
}

func validateDatabricksWorkspaceName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

// Only alphanumeric characters, underscores, and hyphens are allowed, and the name must be 1-30 characters long.
func validateDatabricksWorkspaceName(i interface{}, k string) (ws []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected %q type to be string", k))
return ws, errors
}

// Cannot be empty
if len(value) == 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be an empty string: %q", k, value))
if len(v) == 0 {
errors = append(errors, fmt.Errorf("%q cannot be an empty string: %q", k, v))
return ws, errors
}

// Cannot be more than 128 characters
if len(value) > 30 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 128 characters: %q", k, value))
// First, second, and last characters must be a letter or number with a total length between 3 to 64 characters
// NOTE: Restricted name to 30 characters because that is the restriction in Azure Portal even though the API supports 64 characters
if !regexp.MustCompile("^[a-zA-Z0-9]{2}[-a-zA-Z0-9]{0,27}[a-zA-Z0-9]{1}$").MatchString(v) {
errors = append(errors, fmt.Errorf("%q must be 3 - 30 characters in length", k))
errors = append(errors, fmt.Errorf("%q first, second, and last characters must be a letter or number", k))
errors = append(errors, fmt.Errorf("%q can only contain letters, numbers, and hyphens", k))
}

// Must only contain alphanumeric characters or hyphens
if !regexp.MustCompile(`^[A-Za-z0-9-]*$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain alphanumeric characters and hyphens: %q",
k, value))
// No consecutive hyphens
if regexp.MustCompile("(--)").MatchString(v) {
errors = append(errors, fmt.Errorf("%q must not contain any consecutive hyphens", k))
}

return ws, errors
Expand Down
8 changes: 6 additions & 2 deletions azurerm/resource_arm_databricks_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,20 @@ func TestAzureRMDatabrickWorkspaceName(t *testing.T) {
},
{
Value: "hello-1-2-3-",
ShouldError: false,
ShouldError: true,
},
{
Value: "-hello-1-2-3",
ShouldError: false,
ShouldError: true,
},
{
Value: "hello!there",
ShouldError: true,
},
{
Value: "hello--there",
ShouldError: true,
},
{
Value: "!hellothere",
ShouldError: true,
Expand Down