-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathresource_opensearch_ism_policy.go
260 lines (222 loc) · 7.23 KB
/
resource_opensearch_ism_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package provider
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/olivere/elastic/uritemplates"
elastic7 "github.com/olivere/elastic/v7"
)
var openSearchISMPolicySchema = map[string]*schema.Schema{
"policy_id": {
Description: "The id of the ISM policy.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"body": {
Description: "The policy document.",
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: diffSuppressPolicy,
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v)
return json
},
},
"primary_term": {
Description: "The primary term of the ISM policy version.",
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"seq_no": {
Description: "The sequence number of the ISM policy version.",
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
}
func resourceOpenSearchISMPolicy() *schema.Resource {
return &schema.Resource{
Description: "Provides an OpenSearch Index State Management (ISM) policy. Please refer to the OpenSearch ISM documentation for details.",
Create: resourceOpensearchISMPolicyCreate,
Read: resourceOpensearchISMPolicyRead,
Update: resourceOpensearchISMPolicyUpdate,
Delete: resourceOpensearchISMPolicyDelete,
Schema: openSearchISMPolicySchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
}
}
func resourceOpensearchISMPolicyCreate(d *schema.ResourceData, m interface{}) error {
if _, err := resourceOpensearchPutISMPolicy(d, m); err != nil {
log.Printf("[INFO] Failed to create OpensearchPolicy: %+v", err)
return err
}
policyID := d.Get("policy_id").(string)
d.SetId(policyID)
return resourceOpensearchISMPolicyRead(d, m)
}
func resourceOpensearchISMPolicyRead(d *schema.ResourceData, m interface{}) error {
policyResponse, err := resourceOpensearchGetISMPolicy(d.Id(), m)
if err != nil {
if elastic7.IsNotFound(err) {
log.Printf("[WARN] OpenSearch Policy (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
}
bodyString, err := json.Marshal(policyResponse.Policy)
if err != nil {
return err
}
// Need encapsulation as the response from the GET is different than the one in the PUT
bodyStringNormalized, _ := structure.NormalizeJsonString(fmt.Sprintf("{\"policy\": %+s}", string(bodyString)))
if err := d.Set("policy_id", policyResponse.PolicyID); err != nil {
return fmt.Errorf("error setting policy_id: %s", err)
}
if err := d.Set("body", bodyStringNormalized); err != nil {
return fmt.Errorf("error setting body: %s", err)
}
if err := d.Set("primary_term", policyResponse.PrimaryTerm); err != nil {
return fmt.Errorf("error setting primary_term: %s", err)
}
if err := d.Set("seq_no", policyResponse.SeqNo); err != nil {
return fmt.Errorf("error setting seq_no: %s", err)
}
return nil
}
func resourceOpensearchISMPolicyUpdate(d *schema.ResourceData, m interface{}) error {
if _, err := resourceOpensearchPutISMPolicy(d, m); err != nil {
return err
}
return resourceOpensearchISMPolicyRead(d, m)
}
func resourceOpensearchISMPolicyDelete(d *schema.ResourceData, m interface{}) error {
path, err := uritemplates.Expand("/_plugins/_ism/policies/{policy_id}", map[string]string{
"policy_id": d.Id(),
})
if err != nil {
return fmt.Errorf("error building URL path for policy: %+v", err)
}
osclient, err := getClient(m.(*ProviderConf))
if err != nil {
return err
}
_, err = osclient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "DELETE",
Path: path,
RetryStatusCodes: []int{http.StatusConflict},
Retrier: elastic7.NewBackoffRetrier(
elastic7.NewExponentialBackoff(100*time.Millisecond, 30*time.Second),
),
})
if err != nil {
return fmt.Errorf("error deleting policy: %+v : %+v", path, err)
}
if err != nil {
return fmt.Errorf("error deleting policy: %+v : %+v", path, err)
}
return err
}
func resourceOpensearchGetISMPolicy(policyID string, m interface{}) (GetPolicyResponse, error) {
var err error
response := new(GetPolicyResponse)
path, err := uritemplates.Expand("/_plugins/_ism/policies/{policy_id}", map[string]string{
"policy_id": policyID,
})
if err != nil {
return *response, fmt.Errorf("error building URL path for policy: %+v", err)
}
var body *json.RawMessage
osclient, err := getClient(m.(*ProviderConf))
if err != nil {
return *response, err
}
var res *elastic7.Response
res, err = osclient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "GET",
Path: path,
})
if err != nil {
return *response, fmt.Errorf("error getting policy: %+v : %+v", path, err)
}
body = &res.Body
if err != nil {
return *response, err
}
if err := json.Unmarshal(*body, &response); err != nil {
return *response, fmt.Errorf("error unmarshalling policy body: %+v: %+v", err, body)
}
normalizePolicy(response.Policy)
return *response, err
}
func resourceOpensearchPutISMPolicy(d *schema.ResourceData, m interface{}) (*PutPolicyResponse, error) {
response := new(PutPolicyResponse)
policyJSON := d.Get("body").(string)
seq := d.Get("seq_no").(int)
primTerm := d.Get("primary_term").(int)
params := url.Values{}
if seq >= 0 && primTerm > 0 {
params.Set("if_seq_no", strconv.Itoa(seq))
params.Set("if_primary_term", strconv.Itoa(primTerm))
}
path, err := uritemplates.Expand("/_plugins/_ism/policies/{policy_id}", map[string]string{
"policy_id": d.Get("policy_id").(string),
})
if err != nil {
return response, fmt.Errorf("error building URL path for policy: %+v", err)
}
var body *json.RawMessage
osclient, err := getClient(m.(*ProviderConf))
if err != nil {
return nil, err
}
var res *elastic7.Response
res, err = osclient.PerformRequest(context.TODO(), elastic7.PerformRequestOptions{
Method: "PUT",
Path: path,
Params: params,
Body: string(policyJSON),
RetryStatusCodes: []int{http.StatusConflict},
Retrier: elastic7.NewBackoffRetrier(
elastic7.NewExponentialBackoff(100*time.Millisecond, 30*time.Second),
),
})
if err != nil {
return response, fmt.Errorf("error putting policy: %+v : %+v : %+v", path, policyJSON, err)
}
body = &res.Body
if err != nil {
return response, fmt.Errorf("error creating policy mapping: %+v", err)
}
if err := json.Unmarshal(*body, response); err != nil {
return response, fmt.Errorf("error unmarshalling policy body: %+v: %+v", err, body)
}
return response, nil
}
type GetPolicyResponse struct {
PolicyID string `json:"_id"`
Version int `json:"_version"`
PrimaryTerm int `json:"_primary_term"`
SeqNo int `json:"_seq_no"`
Policy map[string]interface{} `json:"policy"`
}
type PutPolicyResponse struct {
PolicyID string `json:"_id"`
Version int `json:"_version"`
PrimaryTerm int `json:"_primary_term"`
SeqNo int `json:"_seq_no"`
Policy struct {
Policy map[string]interface{} `json:"policy"`
} `json:"policy"`
}