Skip to content

Commit

Permalink
Report: Support formats attribute
Browse files Browse the repository at this point in the history
Closes #862
  • Loading branch information
julienduchesne committed Jun 2, 2023
1 parent faf9da5 commit 14d0f44
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
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"]
}
22 changes: 22 additions & 0 deletions internal/resources/grafana/resource_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const (

reportLayoutGrid = "grid"
reportLayoutSimple = "simple"

reportFormatPDF = "pdf"
reportFormatCSV = "csv"
reportFormatImage = "image"
)

var (
Expand Down Expand Up @@ -130,6 +134,15 @@ func ResourceReport() *schema.Resource {
Default: reportOrientationLandscape,
ValidateFunc: validation.StringInSlice(reportOrientations, false),
},
"formats": {
Type: schema.TypeSet,
Optional: true,
Description: "specifies what kind of attachment to generate for the report",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{reportFormatPDF, reportFormatCSV, reportFormatImage}, false),
},
},
"time_range": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -273,6 +286,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 +397,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

0 comments on commit 14d0f44

Please sign in to comment.