Skip to content

Commit d27d396

Browse files
authored
Add workspace_path attribute to databricks_workspace_file resource (#2984)
This new attribute will help with references to files on WSFS, for example, when using exporter (will be added in a separate PR after testing). Also, refactored tests a bit.
1 parent c24388e commit d27d396

File tree

3 files changed

+57
-45
lines changed

3 files changed

+57
-45
lines changed

docs/resources/workspace_file.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ In addition to all arguments above, the following attributes are exported:
4949
* `id` - Path of workspace file
5050
* `url` - Routable URL of the workspace file
5151
* `object_id` - Unique identifier for a workspace file
52+
* `workspace_path` - path on Workspace File System (WSFS) in form of `/Workspace` + `path`
5253

5354
## Access Control
5455

workspace/resource_workspace_file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func ResourceWorkspaceFile() *schema.Resource {
2424
Optional: true,
2525
Computed: true,
2626
},
27+
"workspace_path": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
2731
})
2832
return common.Resource{
2933
Schema: s,
@@ -73,6 +77,7 @@ func ResourceWorkspaceFile() *schema.Resource {
7377
return err
7478
}
7579
d.Set("url", c.FormatURL("#workspace", d.Id()))
80+
d.Set("workspace_path", "/Workspace"+objectStatus.Path)
7681
return common.StructToData(objectStatus, s, d)
7782
},
7883
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {

workspace/resource_workspace_file_test.go

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,41 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
)
1313

14+
var (
15+
dummyWorkspaceFilePath = "/foo/path.py"
16+
dummyWorkspaceFilePathUrl = "path=%2Ffoo%2Fpath.py"
17+
dummyWorkspaceFilePayload = "YWJjCg=="
18+
)
19+
1420
func TestResourceWorkspaceFileRead(t *testing.T) {
15-
path := "/test/path.py"
1621
objectID := 12345
17-
d, err := qa.ResourceFixture{
22+
qa.ResourceFixture{
1823
Fixtures: []qa.HTTPFixture{
1924
{
2025
Method: http.MethodGet,
21-
Resource: "/api/2.0/workspace/get-status?path=%2Ftest%2Fpath.py",
26+
Resource: "/api/2.0/workspace/get-status?" + dummyWorkspaceFilePathUrl,
2227
Response: ObjectStatus{
2328
ObjectID: int64(objectID),
2429
ObjectType: File,
25-
Path: path,
30+
Path: dummyWorkspaceFilePath,
2631
},
2732
},
2833
},
2934
Resource: ResourceWorkspaceFile(),
3035
Read: true,
3136
New: true,
32-
ID: path,
33-
}.Apply(t)
34-
assert.NoError(t, err)
35-
assert.Equal(t, path, d.Id())
36-
assert.Equal(t, path, d.Get("path"))
37-
assert.Equal(t, objectID, d.Get("object_id"))
37+
ID: dummyWorkspaceFilePath,
38+
}.ApplyAndExpectData(t, map[string]any{
39+
"id": dummyWorkspaceFilePath,
40+
"path": dummyWorkspaceFilePath,
41+
"workspace_path": "/Workspace" + dummyWorkspaceFilePath,
42+
"object_id": objectID,
43+
})
3844
}
3945

