From d6428800427227a55ebd988be56b7f4cc74a14c3 Mon Sep 17 00:00:00 2001 From: Tanmay Rustagi Date: Mon, 29 Jul 2024 01:45:15 +0200 Subject: [PATCH] TF Apply works --- pluginframework/resource_quality_monitor.go | 46 +++++++++++++------ .../quality-monitor/quality_monitor.tf | 18 ++++---- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/pluginframework/resource_quality_monitor.go b/pluginframework/resource_quality_monitor.go index 6ef4398b3f..01f021faca 100644 --- a/pluginframework/resource_quality_monitor.go +++ b/pluginframework/resource_quality_monitor.go @@ -52,7 +52,8 @@ func (r *QualityMonitorResource) Metadata(ctx context.Context, req resource.Meta func (r *QualityMonitorResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ Description: "Terraform schema for Databricks Lakehouse Monitor. MonitorInfo struct is used to create the schema", - Attributes: common.PluginFrameworkResourceStructToSchemaMap(catalog_tf.MonitorInfo{}), + // TODO: Add CustomizeSchemaPath once it is supported in the plugin framework + Attributes: common.PluginFrameworkResourceStructToSchemaMap(catalog_tf.MonitorInfo{}), } } @@ -77,13 +78,20 @@ func (r *QualityMonitorResource) Create(ctx context.Context, req resource.Create resp.Diagnostics.AddError("Failed to get workspace client", err.Error()) return } - var create catalog.CreateMonitor - diags := req.Plan.Get(ctx, &create) + var createMonitorTfSDK catalog_tf.CreateMonitor + diags := req.Plan.Get(ctx, &createMonitorTfSDK) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } - endpoint, err := w.QualityMonitors.Create(ctx, create) + + var createMonitorGoSDK catalog.CreateMonitor + err = common.TfSdkToGoSdkStruct(createMonitorTfSDK, &createMonitorGoSDK, ctx) + if err != nil { + resp.Diagnostics.AddError("Failed to convert Tf SDK struct to Go SDK struct", err.Error()) + return + } + endpoint, err := w.QualityMonitors.Create(ctx, createMonitorGoSDK) if err != nil { resp.Diagnostics.AddError("Failed to get create monitor", err.Error()) return @@ -101,18 +109,23 @@ func (r *QualityMonitorResource) Read(ctx context.Context, req resource.ReadRequ resp.Diagnostics.AddError("Failed to get workspace client", err.Error()) return } - var getMonitor catalog.GetQualityMonitorRequest + var getMonitor catalog_tf.GetQualityMonitorRequest diags := req.State.Get(ctx, &getMonitor) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } - endpoint, err := w.QualityMonitors.GetByTableName(ctx, getMonitor.TableName) + endpoint, err := w.QualityMonitors.GetByTableName(ctx, getMonitor.TableName.ValueString()) if err != nil { resp.Diagnostics.AddError("Failed to get monitor", err.Error()) return } - diags = resp.State.Set(ctx, endpoint) + var monitorInfoTfSDK catalog_tf.MonitorInfo + err = common.GoSdkToTfSdkStruct(endpoint, &monitorInfoTfSDK, ctx) + if err != nil { + resp.Diagnostics.AddError("Failed to convert Go SDK struct to TF SDK struct", err.Error()) + } + diags = resp.State.Set(ctx, monitorInfoTfSDK) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -125,18 +138,25 @@ func (r *QualityMonitorResource) Update(ctx context.Context, req resource.Update resp.Diagnostics.AddError("Failed to get workspace client", err.Error()) return } - var updateRequest catalog.UpdateMonitor - diags := req.Plan.Get(ctx, &updateRequest) + var updateMonitorTfSDK catalog_tf.UpdateMonitor + diags := req.Plan.Get(ctx, &updateMonitorTfSDK) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } - _, err = w.QualityMonitors.Update(ctx, updateRequest) + + var updateMonitorGoSDK catalog.UpdateMonitor + err = common.TfSdkToGoSdkStruct(updateMonitorTfSDK, &updateMonitorGoSDK, ctx) + if err != nil { + resp.Diagnostics.AddError("Failed to convert Tf SDK struct to Go SDK struct", err.Error()) + return + } + _, err = w.QualityMonitors.Update(ctx, updateMonitorGoSDK) if err != nil { resp.Diagnostics.AddError("Failed to update monitor", err.Error()) return } - err = WaitForMonitor(w, ctx, updateRequest.TableName) + err = WaitForMonitor(w, ctx, updateMonitorGoSDK.TableName) if err != nil { resp.Diagnostics.AddError("Failed to get updated monitor", err.Error()) return @@ -149,13 +169,13 @@ func (r *QualityMonitorResource) Delete(ctx context.Context, req resource.Delete resp.Diagnostics.AddError("Failed to get workspace client", err.Error()) return } - var deleteRequest catalog.DeleteQualityMonitorRequest + var deleteRequest catalog_tf.DeleteQualityMonitorRequest diags := req.State.Get(ctx, &deleteRequest) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } - err = w.QualityMonitors.DeleteByTableName(ctx, deleteRequest.TableName) + err = w.QualityMonitors.DeleteByTableName(ctx, deleteRequest.TableName.ValueString()) if err != nil { resp.Diagnostics.AddError("Failed to delete monitor", err.Error()) return diff --git a/pluginframework/test/resource/quality-monitor/quality_monitor.tf b/pluginframework/test/resource/quality-monitor/quality_monitor.tf index 307f9e01fc..665a982d20 100644 --- a/pluginframework/test/resource/quality-monitor/quality_monitor.tf +++ b/pluginframework/test/resource/quality-monitor/quality_monitor.tf @@ -10,16 +10,16 @@ provider "databricks" { profile = "aws-prod-ucws" } -resource "databricks_catalog" "testCatalog" { - name = "testCatalog" +resource "databricks_catalog" "testcatalog" { + name = "testcatalog" comment = "Plugin Framework PoC" properties = { purpose = "testing" } } -resource "databricks_schema" "testSchema" { - catalog_name = databricks_catalog.testCatalog.name +resource "databricks_schema" "testschema" { + catalog_name = databricks_catalog.testcatalog.name name = "testSchema" comment = "Plugin Framework PoC" properties = { @@ -27,9 +27,9 @@ resource "databricks_schema" "testSchema" { } } -resource "databricks_sql_table" "testTable" { - catalog_name = databricks_catalog.testCatalog.name - schema_name = databricks_schema.testSchema.name +resource "databricks_sql_table" "testtable" { + catalog_name = databricks_catalog.testcatalog.name + schema_name = databricks_schema.testschema.name name = "testTable" table_type = "MANAGED" data_source_format = "DELTA" @@ -40,7 +40,7 @@ resource "databricks_sql_table" "testTable" { } } -resource "databricks_lakehouse_monitor_pluginframework" "testMonitor" { - table_name = "${databricks_catalog.testCatalog.name}.${databricks_schema.testSchema.name}.${databricks_sql_table.testTable.name}" +resource "databricks_lakehouse_monitor_pluginframework" "testmonitor" { + table_name = "${databricks_catalog.testcatalog.name}.${databricks_schema.testschema.name}.${databricks_sql_table.testtable.name}" }