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

[WIP] Generic Resource Wrapper #4172

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions internal/providers/pluginfw/framework/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package framework

import (
"context"

pluginfwcontext "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/context"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

func WrapResource(r resource.Resource) resource.Resource {
var wrapped resource.Resource = r
if c, ok := wrapped.(resource.ResourceWithConfigure); ok {
wrapped = Configurer{c}
}
if c, ok := wrapped.(resource.ResourceWithConfigValidators); ok {
wrapped = ConfigValidatorer{c}
}
if c, ok := wrapped.(resource.ResourceWithImportState); ok {
wrapped = StateImporter{c}
}
if c, ok := wrapped.(resource.ResourceWithModifyPlan); ok {
wrapped = PlanModifier{c}
}
if c, ok := wrapped.(resource.ResourceWithMoveState); ok {
wrapped = StateMover{c}
}
if c, ok := wrapped.(resource.ResourceWithUpgradeState); ok {
wrapped = StateUpgrader{c}
}
if c, ok := wrapped.(resource.ResourceWithValidateConfig); ok {
wrapped = ConfigValidator{c}
}
return wrapped
}

type Resource struct {
resource.Resource
}

func configureContext(ctx context.Context, r resource.Resource) context.Context {
resp := &resource.MetadataResponse{}
r.Metadata(ctx, resource.MetadataRequest{}, resp)
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resp.TypeName)
return ctx
}

func (r Resource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
r.Resource.Metadata(ctx, req, resp)
}

func (r Resource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
r.Resource.Schema(ctx, req, resp)
}

func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
ctx = configureContext(ctx, r)
r.Resource.Create(ctx, req, resp)
}

func (r Resource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
ctx = configureContext(ctx, r)
r.Resource.Read(ctx, req, resp)
}

func (r Resource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
ctx = configureContext(ctx, r)
r.Resource.Update(ctx, req, resp)
}

func (r Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
ctx = configureContext(ctx, r)
r.Resource.Delete(ctx, req, resp)
}

type Configurer struct {
resource.ResourceWithConfigure
}

func (c Configurer) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
ctx = configureContext(ctx, c)
c.ResourceWithConfigure.Configure(ctx, req, resp)
}

type ConfigValidatorer struct {
resource.ResourceWithConfigValidators
}

func (c ConfigValidatorer) ConfigValidators(ctx context.Context) []resource.ConfigValidator {
ctx = configureContext(ctx, c)
return c.ResourceWithConfigValidators.ConfigValidators(ctx)
}

type StateImporter struct {
resource.ResourceWithImportState
}

func (s StateImporter) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
ctx = configureContext(ctx, s)
s.ResourceWithImportState.ImportState(ctx, req, resp)
}

type PlanModifier struct {
resource.ResourceWithModifyPlan
}

func (p PlanModifier) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
ctx = configureContext(ctx, p)
p.ResourceWithModifyPlan.ModifyPlan(ctx, req, resp)
}

type StateMover struct {
resource.ResourceWithMoveState
}

func (s StateMover) MoveState(ctx context.Context) []resource.StateMover {
ctx = configureContext(ctx, s)
return s.ResourceWithMoveState.MoveState(ctx)
}

type StateUpgrader struct {
resource.ResourceWithUpgradeState
}

func (s StateUpgrader) UpgradeState(ctx context.Context) map[int64]resource.StateUpgrader {
ctx = configureContext(ctx, s)
return s.ResourceWithUpgradeState.UpgradeState(ctx)
}

type ConfigValidator struct {
resource.ResourceWithValidateConfig
}

func (c ConfigValidator) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
ctx = configureContext(ctx, c)
c.ResourceWithValidateConfig.ValidateConfig(ctx, req, resp)
}
13 changes: 12 additions & 1 deletion internal/providers/pluginfw/pluginfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/databricks/terraform-provider-databricks/commands"
"github.com/databricks/terraform-provider-databricks/common"
providercommon "github.com/databricks/terraform-provider-databricks/internal/providers/common"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/framework"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand All @@ -35,10 +36,20 @@ type DatabricksProviderPluginFramework struct {
sdkV2Fallbacks []SdkV2FallbackOption
}

func preprocessResources(resources []func() resource.Resource) []func() resource.Resource {
var res []func() resource.Resource
for _, r := range resources {
res = append(res, func() resource.Resource {
return framework.WrapResource(r())
})
}
return res
}

var _ provider.Provider = (*DatabricksProviderPluginFramework)(nil)

func (p *DatabricksProviderPluginFramework) Resources(ctx context.Context) []func() resource.Resource {
return getPluginFrameworkResourcesToRegister(p.sdkV2Fallbacks...)
return preprocessResources(getPluginFrameworkResourcesToRegister(p.sdkV2Fallbacks...))
}

func (p *DatabricksProviderPluginFramework) DataSources(ctx context.Context) []func() datasource.DataSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/databricks/terraform-provider-databricks/clusters"
"github.com/databricks/terraform-provider-databricks/common"
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
pluginfwcontext "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/context"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/databricks/terraform-provider-databricks/internal/service/compute_tf"
Expand Down Expand Up @@ -105,7 +104,6 @@ func (r *LibraryResource) Configure(ctx context.Context, req resource.ConfigureR
}

func (r *LibraryResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -150,7 +148,6 @@ func (r *LibraryResource) Create(ctx context.Context, req resource.CreateRequest
}

func (r *LibraryResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -188,7 +185,6 @@ func (r *LibraryResource) Update(ctx context.Context, req resource.UpdateRequest
}

func (r *LibraryResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/databricks/databricks-sdk-go/service/catalog"
"github.com/databricks/terraform-provider-databricks/common"
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
pluginfwcontext "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/context"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/databricks/terraform-provider-databricks/internal/service/catalog_tf"
Expand Down Expand Up @@ -99,7 +98,6 @@ func (d *QualityMonitorResource) ImportState(ctx context.Context, req resource.I
}

func (r *QualityMonitorResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -139,7 +137,6 @@ func (r *QualityMonitorResource) Create(ctx context.Context, req resource.Create
}

func (r *QualityMonitorResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -172,7 +169,6 @@ func (r *QualityMonitorResource) Read(ctx context.Context, req resource.ReadRequ
}

func (r *QualityMonitorResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -215,7 +211,6 @@ func (r *QualityMonitorResource) Update(ctx context.Context, req resource.Update
}

func (r *QualityMonitorResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)
w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/databricks/databricks-sdk-go/service/sharing"
"github.com/databricks/terraform-provider-databricks/common"
pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common"
pluginfwcontext "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/context"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters"
"github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema"
"github.com/databricks/terraform-provider-databricks/internal/service/sharing_tf"
Expand Down Expand Up @@ -163,8 +162,6 @@ func (d *ShareResource) Configure(ctx context.Context, req resource.ConfigureReq
}

func (r *ShareResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)

w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -226,8 +223,6 @@ func (r *ShareResource) Create(ctx context.Context, req resource.CreateRequest,
}

func (r *ShareResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)

var existingState ShareInfoExtended
resp.Diagnostics.Append(req.State.Get(ctx, &existingState)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -281,8 +276,6 @@ func (r *ShareResource) Read(ctx context.Context, req resource.ReadRequest, resp
}

func (r *ShareResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)

var state ShareInfoExtended
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -380,8 +373,6 @@ func (r *ShareResource) Update(ctx context.Context, req resource.UpdateRequest,
}

func (r *ShareResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
ctx = pluginfwcontext.SetUserAgentInResourceContext(ctx, resourceName)

w, diags := r.Client.GetWorkspaceClient()
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
Expand Down
Loading