Skip to content

Commit 5ca17fe

Browse files
authored
feat: Add suport for new composable template (#9)
* feat: rename index_template to index_template_legacy. Add index_component_template resource Signed-off-by: disaster37 <linuxworkgroup@hotmail.com> * feat: Add support for new index template API Signed-off-by: disaster37 <linuxworkgroup@hotmail.com>
1 parent 16da12c commit 5ca17fe

15 files changed

Lines changed: 1000 additions & 86 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ trial-license:
9393
curl -XPOST -u ${ELASTICSEARCH_USERNAME}:${ELASTICSEARCH_PASSWORD} ${ELASTICSEARCH_URLS}/_license/start_trial?acknowledge=true
9494

9595
start-pods: clean-pods
96-
kubectl run elasticsearch --image docker.elastic.co/elasticsearch/elasticsearch:7.5.1 --port "9200" --expose --env "cluster.name=test" --env "discovery.type=single-node" --env "ELASTIC_PASSWORD=changeme" --env "xpack.security.enabled=true" --env "ES_JAVA_OPTS=-Xms512m -Xmx512m" --env "path.repo=/tmp" --limits "cpu=500m,memory=1024Mi"
96+
kubectl run elasticsearch --image docker.elastic.co/elasticsearch/elasticsearch:7.16.2 --port "9200" --expose --env "cluster.name=test" --env "discovery.type=single-node" --env "ELASTIC_PASSWORD=changeme" --env "xpack.security.enabled=true" --env "ES_JAVA_OPTS=-Xms512m -Xmx512m" --env "path.repo=/tmp" --limits "cpu=500m,memory=1024Mi"
9797

9898
clean-pods:
9999
kubectl delete --ignore-not-found pod/elasticsearch

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ provider "elasticsearch" {
4141

4242
- [elasticsearch_index_lifecycle_policy](resources/elasticsearch_index_lifecycle_policy.md)
4343
- [elasticsearch_index_template](resources/elasticsearch_index_template.md)
44+
- [elasticsearch_index_component_template](resources/elasticsearch_index_component_template.md)
45+
- [elasticsearch_index_template_legacy](resources/elasticsearch_index_template_legacy.md)
4446
- [elasticsearch_role](resources/elasticsearch_role.md)
4547
- [elasticsearch_role_mapping](resources/elasticsearch_role_mapping.md)
4648
- [elasticsearch_user](resources/elasticsearch_user.md)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# elasticsearch_index_component_template Resource Source
2+
3+
This resource permit to manage the index component template in Elasticsearch.
4+
You can see the API documentation: https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-component-template.html
5+
6+
***Supported Elasticsearch version:***
7+
- v7
8+
9+
## Example Usage
10+
11+
It will create index template.
12+
13+
```tf
14+
resource elasticsearch_index_component_template "test" {
15+
name = "terraform-test"
16+
template = <<EOF
17+
{
18+
"template": {
19+
"settings": {
20+
"index.refresh_interval": "3s"
21+
},
22+
"mappings": {
23+
"_source": {
24+
"enabled": false
25+
},
26+
"properties": {
27+
"host_name": {
28+
"type": "keyword"
29+
},
30+
"created_at": {
31+
"type": "date",
32+
"format": "EEE MMM dd HH:mm:ss Z yyyy"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
EOF
39+
}
40+
```
41+
42+
## Argument Reference
43+
44+
***The following arguments are supported:***
45+
- **name**: (required) Identifier for the template.
46+
- **template**: (required) The template specification. It's a string as JSON object.
47+
48+
## Attribute Reference
49+
50+
NA

docs/resources/elasticsearch_index_template.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# elasticsearch_index_template Resource Source
22

33
This resource permit to manage the index template in Elasticsearch.
4-
You can see the API documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
4+
You can see the API documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/index-templates.html
55

66
***Supported Elasticsearch version:***
7-
- v6
87
- v7
98

109
## Example Usage
@@ -16,15 +15,15 @@ resource elasticsearch_index_template "test" {
1615
name = "terraform-test"
1716
template = <<EOF
1817
{
19-
"index_patterns": [
20-
"test"
21-
],
22-
"settings": {
23-
"index.refresh_interval": "5s",
24-
"index.lifecycle.name": "policy-logstash-backup",
25-
"index.lifecycle.rollover_alias": "logstash-backup-alias"
26-
},
27-
"order": 2
18+
"index_patterns": ["test-index-template"],
19+
"template": {
20+
"settings": {
21+
"index.refresh_interval": "5s",
22+
"index.lifecycle.name": "policy-logstash-backup",
23+
"index.lifecycle.rollover_alias": "logstash-backup-alias"
24+
}
25+
},
26+
"priority": 2
2827
}
2928
EOF
3029
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# elasticsearch_index_template_legacy Resource Source
2+
3+
This resource permit to manage the index template in Elasticsearch (the legacy API).
4+
You can see the API documentation: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html
5+
6+
***Supported Elasticsearch version:***
7+
- v6
8+
- v7
9+
10+
## Example Usage
11+
12+
It will create index template.
13+
14+
```tf
15+
resource elasticsearch_index_template_legacy "test" {
16+
name = "terraform-test"
17+
template = <<EOF
18+
{
19+
"index_patterns": [
20+
"test"
21+
],
22+
"settings": {
23+
"index.refresh_interval": "5s",
24+
"index.lifecycle.name": "policy-logstash-backup",
25+
"index.lifecycle.rollover_alias": "logstash-backup-alias"
26+
},
27+
"order": 2
28+
}
29+
EOF
30+
}
31+
```
32+
33+
## Argument Reference
34+
35+
***The following arguments are supported:***
36+
- **name**: (required) Identifier for the template.
37+
- **template**: (required) The template specification. It's a string as JSON object.
38+
39+
## Attribute Reference
40+
41+
NA

es/diff_suppress_funcs.go

Lines changed: 147 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,65 @@ package es
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"reflect"
67
"strconv"
78
"strings"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/olivere/elastic/v7"
1012
log "github.com/sirupsen/logrus"
1113
)
1214

13-
// diffSuppressIndexTemplate permit to compare template in current state vs from API
14-
func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool {
15-
var oo, no map[string]interface{}
15+
// diffSuppressIndexTemplateLegacy permit to compare template in current state vs from API
16+
func diffSuppressIndexTemplateLegacy(k, old, new string, d *schema.ResourceData) bool {
17+
18+
oo := &elastic.IndicesGetTemplateResponse{}
19+
no := &elastic.IndicesGetTemplateResponse{}
20+
1621
if err := json.Unmarshal([]byte(old), &oo); err != nil {
22+
fmt.Printf("[ERR] Error when converting to IndicesGetComponentTemplate: %s", err.Error())
23+
log.Errorf("Error when converting to IndicesGetComponentTemplate: %s", err.Error())
1724
return false
1825
}
1926
if err := json.Unmarshal([]byte(new), &no); err != nil {
27+
fmt.Printf("[ERR] Error when converting to IndicesGetComponentTemplate: %s", err.Error())
28+
log.Errorf("Error when converting to IndicesGetComponentTemplate: %s", err.Error())
2029
return false
2130
}
2231

23-
// Add default parameters on new index template if needed
24-
if _, ok := no["order"]; !ok {
25-
no["order"] = 0
32+
// inits default values
33+
34+
if oo.Aliases == nil {
35+
oo.Aliases = make(map[string]interface{})
2636
}
27-
if _, ok := no["settings"]; !ok {
28-
no["settings"] = make(map[string]interface{})
37+
if oo.Mappings == nil {
38+
oo.Mappings = make(map[string]interface{})
39+
}
40+
if oo.Settings == nil {
41+
oo.Settings = make(map[string]interface{})
42+
}
43+
44+
if no.Aliases == nil {
45+
no.Aliases = make(map[string]interface{})
2946
}
30-
if _, ok := no["mappings"]; !ok {
31-
no["mappings"] = make(map[string]interface{})
47+
if no.Mappings == nil {
48+
no.Mappings = make(map[string]interface{})
3249
}
33-
if _, ok := no["aliases"]; !ok {
34-
no["aliases"] = make(map[string]interface{})
50+
if no.Settings == nil {
51+
no.Settings = make(map[string]interface{})
3552
}
3653

37-
ob, _ := json.Marshal(oo)
38-
nb, _ := json.Marshal(parseAllDotProperties(no))
54+
// force undot properties to compare the same think
55+
oo.Aliases = parseAllDotProperties(oo.Aliases)
56+
oo.Mappings = parseAllDotProperties(oo.Mappings)
57+
oo.Settings = parseAllDotProperties(oo.Settings)
3958

40-
log.Debugf("Old: %s", string(ob))
41-
log.Debugf("New: %s", string(nb))
59+
no.Aliases = parseAllDotProperties(no.Aliases)
60+
no.Mappings = parseAllDotProperties(no.Mappings)
61+
no.Settings = parseAllDotProperties(no.Settings)
4262

43-
return reflect.DeepEqual(oo, parseAllDotProperties(no))
63+
return reflect.DeepEqual(oo, no)
4464
}
4565

4666
// suppressEquivalentJSON permit to compare state store as JSON string
@@ -121,3 +141,113 @@ func parseDotPropertie(key string, value interface{}, result map[string]interfac
121141
}
122142

123143
}
144+
145+
// diffSuppressIndexComponentTemplate permit to compare index component template in current state vs from API
146+
func diffSuppressIndexComponentTemplate(k, old, new string, d *schema.ResourceData) bool {
147+
oo := &elastic.IndicesGetComponentTemplate{}
148+
no := &elastic.IndicesGetComponentTemplate{}
149+
150+
if err := json.Unmarshal([]byte(old), &oo); err != nil {
151+
fmt.Printf("[ERR] Error when converting to IndicesGetComponentTemplate: %s", err.Error())
152+
log.Errorf("Error when converting to IndicesGetComponentTemplate: %s", err.Error())
153+
return false
154+
}
155+
if err := json.Unmarshal([]byte(new), &no); err != nil {
156+
fmt.Printf("[ERR] Error when converting to IndicesGetComponentTemplate: %s", err.Error())
157+
log.Errorf("Error when converting to IndicesGetComponentTemplate: %s", err.Error())
158+
return false
159+
}
160+
161+
// inits default values
162+
if oo.Template != nil {
163+
if oo.Template.Aliases == nil {
164+
oo.Template.Aliases = make(map[string]interface{})
165+
}
166+
if oo.Template.Mappings == nil {
167+
oo.Template.Mappings = make(map[string]interface{})
168+
}
169+
if oo.Template.Settings == nil {
170+
oo.Template.Settings = make(map[string]interface{})
171+
}
172+
}
173+
if no.Template != nil {
174+
if no.Template.Aliases == nil {
175+
no.Template.Aliases = make(map[string]interface{})
176+
}
177+
if no.Template.Mappings == nil {
178+
no.Template.Mappings = make(map[string]interface{})
179+
}
180+
if no.Template.Settings == nil {
181+
no.Template.Settings = make(map[string]interface{})
182+
}
183+
}
184+
185+
// force undot properties to compare the same think
186+
if oo.Template != nil {
187+
oo.Template.Aliases = parseAllDotProperties(oo.Template.Aliases)
188+
oo.Template.Mappings = parseAllDotProperties(oo.Template.Mappings)
189+
oo.Template.Settings = parseAllDotProperties(oo.Template.Settings)
190+
}
191+
if no.Template != nil {
192+
no.Template.Aliases = parseAllDotProperties(no.Template.Aliases)
193+
no.Template.Mappings = parseAllDotProperties(no.Template.Mappings)
194+
no.Template.Settings = parseAllDotProperties(no.Template.Settings)
195+
}
196+
197+
return reflect.DeepEqual(oo, no)
198+
}
199+
200+
// diffSuppressIndexTemplate permit to compare index template in current state vs from API
201+
func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool {
202+
oo := &elastic.IndicesGetIndexTemplate{}
203+
no := &elastic.IndicesGetIndexTemplate{}
204+
205+
if err := json.Unmarshal([]byte(old), &oo); err != nil {
206+
fmt.Printf("[ERR] Error when converting to IndicesGetIndexTemplate on old object: %s", err.Error())
207+
log.Errorf("Error when converting to IndicesGetIndexTemplate on old object: %s\n%s", err.Error(), old)
208+
return false
209+
}
210+
if err := json.Unmarshal([]byte(new), &no); err != nil {
211+
fmt.Printf("[ERR] Error when converting to IndicesGetIndexTemplate on new object: %s", err.Error())
212+
log.Errorf("Error when converting to IndicesGetIndexTemplate on new object: %s\n%s", err.Error(), new)
213+
return false
214+
}
215+
216+
// inits default values
217+
if oo.Template != nil {
218+
if oo.Template.Aliases == nil {
219+
oo.Template.Aliases = make(map[string]interface{})
220+
}
221+
if oo.Template.Mappings == nil {
222+
oo.Template.Mappings = make(map[string]interface{})
223+
}
224+
if oo.Template.Settings == nil {
225+
oo.Template.Settings = make(map[string]interface{})
226+
}
227+
}
228+
if no.Template != nil {
229+
if no.Template.Aliases == nil {
230+
no.Template.Aliases = make(map[string]interface{})
231+
}
232+
if no.Template.Mappings == nil {
233+
no.Template.Mappings = make(map[string]interface{})
234+
}
235+
if no.Template.Settings == nil {
236+
no.Template.Settings = make(map[string]interface{})
237+
}
238+
}
239+
240+
// force undot properties to compare the same think
241+
if oo.Template != nil {
242+
oo.Template.Aliases = parseAllDotProperties(oo.Template.Aliases)
243+
oo.Template.Mappings = parseAllDotProperties(oo.Template.Mappings)
244+
oo.Template.Settings = parseAllDotProperties(oo.Template.Settings)
245+
}
246+
if no.Template != nil {
247+
no.Template.Aliases = parseAllDotProperties(no.Template.Aliases)
248+
no.Template.Mappings = parseAllDotProperties(no.Template.Mappings)
249+
no.Template.Settings = parseAllDotProperties(no.Template.Settings)
250+
}
251+
252+
return reflect.DeepEqual(no, oo)
253+
}

es/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ func Provider() *schema.Provider {
7070

7171
ResourcesMap: map[string]*schema.Resource{
7272
"elasticsearch_index_lifecycle_policy": resourceElasticsearchIndexLifecyclePolicy(),
73+
"elasticsearch_index_template_legacy": resourceElasticsearchIndexTemplateLegacy(),
7374
"elasticsearch_index_template": resourceElasticsearchIndexTemplate(),
75+
"elasticsearch_index_component_template": resourceElasticsearchIndexComponentTemplate(),
7476
"elasticsearch_role": resourceElasticsearchSecurityRole(),
7577
"elasticsearch_role_mapping": resourceElasticsearchSecurityRoleMapping(),
7678
"elasticsearch_user": resourceElasticsearchSecurityUser(),

0 commit comments

Comments
 (0)