|
| 1 | +package dashboards |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/databricks/databricks-sdk-go/service/dashboards" |
| 9 | + "github.com/databricks/terraform-provider-databricks/common" |
| 10 | + pluginfwcommon "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/common" |
| 11 | + pluginfwcontext "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/context" |
| 12 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/converters" |
| 13 | + "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw/tfschema" |
| 14 | + "github.com/databricks/terraform-provider-databricks/internal/service/dashboards_tf" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 19 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 20 | +) |
| 21 | + |
| 22 | +const dataSourceName = "dashboards" |
| 23 | + |
| 24 | +func DataSourceDashboards() datasource.DataSource { |
| 25 | + return &DashboardsDataSource{} |
| 26 | +} |
| 27 | + |
| 28 | +var _ datasource.DataSourceWithConfigure = &DashboardsDataSource{} |
| 29 | + |
| 30 | +type DashboardsDataSource struct { |
| 31 | + Client *common.DatabricksClient |
| 32 | +} |
| 33 | + |
| 34 | +type DashboardsInfo struct { |
| 35 | + DashboardNameContains types.String `tfsdk:"dashboard_name_contains"` |
| 36 | + Dashboards types.List `tfsdk:"dashboards"` |
| 37 | +} |
| 38 | + |
| 39 | +func (DashboardsInfo) ApplySchemaCustomizations(attrs map[string]tfschema.AttributeBuilder) map[string]tfschema.AttributeBuilder { |
| 40 | + attrs["dashboard_name_contains"] = attrs["dashboard_name_contains"].SetOptional() |
| 41 | + attrs["dashboards"] = attrs["dashboards"].SetComputed() |
| 42 | + |
| 43 | + return attrs |
| 44 | +} |
| 45 | + |
| 46 | +func (DashboardsInfo) GetComplexFieldTypes(context.Context) map[string]reflect.Type { |
| 47 | + return map[string]reflect.Type{ |
| 48 | + "dashboards": reflect.TypeOf(dashboards_tf.Dashboard{}), |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (d *DashboardsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 53 | + resp.TypeName = pluginfwcommon.GetDatabricksProductionName(dataSourceName) |
| 54 | +} |
| 55 | + |
| 56 | +func (d *DashboardsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 57 | + attrs, blocks := tfschema.DataSourceStructToSchemaMap(ctx, DashboardsInfo{}, nil) |
| 58 | + resp.Schema = schema.Schema{ |
| 59 | + Attributes: attrs, |
| 60 | + Blocks: blocks, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (d *DashboardsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 65 | + if d.Client == nil { |
| 66 | + d.Client = pluginfwcommon.ConfigureDataSource(req, resp) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func AppendDiagAndCheckErrors(resp *datasource.ReadResponse, diags diag.Diagnostics) bool { |
| 71 | + resp.Diagnostics.Append(diags...) |
| 72 | + return resp.Diagnostics.HasError() |
| 73 | +} |
| 74 | + |
| 75 | +func (d *DashboardsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 76 | + ctx = pluginfwcontext.SetUserAgentInDataSourceContext(ctx, dataSourceName) |
| 77 | + w, diags := d.Client.GetWorkspaceClient() |
| 78 | + if AppendDiagAndCheckErrors(resp, diags) { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + var dashboardInfo DashboardsInfo |
| 83 | + if AppendDiagAndCheckErrors(resp, req.Config.Get(ctx, &dashboardInfo)) { |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + dashboardName := strings.ToLower(dashboardInfo.DashboardNameContains.ValueString()) |
| 88 | + |
| 89 | + dashboardsGoSdk, err := w.Lakeview.ListAll(ctx, dashboards.ListDashboardsRequest{}) |
| 90 | + if err != nil { |
| 91 | + resp.Diagnostics.AddError("Failed to fetch Dashboards", err.Error()) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + var dashboardsTfSdk []attr.Value |
| 96 | + for _, dashboard := range dashboardsGoSdk { |
| 97 | + if dashboardName != "" && !strings.Contains(strings.ToLower(dashboard.DisplayName), dashboardName) { |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + var dashboardResponse dashboards_tf.Dashboard |
| 102 | + if AppendDiagAndCheckErrors(resp, converters.GoSdkToTfSdkStruct(ctx, dashboard, &dashboardResponse)) { |
| 103 | + return |
| 104 | + } |
| 105 | + dashboardsTfSdk = append(dashboardsTfSdk, dashboardResponse.ToObjectValue(ctx)) |
| 106 | + } |
| 107 | + |
| 108 | + dashboardInfo.Dashboards = types.ListValueMust(dashboards_tf.Dashboard{}.Type(ctx), dashboardsTfSdk) |
| 109 | + resp.Diagnostics.Append(resp.State.Set(ctx, dashboardInfo)...) |
| 110 | + |
| 111 | +} |
0 commit comments