Skip to content

Commit 58418cd

Browse files
committed
Check errors on setting resource data.
1 parent 5d1b268 commit 58418cd

11 files changed

+90
-44
lines changed

es/data_source_elasticsearch_opendistro_destination.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ func dataSourceElasticsearchOpenDistroDestinationRead(d *schema.ResourceData, m
8282
log.Printf("[INFO] couldn't simplify: %+v", value)
8383
}
8484
}
85-
d.Set("body", simplifiedBody)
86-
87-
return nil
85+
err = d.Set("body", simplifiedBody)
86+
return err
8887
}
8988

9089
func elastic7Search(client *elastic7.Client, index string, name string) (string, *json.RawMessage, error) {

es/diff_suppress_funcs.go

+20
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ func diffSuppressDestination(k, old, new string, d *schema.ResourceData) bool {
4747
return reflect.DeepEqual(oo, no)
4848
}
4949

50+
func diffSuppressMonitor(k, old, new string, d *schema.ResourceData) bool {
51+
var oo, no interface{}
52+
if err := json.Unmarshal([]byte(old), &oo); err != nil {
53+
return false
54+
}
55+
if err := json.Unmarshal([]byte(new), &no); err != nil {
56+
return false
57+
}
58+
59+
if om, ok := oo.(map[string]interface{}); ok {
60+
normalizeMonitor(om)
61+
}
62+
63+
if nm, ok := no.(map[string]interface{}); ok {
64+
normalizeMonitor(nm)
65+
}
66+
67+
return reflect.DeepEqual(oo, no)
68+
}
69+
5070
func suppressEquivalentJson(k, old, new string, d *schema.ResourceData) bool {
5171
var oldObj, newObj interface{}
5272
if err := json.Unmarshal([]byte(old), &oldObj); err != nil {

es/resource_elasticsearch_kibana_object.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,11 @@ func resourceElasticsearchKibanaObjectRead(d *schema.ResourceData, meta interfac
188188
return err
189189
}
190190

191-
d.Set("index", index)
191+
ds := &resourceDataSetter{d: d}
192+
ds.set("index", index)
192193
d.Set("body", result)
193194

194-
return nil
195+
return ds.err
195196
}
196197

197198
func resourceElasticsearchKibanaObjectUpdate(d *schema.ResourceData, meta interface{}) error {

es/resource_elasticsearch_kibana_object_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func testCheckElasticsearchKibanaObjectDestroy(s *terraform.State) error {
8787
var err error
8888
switch client := meta.(type) {
8989
case *elastic7.Client:
90-
_, err = client.Get().Index(".kibana").Type("visualization").Id("response-time-percentile").Do(context.TODO())
90+
_, err = client.Get().Index(".kibana").Id("response-time-percentile").Do(context.TODO())
9191
case *elastic6.Client:
9292
_, err = client.Get().Index(".kibana").Type("visualization").Id("response-time-percentile").Do(context.TODO())
9393
default:

es/resource_elasticsearch_opendistro_destination.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ func resourceElasticsearchOpenDistroDestinationCreate(d *schema.ResourceData, m
6363
}
6464

6565
d.SetId(res.ID)
66-
bodyString, err := json.Marshal(res.Destination)
66+
destination, err := json.Marshal(res.Destination)
6767
if err != nil {
6868
return err
6969
}
70-
d.Set("body", bodyString)
71-
72-
return nil
70+
err = d.Set("body", string(destination))
71+
return err
7372
}
7473

7574
func resourceElasticsearchOpenDistroDestinationRead(d *schema.ResourceData, m interface{}) error {
@@ -85,9 +84,8 @@ func resourceElasticsearchOpenDistroDestinationRead(d *schema.ResourceData, m in
8584
return err
8685
}
8786

88-
d.Set("body", res)
89-
90-
return nil
87+
err = d.Set("body", res)
88+
return err
9189
}
9290

9391
func resourceElasticsearchOpenDistroDestinationUpdate(d *schema.ResourceData, m interface{}) error {

es/resource_elasticsearch_opendistro_ism_policy_mapping.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ func resourceElasticsearchOpenDistroISMPolicyMappingRead(d *schema.ResourceData,
8888
}
8989

9090
d.SetId(d.Get("indexes").(string))
91-
d.Set("managed_indexes", concernIndexes)
92-
return nil
91+
err = d.Set("managed_indexes", concernIndexes)
92+
return err
9393
}
9494

9595
func resourceElasticsearchOpenDistroISMPolicyMappingUpdate(d *schema.ResourceData, m interface{}) error {

es/resource_elasticsearch_opendistro_monitor.go

+27-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
1112
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
1213
"github.com/olivere/elastic/uritemplates"
1314

@@ -17,8 +18,13 @@ import (
1718

1819
var openDistroMonitorSchema = map[string]*schema.Schema{
1920
"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+
},
2228
ValidateFunc: validation.StringIsJSON,
2329
},
2430
}
@@ -59,10 +65,12 @@ func resourceElasticsearchOpenDistroMonitorCreate(d *schema.ResourceData, m inte
5965
}
6066

6167
d.SetId(res.ID)
62-
d.Set("body", res.Monitor)
6368
log.Printf("[INFO] Object ID: %s", d.Id())
6469

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)
6674
}
6775

6876
func resourceElasticsearchOpenDistroMonitorRead(d *schema.ResourceData, m interface{}) error {
@@ -78,10 +86,18 @@ func resourceElasticsearchOpenDistroMonitorRead(d *schema.ResourceData, m interf
7886
return err
7987
}
8088

81-
d.Set("body", res.Monitor)
8289
d.SetId(res.ID)
8390

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
85101
}
86102

87103
func resourceElasticsearchOpenDistroMonitorUpdate(d *schema.ResourceData, m interface{}) error {
@@ -160,7 +176,7 @@ func resourceElasticsearchOpenDistroGetMonitor(monitorID string, m interface{})
160176
if err := json.Unmarshal(body, response); err != nil {
161177
return response, fmt.Errorf("error unmarshalling monitor body: %+v: %+v", err, body)
162178
}
163-
179+
normalizeMonitor(response.Monitor)
164180
return response, err
165181
}
166182

@@ -201,7 +217,7 @@ func resourceElasticsearchOpenDistroPostMonitor(d *schema.ResourceData, m interf
201217
if err := json.Unmarshal(body, response); err != nil {
202218
return response, fmt.Errorf("error unmarshalling monitor body: %+v: %+v", err, body)
203219
}
204-
220+
normalizeMonitor(response.Monitor)
205221
return response, nil
206222
}
207223

@@ -252,7 +268,7 @@ func resourceElasticsearchOpenDistroPutMonitor(d *schema.ResourceData, m interfa
252268
}
253269

254270
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"`
258274
}

es/resource_elasticsearch_opendistro_monitor_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,20 @@ resource "elasticsearch_opendistro_monitor" "test_monitor" {
128128
"aggregations": {},
129129
"query": {
130130
"bool": {
131-
"filter": {
131+
"adjust_pure_negative":true,
132+
"boost":1,
133+
"filter": [{
132134
"range": {
133135
"@timestamp": {
134-
"gte": "||-1h",
135-
"lte": "",
136+
"boost":1,
137+
"from":"||-1h",
138+
"to":"",
139+
"include_lower":true,
140+
"include_upper":true,
136141
"format": "epoch_millis"
137142
}
138143
}
139-
}
144+
}]
140145
}
141146
}
142147
}

es/resource_elasticsearch_xpack_role.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func resourceElasticsearchXpackRoleRead(d *schema.ResourceData, m interface{}) e
172172
return err
173173
}
174174

