44 "context"
55 "errors"
66 "fmt"
7+ "reflect"
78 "strconv"
89 "time"
910
@@ -450,20 +451,24 @@ func panelsFromResourceData(data *schema.ResourceData) (panels []*v2.Panels, err
450451 return
451452}
452453
453- func legendFromResourceData (data interface {}) * v2.LegendConfiguration {
454- defaultLegend := & v2.LegendConfiguration {
455- Position : "bottom" ,
456- Layout : "table" ,
454+ func defaultLegendConfiguration () * v2.LegendConfiguration {
455+ return & v2.LegendConfiguration {
456+ Enabled : false ,
457+ Position : "bottom" ,
458+ Layout : "table" ,
459+ ShowCurrent : false ,
457460 }
461+ }
458462
463+ func legendFromResourceData (data interface {}) * v2.LegendConfiguration {
459464 if data == nil {
460- return defaultLegend
465+ return defaultLegendConfiguration ()
461466 }
462467
463468 legendList := data .(* schema.Set ).List ()
464469
465470 if len (legendList ) == 0 {
466- return defaultLegend
471+ return defaultLegendConfiguration ()
467472 }
468473
469474 legend := legendList [0 ].(map [string ]interface {})
@@ -727,8 +732,14 @@ func dashboardToResourceData(dashboard *v2.Dashboard, data *schema.ResourceData)
727732 _ = data .Set ("public_token" , dashboard .PublicToken )
728733
729734 var panels []map [string ]interface {}
730- for _ , panel := range dashboard .Panels {
731- dPanel , err := panelToResourceData (panel , dashboard .Layout )
735+ for i , panel := range dashboard .Panels {
736+ panelsData := data .Get ("panel" ).(* schema.Set ).List ()
737+ panelData := map [string ]interface {}{}
738+ if len (panelsData ) > i {
739+ panelData = panelsData [i ].(map [string ]interface {})
740+ }
741+
742+ dPanel , err := panelToResourceData (panel , dashboard .Layout , panelData )
732743 if err != nil {
733744 return err
734745 }
@@ -824,7 +835,7 @@ func scopeFromResourceData(data *schema.ResourceData) ([]*v2.ScopeExpressionList
824835 return scopes , nil
825836}
826837
827- func panelToResourceData (panel * v2.Panels , layout []* v2.Layout ) (map [string ]interface {}, error ) {
838+ func panelToResourceData (panel * v2.Panels , layout []* v2.Layout , panelData map [ string ] interface {} ) (map [string ]interface {}, error ) {
828839 var panelLayout * v2.Layout
829840
830841 for _ , l := range layout {
@@ -838,18 +849,18 @@ func panelToResourceData(panel *v2.Panels, layout []*v2.Layout) (map[string]inte
838849
839850 switch panel .Type {
840851 case v2 .PanelTypeTimechart :
841- return timechartPanelToResourceData (panel , panelLayout )
852+ return timechartPanelToResourceData (panel , panelLayout , panelData )
842853 case v2 .PanelTypeNumber :
843- return numberPanelToResourceData (panel , panelLayout )
854+ return numberPanelToResourceData (panel , panelLayout , panelData )
844855 case v2 .PanelTypeText :
845856 return textPanelToResourceData (panel , panelLayout )
846857 default :
847858 return nil , fmt .Errorf ("unsupported panel type %s" , panel .Type )
848859 }
849860}
850861
851- func timechartPanelToResourceData (panel * v2.Panels , panelLayout * v2.Layout ) (map [string ]interface {}, error ) {
852- queries , err := queriesToResourceData (panel .AdvancedQueries )
862+ func timechartPanelToResourceData (panel * v2.Panels , panelLayout * v2.Layout , panelData map [ string ] interface {} ) (map [string ]interface {}, error ) {
863+ queries , err := queriesToResourceData (panel .AdvancedQueries , panelData )
853864 if err != nil {
854865 return nil , err
855866 }
@@ -863,23 +874,31 @@ func timechartPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout) (map
863874 "description" : panel .Description ,
864875 "type" : "timechart" ,
865876 "query" : queries ,
866- "legend" : []map [string ]interface {}{
867- legendConfigurationToResourceData (panel .LegendConfiguration ),
868- },
877+ "legend" : legendConfigurationToResourceData (panel .LegendConfiguration , panelData ),
869878 }, nil
870879}
871880
872- func legendConfigurationToResourceData (legend * v2.LegendConfiguration ) map [string ]interface {} {
873- return map [string ]interface {}{
881+ func legendConfigurationToResourceData (legend * v2.LegendConfiguration , panelData map [string ]interface {}) []map [string ]interface {} {
882+
883+ legendData := panelData ["legend" ]
884+ // If legend is not defined in the user configuration and the dashboard legend is the same as the default one
885+ // we don't set the legend in the resource data to avoid drifts
886+ if legendData == nil || len (legendData .(* schema.Set ).List ()) == 0 {
887+ if * legend == * defaultLegendConfiguration () {
888+ return nil
889+ }
890+ }
891+
892+ return []map [string ]interface {}{{
874893 "enabled" : legend .Enabled ,
875894 "show_current" : legend .ShowCurrent ,
876895 "position" : legend .Position ,
877896 "layout" : legend .Layout ,
878- }
897+ }}
879898}
880899
881- func numberPanelToResourceData (panel * v2.Panels , panelLayout * v2.Layout ) (map [string ]interface {}, error ) {
882- queries , err := queriesToResourceData (panel .AdvancedQueries )
900+ func numberPanelToResourceData (panel * v2.Panels , panelLayout * v2.Layout , panelData map [ string ] interface {} ) (map [string ]interface {}, error ) {
901+ queries , err := queriesToResourceData (panel .AdvancedQueries , panelData )
883902 if err != nil {
884903 return nil , err
885904 }
@@ -911,23 +930,30 @@ func textPanelToResourceData(panel *v2.Panels, panelLayout *v2.Layout) (map[stri
911930 }, nil
912931}
913932
914- func queriesToResourceData (advancedQueries []* v2.AdvancedQueries ) ([]map [string ]interface {}, error ) {
933+ func queriesToResourceData (advancedQueries []* v2.AdvancedQueries , panelsData map [ string ] interface {} ) ([]map [string ]interface {}, error ) {
915934 var queries []map [string ]interface {}
916- for _ , query := range advancedQueries {
935+ for queryIndex , query := range advancedQueries {
917936 unit := ""
937+ var defaultFormat v2.Format
918938 switch query .Format .Unit {
919939 case v2 .FormatUnitPercentage :
920940 unit = "percent"
941+ defaultFormat = v2 .NewPercentFormat ()
921942 case v2 .FormatUnitData :
922943 unit = "data"
944+ defaultFormat = v2 .NewDataFormat ()
923945 case v2 .FormatUnitDataRate :
924946 unit = "data rate"
947+ defaultFormat = v2 .NewDataRateFormat ()
925948 case v2 .FormatUnitNumber :
926949 unit = "number"
950+ defaultFormat = v2 .NewNumberFormat ()
927951 case v2 .FormatUnitNumberRate :
928952 unit = "number rate"
953+ defaultFormat = v2 .NewNumberRateFormat ()
929954 case v2 .FormatUnitTime :
930955 unit = "time"
956+ defaultFormat = v2 .NewTimeFormat ()
931957 default :
932958 return nil , fmt .Errorf ("unsupported query format unit: %s" , query .Format .Unit )
933959 }
@@ -954,6 +980,21 @@ func queriesToResourceData(advancedQueries []*v2.AdvancedQueries) ([]map[string]
954980 "y_axis" : query .Format .YAxis ,
955981 }}
956982
983+ queriesData := panelsData ["query" ]
984+ queryData := map [string ]interface {}{}
985+ if queriesData != nil && queriesData .(* schema.Set ).Len () > queryIndex {
986+ queryData = queriesData .(* schema.Set ).List ()[queryIndex ].(map [string ]interface {})
987+ }
988+ formatData := queryData ["format" ]
989+
990+ // If format is not defined in the user configuration and the dashboard format is the same as the default one
991+ // we don't set the format in the resource data to avoid drifts
992+ if formatData == nil || formatData .(* schema.Set ).Len () == 0 {
993+ if reflect .DeepEqual (query .Format , defaultFormat ) {
994+ q ["format" ] = nil
995+ }
996+ }
997+
957998 queries = append (queries , q )
958999 }
9591000 return queries , nil
0 commit comments