Skip to content

Commit

Permalink
[Internal] Migrate Share Resource to Plugin Framework (#4047)
Browse files Browse the repository at this point in the history
## Changes
<!-- Summary of your changes that are easy to understand -->
This PR migrates the share resource to the Plugin framework. The code
was largely copied "as is" from the previous implementation of the share
resource, with the necessary adaptations made for integration with the
Plugin framework.

This implementation utilizes the newly generated Effective fields to
provide the functionality that was previously achieved through diff
suppression.

## Tests
<!-- 
How is this tested? Please see the checklist below and also describe any
other relevant tests
-->

- [x] `make test` run locally
- [ ] relevant change in `docs/` folder
- [x] covered with integration tests in `internal/acceptance`
- [x] relevant acceptance tests are passing
- [x] using Go SDK

---------

Co-authored-by: Omer Lachish <[email protected]>
  • Loading branch information
rauchy and rauchy authored Oct 29, 2024
1 parent f382e4f commit 0975310
Show file tree
Hide file tree
Showing 12 changed files with 702 additions and 1 deletion.
1 change: 1 addition & 0 deletions .codegen/model.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (newState *{{.PascalName}}) SyncEffectiveFieldsDuringRead(existingState {{.
{{- if .Entity.IsFloat64}}{{$type = "Float64"}}{{end}}
{{- if .Entity.IsInt}}{{$type = "Int64"}}{{end}}
{{- if .Entity.Enum}}{{$type = "String"}}{{end}}
newState.Effective{{.PascalName}} = existingState.Effective{{.PascalName}}
if existingState.Effective{{.PascalName}}.Value{{$type}}() == newState.{{.PascalName}}.Value{{$type}}() {
newState.{{.PascalName}} = existingState.{{.PascalName}}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/providers/pluginfw/converters/tf_to_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func tfsdkToGoSdkStructField(srcField reflect.Value, destField reflect.Value, sr
// This is the case for enum.

// Skip unset value.
if srcField.IsZero() {
if srcField.IsZero() || v.ValueString() == "" {
return
}

Expand Down
2 changes: 2 additions & 0 deletions internal/providers/pluginfw/pluginfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/notificationdestinations"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/qualitymonitor"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/registered_model"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/sharing"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/volume"

"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -47,6 +48,7 @@ func (p *DatabricksProviderPluginFramework) Resources(ctx context.Context) []fun
return []func() resource.Resource{
qualitymonitor.ResourceQualityMonitor,
library.ResourceLibrary,
sharing.ResourceShare,
}
}

Expand Down
204 changes: 204 additions & 0 deletions internal/providers/pluginfw/resources/sharing/resource_acc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package sharing_test

import (
"fmt"
"testing"

"github.com/databricks/terraform-provider-databricks/internal/acceptance"
)

const preTestTemplate = `
resource "databricks_catalog" "sandbox" {
name = "sandbox{var.STICKY_RANDOM}"
comment = "this catalog is managed by terraform"
properties = {
purpose = "testing"
}
}
resource "databricks_schema" "things" {
catalog_name = databricks_catalog.sandbox.id
name = "things{var.STICKY_RANDOM}"
comment = "this database is managed by terraform"
properties = {
kind = "various"
}
}
resource "databricks_table" "mytable" {
catalog_name = databricks_catalog.sandbox.id
schema_name = databricks_schema.things.name
name = "bar"
table_type = "MANAGED"
data_source_format = "DELTA"
column {
name = "id"
position = 0
type_name = "INT"
type_text = "int"
type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}"
}
}
resource "databricks_table" "mytable_2" {
catalog_name = databricks_catalog.sandbox.id
schema_name = databricks_schema.things.name
name = "bar_2"
table_type = "MANAGED"
data_source_format = "DELTA"
column {
name = "id"
position = 0
type_name = "INT"
type_text = "int"
type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}"
}
}
resource "databricks_table" "mytable_3" {
catalog_name = databricks_catalog.sandbox.id
schema_name = databricks_schema.things.name
name = "bar_3"
table_type = "MANAGED"
data_source_format = "DELTA"
column {
name = "id"
position = 0
type_name = "INT"
type_text = "int"
type_json = "{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}"
}
}
`

const preTestTemplateUpdate = `
resource "databricks_grants" "some" {
catalog = databricks_catalog.sandbox.id
grant {
principal = "account users"
privileges = ["ALL_PRIVILEGES"]
}
grant {
principal = "{env.TEST_METASTORE_ADMIN_GROUP_NAME}"
privileges = ["ALL_PRIVILEGES"]
}
}
`

func TestUcAccCreateShare(t *testing.T) {
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
Template: preTestTemplate + `
resource "databricks_share_pluginframework" "myshare" {
name = "{var.STICKY_RANDOM}-terraform-delta-share"
owner = "account users"
object {
name = databricks_table.mytable.id
comment = "c"
data_object_type = "TABLE"
}
object {
name = databricks_table.mytable_2.id
cdf_enabled = false
comment = "c"
data_object_type = "TABLE"
}
}
resource "databricks_recipient" "db2open" {
name = "{var.STICKY_RANDOM}-terraform-db2open-recipient"
comment = "made by terraform"
authentication_type = "TOKEN"
sharing_code = "{var.STICKY_RANDOM}"
ip_access_list {
// using private ip for acc testing
allowed_ip_addresses = ["10.0.0.0/16"]
}
}
resource "databricks_grants" "some" {
share = databricks_share_pluginframework.myshare.name
grant {
principal = databricks_recipient.db2open.name
privileges = ["SELECT"]
}
}
`,
})
}

func shareTemplateWithOwner(comment string, owner string) string {
return fmt.Sprintf(`
resource "databricks_share_pluginframework" "myshare" {
name = "{var.STICKY_RANDOM}-terraform-delta-share"
owner = "%s"
object {
name = databricks_table.mytable.id
comment = "%s"
data_object_type = "TABLE"
history_data_sharing_status = "DISABLED"
}
}`, owner, comment)
}

func TestUcAccUpdateShare(t *testing.T) {
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
Template: preTestTemplate + preTestTemplateUpdate + shareTemplateWithOwner("c", "account users"),
}, acceptance.Step{
Template: preTestTemplate + preTestTemplateUpdate + shareTemplateWithOwner("e", "account users"),
}, acceptance.Step{
Template: preTestTemplate + preTestTemplateUpdate + shareTemplateWithOwner("e", "{env.TEST_DATA_ENG_GROUP}"),
}, acceptance.Step{
Template: preTestTemplate + preTestTemplateUpdate + shareTemplateWithOwner("f", "{env.TEST_METASTORE_ADMIN_GROUP_NAME}"),
})
}

func TestUcAccUpdateShareAddObject(t *testing.T) {
acceptance.UnityWorkspaceLevel(t, acceptance.Step{
Template: preTestTemplate + preTestTemplateUpdate +
`resource "databricks_share_pluginframework" "myshare" {
name = "{var.STICKY_RANDOM}-terraform-delta-share"
owner = "account users"
object {
name = databricks_table.mytable.id
comment = "A"
data_object_type = "TABLE"
history_data_sharing_status = "DISABLED"
}
object {
name = databricks_table.mytable_3.id
comment = "C"
data_object_type = "TABLE"
history_data_sharing_status = "DISABLED"
}
}`,
}, acceptance.Step{
Template: preTestTemplate + preTestTemplateUpdate +
`resource "databricks_share_pluginframework" "myshare" {
name = "{var.STICKY_RANDOM}-terraform-delta-share"
owner = "account users"
object {
name = databricks_table.mytable.id
comment = "AA"
data_object_type = "TABLE"
history_data_sharing_status = "DISABLED"
}
object {
name = databricks_table.mytable_2.id
comment = "BB"
data_object_type = "TABLE"
history_data_sharing_status = "DISABLED"
}
object {
name = databricks_table.mytable_3.id
comment = "CC"
data_object_type = "TABLE"
history_data_sharing_status = "DISABLED"
}
}`,
})
}
Loading

0 comments on commit 0975310

Please sign in to comment.