Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report: Support formats attribute #925

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions examples/resources/grafana_report/all-options.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ resource "grafana_report" "test" {
from = "now-1h"
to = "now"
}
formats = ["csv", "image", "pdf"]
}
23 changes: 23 additions & 0 deletions internal/resources/grafana/resource_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions internal/resources/grafana/resource_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
{
Expand Down