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

Add validation for path in databricks_repo #1702

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 19 additions & 8 deletions repos/resource_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ func (a ReposAPI) Create(r reposCreateRequest) (ReposInformation, error) {
return resp, fmt.Errorf("git_provider isn't specified and we can't detect provider from URL")
}
if r.Path != "" {
if !strings.HasPrefix(r.Path, "/Repos/") {
return resp, fmt.Errorf("path should start with /Repos/")
}
p := r.Path
if strings.HasSuffix(r.Path, "/") {
p = strings.TrimSuffix(r.Path, "/")
}
p = path.Dir(p)
p := path.Dir(strings.TrimSuffix(r.Path, "/"))
if err := workspace.NewNotebooksAPI(a.context, a.client).Mkdirs(p); err != nil {
return resp, err
}
Expand Down Expand Up @@ -143,6 +136,23 @@ func GetGitProviderFromUrl(uri string) string {
return provider
}

func validatePath(i interface{}, k string) (_ []string, errors []error) {
v := i.(string)
if v != "" {
if !strings.HasPrefix(v, "/Repos/") {
errors = append(errors, fmt.Errorf("should start with /Repos/, got '%s'", v))
return
}
v = strings.TrimSuffix(v, "/")
parts := strings.Split(v, "/")
if len(parts) != 4 { // we require 3 path parts + starting /
errors = append(errors, fmt.Errorf("should have 3 components (/Repos/<directory>/<repo>), got %d", len(parts)-1))
return
}
}
return
}

func ResourceRepo() *schema.Resource {
s := common.StructToSchema(ReposInformation{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
s["url"].ValidateFunc = validation.IsURLWithScheme([]string{"https", "http"})
Expand All @@ -151,6 +161,7 @@ func ResourceRepo() *schema.Resource {
}
s["branch"].ConflictsWith = []string{"tag"}
s["branch"].ValidateFunc = validation.StringIsNotWhiteSpace
s["path"].ValidateFunc = validatePath

s["tag"] = &schema.Schema{
Type: schema.TypeString,
Expand Down
14 changes: 12 additions & 2 deletions repos/resource_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestGetGitProviderFromUrl(t *testing.T) {
assert.Equal(t, "bitbucketCloud", GetGitProviderFromUrl("https://user@bitbucket.org/user/repo.git"))
assert.Equal(t, "gitHub", GetGitProviderFromUrl("https://github.com//user/repo.git"))
assert.Equal(t, "azureDevOpsServices", GetGitProviderFromUrl("https://user@dev.azure.com/user/project/_git/repo"))
// assert.Equal(t, "bitbucketCloud", GetGitProviderFromUrl("https://user@bitbucket.org/user/repo.git"))
assert.Equal(t, "", GetGitProviderFromUrl("https://abc/user/repo.git"))
assert.Equal(t, "", GetGitProviderFromUrl("ewfgwergfwe"))
}
Expand Down Expand Up @@ -206,7 +205,18 @@ func TestResourceRepoCreateCustomDirectoryWrongLocation(t *testing.T) {
"path": "/NotRepos/Production/test/",
},
Create: true,
}.ExpectError(t, "path should start with /Repos/")
}.ExpectError(t, "invalid config supplied. [path] should start with /Repos/, got '/NotRepos/Production/test/'")
}

func TestResourceRepoCreateCustomDirectoryWrongPath(t *testing.T) {
qa.ResourceFixture{
Resource: ResourceRepo(),
State: map[string]any{
"url": "https://github.com/user/test.git",
"path": "/Repos/test/",
},
Create: true,
}.ExpectError(t, "invalid config supplied. [path] should have 3 components (/Repos/<directory>/<repo>), got 2")
}

func TestResourceRepoCreateWithBranch(t *testing.T) {
Expand Down