@@ -3,6 +3,7 @@ package sumologic
33import (
44 "encoding/json"
55 "log"
6+ "strings"
67 "time"
78
89 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -28,6 +29,7 @@ func resourceSumologicContent() *schema.Resource {
2829 ValidateFunc : validation .StringIsJSON ,
2930 Required : true ,
3031 DiffSuppressFunc : structure .SuppressJsonDiff ,
32+ StateFunc : configStateFunc ,
3133 },
3234 },
3335 Timeouts : & schema.ResourceTimeout {
@@ -39,6 +41,73 @@ func resourceSumologicContent() *schema.Resource {
3941 }
4042}
4143
44+ func configStateFunc (value interface {}) string {
45+ return normalizeConfig (value .(string ))
46+ }
47+
48+ // modify json config to remove logically equivalent changes in terrafrom diff output
49+ // e.g. absent map entry vs map entry with null value
50+ // or absent map entry vs map entry with default value
51+ func normalizeConfig (originalConfig string ) string {
52+ config , err := structure .ExpandJsonFromString (originalConfig )
53+
54+ if err != nil {
55+ log .Println ("Couldn't expand config json from string" )
56+ return originalConfig
57+ }
58+
59+ removeEmptyValues (config )
60+ fillPanelQueriesDefaultValues (config )
61+
62+ if config ["theme" ] != nil {
63+ config ["theme" ] = strings .ToLower (config ["theme" ].(string ))
64+ }
65+
66+ children , ok := config ["children" ].([]interface {})
67+ if ok {
68+ for _ , childItemObject := range children {
69+ childItemMap , ok := childItemObject .(map [string ]interface {})
70+ if ok {
71+ fillPanelQueriesDefaultValues (childItemMap )
72+ }
73+ }
74+ }
75+ configString , err := structure .FlattenJsonToString (config )
76+ if err != nil {
77+ log .Println ("Couldn't flatten config json to string" )
78+ return originalConfig
79+ }
80+
81+ return configString
82+ }
83+
84+ func fillPanelQueriesDefaultValues (config map [string ]interface {}) {
85+ if config ["panels" ] != nil {
86+ panels := config ["panels" ].([]interface {})
87+
88+ for _ , panelInterface := range panels {
89+ panelItem := panelInterface .(map [string ]interface {})
90+
91+ for range panelItem {
92+ if panelItem ["queries" ] != nil {
93+ queries := panelItem ["queries" ].([]interface {})
94+ for _ , queryInterface := range queries {
95+ queryItem := queryInterface .(map [string ]interface {})
96+
97+ if queryItem ["outputCardinalityLimit" ] == nil {
98+ queryItem ["outputCardinalityLimit" ] = 1000
99+ }
100+
101+ if queryItem ["transient" ] == nil {
102+ queryItem ["transient" ] = false
103+ }
104+ }
105+ }
106+ }
107+ }
108+ }
109+ }
110+
42111func resourceSumologicContentRead (d * schema.ResourceData , meta interface {}) error {
43112 c := meta .(* Client )
44113 // Retrieve the content Id from the state
@@ -61,6 +130,9 @@ func resourceSumologicContentRead(d *schema.ResourceData, meta interface{}) erro
61130
62131 // Write the newly read content object into the schema
63132 d .Set ("config" , content .Config )
133+
134+ normalizedConfig := normalizeConfig (content .Config )
135+ d .Set ("config" , normalizedConfig )
64136 return nil
65137}
66138
0 commit comments