4046
func TestResourceWorkspaceFileDelete(t *testing.T) {
4147
path := "/test/path.py"
42-
d, err := qa.ResourceFixture{
48+
qa.ResourceFixture{
4349
Fixtures: []qa.HTTPFixture{
4450
{
4551
Method: http.MethodPost,
@@ -51,9 +57,9 @@ func TestResourceWorkspaceFileDelete(t *testing.T) {
5157
Resource: ResourceWorkspaceFile(),
5258
Delete: true,
5359
ID: path,
54-
}.Apply(t)
55-
assert.NoError(t, err)
56-
assert.Equal(t, path, d.Id())
60+
}.ApplyAndExpectData(t, map[string]any{
61+
"id": path,
62+
})
5763
}
5864

5965
func TestResourceWorkspaceFileRead_NotFound(t *testing.T) {
@@ -98,7 +104,7 @@ func TestResourceWorkspaceFileRead_Error(t *testing.T) {
98104
}
99105

100106
func TestResourceWorkspaceFileCreate_DirectoryExist(t *testing.T) {
101-
d, err := qa.ResourceFixture{
107+
qa.ResourceFixture{
102108
Fixtures: []qa.HTTPFixture{
103109
{
104110
Method: "POST",
@@ -111,38 +117,38 @@ func TestResourceWorkspaceFileCreate_DirectoryExist(t *testing.T) {
111117
Method: http.MethodPost,
112118
Resource: "/api/2.0/workspace/import",
113119
ExpectedRequest: ws_api.Import{
114-
Content: "YWJjCg==",
115-
Path: "/foo/path.py",
120+
Content: dummyWorkspaceFilePayload,
121+
Path: dummyWorkspaceFilePath,
116122
Overwrite: true,
117123
Format: "AUTO",
118124
},
119125
},
120126
{
121127
Method: http.MethodGet,
122-
Resource: "/api/2.0/workspace/export?format=SOURCE&path=%2Ffoo%2Fpath.py",
128+
Resource: "/api/2.0/workspace/export?format=SOURCE&" + dummyWorkspaceFilePathUrl,
123129
Response: ExportPath{
124-
Content: "YWJjCg==",
130+
Content: dummyWorkspaceFilePayload,
125131
},
126132
},
127133
{
128134
Method: http.MethodGet,
129-
Resource: "/api/2.0/workspace/get-status?path=%2Ffoo%2Fpath.py",
135+
Resource: "/api/2.0/workspace/get-status?" + dummyWorkspaceFilePathUrl,
130136
Response: ObjectStatus{
131137
ObjectID: 4567,
132138
ObjectType: File,
133-
Path: "/foo/path.py",
139+
Path: dummyWorkspaceFilePath,
134140
},
135141
},
136142
},
137143
Resource: ResourceWorkspaceFile(),
138144
State: map[string]any{
139-
"content_base64": "YWJjCg==",
140-
"path": "/foo/path.py",
145+
"content_base64": dummyWorkspaceFilePayload,
146+
"path": dummyWorkspaceFilePath,
141147
},
142148
Create: true,
143-
}.Apply(t)
144-
assert.NoError(t, err)
145-
assert.Equal(t, "/foo/path.py", d.Id())
149+
}.ApplyAndExpectData(t, map[string]any{
150+
"id": dummyWorkspaceFilePath,
151+
})
146152
}
147153

148154
func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
@@ -159,8 +165,8 @@ func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
159165
Method: http.MethodPost,
160166
Resource: "/api/2.0/workspace/import",
161167
ExpectedRequest: ws_api.Import{
162-
Content: "YWJjCg==",
163-
Path: "/foo/path.py",
168+
Content: dummyWorkspaceFilePayload,
169+
Path: dummyWorkspaceFilePath,
164170
Overwrite: true,
165171
Format: "AUTO",
166172
},
@@ -174,38 +180,38 @@ func TestResourceWorkspaceFileCreate_DirectoryDoesntExist(t *testing.T) {
174180
Method: http.MethodPost,
175181
Resource: "/api/2.0/workspace/import",
176182
ExpectedRequest: ws_api.Import{
177-
Content: "YWJjCg==",
178-
Path: "/foo/path.py",
183+
Content: dummyWorkspaceFilePayload,
184+
Path: dummyWorkspaceFilePath,
179185
Overwrite: true,
180186
Format: "AUTO",
181187
},
182188
},
183189
{
184190
Method: http.MethodGet,
185-
Resource: "/api/2.0/workspace/export?format=SOURCE&path=%2Ffoo%2Fpath.py",
191+
Resource: "/api/2.0/workspace/export?format=SOURCE&" + dummyWorkspaceFilePathUrl,
186192
Response: ExportPath{
187-
Content: "YWJjCg==",
193+
Content: dummyWorkspaceFilePayload,
188194
},
189195
},
190196
{
191197
Method: http.MethodGet,
192-
Resource: "/api/2.0/workspace/get-status?path=%2Ffoo%2Fpath.py",
198+
Resource: "/api/2.0/workspace/get-status?" + dummyWorkspaceFilePathUrl,
193199
Response: ObjectStatus{
194200
ObjectID: 4567,
195201
ObjectType: File,
196-
Path: "/foo/path.py",
202+
Path: dummyWorkspaceFilePath,
197203
},
198204
},
199205
},
200206
Resource: ResourceWorkspaceFile(),
201207
State: map[string]any{
202-
"content_base64": "YWJjCg==",
203-
"path": "/foo/path.py",
208+
"content_base64": dummyWorkspaceFilePayload,
209+
"path": dummyWorkspaceFilePath,
204210
},
205211
Create: true,
206212
}.Apply(t)
207213
assert.NoError(t, err)
208-
assert.Equal(t, "/foo/path.py", d.Id())
214+
assert.Equal(t, dummyWorkspaceFilePath, d.Id())
209215
}
210216

211217
func TestResourceWorkspaceFileCreate_DirectoryCreateError(t *testing.T) {
@@ -227,8 +233,8 @@ func TestResourceWorkspaceFileCreate_DirectoryCreateError(t *testing.T) {
227233
Method: http.MethodPost,
228234
Resource: "/api/2.0/workspace/import",
229235
ExpectedRequest: ws_api.Import{
230-
Content: "YWJjCg==",
231-
Path: "/foo/path.py",
236+
Content: dummyWorkspaceFilePayload,
237+
Path: dummyWorkspaceFilePath,
232238
Overwrite: true,
233239
Format: "AUTO",
234240
},
@@ -241,8 +247,8 @@ func TestResourceWorkspaceFileCreate_DirectoryCreateError(t *testing.T) {
241247
},
242248
Resource: ResourceWorkspaceFile(),
243249
State: map[string]any{
244-
"content_base64": "YWJjCg==",
245-
"path": "/foo/path.py",
250+
"content_base64": dummyWorkspaceFilePayload,
251+
"path": dummyWorkspaceFilePath,
246252
},
247253
Create: true,
248254
}.Apply(t)
@@ -327,7 +333,7 @@ func TestResourceWorkspaceFileCreate_Error(t *testing.T) {
327333
Method: http.MethodPost,
328334
Resource: "/api/2.0/workspace/import",
329335
ExpectedRequest: map[string]interface{}{
330-
"content": "YWJjCg==",
336+
"content": dummyWorkspaceFilePayload,
331337
"format": "AUTO",
332338
"overwrite": true,
333339
"path": "/path.py",
@@ -341,7 +347,7 @@ func TestResourceWorkspaceFileCreate_Error(t *testing.T) {
341347
},
342348
Resource: ResourceWorkspaceFile(),
343349
State: map[string]any{
344-
"content_base64": "YWJjCg==",
350+
"content_base64": dummyWorkspaceFilePayload,
345351
"path": "/path.py",
346352
},
347353
Create: true,
@@ -380,7 +386,7 @@ func TestResourceWorkspaceFileUpdate(t *testing.T) {
380386
ExpectedRequest: ws_api.Import{
381387
Format: "AUTO",
382388
Overwrite: true,
383-
Content: "YWJjCg==",
389+
Content: dummyWorkspaceFilePayload,
384390
Path: "abc",
385391
},
386392
},
@@ -396,7 +402,7 @@ func TestResourceWorkspaceFileUpdate(t *testing.T) {
396402
},
397403
Resource: ResourceWorkspaceFile(),
398404
State: map[string]any{
399-
"content_base64": "YWJjCg==",
405+
"content_base64": dummyWorkspaceFilePayload,
400406
"path": "/path.py",
401407
},
402408
ID: "abc",

0 commit comments

Comments
 (0)