From a3eadbe856f4d5e6cee13393b07a7792dcafdde7 Mon Sep 17 00:00:00 2001 From: Maksym Ochenashko Date: Tue, 30 Jan 2024 22:25:35 +0200 Subject: [PATCH] Implement compact mode --- docs/data-sources/bar_gauge.md | 1 + docs/data-sources/dashboard.md | 1 + docs/data-sources/gauge.md | 1 + docs/data-sources/stat.md | 1 + docs/data-sources/table.md | 1 + docs/data-sources/text.md | 1 + docs/data-sources/timeseries.md | 1 + docs/index.md | 1 + internal/provider/bar_gauge_data_source.go | 24 +++++++++---- internal/provider/dashboard_data_source.go | 21 +++++++---- .../provider/dashboard_data_source_test.go | 35 +++++++++++++++++++ internal/provider/gauge_data_source.go | 24 +++++++++---- internal/provider/provider.go | 26 ++++++++------ internal/provider/stat_data_source.go | 24 +++++++++---- internal/provider/table_data_source.go | 24 +++++++++---- internal/provider/text_data_source.go | 33 +++++++++++++---- internal/provider/timeseries_data_source.go | 24 +++++++++---- internal/provider/utils.go | 7 ++++ 18 files changed, 198 insertions(+), 52 deletions(-) diff --git a/docs/data-sources/bar_gauge.md b/docs/data-sources/bar_gauge.md index 1e0095e..517b32d 100644 --- a/docs/data-sources/bar_gauge.md +++ b/docs/data-sources/bar_gauge.md @@ -133,6 +133,7 @@ data "gdashboard_bar_gauge" "mails_sent" { ### Optional +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of this panel. - `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field)) - `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph)) diff --git a/docs/data-sources/dashboard.md b/docs/data-sources/dashboard.md index 0f38fee..41de262 100644 --- a/docs/data-sources/dashboard.md +++ b/docs/data-sources/dashboard.md @@ -186,6 +186,7 @@ The "HTTP" section has two panel blocks, both with dimensions `8x10` and data so ### Optional - `annotations` (Block List) The annotations to add to the dashboard. (see [below for nested schema](#nestedblock--annotations)) +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of the dashboard. - `editable` (Boolean) Whether to make the dashboard editable or not. - `graph_tooltip` (String) Controls tooltip and hover highlight behavior across different panels: `default`, `shared-crosshair`, `shared-tooltip`. diff --git a/docs/data-sources/gauge.md b/docs/data-sources/gauge.md index 0ab9836..1298d13 100644 --- a/docs/data-sources/gauge.md +++ b/docs/data-sources/gauge.md @@ -171,6 +171,7 @@ data "gdashboard_gauge" "native_memory" { ### Optional +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of this panel. - `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field)) - `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph)) diff --git a/docs/data-sources/stat.md b/docs/data-sources/stat.md index 33cfff4..d922adc 100644 --- a/docs/data-sources/stat.md +++ b/docs/data-sources/stat.md @@ -166,6 +166,7 @@ data "gdashboard_stat" "status_2" { ### Optional +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of this panel. - `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field)) - `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph)) diff --git a/docs/data-sources/table.md b/docs/data-sources/table.md index 38b4a54..f6d59c9 100644 --- a/docs/data-sources/table.md +++ b/docs/data-sources/table.md @@ -194,6 +194,7 @@ data "gdashboard_table" "test" { ### Optional +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of this panel. - `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field)) - `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph)) diff --git a/docs/data-sources/text.md b/docs/data-sources/text.md index f7c0e2a..be01398 100644 --- a/docs/data-sources/text.md +++ b/docs/data-sources/text.md @@ -62,6 +62,7 @@ data "gdashboard_text" "code" { ### Optional +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of this panel. - `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph)) diff --git a/docs/data-sources/timeseries.md b/docs/data-sources/timeseries.md index f1dcd10..1ee5fe4 100644 --- a/docs/data-sources/timeseries.md +++ b/docs/data-sources/timeseries.md @@ -177,6 +177,7 @@ data "gdashboard_timeseries" "native_memory" { ### Optional - `axis` (Block List) Axis display options. (see [below for nested schema](#nestedblock--axis)) +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `description` (String) The description of this panel. - `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field)) - `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph)) diff --git a/docs/index.md b/docs/index.md index 5f33405..008dac0 100644 --- a/docs/index.md +++ b/docs/index.md @@ -122,6 +122,7 @@ data "gdashboard_timeseries" "override_defaults" { ### Optional +- `compact_json` (Boolean) Whether to use compat JSON encoding or not. - `defaults` (Block List) The default values to use with when an attribute is missing in the data source definition. (see [below for nested schema](#nestedblock--defaults)) diff --git a/internal/provider/bar_gauge_data_source.go b/internal/provider/bar_gauge_data_source.go index 236fd22..cba42f8 100644 --- a/internal/provider/bar_gauge_data_source.go +++ b/internal/provider/bar_gauge_data_source.go @@ -24,7 +24,8 @@ func NewBarGaugeDataSource() datasource.DataSource { // BarGaugeDataSource defines the data source implementation. type BarGaugeDataSource struct { - Defaults BarGaugeDefaults + CompactJson bool + Defaults BarGaugeDefaults } type BarGaugeDefaults struct { @@ -44,6 +45,7 @@ type BarGaugeGraphDefault struct { type BarGaugeDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Queries []Query `tfsdk:"queries"` @@ -121,10 +123,11 @@ func (d *BarGaugeDataSource) Schema(ctx context.Context, req datasource.SchemaRe }, Attributes: map[string]schema.Attribute{ - "id": idAttribute(), - "json": jsonAttribute(), - "title": titleAttribute(), - "description": descriptionAttribute(), + "id": idAttribute(), + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), + "title": titleAttribute(), + "description": descriptionAttribute(), }, } } @@ -144,6 +147,7 @@ func (d *BarGaugeDataSource) Configure(_ context.Context, req datasource.Configu ) } + d.CompactJson = defaults.CompactJson d.Defaults = defaults.BarGauge } @@ -218,7 +222,15 @@ func (d *BarGaugeDataSource) Read(ctx context.Context, req datasource.ReadReques panel.CommonPanel.Description = &description } - jsonData, err := json.MarshalIndent(panel, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.Marshal(panel) + } else { + jsonData, err = json.MarshalIndent(panel, "", " ") + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return diff --git a/internal/provider/dashboard_data_source.go b/internal/provider/dashboard_data_source.go index 6f41b08..4f030e9 100644 --- a/internal/provider/dashboard_data_source.go +++ b/internal/provider/dashboard_data_source.go @@ -28,7 +28,8 @@ func NewDashboardDataSource() datasource.DataSource { // DashboardDataSource defines the data source implementation. type DashboardDataSource struct { - Defaults DashboardDefaults + CompactJson bool + Defaults DashboardDefaults } type DashboardDefaults struct { @@ -47,6 +48,7 @@ type Time struct { type DashboardDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Version types.Int64 `tfsdk:"version"` @@ -1171,10 +1173,8 @@ func (d *DashboardDataSource) Schema(ctx context.Context, req datasource.SchemaR "id": schema.StringAttribute{ Computed: true, }, - "json": schema.StringAttribute{ - Computed: true, - Description: "The Grafana-API-compatible JSON of this panel.", - }, + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), "title": schema.StringAttribute{ Required: true, Description: "The title of the dashboard.", @@ -1221,6 +1221,7 @@ func (d *DashboardDataSource) Configure(_ context.Context, req datasource.Config ) } + d.CompactJson = defaults.CompactJson d.Defaults = defaults.Dashboard } @@ -2009,7 +2010,15 @@ func (d *DashboardDataSource) Read(ctx context.Context, req datasource.ReadReque dashboard.GraphTooltip = 2 } - jsonData, err := json.MarshalIndent(dashboard, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.MarshalIndent(dashboard, "", " ") + } else { + jsonData, err = json.Marshal(dashboard) + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return diff --git a/internal/provider/dashboard_data_source_test.go b/internal/provider/dashboard_data_source_test.go index 73c00c7..63eaf0b 100644 --- a/internal/provider/dashboard_data_source_test.go +++ b/internal/provider/dashboard_data_source_test.go @@ -163,6 +163,10 @@ func TestAccDashboardDataSource(t *testing.T) { Config: testAccDashboardDataSourceProvider_Links_Datasource_Valid, Check: resource.TestCheckResourceAttr("data.gdashboard_dashboard.test", "json", testAccDashboardDataSourceProvider_Links_Datasource_Valid_ExpectedJson), }, + { + Config: testAccDashboardDataSourceProvider_Compact_Json, + Check: resource.TestCheckResourceAttr("data.gdashboard_dashboard.test", "json", testAccDashboardDataSourceProvider_Compact_Json_ExpectedJson), + }, }, }) } @@ -2064,3 +2068,34 @@ const testAccDashboardDataSourceProvider_Links_Datasource_Valid_ExpectedJson = ` }` //// + +const testAccDashboardDataSourceProvider_Compact_Json = ` +data "gdashboard_dashboard" "test" { + title = "Test" + compact_json = true + + links { + dashboards { + title = "My dashboards" + tags = ["tag 1", "tag-2"] + as_dropdown = true + include_time_range = true + include_template_variables = true + new_tab = true + } + + external { + title = "My dashboards" + url = "https://grafana.com" + tooltip = "Some tooltip" + icon = "cloud" + include_time_range = true + include_template_variables = true + new_tab = true + } + } + + layout { } +}` + +const testAccDashboardDataSourceProvider_Compact_Json_ExpectedJson = `{\"uid\":\"test-uid\",\"title\":\"Test\",\"description\":\"Terraform-managed dashboard\",\"tags\":[\"terraform\",\"test\"],\"style\":\"light\",\"timezone\":\"Europe/Kyiv\",\"weekStart\":\"monday\",\"liveNow\":true,\"editable\":false,\"panels\":[{\"editable\":false,\"error\":false,\"gridPos\":{\"h\":8,\"w\":10,\"x\":0,\"y\":0},\"id\":0,\"isNew\":false,\"span\":0,\"title\":\"Panel 1\",\"transparent\":false,\"type\":\"\",\"title\":\"Panel 1\"},{\"editable\":false,\"error\":false,\"gridPos\":{\"h\":3,\"w\":24,\"x\":0,\"y\":8},\"id\":0,\"isNew\":false,\"span\":0,\"title\":\"Panel 2\",\"transparent\":false,\"type\":\"\",\"title\":\"Panel 2\"},{\"editable\":false,\"error\":false,\"gridPos\":{\"h\":4,\"w\":24,\"x\":0,\"y\":12},\"id\":0,\"isNew\":false,\"span\":0,\"title\":\"Panel 3\",\"transparent\":false,\"type\":\"\",\"title\":\"Panel 3\"},{\"editable\":false,\"error\":false,\"gridPos\":{\"h\":3,\"w\":3,\"x\":0,\"y\":16},\"id\":0,\"isNew\":false,\"span\":0,\"title\":\"Panel 4\",\"transparent\":false,\"type\":\"\",\"title\":\"Panel 4\"}],\"templating\":{\"list\":[{\"type\":\"custom\",\"name\":\"custom\",\"label\":\"\",\"hide\":1,\"refresh\":false,\"options\":[{\"text\":\"entry-1\",\"value\":\"value\",\"selected\":false},{\"text\":\"entry-2\",\"value\":\"value\",\"selected\":true}],\"includeAll\":false,\"allValue\":\"\",\"multi\":false,\"query\":\"entry-1 : value, entry-2 : value\",\"regex\":\"\",\"current\":{\"text\":\"entry-2\",\"value\":\"value\",\"selected\":true},\"sort\":0},{\"type\":\"constant\",\"name\":\"var\",\"label\":\"\",\"hide\":2,\"refresh\":false,\"options\":[],\"includeAll\":false,\"allValue\":\"\",\"multi\":false,\"query\":\"const-value\",\"regex\":\"\",\"current\":{\"text\":null,\"value\":\"\",\"selected\":false},\"sort\":0}]},\"annotations\":{\"list\":null},\"schemaVersion\":0,\"version\":2,\"links\":null,\"time\":{\"from\":\"now-1h\",\"to\":\"now+1h\"},\"timepicker\":{\"hidden\":true,\"now_delay\":\"15s\",\"refresh_intervals\":[\"1s\",\"1m\",\"1d\"],\"time_options\":[\"10m\",\"30m\",\"50m\"]},\"graphTooltip\":1}"` diff --git a/internal/provider/gauge_data_source.go b/internal/provider/gauge_data_source.go index 192aaee..41e6c56 100644 --- a/internal/provider/gauge_data_source.go +++ b/internal/provider/gauge_data_source.go @@ -24,7 +24,8 @@ func NewGaugeDataSource() datasource.DataSource { // GaugeDataSource defines the data source implementation. type GaugeDataSource struct { - Defaults GaugeDefaults + CompactJson bool + Defaults GaugeDefaults } type GaugeDefaults struct { @@ -44,6 +45,7 @@ type GaugeGraphDefault struct { type GaugeDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Queries []Query `tfsdk:"queries"` @@ -113,10 +115,11 @@ func (d *GaugeDataSource) Schema(ctx context.Context, req datasource.SchemaReque }, Attributes: map[string]schema.Attribute{ - "id": idAttribute(), - "json": jsonAttribute(), - "title": titleAttribute(), - "description": descriptionAttribute(), + "id": idAttribute(), + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), + "title": titleAttribute(), + "description": descriptionAttribute(), }, } } @@ -136,6 +139,7 @@ func (d *GaugeDataSource) Configure(_ context.Context, req datasource.ConfigureR ) } + d.CompactJson = defaults.CompactJson d.Defaults = defaults.Gauge } @@ -210,7 +214,15 @@ func (d *GaugeDataSource) Read(ctx context.Context, req datasource.ReadRequest, panel.CommonPanel.Description = &description } - jsonData, err := json.MarshalIndent(panel, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.Marshal(panel) + } else { + jsonData, err = json.MarshalIndent(panel, "", " ") + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 678f468..2d71328 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -23,23 +23,25 @@ type GrafanaDashboardBuilderProvider struct { } type Defaults struct { - Dashboard DashboardDefaults - Timeseries TimeseriesDefaults - BarGauge BarGaugeDefaults - Stat StatDefaults - Gauge GaugeDefaults - Table TableDefaults + CompactJson bool + Dashboard DashboardDefaults + Timeseries TimeseriesDefaults + BarGauge BarGaugeDefaults + Stat StatDefaults + Gauge GaugeDefaults + Table TableDefaults } // GrafanaDashboardBuilderProviderModel describes the provider data model. type GrafanaDashboardBuilderProviderModel struct { - Defaults []DefaultsModel `tfsdk:"defaults"` + CompactJson types.Bool `tfsdk:"compact_json"` + Defaults []DefaultsModel `tfsdk:"defaults"` } type DefaultsModel struct { Dashboard []DashboardDefaultsModel `tfsdk:"dashboard"` Timeseries []TimeseriesDefaultsModel `tfsdk:"timeseries"` - BarGuage []BarGaugeDefaultsModel `tfsdk:"bar_gauge"` + BarGauge []BarGaugeDefaultsModel `tfsdk:"bar_gauge"` Stat []StatDefaultsModel `tfsdk:"stat"` Gauge []GaugeDefaultsModel `tfsdk:"gauge"` Table []TableDefaultsModel `tfsdk:"table"` @@ -92,6 +94,9 @@ func (p *GrafanaDashboardBuilderProvider) Metadata(_ context.Context, req provid func (p *GrafanaDashboardBuilderProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) { resp.Schema = schema.Schema{ Description: "The provider offers a handy syntax to define Grafana dashboards: time series, gauge, bar gauge, stat, etc.", + Attributes: map[string]schema.Attribute{ + "compact_json": compactJsonAttribute(), + }, Blocks: map[string]schema.Block{ "defaults": schema.ListNestedBlock{ Description: "The default values to use with when an attribute is missing in the data source definition.", @@ -195,6 +200,7 @@ func (p *GrafanaDashboardBuilderProvider) Configure(ctx context.Context, req pro } defaults := Defaults{ + CompactJson: data.CompactJson.ValueBool(), Dashboard: DashboardDefaults{ Editable: true, Style: "dark", @@ -390,8 +396,8 @@ func (p *GrafanaDashboardBuilderProvider) Configure(ctx context.Context, req pro } } - if len(data.Defaults) > 0 && len(data.Defaults[0].BarGuage) > 0 { - opts := data.Defaults[0].BarGuage[0] + if len(data.Defaults) > 0 && len(data.Defaults[0].BarGauge) > 0 { + opts := data.Defaults[0].BarGauge[0] updateFieldDefaults(&defaults.BarGauge.Field, opts.Field) diff --git a/internal/provider/stat_data_source.go b/internal/provider/stat_data_source.go index 34d0953..ab34815 100644 --- a/internal/provider/stat_data_source.go +++ b/internal/provider/stat_data_source.go @@ -24,7 +24,8 @@ func NewStatDataSource() datasource.DataSource { // StatDataSource defines the data source implementation. type StatDataSource struct { - Defaults StatDefaults + CompactJson bool + Defaults StatDefaults } type StatDefaults struct { @@ -46,6 +47,7 @@ type StatGraphDefaults struct { type StatDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Queries []Query `tfsdk:"queries"` @@ -141,10 +143,11 @@ func (d *StatDataSource) Schema(ctx context.Context, req datasource.SchemaReques }, Attributes: map[string]schema.Attribute{ - "id": idAttribute(), - "json": jsonAttribute(), - "title": titleAttribute(), - "description": descriptionAttribute(), + "id": idAttribute(), + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), + "title": titleAttribute(), + "description": descriptionAttribute(), }, } } @@ -164,6 +167,7 @@ func (d *StatDataSource) Configure(_ context.Context, req datasource.ConfigureRe ) } + d.CompactJson = defaults.CompactJson d.Defaults = defaults.Stat } @@ -248,7 +252,15 @@ func (d *StatDataSource) Read(ctx context.Context, req datasource.ReadRequest, r panel.CommonPanel.Description = &description } - jsonData, err := json.MarshalIndent(panel, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.Marshal(panel) + } else { + jsonData, err = json.MarshalIndent(panel, "", " ") + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return diff --git a/internal/provider/table_data_source.go b/internal/provider/table_data_source.go index c609156..4e3784f 100644 --- a/internal/provider/table_data_source.go +++ b/internal/provider/table_data_source.go @@ -25,7 +25,8 @@ func NewTableDataSource() datasource.DataSource { // TableDataSource defines the data source implementation. type TableDataSource struct { - Defaults TableDefaults + CompactJson bool + Defaults TableDefaults } type TableDefaults struct { @@ -36,6 +37,7 @@ type TableDefaults struct { type TableDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Queries []Query `tfsdk:"queries"` @@ -211,10 +213,11 @@ func (d *TableDataSource) Schema(ctx context.Context, req datasource.SchemaReque }, Attributes: map[string]schema.Attribute{ - "id": idAttribute(), - "json": jsonAttribute(), - "title": titleAttribute(), - "description": descriptionAttribute(), + "id": idAttribute(), + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), + "title": titleAttribute(), + "description": descriptionAttribute(), }, } } @@ -234,6 +237,7 @@ func (d *TableDataSource) Configure(_ context.Context, req datasource.ConfigureR ) } + d.CompactJson = defaults.CompactJson d.Defaults = defaults.Table } @@ -351,7 +355,15 @@ func (d *TableDataSource) Read(ctx context.Context, req datasource.ReadRequest, panel.CommonPanel.Description = &description } - jsonData, err := json.MarshalIndent(panel, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.Marshal(panel) + } else { + jsonData, err = json.MarshalIndent(panel, "", " ") + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return diff --git a/internal/provider/text_data_source.go b/internal/provider/text_data_source.go index 9a8c42b..ce6e95e 100644 --- a/internal/provider/text_data_source.go +++ b/internal/provider/text_data_source.go @@ -24,12 +24,14 @@ func NewTextDataSource() datasource.DataSource { // TextDataSource defines the data source implementation. type TextDataSource struct { + CompactJson bool } // TextDataSourceModel describes the data source data model. type TextDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Graph []TextOptions `tfsdk:"graph"` @@ -119,10 +121,11 @@ func (d *TextDataSource) Schema(ctx context.Context, req datasource.SchemaReques }, Attributes: map[string]schema.Attribute{ - "id": idAttribute(), - "json": jsonAttribute(), - "title": titleAttribute(), - "description": descriptionAttribute(), + "id": idAttribute(), + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), + "title": titleAttribute(), + "description": descriptionAttribute(), }, } } @@ -132,6 +135,17 @@ func (d *TextDataSource) Configure(_ context.Context, req datasource.ConfigureRe if req.ProviderData == nil { return } + + defaults, ok := req.ProviderData.(Defaults) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected Data Source Configure Type", + fmt.Sprintf("Expected Defaults, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + } + + d.CompactJson = defaults.CompactJson } func (d *TextDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { @@ -199,12 +213,19 @@ func (d *TextDataSource) Read(ctx context.Context, req datasource.ReadRequest, r panel.CommonPanel.Description = &description } - jsonData, err := json.MarshalIndent(panel, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.Marshal(panel) + } else { + jsonData, err = json.MarshalIndent(panel, "", " ") + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return } - data.Json = types.StringValue(string(jsonData)) data.Id = types.StringValue(strconv.Itoa(hashcode(jsonData))) diff --git a/internal/provider/timeseries_data_source.go b/internal/provider/timeseries_data_source.go index 470a92e..52105fc 100644 --- a/internal/provider/timeseries_data_source.go +++ b/internal/provider/timeseries_data_source.go @@ -25,7 +25,8 @@ func NewTimeseriesDataSource() datasource.DataSource { // TimeseriesDataSource defines the data source implementation. type TimeseriesDataSource struct { - Defaults TimeseriesDefaults + CompactJson bool + Defaults TimeseriesDefaults } type TimeseriesDefaults struct { @@ -63,6 +64,7 @@ type TimeseriesLegendDefault struct { type TimeseriesDataSourceModel struct { Id types.String `tfsdk:"id"` Json types.String `tfsdk:"json"` + CompactJson types.Bool `tfsdk:"compact_json"` Title types.String `tfsdk:"title"` Description types.String `tfsdk:"description"` Queries []Query `tfsdk:"queries"` @@ -264,10 +266,11 @@ func (d *TimeseriesDataSource) Schema(ctx context.Context, req datasource.Schema }, Attributes: map[string]schema.Attribute{ - "id": idAttribute(), - "json": jsonAttribute(), - "title": titleAttribute(), - "description": descriptionAttribute(), + "id": idAttribute(), + "json": jsonAttribute(), + "compact_json": compactJsonAttribute(), + "title": titleAttribute(), + "description": descriptionAttribute(), }, } } @@ -287,6 +290,7 @@ func (d *TimeseriesDataSource) Configure(_ context.Context, req datasource.Confi ) } + d.CompactJson = defaults.CompactJson d.Defaults = defaults.Timeseries } @@ -457,7 +461,15 @@ func (d *TimeseriesDataSource) Read(ctx context.Context, req datasource.ReadRequ panel.CommonPanel.Description = &description } - jsonData, err := json.MarshalIndent(panel, "", " ") + var jsonData []byte + var err error + + if data.CompactJson.ValueBool() || d.CompactJson { + jsonData, err = json.Marshal(panel) + } else { + jsonData, err = json.MarshalIndent(panel, "", " ") + } + if err != nil { resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err)) return diff --git a/internal/provider/utils.go b/internal/provider/utils.go index 5c1fe6c..9dbcc02 100644 --- a/internal/provider/utils.go +++ b/internal/provider/utils.go @@ -892,6 +892,13 @@ func jsonAttribute() schema.StringAttribute { } } +func compactJsonAttribute() schema.BoolAttribute { + return schema.BoolAttribute{ + Optional: true, + Description: "Whether to use compat JSON encoding or not.", + } +} + func titleAttribute() schema.StringAttribute { return schema.StringAttribute{ Required: true,