Skip to content

Commit 5fe12a4

Browse files
authored
Create parent directory for Repos if it uses non-standard location (#895)
1 parent 605dae3 commit 5fe12a4

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

workspace/resource_repo.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/url"
7+
"path/filepath"
78
"strings"
89

910
"github.com/databrickslabs/terraform-provider-databricks/common"
@@ -51,6 +52,19 @@ func (a ReposAPI) Create(r createRequest) (ReposInformation, error) {
5152
if r.Provider == "" {
5253
return resp, fmt.Errorf("git_provider isn't specified and we can't detect provider from URL")
5354
}
55+
if r.Path != "" {
56+
if !strings.HasPrefix(r.Path, "/Repos/") {
57+
return resp, fmt.Errorf("path should start with /Repos/")
58+
}
59+
p := r.Path
60+
if strings.HasSuffix(r.Path, "/") {
61+
p = strings.TrimSuffix(r.Path, "/")
62+
}
63+
p = filepath.Dir(p)
64+
if err := NewNotebooksAPI(a.context, a.client).Mkdirs(p); err != nil {
65+
return resp, err
66+
}
67+
}
5468

5569
err := a.client.Post(a.context, "/repos", r, &resp)
5670
return resp, err

workspace/resource_repo_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,93 @@ func TestResourceRepoCreateNoBranch(t *testing.T) {
138138
assert.Equal(t, resp.HeadCommitID, d.Get("commit_hash"))
139139
}
140140

141+
func TestResourceRepoCreateCustomDirectory(t *testing.T) {
142+
resp := ReposInformation{
143+
ID: 121232342,
144+
Url: "https://github.com/user/test.git",
145+
Provider: "gitHub",
146+
Branch: "main",
147+
Path: "/Repos/user@domain/test",
148+
HeadCommitID: "1124323423abc23424",
149+
}
150+
d, err := qa.ResourceFixture{
151+
Fixtures: []qa.HTTPFixture{
152+
{
153+
Method: "POST",
154+
Resource: "/api/2.0/repos",
155+
ExpectedRequest: createRequest{
156+
Url: "https://github.com/user/test.git",
157+
Provider: "gitHub",
158+
Path: "/Repos/Production/test/",
159+
},
160+
Response: resp,
161+
},
162+
{
163+
Method: "POST",
164+
Resource: "/api/2.0/workspace/mkdirs",
165+
ExpectedRequest: map[string]string{
166+
"path": "/Repos/Production",
167+
},
168+
},
169+
{
170+
Method: "GET",
171+
Resource: "/api/2.0/repos/121232342",
172+
Response: resp,
173+
},
174+
},
175+
Resource: ResourceRepo(),
176+
State: map[string]interface{}{
177+
"url": "https://github.com/user/test.git",
178+
"path": "/Repos/Production/test/",
179+
},
180+
Create: true,
181+
}.Apply(t)
182+
assert.NoError(t, err, err)
183+
assert.Equal(t, resp.RepoID(), d.Id())
184+
assert.Equal(t, resp.Branch, d.Get("branch"))
185+
assert.Equal(t, resp.Provider, d.Get("git_provider"))
186+
assert.Equal(t, resp.Path, d.Get("path"))
187+
assert.Equal(t, resp.HeadCommitID, d.Get("commit_hash"))
188+
}
189+
190+
func TestResourceRepoCreateCustomDirectoryError(t *testing.T) {
191+
_, err := qa.ResourceFixture{
192+
Fixtures: []qa.HTTPFixture{
193+
{
194+
Method: "POST",
195+
Resource: "/api/2.0/workspace/mkdirs",
196+
ExpectedRequest: map[string]string{
197+
"path": "/Repos/Production",
198+
},
199+
Response: common.APIErrorBody{
200+
ErrorCode: "INVALID_REQUEST",
201+
Message: "Internal error happened",
202+
},
203+
Status: 400,
204+
},
205+
},
206+
Resource: ResourceRepo(),
207+
State: map[string]interface{}{
208+
"url": "https://github.com/user/test.git",
209+
"path": "/Repos/Production/test/",
210+
},
211+
Create: true,
212+
}.Apply(t)
213+
qa.AssertErrorStartsWith(t, err, "Internal error happened")
214+
}
215+
216+
func TestResourceRepoCreateCustomDirectoryWrongLocation(t *testing.T) {
217+
_, err := qa.ResourceFixture{
218+
Resource: ResourceRepo(),
219+
State: map[string]interface{}{
220+
"url": "https://github.com/user/test.git",
221+
"path": "/NotRepos/Production/test/",
222+
},
223+
Create: true,
224+
}.Apply(t)
225+
qa.AssertErrorStartsWith(t, err, "path should start with /Repos/")
226+
}
227+
141228
func TestResourceRepoCreateWithBranch(t *testing.T) {
142229
resp := ReposInformation{
143230
ID: 121232342,

0 commit comments

Comments
 (0)