From 85ac02950ec8409578adcdc9d49c4cabf52e0514 Mon Sep 17 00:00:00 2001 From: Dave Storey Date: Fri, 12 Jun 2020 11:37:00 +0000 Subject: [PATCH] standardised the validate logic found azure mounts --- databricks/mounts.go | 9 ++++++++ databricks/mounts_test.go | 23 +++++++++++++++++++ ...source_databricks_azure_adls_gen1_mount.go | 9 +------- ...source_databricks_azure_adls_gen2_mount.go | 1 + .../resource_databricks_azure_blob_mount.go | 9 +------- 5 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 databricks/mounts_test.go diff --git a/databricks/mounts.go b/databricks/mounts.go index 8e546afae..bd9a45e07 100644 --- a/databricks/mounts.go +++ b/databricks/mounts.go @@ -435,3 +435,12 @@ func ProcessAzureWasbAbfssUris(uri string) (string, string, string, error) { } return containerName, storageAccount, directory, nil } + +// ValidateMountDirectory is a ValidateFunc that ensures the mount directory starts with a '/' +func ValidateMountDirectory(val interface{}, key string) (warns []string, errs []error) { + v := val.(string) + if v != "" && !strings.HasPrefix(v, "/") { + return nil, []error{fmt.Errorf("%s must start with /, got: %s", key, v)} + } + return nil, nil +} \ No newline at end of file diff --git a/databricks/mounts_test.go b/databricks/mounts_test.go new file mode 100644 index 000000000..ac6c2d7f0 --- /dev/null +++ b/databricks/mounts_test.go @@ -0,0 +1,23 @@ +package databricks + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidateMountDirectory(t *testing.T) { + testCases := []struct { + directory string + errorCount int + }{ + {"", 0}, + {"/directory", 0}, + {"directory", 1}, + } + for _, tc := range testCases { + _, errs := ValidateMountDirectory(tc.directory, "key") + + assert.Lenf(t, errs, tc.errorCount, "directory '%s' does not generate the expected error count", tc.directory) + } +} diff --git a/databricks/resource_databricks_azure_adls_gen1_mount.go b/databricks/resource_databricks_azure_adls_gen1_mount.go index 71032003f..4d6d28bd6 100644 --- a/databricks/resource_databricks_azure_adls_gen1_mount.go +++ b/databricks/resource_databricks_azure_adls_gen1_mount.go @@ -40,14 +40,7 @@ func resourceAzureAdlsGen1Mount() *schema.Resource { Computed: true, //Default: "/", ForceNew: true, - ValidateFunc: func(val interface{}, key string) (warns []string, errors []error) { - directory := val.(string) - if strings.HasPrefix(directory, "/") { - return - } - errors = append(errors, fmt.Errorf("%s must start with /, got: %s", key, val)) - return - }, + ValidateFunc: ValidateMountDirectory, }, "mount_name": { Type: schema.TypeString, diff --git a/databricks/resource_databricks_azure_adls_gen2_mount.go b/databricks/resource_databricks_azure_adls_gen2_mount.go index fc574fa1e..36388f7e7 100644 --- a/databricks/resource_databricks_azure_adls_gen2_mount.go +++ b/databricks/resource_databricks_azure_adls_gen2_mount.go @@ -36,6 +36,7 @@ func resourceAzureAdlsGen2Mount() *schema.Resource { Optional: true, Computed: true, ForceNew: true, + ValidateFunc: ValidateMountDirectory, }, "mount_name": { Type: schema.TypeString, diff --git a/databricks/resource_databricks_azure_blob_mount.go b/databricks/resource_databricks_azure_blob_mount.go index d09ea33cd..d9b6e9ba7 100644 --- a/databricks/resource_databricks_azure_blob_mount.go +++ b/databricks/resource_databricks_azure_blob_mount.go @@ -38,14 +38,7 @@ func resourceAzureBlobMount() *schema.Resource { Computed: true, //Default: "/", ForceNew: true, - ValidateFunc: func(val interface{}, key string) (warns []string, errors []error) { - directory := val.(string) - if strings.HasPrefix(directory, "/") { - return - } - errors = append(errors, fmt.Errorf("%s must start with /, got: %s", key, val)) - return - }, + ValidateFunc: ValidateMountDirectory, }, "mount_name": { Type: schema.TypeString,