@@ -18,6 +18,28 @@ import (
1818 gapi "github.com/grafana/grafana-api-golang-client"
1919)
2020
21+ const (
22+ reportFrequencyHourly = "hourly"
23+ reportFrequencyDaily = "daily"
24+ reportFrequencyWeekly = "weekly"
25+ reportFrequencyMonthly = "monthly"
26+ reportFrequencyCustom = "custom"
27+ reportFrequencyOnce = "once"
28+ reportFrequencyNever = "never"
29+
30+ reportOrientationPortrait = "portrait"
31+ reportOrientationLandscape = "landscape"
32+
33+ reportLayoutGrid = "grid"
34+ reportLayoutSimple = "simple"
35+ )
36+
37+ var (
38+ reportLayouts = []string {reportLayoutSimple , reportLayoutGrid }
39+ reportOrientations = []string {reportOrientationLandscape , reportOrientationPortrait }
40+ reportFrequencies = []string {reportFrequencyNever , reportFrequencyOnce , reportFrequencyHourly , reportFrequencyDaily , reportFrequencyWeekly , reportFrequencyMonthly , reportFrequencyCustom }
41+ )
42+
2143func ResourceReport () * schema.Resource {
2244 return & schema.Resource {
2345 Description : `
@@ -85,16 +107,16 @@ func ResourceReport() *schema.Resource {
85107 "layout" : {
86108 Type : schema .TypeString ,
87109 Optional : true ,
88- Description : "Layout of the report. `simple` or `grid`" ,
89- Default : "grid" ,
90- ValidateFunc : validation .StringInSlice ([] string { "simple" , "grid" } , false ),
110+ Description : allowedValuesDescription ( "Layout of the report" , reportLayouts ) ,
111+ Default : reportLayoutGrid ,
112+ ValidateFunc : validation .StringInSlice (reportLayouts , false ),
91113 },
92114 "orientation" : {
93115 Type : schema .TypeString ,
94116 Optional : true ,
95- Description : "Orientation of the report. `landscape` or `portrait`" ,
96- Default : "landscape" ,
97- ValidateFunc : validation .StringInSlice ([] string { "landscape" , "portrait" } , false ),
117+ Description : allowedValuesDescription ( "Orientation of the report" , reportOrientations ) ,
118+ Default : reportOrientationLandscape ,
119+ ValidateFunc : validation .StringInSlice (reportOrientations , false ),
98120 },
99121 "time_range" : {
100122 Type : schema .TypeList ,
@@ -129,8 +151,8 @@ func ResourceReport() *schema.Resource {
129151 "frequency" : {
130152 Type : schema .TypeString ,
131153 Required : true ,
132- Description : "Frequency of the report. One of `never`, `once`, `hourly`, `daily`, `weekly`, `monthly` or `custom`." ,
133- ValidateFunc : validation .StringInSlice ([] string { "never" , "once" , "hourly" , "daily" , "weekly" , "monthly" , "custom" } , false ),
154+ Description : allowedValuesDescription ( "Frequency of the report" , reportFrequencies ) ,
155+ ValidateFunc : validation .StringInSlice (reportFrequencies , false ),
134156 },
135157 "start_time" : {
136158 Type : schema .TypeString ,
@@ -179,6 +201,12 @@ func ResourceReport() *schema.Resource {
179201 return diag .FromErr (err )
180202 },
181203 },
204+ "last_day_of_month" : {
205+ Type : schema .TypeBool ,
206+ Optional : true ,
207+ Description : "Send the report on the last day of the month" ,
208+ Default : false ,
209+ },
182210 },
183211 },
184212 },
@@ -253,6 +281,9 @@ func ReadReport(ctx context.Context, d *schema.ResourceData, meta interface{}) d
253281 if r .Schedule .EndDate != nil {
254282 schedule ["end_time" ] = r .Schedule .EndDate .Format (time .RFC3339 )
255283 }
284+ if r .Schedule .DayOfMonth == "last" {
285+ schedule ["last_day_of_month" ] = true
286+ }
256287
257288 d .Set ("schedule" , []interface {}{schedule })
258289
@@ -320,7 +351,7 @@ func schemaToReport(d *schema.ResourceData) (gapi.Report, error) {
320351 }
321352
322353 // Set schedule start time
323- if frequency != "never" {
354+ if frequency != reportFrequencyNever {
324355 if startTimeStr := d .Get ("schedule.0.start_time" ).(string ); startTimeStr != "" {
325356 startDate , err := time .Parse (time .RFC3339 , startTimeStr )
326357 if err != nil {
@@ -332,7 +363,7 @@ func schemaToReport(d *schema.ResourceData) (gapi.Report, error) {
332363 }
333364
334365 // Set schedule end time
335- if frequency != "once" && frequency != "never" {
366+ if frequency != reportFrequencyOnce && frequency != reportFrequencyNever {
336367 if endTimeStr := d .Get ("schedule.0.end_time" ).(string ); endTimeStr != "" {
337368 endDate , err := time .Parse (time .RFC3339 , endTimeStr )
338369 if err != nil {
@@ -343,10 +374,16 @@ func schemaToReport(d *schema.ResourceData) (gapi.Report, error) {
343374 }
344375 }
345376
377+ if frequency == reportFrequencyMonthly {
378+ if lastDayOfMonth := d .Get ("schedule.0.last_day_of_month" ).(bool ); lastDayOfMonth {
379+ report .Schedule .DayOfMonth = "last"
380+ }
381+ }
382+
346383 if reportWorkdaysOnlyConfigAllowed (frequency ) {
347384 report .Schedule .WorkdaysOnly = d .Get ("schedule.0.workdays_only" ).(bool )
348385 }
349- if frequency == "custom" {
386+ if frequency == reportFrequencyCustom {
350387 customInterval := d .Get ("schedule.0.custom_interval" ).(string )
351388 amount , unit , err := parseCustomReportInterval (customInterval )
352389 if err != nil {
@@ -360,7 +397,7 @@ func schemaToReport(d *schema.ResourceData) (gapi.Report, error) {
360397}
361398
362399func reportWorkdaysOnlyConfigAllowed (frequency string ) bool {
363- return frequency == "hourly" || frequency == "daily" || frequency == "custom"
400+ return frequency == reportFrequencyHourly || frequency == reportFrequencyDaily || frequency == reportFrequencyCustom
364401}
365402
366403func parseCustomReportInterval (i interface {}) (int , string , error ) {
0 commit comments