Skip to content

Commit

Permalink
Fixed value length check (#2221)
Browse files Browse the repository at this point in the history
* Fixed value length check

* Updated validation check to match swagger and sdk

* Update fmt

* Restricted name length to 30 chars

* Updated test case

* Fixed test case
  • Loading branch information
WodansSon authored and tombuildsstuff committed Nov 6, 2018
1 parent 51088df commit 7b65540
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
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

0 comments on commit 7b65540

Please sign in to comment.