Skip to content

Commit

Permalink
Create parent directory for Repos if it uses non-standard location
Browse files Browse the repository at this point in the history
this fixes #862
  • Loading branch information
alexott committed Nov 3, 2021
1 parent 605dae3 commit 7f867ec
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
11 changes: 11 additions & 0 deletions workspace/resource_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"path/filepath"
"strings"

"github.com/databrickslabs/terraform-provider-databricks/common"
Expand Down Expand Up @@ -51,6 +52,16 @@ func (a ReposAPI) Create(r createRequest) (ReposInformation, error) {
if r.Provider == "" {
return resp, fmt.Errorf("git_provider isn't specified and we can't detect provider from URL")
}
if r.Path != "" {
p := r.Path
if strings.HasSuffix(r.Path, "/") {
p = strings.TrimSuffix(r.Path, "/")
}
p = filepath.Dir(p)
if err := NewNotebooksAPI(a.context, a.client).Mkdirs(p); err != nil {
return resp, err
}
}

err := a.client.Post(a.context, "/repos", r, &resp)
return resp, err
Expand Down
75 changes: 75 additions & 0 deletions workspace/resource_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,81 @@ func TestResourceRepoCreateNoBranch(t *testing.T) {
assert.Equal(t, resp.HeadCommitID, d.Get("commit_hash"))
}

func TestResourceRepoCreateCustomDirectory(t *testing.T) {
resp := ReposInformation{
ID: 121232342,
Url: "https://github.com/user/test.git",
Provider: "gitHub",
Branch: "main",
Path: "/Repos/user@domain/test",
HeadCommitID: "1124323423abc23424",
}
d, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "POST",
Resource: "/api/2.0/repos",
ExpectedRequest: createRequest{
Url: "https://github.com/user/test.git",
Provider: "gitHub",
Path: "/Repos/Production/test/",
},
Response: resp,
},
{
Method: "POST",
Resource: "/api/2.0/workspace/mkdirs",
ExpectedRequest: map[string]string{
"path": "/Repos/Production",
},
},
{
Method: "GET",
Resource: "/api/2.0/repos/121232342",
Response: resp,
},
},
Resource: ResourceRepo(),
State: map[string]interface{}{
"url": "https://github.com/user/test.git",
"path": "/Repos/Production/test/",
},
Create: true,
}.Apply(t)
assert.NoError(t, err, err)
assert.Equal(t, resp.RepoID(), d.Id())
assert.Equal(t, resp.Branch, d.Get("branch"))
assert.Equal(t, resp.Provider, d.Get("git_provider"))
assert.Equal(t, resp.Path, d.Get("path"))
assert.Equal(t, resp.HeadCommitID, d.Get("commit_hash"))
}

func TestResourceRepoCreateCustomDirectoryDirectoryError(t *testing.T) {
_, err := qa.ResourceFixture{
Fixtures: []qa.HTTPFixture{
{
Method: "POST",
Resource: "/api/2.0/workspace/mkdirs",
ExpectedRequest: map[string]string{
"path": "/Repos/Production",
},
Response: common.APIErrorBody{
ErrorCode: "INVALID_REQUEST",
Message: "Internal error happened",
},
Status: 400,
},
},
Resource: ResourceRepo(),
State: map[string]interface{}{
"url": "https://github.com/user/test.git",
"path": "/Repos/Production/test/",
},
Create: true,
}.Apply(t)
qa.AssertErrorStartsWith(t, err, "Internal error happened")
}

func TestResourceRepoCreateWithBranch(t *testing.T) {
resp := ReposInformation{
ID: 121232342,
Expand Down

0 comments on commit 7f867ec

Please sign in to comment.