From de0fa21a6dae4d709d2cbeeef6c12cb601983f95 Mon Sep 17 00:00:00 2001 From: Alex Ott Date: Wed, 3 Nov 2021 21:51:18 +0100 Subject: [PATCH] Create parent directory for Repos if it uses non-standard location this fixes #862 --- workspace/resource_repo.go | 14 ++++++ workspace/resource_repo_test.go | 87 +++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/workspace/resource_repo.go b/workspace/resource_repo.go index 994788e1be..fa426fbd3a 100644 --- a/workspace/resource_repo.go +++ b/workspace/resource_repo.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/url" + "path/filepath" "strings" "github.com/databrickslabs/terraform-provider-databricks/common" @@ -51,6 +52,19 @@ 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 != "" { + 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 = 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 diff --git a/workspace/resource_repo_test.go b/workspace/resource_repo_test.go index 1f089b1f1c..cc98c0b58e 100644 --- a/workspace/resource_repo_test.go +++ b/workspace/resource_repo_test.go @@ -138,6 +138,93 @@ 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 TestResourceRepoCreateCustomDirectoryError(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 TestResourceRepoCreateCustomDirectoryWrongLocation(t *testing.T) { + _, err := qa.ResourceFixture{ + Resource: ResourceRepo(), + State: map[string]interface{}{ + "url": "https://github.com/user/test.git", + "path": "/NotRepos/Production/test/", + }, + Create: true, + }.Apply(t) + qa.AssertErrorStartsWith(t, err, "path should start with /Repos/") +} + func TestResourceRepoCreateWithBranch(t *testing.T) { resp := ReposInformation{ ID: 121232342,