175-
d.Set("role_name", d.Id())
175+
ds := &resourceDataSetter{d: d}
176+
ds.set("role_name", d.Id())
176177

177178
if len(role.Indices) > 0 {
178179
indices := make([]map[string]interface{}, 0, len(role.Indices))
@@ -185,10 +186,10 @@ func resourceElasticsearchXpackRoleRead(d *schema.ResourceData, m interface{}) e
185186
}
186187
indices = append(indices, ip)
187188
}
188-
d.Set("indices", indices)
189+
ds.set("indices", indices)
189190
}
190191

191-
d.Set("cluster", role.Cluster)
192+
ds.set("cluster", role.Cluster)
192193

193194
if len(role.Applications) > 0 {
194195
applications := make([]map[string]interface{}, 0, len(role.Applications))
@@ -200,13 +201,13 @@ func resourceElasticsearchXpackRoleRead(d *schema.ResourceData, m interface{}) e
200201
}
201202
applications = append(applications, ap)
202203
}
203-
d.Set("applications", applications)
204+
ds.set("applications", applications)
204205
}
205206

206-
d.Set("global", role.Global)
207-
d.Set("run_as", role.RunAs)
208-
d.Set("metadata", role.Metadata)
209-
return nil
207+
ds.set("global", role.Global)
208+
ds.set("run_as", role.RunAs)
209+
ds.set("metadata", role.Metadata)
210+
return ds.err
210211
}
211212

212213
func resourceElasticsearchXpackRoleUpdate(d *schema.ResourceData, m interface{}) error {

es/resource_elasticsearch_xpack_role_mapping.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ func resourceElasticsearchXpackRoleMappingRead(d *schema.ResourceData, m interfa
9393
return err
9494
}
9595

96-
d.Set("name", roleMapping.Name)
97-
d.Set("roles", roleMapping.Roles)
98-
d.Set("enabled", roleMapping.Enabled)
99-
d.Set("rules", roleMapping.Rules)
100-
d.Set("metadata", roleMapping.Metadata)
101-
return nil
96+
ds := &resourceDataSetter{d: d}
97+
ds.set("role_mapping_name", roleMapping.Name)
98+
ds.set("roles", roleMapping.Roles)
99+
ds.set("enabled", roleMapping.Enabled)
100+
ds.set("rules", roleMapping.Rules)
101+
ds.set("metadata", roleMapping.Metadata)
102+
return ds.err
102103
}
103104

104105
func resourceElasticsearchXpackRoleMappingUpdate(d *schema.ResourceData, m interface{}) error {

es/util.go

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func normalizeDestination(tpl map[string]interface{}) {
7373
delete(tpl, "last_update_time")
7474
}
7575

76+
func normalizeMonitor(tpl map[string]interface{}) {
77+
delete(tpl, "last_update_time")
78+
delete(tpl, "enabled_time")
79+
}
80+
7681
func normalizePolicy(tpl map[string]interface{}) {
7782
delete(tpl, "last_updated_time")
7883
delete(tpl, "error_notification")

0 commit comments

Comments
 (0)