From 4cc5236b814ff030a6fa762997f53fb797812c7e Mon Sep 17 00:00:00 2001 From: Miles Yucht Date: Wed, 30 Oct 2024 11:27:34 +0100 Subject: [PATCH] WIP --- .../providers/pluginfw/framework/resource.go | 137 ++++++++++++++++++ internal/providers/pluginfw/pluginfw.go | 15 +- 2 files changed, 150 insertions(+), 2 deletions(-) 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 53b361f99..449758aea 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/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/cluster" "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/library" "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/resources/notificationdestinations" @@ -42,14 +43,24 @@ func GetDatabricksProviderPluginFramework() provider.Provider { type DatabricksProviderPluginFramework struct { } +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 []func() resource.Resource{ + return preprocessResources([]func() resource.Resource{ qualitymonitor.ResourceQualityMonitor, library.ResourceLibrary, sharing.ResourceShare, - } + }) } func (p *DatabricksProviderPluginFramework) DataSources(ctx context.Context) []func() datasource.DataSource {