@@ -19,181 +19,40 @@ package dashboardsbase
1919
2020import (
2121 "encoding/json"
22- "os"
23- "strings"
2422
2523 "github.com/google/uuid"
2624
27- "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/settings/meta"
28-
2925 "github.com/dynatrace-oss/terraform-provider-dynatrace/terraform/hcl"
3026
3127 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3228)
3329
34- // var JSON_DASHBOARD_BASE_PLUS = os.Getenv("DYNATRACE_JSON_DASHBOARD_BASE_PLUS") == "true"
35- var JSON_DASHBOARD_BASE_PLUS = true
36- var DYNATRACE_DASHBOARD_TESTS = len (os .Getenv ("DYNATRACE_DASHBOARD_TESTS" )) > 0
37-
3830type JSONDashboardBase struct {
3931 Name string
4032 Contents string
4133}
4234
43- func get (v any , key string ) any {
44- if v == nil {
45- return nil
46- }
47- if m , ok := v .(map [string ]any ); ok {
48- if d , found := m [key ]; found {
49- return d
50- }
51- return nil
52- }
53- return nil
54- }
55-
56- func enrs (s []any , bc string , fordiff bool ) {
57- bc = strings .TrimPrefix (bc , "." )
58- if len (s ) == 0 {
59- return
60- }
61- for _ , v := range s {
62- switch tv := v .(type ) {
63- case map [string ]any :
64- enrm (tv , bc + "[#]" , fordiff )
65- case []any :
66- enrs (tv , bc + "[#]" , fordiff )
67- default :
68- }
69- }
70- }
71-
72- func ensure (m map [string ]any , key string , value any ) {
73- if _ , found := m [key ]; ! found {
74- m [key ] = value
75- }
76- }
77-
78- func enrm (m map [string ]any , bc string , fordiff bool ) {
79- bc = strings .TrimPrefix (bc , "." )
80- if m == nil {
81- return
82- }
83- if bc == "dashboardMetadata" {
84- delete (m , "popularity" )
85- }
86- if bc == "" {
87- ensure (m , "tiles" , []any {})
88- }
89-
90- }
91-
92- func (me * JSONDashboardBase ) EnrichRequireds () * JSONDashboardBase {
93- m := map [string ]any {}
94- json .Unmarshal ([]byte (me .Contents ), & m )
95- enrm (m , "" , false )
96- data , _ := json .Marshal (m )
97- me .Contents = string (data )
98- return me
99- }
100-
101- func diffSuppressedContent (content string ) string {
102- m := map [string ]any {}
103- json .Unmarshal ([]byte (content ), & m )
104- if DYNATRACE_DASHBOARD_TESTS {
105- if dmd := get (m , "dashboardMetadata" ); dmd != nil {
106- if df := get (dmd , "dashboardFilter" ); df != nil {
107- if mgmz := get (df , "managementZone" ); mgmz != nil {
108- delete (df .(map [string ]any ), "managementZone" )
109- }
110- }
111- }
112- }
113- delete (m , "metadata" )
114- enrm (m , "" , true )
115- if DYNATRACE_DASHBOARD_TESTS {
116- if tiles , found := m ["tiles" ]; found {
117- if tileSlice , ok := tiles .([]any ); ok {
118- for _ , tile := range tileSlice {
119- if tm , ok := tile .(map [string ]any ); ok {
120- delete (tm , "metricExpressions" )
121- }
122- }
123-
124- }
125- }
126- }
127- data , _ := json .Marshal (m )
128- return string (data )
129- }
130-
13135func (me * JSONDashboardBase ) Schema () map [string ]* schema.Schema {
132- if JSON_DASHBOARD_BASE_PLUS {
133- return map [string ]* schema.Schema {
134- "contents" : {
135- Type : schema .TypeString ,
136- Optional : true ,
137- Description : "Contains the JSON Code of the Dashboard" ,
138- DiffSuppressFunc : func (k , old , new string , d * schema.ResourceData ) bool {
139- if len (old ) == 0 && len (new ) != 0 {
140- return false
141- }
142- if len (old ) != 0 && len (new ) == 0 {
143- return false
144- }
145- if ! json .Valid ([]byte (old )) || ! json .Valid ([]byte (new )) {
146- return false
147- }
148- old = diffSuppressedContent (old )
149- new = diffSuppressedContent (new )
150- result := hcl .JSONStringsEqual (old , new )
151- return result
152- },
153- StateFunc : func (val any ) string {
154- if json .Valid ([]byte (val .(string ))) {
155- content := diffSuppressedContent (val .(string ))
156- return content
157- }
158- return val .(string )
159- },
160- },
161- }
162- }
163-
16436 return map [string ]* schema.Schema {
16537 "contents" : {
16638 Type : schema .TypeString ,
167- Computed : true ,
16839 Optional : true ,
16940 Description : "This attribute exists for backwards compatibility. You do not have to define it." ,
170- DiffSuppressFunc : func (k , old , new string , d * schema.ResourceData ) bool { return len (old ) != 0 },
41+ DiffSuppressFunc : func (k , old , new string , d * schema.ResourceData ) bool { return true },
42+ StateFunc : func (val any ) string { return "" },
17143 },
17244 }
17345}
17446
175- var lifecycleIgnoreChanges = meta.LifeCycle {
176- IgnoreChanges : []string {
177- "contents" ,
178- },
179- }
180-
18147func (me * JSONDashboardBase ) MarshalHCL (properties hcl.Properties ) error {
182- if JSON_DASHBOARD_BASE_PLUS {
183- return properties .EncodeAll (map [string ]any {
184- "contents" : me .Contents ,
185- "lifecycle" : & lifecycleIgnoreChanges ,
186- })
187- }
48+ return properties .EncodeAll (map [string ]any {"contents" : me .Contents })
18849 return nil
18950}
19051
19152func (me * JSONDashboardBase ) UnmarshalHCL (decoder hcl.Decoder ) error {
192- if JSON_DASHBOARD_BASE_PLUS {
193- v , ok := decoder .GetOk ("contents" )
194- if ok {
195- me .Contents = v .(string )
196- }
53+ v , ok := decoder .GetOk ("contents" )
54+ if ok {
55+ me .Contents = v .(string )
19756 }
19857 return nil
19958}
@@ -225,10 +84,7 @@ func (me *JSONDashboardBase) UnmarshalJSON(data []byte) error {
22584 return err
22685 }
22786 me .Name = md .Metadata .Name
228-
229- if JSON_DASHBOARD_BASE_PLUS {
230- me .Contents = string (data )
231- }
87+ me .Contents = string (data )
23288
23389 return nil
23490}
0 commit comments