Skip to content

Commit b6bfa78

Browse files
tanmay-dbnkvuong
andauthored
[Internal] Make databricks_share_pluginframework SDKv2 compatible (#4965)
## Changes <!-- Summary of your changes that are easy to understand --> Plugin framework implementation of share resource is not compatible with SDKv2. Compatibility difference arises because of how these libraries apply the changes internally: Plugin Framework checks if the state is null for creation: https://github.com/hashicorp/terraform-plugin-framework/blob/main/internal/fwserver/server_applyresourcechange.go#L47 ````go // If PriorState is missing/null, its a Create request. if req.PriorState == nil || req.PriorState.Raw.IsNull() {...} ```` Whereas SDKv2 checks the ID: https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/schema/resource.go#L976 ````go if data.Id() == "" { // We're creating, it is a new resource. ... } ```` Because of the incompatibility, SDKv2 re-creates the share resource after it has been created by plugin framework. The PR adds the ID field as part of schema. This change makes the plugin framework implementation compatible with SDKv2 ## Tests <!-- How is this tested? Please see the checklist below and also describe any other relevant tests --> - Integration tests - Also E2E tested for applying share resource in SDKv2 after it's been applied by plugin framework in: #4967 --------- Co-authored-by: vuong-nguyen <[email protected]>
1 parent 45e7436 commit b6bfa78

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
* Remove incorrect customization for `databricks_catalog` ([#5021](https://github.com/databricks/terraform-provider-databricks/pull/5021))
1414
* Fix filling of `active` attribute in `databricks_user` data source ([#5026](https://github.com/databricks/terraform-provider-databricks/pull/5026))
15+
* Add support for share resource in plugin framework implementation to be SDKv2 compatible ([#4965](https://github.com/databricks/terraform-provider-databricks/pull/4965))
1516

1617
### Documentation
1718

internal/providers/pluginfw/products/sharing/resource_acc_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/databricks/terraform-provider-databricks/internal/acceptance"
8+
"github.com/hashicorp/terraform-plugin-testing/terraform"
89
)
910

1011
const preTestTemplate = `
@@ -230,3 +231,41 @@ func TestUcAccUpdateShareReorderObject(t *testing.T) {
230231
}`,
231232
})
232233
}
234+
235+
func shareUpdateWithName(name string) string {
236+
return fmt.Sprintf(`resource "databricks_share_pluginframework" "myshare" {
237+
name = "%s"
238+
owner = "account users"
239+
object {
240+
name = databricks_sql_table.mytable.id
241+
comment = "A"
242+
data_object_type = "TABLE"
243+
history_data_sharing_status = "ENABLED"
244+
}
245+
}`, name)
246+
}
247+
248+
func shareCheckStateforID() func(s *terraform.State) error {
249+
return func(s *terraform.State) error {
250+
r, ok := s.RootModule().Resources["databricks_share_pluginframework.myshare"]
251+
if !ok {
252+
return fmt.Errorf("resource not found in state")
253+
}
254+
id := r.Primary.Attributes["id"]
255+
name := r.Primary.Attributes["name"]
256+
if id != name {
257+
return fmt.Errorf("resource ID is not equal to the name. Attributes: %v", r.Primary.Attributes)
258+
}
259+
return nil
260+
}
261+
}
262+
263+
func TestUcAccUpdateShareName(t *testing.T) {
264+
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
265+
Template: preTestTemplate + shareUpdateWithName("{var.STICKY_RANDOM}-terraform-delta-share-before"),
266+
Check: shareCheckStateforID(),
267+
}, acceptance.Step{
268+
Template: preTestTemplate + shareUpdateWithName("{var.STICKY_RANDOM}-terraform-delta-share-after"),
269+
Check: shareCheckStateforID(),
270+
})
271+
}

internal/providers/pluginfw/products/sharing/resource_share.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
2020
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
2121
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
22+
"github.com/hashicorp/terraform-plugin-framework/types"
2223
)
2324

2425
const resourceName = "share"
@@ -31,6 +32,7 @@ func ResourceShare() resource.Resource {
3132

3233
type ShareInfoExtended struct {
3334
sharing_tf.ShareInfo_SdkV2
35+
ID types.String `tfsdk:"id"` // Adding ID field to stay compatible with SDKv2
3436
}
3537

3638
var _ pluginfwcommon.ComplexFieldTypeProvider = ShareInfoExtended{}
@@ -156,6 +158,8 @@ func (r *ShareResource) Schema(ctx context.Context, req resource.SchemaRequest,
156158
c.SetRequired("object", "partition", "value", "op")
157159
c.SetRequired("object", "partition", "value", "name")
158160

161+
c.SetComputed("id")
162+
159163
return c
160164
})
161165
resp.Schema = schema.Schema{
@@ -228,6 +232,8 @@ func (r *ShareResource) Create(ctx context.Context, req resource.CreateRequest,
228232
return
229233
}
230234

235+
newState.ID = newState.Name
236+
231237
resp.Diagnostics.Append(resp.State.Set(ctx, newState)...)
232238
}
233239

0 commit comments

Comments
 (0)