From ce5792c256801fdff1bee8c3fb7cd782af6a0756 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Sep 2024 14:40:10 +0200 Subject: [PATCH] fix convertor and add unit test --- .../deploy/terraform/tfdyn/convert_volume.go | 19 ++--- .../terraform/tfdyn/convert_volume_test.go | 70 +++++++++++++++++++ 2 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 bundle/deploy/terraform/tfdyn/convert_volume_test.go diff --git a/bundle/deploy/terraform/tfdyn/convert_volume.go b/bundle/deploy/terraform/tfdyn/convert_volume.go index d841c7c60..f63e5069f 100644 --- a/bundle/deploy/terraform/tfdyn/convert_volume.go +++ b/bundle/deploy/terraform/tfdyn/convert_volume.go @@ -10,24 +10,15 @@ import ( "github.com/databricks/cli/libs/log" ) +// TODO: Articulate the consequences of deleting a UC volume in the prompt message that +// is displayed. func convertVolumeResource(ctx context.Context, vin dyn.Value) (dyn.Value, error) { // Normalize the output value to the target schema. - v, diags := convert.Normalize(schema.ResourceVolume{}, vin) + vout, diags := convert.Normalize(schema.ResourceVolume{}, vin) for _, diag := range diags { log.Debugf(ctx, "volume normalization diagnostic: %s", diag.Summary) } - // TODO: What happens if I try to delete a UC volume that has data in it? - // Do I need force destroy functionality here. - - // We always set force destroy as it allows DABs to manage the lifecycle - // of the schema. It's the responsibility of the CLI to ensure the user - // is adequately warned when they try to delete a UC Volume. - vout, err := dyn.SetByPath(v, dyn.MustPathFromString("force_destroy"), dyn.V(true)) - if err != nil { - return dyn.InvalidValue, err - } - return vout, nil } @@ -44,8 +35,8 @@ func (volumeConverter) Convert(ctx context.Context, key string, vin dyn.Value, o // Configure grants for this resource. if grants := convertGrantsResource(ctx, vin); grants != nil { - grants.Schema = fmt.Sprintf("${databricks_schema.%s.id}", key) - out.Grants["schema_"+key] = grants + grants.Volume = fmt.Sprintf("${databricks_volume.%s.id}", key) + out.Grants["volume_"+key] = grants } return nil diff --git a/bundle/deploy/terraform/tfdyn/convert_volume_test.go b/bundle/deploy/terraform/tfdyn/convert_volume_test.go new file mode 100644 index 000000000..ded32fcbc --- /dev/null +++ b/bundle/deploy/terraform/tfdyn/convert_volume_test.go @@ -0,0 +1,70 @@ +package tfdyn + +import ( + "context" + "testing" + + "github.com/databricks/cli/bundle/config/resources" + "github.com/databricks/cli/bundle/internal/tf/schema" + "github.com/databricks/cli/libs/dyn" + "github.com/databricks/cli/libs/dyn/convert" + "github.com/databricks/databricks-sdk-go/service/catalog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestConvertVolume(t *testing.T) { + var src = resources.Volume{ + CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ + CatalogName: "catalog", + Comment: "comment", + Name: "name", + SchemaName: "schema", + StorageLocation: "s3://bucket/path", + VolumeType: "EXTERNAL", + }, + Grants: []resources.Grant{ + { + Privileges: []string{"READ_VOLUME"}, + Principal: "jack@gmail.com", + }, + { + Privileges: []string{"WRITE_VOLUME"}, + Principal: "jane@gmail.com", + }, + }, + } + + vin, err := convert.FromTyped(src, dyn.NilValue) + require.NoError(t, err) + + ctx := context.Background() + out := schema.NewResources() + err = volumeConverter{}.Convert(ctx, "my_volume", vin, out) + require.NoError(t, err) + + // Assert equality on the volume + require.Equal(t, map[string]any{ + "catalog_name": "catalog", + "comment": "comment", + "name": "name", + "schema_name": "schema", + "storage_location": "s3://bucket/path", + "volume_type": "EXTERNAL", + }, out.Schema["my_volume"]) + + // Assert equality on the grants + assert.Equal(t, &schema.ResourceGrants{ + Volume: "${databricks_volume.my_volume.id}", + Grant: []schema.ResourceGrantsGrant{ + { + Privileges: []string{"READ_VOLUME"}, + Principal: "jack@gmail.com", + }, + { + Privileges: []string{"WRITE_VOLUME"}, + Principal: "jane@gmail.com", + }, + }, + }, out.Grants["volume_my_volume"]) +}