From 812283f9c892a7ac4781d8e6b2290c7fbb5ad67d Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Wed, 30 Oct 2024 11:27:34 +0100 Subject: [PATCH 1/3] WIP --- .../providers/pluginfw/framework/resource.go | 137 ++++++++++++++++++ internal/providers/pluginfw/pluginfw.go | 13 +- 2 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 internal/providers/pluginfw/framework/resource.go diff --git a/internal/providers/pluginfw/framework/resource.go b/internal/providers/pluginfw/framework/resource.go new file mode 100644 index 000000000..6fd4fd3fb --- /dev/null +++ b/internal/providers/pluginfw/framework/resource.go @@ -0,0 +1,137 @@ +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 + 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) +} diff --git a/internal/providers/pluginfw/pluginfw.go b/internal/providers/pluginfw/pluginfw.go index 4eaecd993..a22d49407 100644 --- a/internal/providers/pluginfw/pluginfw.go +++ b/internal/providers/pluginfw/pluginfw.go @@ -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" @@ -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 { From 22dc110b14b7b12be018ba33fcfde97a7973ccd7 Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Sun, 3 Nov 2024 18:03:11 +0100 Subject: [PATCH 2/3] avoid wrapping r and lose signatures --- internal/providers/pluginfw/framework/resource.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/providers/pluginfw/framework/resource.go b/internal/providers/pluginfw/framework/resource.go index 6fd4fd3fb..b3980a6e0 100644 --- a/internal/providers/pluginfw/framework/resource.go +++ b/internal/providers/pluginfw/framework/resource.go @@ -8,8 +8,7 @@ import ( ) func WrapResource(r resource.Resource) resource.Resource { - var wrapped resource.Resource - wrapped = Resource{Resource: r} + var wrapped resource.Resource = r if c, ok := wrapped.(resource.ResourceWithConfigure); ok { wrapped = Configurer{c} } From 6f73013f43e5672380bc38d2862c875e834db034 Mon Sep 17 00:00:00 2001 From: Omer Lachish Date: Sun, 3 Nov 2024 18:04:28 +0100 Subject: [PATCH 3/3] initialize context in base resource --- .../pluginfw/resources/library/resource_library.go | 4 ---- .../resources/qualitymonitor/resource_quality_monitor.go | 5 ----- .../pluginfw/resources/sharing/resource_share.go | 9 --------- 3 files changed, 18 deletions(-) diff --git a/internal/providers/pluginfw/resources/library/resource_library.go b/internal/providers/pluginfw/resources/library/resource_library.go index 17ac722bf..95cc36e2a 100644 --- a/internal/providers/pluginfw/resources/library/resource_library.go +++ b/internal/providers/pluginfw/resources/library/resource_library.go @@ -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" @@ -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() { @@ -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() { @@ -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() { diff --git a/internal/providers/pluginfw/resources/qualitymonitor/resource_quality_monitor.go b/internal/providers/pluginfw/resources/qualitymonitor/resource_quality_monitor.go index 7a0445ddb..f87d2b28c 100644 --- a/internal/providers/pluginfw/resources/qualitymonitor/resource_quality_monitor.go +++ b/internal/providers/pluginfw/resources/qualitymonitor/resource_quality_monitor.go @@ -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" @@ -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() { @@ -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() { @@ -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() { @@ -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() { diff --git a/internal/providers/pluginfw/resources/sharing/resource_share.go b/internal/providers/pluginfw/resources/sharing/resource_share.go index b96cd0e97..ce6b6d88b 100644 --- a/internal/providers/pluginfw/resources/sharing/resource_share.go +++ b/internal/providers/pluginfw/resources/sharing/resource_share.go @@ -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" @@ -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() { @@ -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() { @@ -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() { @@ -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() {