Skip to content

Commit d0a7413

Browse files
authored
Add computed volume_path attribute to databricks_volume resource (#3272)
* Add computed `volume_path` attribute to `databricks_volume` resource This attribute will expose path in form of `/Volumes/<catalog>/<schema>/<name>` for use with other resources, such as, `databricks_file`. Plus it's necessary for Terraform Exporter to detect references between resources. * Address review feedback
1 parent d3e6932 commit d0a7413

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

catalog/resource_volume.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func ResourceVolume() common.Resource {
4242
s := common.StructToSchema(VolumeInfo{},
4343
func(m map[string]*schema.Schema) map[string]*schema.Schema {
4444
m["storage_location"].DiffSuppressFunc = ucDirectoryPathSlashAndEmptySuppressDiff
45+
m["volume_path"] = &schema.Schema{
46+
Type: schema.TypeString,
47+
Computed: true,
48+
}
4549
return m
4650
})
4751
return common.Resource{
@@ -82,7 +86,11 @@ func ResourceVolume() common.Resource {
8286
if err != nil {
8387
return err
8488
}
85-
return common.StructToData(v, s, d)
89+
err = common.StructToData(v, s, d)
90+
if err != nil {
91+
return err
92+
}
93+
return d.Set("volume_path", "/Volumes/"+strings.ReplaceAll(v.FullName, ".", "/"))
8694
},
8795
Update: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {
8896
w, err := c.WorkspaceClient()
@@ -135,6 +143,7 @@ func ResourceVolume() common.Resource {
135143
// We need to update the resource Id because Name is updatable and FullName consists of Name,
136144
// So if we don't update the field then the requests would be made to old FullName which doesn't exists.
137145
d.SetId(v.FullName)
146+
d.Set("volume_path", "/Volumes/"+strings.ReplaceAll(v.FullName, ".", "/"))
138147
return nil
139148
},
140149
Delete: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {

catalog/resource_volume_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func TestVolumesRead(t *testing.T) {
270270
assert.Equal(t, "testCatalogName", d.Get("catalog_name"))
271271
assert.Equal(t, "testSchemaName", d.Get("schema_name"))
272272
assert.Equal(t, "This is a test comment.", d.Get("comment"))
273+
assert.Equal(t, "/Volumes/testCatalogName/testSchemaName/testName", d.Get("volume_path"))
273274
}
274275

275276
func TestResourceVolumeRead_Error(t *testing.T) {
@@ -357,6 +358,7 @@ func TestVolumesUpdate(t *testing.T) {
357358
assert.Equal(t, "testCatalogName", d.Get("catalog_name"))
358359
assert.Equal(t, "testSchemaName", d.Get("schema_name"))
359360
assert.Equal(t, "This is a new test comment.", d.Get("comment"))
361+
assert.Equal(t, "/Volumes/testCatalogName/testSchemaName/testNameNew", d.Get("volume_path"))
360362
}
361363

362364
func TestVolumesUpdateForceNewOnCatalog(t *testing.T) {

docs/resources/volume.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ The following arguments are supported:
9393
In addition to all arguments above, the following attributes are exported:
9494

9595
* `id` - ID of this Unity Catalog Volume in form of `<catalog>.<schema>.<name>`.
96+
* `volume_path` - base file path for this Unity Catalog Volume in form of `/Volumes/<catalog>/<schema>/<name>`.
9697

9798
## Import
9899

0 commit comments

Comments
 (0)