Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Internal] Migrate Share Resource to Plugin Framework #4047

Merged
merged 12 commits into from
Oct 29, 2024
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add an integration test which adds an object in middle in the update step? This was a limitation in SDKv2 as ordering used to matter whereas now it should work with plugin framework.

Copy link
Contributor

@tanmay-db tanmay-db Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would fail currently because object in ShareInfo that we use for schema is a list and it should be set instead.


type ShareInfoExtended struct {
	sharing_tf.ShareInfo
}

....

Objects []SharedDataObject `tfsdk:"object" tf:"optional"`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Added support + integration test in 4b7eb0e

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
Loading