diff --git a/docs/resources/report.md b/docs/resources/report.md index 94ab0b092..981348717 100644 --- a/docs/resources/report.md +++ b/docs/resources/report.md @@ -50,6 +50,7 @@ resource "grafana_report" "test" { - `dashboard_id` (Number, Deprecated) Dashboard to be sent in the report. This field is deprecated, use `dashboard_uid` instead. - `dashboard_uid` (String) Dashboard to be sent in the report. +- `formats` (Set of String) Specifies what kind of attachment to generate for the report. Allowed values: `pdf`, `csv`, `image`. - `include_dashboard_link` (Boolean) Whether to include a link to the dashboard in the report. Defaults to `true`. - `include_table_csv` (Boolean) Whether to include a CSV file of table panel data. Defaults to `false`. - `layout` (String) Layout of the report. Allowed values: `simple`, `grid`. Defaults to `grid`. diff --git a/examples/resources/grafana_report/all-options.tf b/examples/resources/grafana_report/all-options.tf index 5f8ca471f..187354d3a 100644 --- a/examples/resources/grafana_report/all-options.tf +++ b/examples/resources/grafana_report/all-options.tf @@ -29,4 +29,5 @@ resource "grafana_report" "test" { from = "now-1h" to = "now" } + formats = ["csv", "image", "pdf"] } diff --git a/internal/resources/grafana/resource_report.go b/internal/resources/grafana/resource_report.go index 25b666402..32c164d97 100644 --- a/internal/resources/grafana/resource_report.go +++ b/internal/resources/grafana/resource_report.go @@ -33,12 +33,17 @@ const ( reportLayoutGrid = "grid" reportLayoutSimple = "simple" + + reportFormatPDF = "pdf" + reportFormatCSV = "csv" + reportFormatImage = "image" ) var ( reportLayouts = []string{reportLayoutSimple, reportLayoutGrid} reportOrientations = []string{reportOrientationLandscape, reportOrientationPortrait} reportFrequencies = []string{reportFrequencyNever, reportFrequencyOnce, reportFrequencyHourly, reportFrequencyDaily, reportFrequencyWeekly, reportFrequencyMonthly, reportFrequencyCustom} + reportFormats = []string{reportFormatPDF, reportFormatCSV, reportFormatImage} ) func ResourceReport() *schema.Resource { @@ -130,6 +135,15 @@ func ResourceReport() *schema.Resource { Default: reportOrientationLandscape, ValidateFunc: validation.StringInSlice(reportOrientations, false), }, + "formats": { + Type: schema.TypeSet, + Optional: true, + Description: common.AllowedValuesDescription("Specifies what kind of attachment to generate for the report", reportFormats), + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateFunc: validation.StringInSlice(reportFormats, false), + }, + }, "time_range": { Type: schema.TypeList, Optional: true, @@ -273,6 +287,10 @@ func ReadReport(ctx context.Context, d *schema.ResourceData, meta interface{}) d d.Set("orientation", r.Options.Orientation) d.Set("org_id", strconv.FormatInt(r.OrgID, 10)) + if _, ok := d.GetOk("formats"); ok { + d.Set("formats", common.StringSliceToSet(r.Formats)) + } + timeRange := r.Dashboards[0].TimeRange if timeRange.From != "" { d.Set("time_range", []interface{}{ @@ -380,6 +398,11 @@ func schemaToReport(client *gapi.Client, d *schema.ResourceData) (gapi.Report, e Frequency: frequency, TimeZone: "GMT", }, + Formats: []string{reportFormatPDF}, + } + + if v, ok := d.GetOk("formats"); ok && v != nil { + report.Formats = common.SetToStringSlice(v.(*schema.Set)) } // Set dashboard time range diff --git a/internal/resources/grafana/resource_report_test.go b/internal/resources/grafana/resource_report_test.go index 633228d53..bd0b6cad4 100644 --- a/internal/resources/grafana/resource_report_test.go +++ b/internal/resources/grafana/resource_report_test.go @@ -69,6 +69,10 @@ func TestAccResourceReport(t *testing.T) { resource.TestCheckResourceAttr("grafana_report.test", "include_table_csv", "true"), resource.TestCheckResourceAttr("grafana_report.test", "time_range.0.from", "now-1h"), resource.TestCheckResourceAttr("grafana_report.test", "time_range.0.to", "now"), + resource.TestCheckResourceAttr("grafana_report.test", "formats.#", "3"), + resource.TestCheckResourceAttr("grafana_report.test", "formats.0", "csv"), + resource.TestCheckResourceAttr("grafana_report.test", "formats.1", "image"), + resource.TestCheckResourceAttr("grafana_report.test", "formats.2", "pdf"), ), }, {