Skip to content

Commit 3f8e519

Browse files
authored
Added validation for path in databricks_repo (#1702)
It was reported by user that error message isn't understandable, so this PR improves this
1 parent 8b3c2c9 commit 3f8e519

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

repos/resource_repo.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,7 @@ func (a ReposAPI) Create(r reposCreateRequest) (ReposInformation, error) {
5555
return resp, fmt.Errorf("git_provider isn't specified and we can't detect provider from URL")
5656
}
5757
if r.Path != "" {
58-
if !strings.HasPrefix(r.Path, "/Repos/") {
59-
return resp, fmt.Errorf("path should start with /Repos/")
60-
}
61-
p := r.Path
62-
if strings.HasSuffix(r.Path, "/") {
63-
p = strings.TrimSuffix(r.Path, "/")
64-
}
65-
p = path.Dir(p)
58+
p := path.Dir(strings.TrimSuffix(r.Path, "/"))
6659
if err := workspace.NewNotebooksAPI(a.context, a.client).Mkdirs(p); err != nil {
6760
return resp, err
6861
}
@@ -151,6 +144,23 @@ func GetGitProviderFromUrl(uri string) string {
151144
return provider
152145
}
153146

147+
func validatePath(i interface{}, k string) (_ []string, errors []error) {
148+
v := i.(string)
149+
if v != "" {
150+
if !strings.HasPrefix(v, "/Repos/") {
151+
errors = append(errors, fmt.Errorf("should start with /Repos/, got '%s'", v))
152+
return
153+
}
154+
v = strings.TrimSuffix(v, "/")
155+
parts := strings.Split(v, "/")
156+
if len(parts) != 4 { // we require 3 path parts + starting /
157+
errors = append(errors, fmt.Errorf("should have 3 components (/Repos/<directory>/<repo>), got %d", len(parts)-1))
158+
return
159+
}
160+
}
161+
return
162+
}
163+
154164
func ResourceRepo() *schema.Resource {
155165
s := common.StructToSchema(ReposInformation{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
156166
s["url"].ValidateFunc = validation.IsURLWithScheme([]string{"https", "http"})
@@ -159,6 +169,7 @@ func ResourceRepo() *schema.Resource {
159169
}
160170
s["branch"].ConflictsWith = []string{"tag"}
161171
s["branch"].ValidateFunc = validation.StringIsNotWhiteSpace
172+
s["path"].ValidateFunc = validatePath
162173

163174
s["tag"] = &schema.Schema{
164175
Type: schema.TypeString,

repos/resource_repo_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ func TestGetGitProviderFromUrl(t *testing.T) {
1818
assert.Equal(t, "bitbucketCloud", GetGitProviderFromUrl("https://[email protected]/user/repo.git"))
1919
assert.Equal(t, "gitHub", GetGitProviderFromUrl("https://github.com//user/repo.git"))
2020
assert.Equal(t, "azureDevOpsServices", GetGitProviderFromUrl("https://[email protected]/user/project/_git/repo"))
21-
// assert.Equal(t, "bitbucketCloud", GetGitProviderFromUrl("https://[email protected]/user/repo.git"))
2221
assert.Equal(t, "", GetGitProviderFromUrl("https://abc/user/repo.git"))
2322
assert.Equal(t, "", GetGitProviderFromUrl("ewfgwergfwe"))
2423
assert.Equal(t, "awsCodeCommit", GetGitProviderFromUrl("https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo"))
@@ -207,7 +206,18 @@ func TestResourceRepoCreateCustomDirectoryWrongLocation(t *testing.T) {
207206
"path": "/NotRepos/Production/test/",
208207
},
209208
Create: true,
210-
}.ExpectError(t, "path should start with /Repos/")
209+
}.ExpectError(t, "invalid config supplied. [path] should start with /Repos/, got '/NotRepos/Production/test/'")
210+
}
211+
212+
func TestResourceRepoCreateCustomDirectoryWrongPath(t *testing.T) {
213+
qa.ResourceFixture{
214+
Resource: ResourceRepo(),
215+
State: map[string]any{
216+
"url": "https://github.com/user/test.git",
217+
"path": "/Repos/test/",
218+
},
219+
Create: true,
220+
}.ExpectError(t, "invalid config supplied. [path] should have 3 components (/Repos/<directory>/<repo>), got 2")
211221
}
212222

213223
func TestResourceRepoCreateWithBranch(t *testing.T) {

0 commit comments

Comments
 (0)