diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 216c0fca42..be3f14d8ec 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -12,6 +12,7 @@ * Remove incorrect customization for `databricks_catalog` ([#5021](https://github.com/databricks/terraform-provider-databricks/pull/5021)) * Fix filling of `active` attribute in `databricks_user` data source ([#5026](https://github.com/databricks/terraform-provider-databricks/pull/5026)) +* Add support for share resource in plugin framework implementation to be SDKv2 compatible ([#4965](https://github.com/databricks/terraform-provider-databricks/pull/4965)) ### Documentation diff --git a/internal/providers/pluginfw/products/sharing/resource_acc_test.go b/internal/providers/pluginfw/products/sharing/resource_acc_test.go index 3b2ac3ee68..82f5aae302 100644 --- a/internal/providers/pluginfw/products/sharing/resource_acc_test.go +++ b/internal/providers/pluginfw/products/sharing/resource_acc_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/databricks/terraform-provider-databricks/internal/acceptance" + "github.com/hashicorp/terraform-plugin-testing/terraform" ) const preTestTemplate = ` @@ -230,3 +231,41 @@ func TestUcAccUpdateShareReorderObject(t *testing.T) { }`, }) } + +func shareUpdateWithName(name string) string { + return fmt.Sprintf(`resource "databricks_share_pluginframework" "myshare" { + name = "%s" + owner = "account users" + object { + name = databricks_sql_table.mytable.id + comment = "A" + data_object_type = "TABLE" + history_data_sharing_status = "ENABLED" + } + }`, name) +} + +func shareCheckStateforID() func(s *terraform.State) error { + return func(s *terraform.State) error { + r, ok := s.RootModule().Resources["databricks_share_pluginframework.myshare"] + if !ok { + return fmt.Errorf("resource not found in state") + } + id := r.Primary.Attributes["id"] + name := r.Primary.Attributes["name"] + if id != name { + return fmt.Errorf("resource ID is not equal to the name. Attributes: %v", r.Primary.Attributes) + } + return nil + } +} + +func TestUcAccUpdateShareName(t *testing.T) { + acceptance.UnityWorkspaceLevel(t, acceptance.Step{ + Template: preTestTemplate + shareUpdateWithName("{var.STICKY_RANDOM}-terraform-delta-share-before"), + Check: shareCheckStateforID(), + }, acceptance.Step{ + Template: preTestTemplate + shareUpdateWithName("{var.STICKY_RANDOM}-terraform-delta-share-after"), + Check: shareCheckStateforID(), + }) +} diff --git a/internal/providers/pluginfw/products/sharing/resource_share.go b/internal/providers/pluginfw/products/sharing/resource_share.go index f451c90cf5..179c6dd937 100644 --- a/internal/providers/pluginfw/products/sharing/resource_share.go +++ b/internal/providers/pluginfw/products/sharing/resource_share.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/types" ) const resourceName = "share" @@ -31,6 +32,7 @@ func ResourceShare() resource.Resource { type ShareInfoExtended struct { sharing_tf.ShareInfo_SdkV2 + ID types.String `tfsdk:"id"` // Adding ID field to stay compatible with SDKv2 } var _ pluginfwcommon.ComplexFieldTypeProvider = ShareInfoExtended{} @@ -156,6 +158,8 @@ func (r *ShareResource) Schema(ctx context.Context, req resource.SchemaRequest, c.SetRequired("object", "partition", "value", "op") c.SetRequired("object", "partition", "value", "name") + c.SetComputed("id") + return c }) resp.Schema = schema.Schema{ @@ -228,6 +232,8 @@ func (r *ShareResource) Create(ctx context.Context, req resource.CreateRequest, return } + newState.ID = newState.Name + resp.Diagnostics.Append(resp.State.Set(ctx, newState)...) }