Skip to content

Commit 9bbb300

Browse files
authored
Updated docs and tests for databricks_volume resource (#2355)
* comments * comments * comments * update * upd
1 parent ad25de3 commit 9bbb300

File tree

4 files changed

+60
-53
lines changed

4 files changed

+60
-53
lines changed

catalog/resource_volume.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,18 @@ func ResourceVolume() *schema.Resource {
4848
}
4949
d.SetId(v.FullName)
5050

51-
// Update owner if it is provided
52-
if d.Get("owner") != "" {
53-
var updateVolumeRequestContent catalog.UpdateVolumeRequestContent
54-
common.DataToStructPointer(d, s, &updateVolumeRequestContent)
55-
updateVolumeRequestContent.FullNameArg = d.Id()
56-
_, err = w.Volumes.Update(ctx, updateVolumeRequestContent)
57-
if err != nil {
58-
return err
59-
}
51+
// Don't update owner if it is not provided
52+
if d.Get("owner") == "" {
53+
return nil
6054
}
6155

56+
var updateVolumeRequestContent catalog.UpdateVolumeRequestContent
57+
common.DataToStructPointer(d, s, &updateVolumeRequestContent)
58+
updateVolumeRequestContent.FullNameArg = d.Id()
59+
_, err = w.Volumes.Update(ctx, updateVolumeRequestContent)
60+
if err != nil {
61+
return err
62+
}
6263
return nil
6364
},
6465
Read: func(ctx context.Context, d *schema.ResourceData, c *common.DatabricksClient) error {

catalog/resource_volume_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestVolumesCreateWithoutInitialOwner(t *testing.T) {
4141
},
4242
{
4343
Method: http.MethodGet,
44-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
44+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
4545
Response: catalog.VolumeInfo{
4646
Name: "testName",
4747
VolumeType: catalog.VolumeType("testVolumeType"),
@@ -97,7 +97,7 @@ func TestVolumesCreateWithInitialOwner(t *testing.T) {
9797
},
9898
{
9999
Method: http.MethodGet,
100-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
100+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
101101
Response: catalog.VolumeInfo{
102102
Name: "testName",
103103
VolumeType: catalog.VolumeType("testVolumeType"),
@@ -110,7 +110,7 @@ func TestVolumesCreateWithInitialOwner(t *testing.T) {
110110
},
111111
{
112112
Method: http.MethodPatch,
113-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName",
113+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName",
114114
ExpectedRequest: catalog.UpdateVolumeRequestContent{
115115
Name: "testName",
116116
Comment: "This is a test comment.",
@@ -201,7 +201,7 @@ func TestVolumesCreateWithInitialOwner_Error(t *testing.T) {
201201
},
202202
{
203203
Method: http.MethodGet,
204-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
204+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
205205
Response: catalog.VolumeInfo{
206206
Name: "testName",
207207
VolumeType: catalog.VolumeType("testVolumeType"),
@@ -214,7 +214,7 @@ func TestVolumesCreateWithInitialOwner_Error(t *testing.T) {
214214
},
215215
{
216216
Method: http.MethodPatch,
217-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName",
217+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName",
218218
Response: apierr.APIErrorBody{
219219
ErrorCode: "SERVER_ERROR",
220220
Message: "Something unexpected happened",
@@ -241,7 +241,7 @@ func TestVolumesRead(t *testing.T) {
241241
Fixtures: []qa.HTTPFixture{
242242
{
243243
Method: http.MethodGet,
244-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
244+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
245245
Response: catalog.VolumeInfo{
246246
Name: "testName",
247247
VolumeType: catalog.VolumeType("testVolumeType"),
@@ -276,7 +276,7 @@ func TestResourceVolumeRead_Error(t *testing.T) {
276276
Fixtures: []qa.HTTPFixture{
277277
{
278278
Method: "GET",
279-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
279+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
280280
Response: apierr.APIErrorBody{
281281
ErrorCode: "INVALID_REQUEST",
282282
Message: "Internal error happened",
@@ -297,7 +297,7 @@ func TestVolumesUpdate(t *testing.T) {
297297
Fixtures: []qa.HTTPFixture{
298298
{
299299
Method: http.MethodGet,
300-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
300+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
301301
Response: catalog.VolumeInfo{
302302
Name: "testNameNew",
303303
VolumeType: catalog.VolumeType("testVolumeType"),
@@ -310,7 +310,7 @@ func TestVolumesUpdate(t *testing.T) {
310310
},
311311
{
312312
Method: http.MethodPatch,
313-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName",
313+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName",
314314
ExpectedRequest: catalog.UpdateVolumeRequestContent{
315315
Name: "testNameNew",
316316
Comment: "This is a new test comment.",
@@ -352,7 +352,7 @@ func TestVolumeUpdate_Error(t *testing.T) {
352352
Fixtures: []qa.HTTPFixture{
353353
{
354354
Method: http.MethodPatch,
355-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName",
355+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName",
356356
ExpectedRequest: catalog.UpdateVolumeRequestContent{
357357
Name: "testNameNew",
358358
Comment: "This is a new test comment.",
@@ -385,7 +385,7 @@ func TestVolumeDelete(t *testing.T) {
385385
Fixtures: []qa.HTTPFixture{
386386
{
387387
Method: http.MethodDelete,
388-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
388+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
389389
},
390390
},
391391
Resource: ResourceVolume(),
@@ -401,7 +401,7 @@ func TestVolumeDelete_Error(t *testing.T) {
401401
Fixtures: []qa.HTTPFixture{
402402
{
403403
Method: http.MethodDelete,
404-
Resource: "/api/2.1/unity-catalog/volumes/" + "testCatalogName.testSchemaName.testName" + "?",
404+
Resource: "/api/2.1/unity-catalog/volumes/testCatalogName.testSchemaName.testName?",
405405
Response: apierr.APIErrorBody{
406406
ErrorCode: "INVALID_STATE",
407407
Message: "Something went wrong",

docs/resources/volume.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,26 @@ resource "databricks_schema" "things" {
2929
}
3030
}
3131
32+
resource "databricks_storage_credential" "external" {
33+
name = "creds"
34+
aws_iam_role {
35+
role_arn = aws_iam_role.external_data_access.arn
36+
}
37+
}
38+
39+
resource "databricks_external_location" "some" {
40+
name = "external-location"
41+
url = "s3://${aws_s3_bucket.external.id}/some"
42+
credential_name = databricks_storage_credential.external.id
43+
}
44+
3245
resource "databricks_volume" "this" {
3346
name = "quickstart_volume"
3447
catalog_name = databricks_catalog.sandbox.name
3548
schema_name = databricks_schema.things.name
3649
owner = "volume_owner"
3750
volume_type = "EXTERNAL"
38-
storage_location = ""
51+
storage_location = databricks_external_location.some.url
3952
comment = "this volume is managed by terraform"
4053
}
4154
```

internal/acceptance/volume_test.go

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,28 @@
11
package acceptance
22

33
import (
4-
"fmt"
54
"testing"
6-
7-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
85
)
96

107
func TestUcAccVolumesResourceWithoutInitialOwnerAWSFullLifecycle(t *testing.T) {
11-
GetEnvOrSkipTest(t, "TEST_BUCKET")
12-
randomName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
138
unityWorkspaceLevel(t, step{
14-
Template: fmt.Sprintf(`
9+
Template: `
1510
resource "databricks_schema" "this" {
16-
name = "schema-%[1]s"
11+
name = "schema-{var.STICKY_RANDOM}"
1712
catalog_name = "main"
1813
}
1914
2015
resource "databricks_storage_credential" "external" {
21-
name = "cred-%[1]s"
16+
name = "cred-{var.STICKY_RANDOM}"
2217
aws_iam_role {
2318
role_arn = "{env.TEST_METASTORE_DATA_ACCESS_ARN}"
2419
}
2520
comment = "Managed by TF"
2621
}
2722
2823
resource "databricks_external_location" "some" {
29-
name = "external-%[1]s"
30-
url = "s3://{env.TEST_BUCKET}/somepath-%[1]s"
24+
name = "external-{var.STICKY_RANDOM}"
25+
url = "s3://{env.TEST_BUCKET}/somepath-{var.STICKY_RANDOM}"
3126
credential_name = databricks_storage_credential.external.id
3227
comment = "Managed by TF"
3328
}
@@ -39,25 +34,25 @@ func TestUcAccVolumesResourceWithoutInitialOwnerAWSFullLifecycle(t *testing.T) {
3934
schema_name = databricks_schema.this.name
4035
volume_type = "EXTERNAL"
4136
storage_location = databricks_external_location.some.url
42-
}`, randomName),
37+
}`,
4338
}, step{
44-
Template: fmt.Sprintf(`
39+
Template: `
4540
resource "databricks_schema" "this" {
46-
name = "schema-%[1]s"
41+
name = "schema-{var.STICKY_RANDOM}"
4742
catalog_name = "main"
4843
}
4944
5045
resource "databricks_storage_credential" "external" {
51-
name = "cred-%[1]s"
46+
name = "cred-{var.STICKY_RANDOM}"
5247
aws_iam_role {
5348
role_arn = "{env.TEST_METASTORE_DATA_ACCESS_ARN}"
5449
}
5550
comment = "Managed by TF"
5651
}
5752
5853
resource "databricks_external_location" "some" {
59-
name = "external-%[1]s"
60-
url = "s3://{env.TEST_BUCKET}/somepath-%[1]s"
54+
name = "external-{var.STICKY_RANDOM}"
55+
url = "s3://{env.TEST_BUCKET}/somepath-{var.STICKY_RANDOM}"
6156
credential_name = databricks_storage_credential.external.id
6257
comment = "Managed by TF"
6358
}
@@ -70,31 +65,29 @@ func TestUcAccVolumesResourceWithoutInitialOwnerAWSFullLifecycle(t *testing.T) {
7065
schema_name = databricks_schema.this.name
7166
volume_type = "EXTERNAL"
7267
storage_location = databricks_external_location.some.url
73-
}`, randomName),
68+
}`,
7469
})
7570
}
7671

7772
func TestUcAccVolumesResourceWithInitialOnwerAWSFullLifecycle(t *testing.T) {
78-
GetEnvOrSkipTest(t, "TEST_BUCKET")
79-
randomName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
8073
unityWorkspaceLevel(t, step{
81-
Template: fmt.Sprintf(`
74+
Template: `
8275
resource "databricks_schema" "this" {
83-
name = "schema-%[1]s"
76+
name = "schema-{var.STICKY_RANDOM}"
8477
catalog_name = "main"
8578
}
8679
8780
resource "databricks_storage_credential" "external" {
88-
name = "cred-%[1]s"
81+
name = "cred-{var.STICKY_RANDOM}"
8982
aws_iam_role {
9083
role_arn = "{env.TEST_METASTORE_DATA_ACCESS_ARN}"
9184
}
9285
comment = "Managed by TF"
9386
}
9487
9588
resource "databricks_external_location" "some" {
96-
name = "external-%[1]s"
97-
url = "s3://{env.TEST_BUCKET}/somepath-%[1]s"
89+
name = "external-{var.STICKY_RANDOM}"
90+
url = "s3://{env.TEST_BUCKET}/somepath-{var.STICKY_RANDOM}"
9891
credential_name = databricks_storage_credential.external.id
9992
comment = "Managed by TF"
10093
}
@@ -107,25 +100,25 @@ func TestUcAccVolumesResourceWithInitialOnwerAWSFullLifecycle(t *testing.T) {
107100
schema_name = databricks_schema.this.name
108101
volume_type = "EXTERNAL"
109102
storage_location = databricks_external_location.some.url
110-
}`, randomName),
103+
}`,
111104
}, step{
112-
Template: fmt.Sprintf(`
105+
Template: `
113106
resource "databricks_schema" "this" {
114-
name = "schema-%[1]s"
107+
name = "schema-{var.STICKY_RANDOM}"
115108
catalog_name = "main"
116109
}
117110
118111
resource "databricks_storage_credential" "external" {
119-
name = "cred-%[1]s"
112+
name = "cred-{var.STICKY_RANDOM}"
120113
aws_iam_role {
121114
role_arn = "{env.TEST_METASTORE_DATA_ACCESS_ARN}"
122115
}
123116
comment = "Managed by TF"
124117
}
125118
126119
resource "databricks_external_location" "some" {
127-
name = "external-%[1]s"
128-
url = "s3://{env.TEST_BUCKET}/somepath-%[1]s"
120+
name = "external-{var.STICKY_RANDOM}"
121+
url = "s3://{env.TEST_BUCKET}/somepath-{var.STICKY_RANDOM}"
129122
credential_name = databricks_storage_credential.external.id
130123
comment = "Managed by TF"
131124
}
@@ -138,6 +131,6 @@ func TestUcAccVolumesResourceWithInitialOnwerAWSFullLifecycle(t *testing.T) {
138131
schema_name = databricks_schema.this.name
139132
volume_type = "EXTERNAL"
140133
storage_location = databricks_external_location.some.url
141-
}`, randomName),
134+
}`,
142135
})
143136
}

0 commit comments

Comments
 (0)