From 532fe760ad329bed2b1e92a8c1f72fb7d1ad0810 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline Date: Fri, 2 Nov 2018 16:22:32 -0700 Subject: [PATCH 1/6] Fixed value length check --- azurerm/resource_arm_databricks_workspace.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_databricks_workspace.go b/azurerm/resource_arm_databricks_workspace.go index b0ddf1f2d739..3cb79a89f83e 100644 --- a/azurerm/resource_arm_databricks_workspace.go +++ b/azurerm/resource_arm_databricks_workspace.go @@ -187,7 +187,7 @@ func validateDatabricksWorkspaceName(v interface{}, k string) (ws []string, erro } // Cannot be more than 128 characters - if len(value) > 30 { + if len(value) > 128 { errors = append(errors, fmt.Errorf( "%q cannot be longer than 128 characters: %q", k, value)) } From a8f64eea745b28f48117b1e3305430569340b6fb Mon Sep 17 00:00:00 2001 From: Jeffrey Cline Date: Mon, 5 Nov 2018 12:33:19 -0800 Subject: [PATCH 2/6] Updated validation check to match swagger and sdk --- azurerm/resource_arm_databricks_workspace.go | 35 ++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/azurerm/resource_arm_databricks_workspace.go b/azurerm/resource_arm_databricks_workspace.go index 3cb79a89f83e..6b5d1d93c519 100644 --- a/azurerm/resource_arm_databricks_workspace.go +++ b/azurerm/resource_arm_databricks_workspace.go @@ -175,28 +175,29 @@ 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)) + // Cannot be empty + 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) > 128 { - 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 + if !regexp.MustCompile("^[a-zA-Z0-9]{2}[-a-zA-Z0-9]{0,61}[a-zA-Z0-9]{1}$").MatchString(v) { + errors = append(errors, fmt.Errorf("%q must be 3 - 64 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 From e98fa14f977ee44f259e9b72d38a7aa5a7ba0d43 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline Date: Mon, 5 Nov 2018 13:02:50 -0800 Subject: [PATCH 3/6] Update fmt --- azurerm/resource_arm_databricks_workspace.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/azurerm/resource_arm_databricks_workspace.go b/azurerm/resource_arm_databricks_workspace.go index 6b5d1d93c519..854057f78899 100644 --- a/azurerm/resource_arm_databricks_workspace.go +++ b/azurerm/resource_arm_databricks_workspace.go @@ -182,9 +182,9 @@ func validateDatabricksWorkspaceName(i interface{}, k string) (ws []string, erro return ws, errors } - // Cannot be empty - if len(v) == 0 { - errors = append(errors, fmt.Errorf("%q cannot be an empty string: %q", k, v)) + // Cannot be empty + if len(v) == 0 { + errors = append(errors, fmt.Errorf("%q cannot be an empty string: %q", k, v)) return ws, errors } From 5acf18ab291351e4f2538ed0aab98b402b28ed54 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline Date: Mon, 5 Nov 2018 15:38:30 -0800 Subject: [PATCH 4/6] Restricted name length to 30 chars --- azurerm/resource_arm_databricks_workspace.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/azurerm/resource_arm_databricks_workspace.go b/azurerm/resource_arm_databricks_workspace.go index 854057f78899..5ef728cc2cd9 100644 --- a/azurerm/resource_arm_databricks_workspace.go +++ b/azurerm/resource_arm_databricks_workspace.go @@ -189,8 +189,9 @@ func validateDatabricksWorkspaceName(i interface{}, k string) (ws []string, erro } // First, second, and last characters must be a letter or number with a total length between 3 to 64 characters - if !regexp.MustCompile("^[a-zA-Z0-9]{2}[-a-zA-Z0-9]{0,61}[a-zA-Z0-9]{1}$").MatchString(v) { - errors = append(errors, fmt.Errorf("%q must be 3 - 64 characters in length", k)) + // 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)) } From cf4aab1047f5d4edfe4b7b037a7e15116081cb57 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline Date: Mon, 5 Nov 2018 16:30:43 -0800 Subject: [PATCH 5/6] Updated test case --- azurerm/resource_arm_databricks_workspace_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/resource_arm_databricks_workspace_test.go b/azurerm/resource_arm_databricks_workspace_test.go index 519cde699c1d..01fab8ace767 100644 --- a/azurerm/resource_arm_databricks_workspace_test.go +++ b/azurerm/resource_arm_databricks_workspace_test.go @@ -29,7 +29,7 @@ func TestAzureRMDatabrickWorkspaceName(t *testing.T) { }, { Value: "hello-1-2-3-", - ShouldError: false, + ShouldError: true, }, { Value: "-hello-1-2-3", From 4696f808009719321a4e331f81ea70e1714ff186 Mon Sep 17 00:00:00 2001 From: Jeffrey Cline Date: Mon, 5 Nov 2018 16:41:31 -0800 Subject: [PATCH 6/6] Fixed test case --- azurerm/resource_arm_databricks_workspace_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/azurerm/resource_arm_databricks_workspace_test.go b/azurerm/resource_arm_databricks_workspace_test.go index 01fab8ace767..3440f70ccfbb 100644 --- a/azurerm/resource_arm_databricks_workspace_test.go +++ b/azurerm/resource_arm_databricks_workspace_test.go @@ -33,12 +33,16 @@ func TestAzureRMDatabrickWorkspaceName(t *testing.T) { }, { Value: "-hello-1-2-3", - ShouldError: false, + ShouldError: true, }, { Value: "hello!there", ShouldError: true, }, + { + Value: "hello--there", + ShouldError: true, + }, { Value: "!hellothere", ShouldError: true,