8
8
"log"
9
9
10
10
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11
+ "github.com/hashicorp/terraform-plugin-sdk/helper/structure"
11
12
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
12
13
"github.com/olivere/elastic/uritemplates"
13
14
@@ -17,8 +18,13 @@ import (
17
18
18
19
var openDistroMonitorSchema = map [string ]* schema.Schema {
19
20
"body" : {
20
- Type : schema .TypeString ,
21
- Required : true ,
21
+ Type : schema .TypeString ,
22
+ Required : true ,
23
+ DiffSuppressFunc : diffSuppressMonitor ,
24
+ StateFunc : func (v interface {}) string {
25
+ json , _ := structure .NormalizeJsonString (v )
26
+ return json
27
+ },
22
28
ValidateFunc : validation .StringIsJSON ,
23
29
},
24
30
}
@@ -59,10 +65,12 @@ func resourceElasticsearchOpenDistroMonitorCreate(d *schema.ResourceData, m inte
59
65
}
60
66
61
67
d .SetId (res .ID )
62
- d .Set ("body" , res .Monitor )
63
68
log .Printf ("[INFO] Object ID: %s" , d .Id ())
64
69
65
- return nil
70
+ // Although we receive the full monitor in the response to the POST,
71
+ // OpenDistro seems to add default values to the ojbect after the resource
72
+ // is saved, e.g. adjust_pure_negative, boost values
73
+ return resourceElasticsearchOpenDistroMonitorRead (d , m )
66
74
}
67
75
68
76
func resourceElasticsearchOpenDistroMonitorRead (d * schema.ResourceData , m interface {}) error {
@@ -78,10 +86,18 @@ func resourceElasticsearchOpenDistroMonitorRead(d *schema.ResourceData, m interf
78
86
return err
79
87
}
80
88
81
- d .Set ("body" , res .Monitor )
82
89
d .SetId (res .ID )
83
90
84
- return nil
91
+ monitorJson , err := json .Marshal (res .Monitor )
92
+ if err != nil {
93
+ return err
94
+ }
95
+ monitorJsonNormalized , err := structure .NormalizeJsonString (string (monitorJson ))
96
+ if err != nil {
97
+ return err
98
+ }
99
+ err = d .Set ("body" , monitorJsonNormalized )
100
+ return err
85
101
}
86
102
87
103
func resourceElasticsearchOpenDistroMonitorUpdate (d * schema.ResourceData , m interface {}) error {
@@ -160,7 +176,7 @@ func resourceElasticsearchOpenDistroGetMonitor(monitorID string, m interface{})
160
176
if err := json .Unmarshal (body , response ); err != nil {
161
177
return response , fmt .Errorf ("error unmarshalling monitor body: %+v: %+v" , err , body )
162
178
}
163
-
179
+ normalizeMonitor ( response . Monitor )
164
180
return response , err
165
181
}
166
182
@@ -201,7 +217,7 @@ func resourceElasticsearchOpenDistroPostMonitor(d *schema.ResourceData, m interf
201
217
if err := json .Unmarshal (body , response ); err != nil {
202
218
return response , fmt .Errorf ("error unmarshalling monitor body: %+v: %+v" , err , body )
203
219
}
204
-
220
+ normalizeMonitor ( response . Monitor )
205
221
return response , nil
206
222
}
207
223
@@ -252,7 +268,7 @@ func resourceElasticsearchOpenDistroPutMonitor(d *schema.ResourceData, m interfa
252
268
}
253
269
254
270
type monitorResponse struct {
255
- Version int `json:"_version"`
256
- ID string `json:"_id"`
257
- Monitor interface {} `json:"monitor"`
271
+ Version int `json:"_version"`
272
+ ID string `json:"_id"`
273
+ Monitor map [ string ] interface {} `json:"monitor"`
258
274
}
0 commit comments