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

Reports: Add missing time range in old reports and update test client #1259

Merged
merged 3 commits into from
Jan 9, 2024
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
8 changes: 8 additions & 0 deletions internal/resources/grafana/common_check_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ var (
return payloadOrError(resp, err)
},
)

reportCheckExists = newCheckExistsHelper(
func(u *models.Report) string { return strconv.FormatInt(u.ID, 10) },
func(client *goapi.GrafanaHTTPAPI, id string) (*models.Report, error) {
resp, err := client.Reports.GetReport(mustParseInt64(id))
return payloadOrError(resp, err)
},
)
)

type checkExistsGetResourceFunc[T interface{}] func(client *goapi.GrafanaHTTPAPI, id string) (*T, error)
Expand Down
17 changes: 10 additions & 7 deletions internal/resources/grafana/resource_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,17 +376,27 @@ func schemaToReport(d *schema.ResourceData) (models.CreateOrUpdateReportConfig,
Formats: []models.Type{reportFormatPDF},
}

// Set dashboard time range
timeRange := d.Get("time_range").([]interface{})
tr := &models.ReportTimeRange{}
if len(timeRange) > 0 {
timeRange := timeRange[0].(map[string]interface{})
tr = &models.ReportTimeRange{From: timeRange["from"].(string), To: timeRange["to"].(string)}
}

id := int64(d.Get("dashboard_id").(int))
uid := d.Get("dashboard_uid").(string)
if uid == "" {
// It triggers the old way to generate reports
report.DashboardID = id
report.Options.TimeRange = tr
} else {
report.Dashboards = []*models.ReportDashboard{
{
Dashboard: &models.ReportDashboardID{
UID: uid,
},
TimeRange: tr,
},
}
}
Expand All @@ -398,13 +408,6 @@ func schemaToReport(d *schema.ResourceData) (models.CreateOrUpdateReportConfig,
}
}

// Set dashboard time range
timeRange := d.Get("time_range").([]interface{})
if len(timeRange) > 0 {
timeRange := timeRange[0].(map[string]interface{})
report.Dashboards[0].TimeRange = &models.ReportTimeRange{From: timeRange["from"].(string), To: timeRange["to"].(string)}
}

// Set schedule start time
if frequency != reportFrequencyNever {
if startTimeStr := d.Get("schedule.0.start_time").(string); startTimeStr != "" {
Expand Down
72 changes: 10 additions & 62 deletions internal/resources/grafana/resource_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (
"strconv"
"testing"

gapi "github.com/grafana/grafana-api-golang-client"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/grafana/terraform-provider-grafana/internal/resources/grafana"
"github.com/grafana/terraform-provider-grafana/internal/testutils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand All @@ -18,16 +15,16 @@ import (
func TestAccResourceReport_basic(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)

var report gapi.Report
var report models.Report

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccReportCheckDestroy(&report),
CheckDestroy: reportCheckExists.destroyed(&report, nil),
Steps: []resource.TestStep{
{
Config: testutils.TestAccExample(t, "resources/grafana_report/resource.tf"),
Check: resource.ComposeTestCheckFunc(
testAccReportCheckExists("grafana_report.test", &report),
reportCheckExists.exists("grafana_report.test", &report),
resource.TestCheckResourceAttrSet("grafana_report.test", "id"),
resource.TestCheckResourceAttr("grafana_report.test", "org_id", "1"),
resource.TestCheckResourceAttrSet("grafana_report.test", "dashboard_id"),
Expand Down Expand Up @@ -80,7 +77,7 @@ func TestAccResourceReport_basic(t *testing.T) {
{
Config: testutils.TestAccExample(t, "resources/grafana_report/monthly.tf"),
Check: resource.ComposeTestCheckFunc(
testAccReportCheckExists("grafana_report.test", &report),
reportCheckExists.exists("grafana_report.test", &report),
resource.TestCheckResourceAttrSet("grafana_report.test", "id"),
resource.TestCheckResourceAttrSet("grafana_report.test", "dashboard_id"),
resource.TestCheckResourceAttr("grafana_report.test", "dashboard_uid", "report"),
Expand Down Expand Up @@ -108,16 +105,16 @@ func TestAccResourceReport_basic(t *testing.T) {
func TestAccResourceReport_CreateFromDashboardID(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)

var report gapi.Report
var report models.Report

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccReportCheckDestroy(&report),
CheckDestroy: reportCheckExists.destroyed(&report, nil),
Steps: []resource.TestStep{
{
Config: testAccReportCreateFromID,
Check: resource.ComposeTestCheckFunc(
testAccReportCheckExists("grafana_report.test", &report),
reportCheckExists.exists("grafana_report.test", &report),
resource.TestCheckResourceAttrSet("grafana_report.test", "dashboard_id"),
resource.TestCheckResourceAttr("grafana_report.test", "dashboard_uid", "report-from-uid"),
),
Expand All @@ -129,18 +126,18 @@ func TestAccResourceReport_CreateFromDashboardID(t *testing.T) {
func TestAccResourceReport_InOrg(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)

var report gapi.Report
var report models.Report
var org models.OrgDetailsDTO
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
CheckDestroy: testAccReportCheckDestroy(&report),
CheckDestroy: reportCheckExists.destroyed(&report, nil),
Steps: []resource.TestStep{
{
Config: testAccReportCreateInOrg(name),
Check: resource.ComposeTestCheckFunc(
testAccReportCheckExists("grafana_report.test", &report),
reportCheckExists.exists("grafana_report.test", &report),
resource.TestCheckResourceAttr("grafana_report.test", "dashboard_uid", "report-in-org"),

// Check that the dashboard is in the correct organization
Expand All @@ -153,55 +150,6 @@ func TestAccResourceReport_InOrg(t *testing.T) {
})
}

func testAccReportCheckExists(rn string, report *gapi.Report) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s\n %#v", rn, s.RootModule().Resources)
}

if rs.Primary.ID == "" {
return fmt.Errorf("resource id not set")
}

client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
orgID, reportIDStr := grafana.SplitOrgResourceID(rs.Primary.ID)
reportID, err := strconv.ParseInt(reportIDStr, 10, 64)
if err != nil {
return err
}

// If the org ID is set, check that the report doesn't exist in the default org
if orgID > 1 {
report, err := client.Report(reportID)
if err == nil || report != nil {
return fmt.Errorf("expected no report with ID %s in default org but found one", reportIDStr)
}
client = client.WithOrgID(orgID)
}

gotReport, err := client.Report(reportID)
if err != nil {
return fmt.Errorf("error getting report: %w", err)
}

*report = *gotReport

return nil
}
}

func testAccReportCheckDestroy(report *gapi.Report) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testutils.Provider.Meta().(*common.Client).DeprecatedGrafanaAPI
_, err := client.Report(report.ID)
if err == nil {
return fmt.Errorf("report still exists")
}
return nil
}
}

const testAccReportCreateFromID = `
resource "grafana_dashboard" "test" {
config_json = <<EOD
Expand Down