Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/Makefile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GOVERSION ?= 1.17

ELASTICSEARCH_NAME ?= terraform-elasticstack-es
ELASTICSEARCH_ENDPOINTS ?= http://$(ELASTICSEARCH_NAME):9200
ELASTICSEARCH_VERSION ?= 7.15.1
ELASTICSEARCH_VERSION ?= 8.0.0
ELASTICSEARCH_USERNAME ?= elastic
ELASTICSEARCH_PASSWORD ?= changeme
ELASTICSEARCH_NETWORK ?= elasticstack-network
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## [Unreleased]
### Fixed
- Add new field `allow_custom_routing` in `data_stream` section of [`index_template`](https://www.elastic.co/guide/en/elasticsearch/reference/8.0/indices-put-template.html#put-index-template-api-request-body), which appears only in Elasticsearch version **8.0.0**. Make sure `index_template` resource can work with both **7.x** and **8.x** versions ([#72](https://github.com/elastic/terraform-provider-elasticstack/pull/72))

## [0.3.0] - 2022-02-17
### Added
Expand Down
8 changes: 4 additions & 4 deletions docs/resources/elasticsearch_cluster_settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ resource "elasticstack_elasticsearch_cluster_settings" "my_cluster_settings" {
value = "50mb"
}
setting {
name = "indices.breaker.accounting.limit"
value = "100%"
name = "indices.breaker.total.limit"
value = "65%"
}
setting {
name = "xpack.security.audit.logfile.events.include"
Expand All @@ -39,8 +39,8 @@ resource "elasticstack_elasticsearch_cluster_settings" "my_cluster_settings" {

transient {
setting {
name = "indices.breaker.accounting.limit"
value = "99%"
name = "indices.breaker.total.limit"
value = "60%"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/resources/elasticsearch_index_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ resource "elasticstack_elasticsearch_index_template" "my_data_stream" {

Optional:

- **allow_custom_routing** (Boolean) If `true`, the data stream supports custom routing. Defaults to `false`. Available only in *8.x*
- **hidden** (Boolean) If true, the data stream is hidden.


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ resource "elasticstack_elasticsearch_cluster_settings" "my_cluster_settings" {
value = "50mb"
}
setting {
name = "indices.breaker.accounting.limit"
value = "100%"
name = "indices.breaker.total.limit"
value = "65%"
}
setting {
name = "xpack.security.audit.logfile.events.include"
Expand All @@ -24,8 +24,8 @@ resource "elasticstack_elasticsearch_cluster_settings" "my_cluster_settings" {

transient {
setting {
name = "indices.breaker.accounting.limit"
value = "99%"
name = "indices.breaker.total.limit"
value = "60%"
}
}
}
26 changes: 13 additions & 13 deletions internal/elasticsearch/cluster/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func TestAccResourceClusterSettings(t *testing.T) {
}),
resource.TestCheckTypeSetElemNestedAttrs("elasticstack_elasticsearch_cluster_settings.test", "persistent.0.setting.*",
map[string]string{
"name": "indices.breaker.accounting.limit",
"value": "100%",
"name": "indices.breaker.total.limit",
"value": "65%",
}),
resource.TestCheckTypeSetElemNestedAttrs("elasticstack_elasticsearch_cluster_settings.test", "transient.0.setting.*",
map[string]string{
"name": "indices.breaker.accounting.limit",
"value": "99%",
"name": "indices.breaker.total.limit",
"value": "60%",
}),
),
},
Expand All @@ -57,8 +57,8 @@ func TestAccResourceClusterSettings(t *testing.T) {
}),
resource.TestCheckTypeSetElemNestedAttrs("elasticstack_elasticsearch_cluster_settings.test", "persistent.0.setting.*",
map[string]string{
"name": "indices.breaker.accounting.limit",
"value": "100%",
"name": "indices.breaker.total.limit",
"value": "60%",
}),
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_cluster_settings.test", "persistent.0.setting.*.value_list.*", "ACCESS_DENIED"),
resource.TestCheckTypeSetElemAttr("elasticstack_elasticsearch_cluster_settings.test", "persistent.0.setting.*.value_list.*", "ACCESS_GRANTED"),
Expand Down Expand Up @@ -86,15 +86,15 @@ resource "elasticstack_elasticsearch_cluster_settings" "test" {
value = "50mb"
}
setting {
name = "indices.breaker.accounting.limit"
value = "100%"
name = "indices.breaker.total.limit"
value = "65%"
}
}

transient {
setting {
name = "indices.breaker.accounting.limit"
value = "99%"
name = "indices.breaker.total.limit"
value = "60%"
}
}
}
Expand All @@ -118,8 +118,8 @@ resource "elasticstack_elasticsearch_cluster_settings" "test" {
value = "40mb"
}
setting {
name = "indices.breaker.accounting.limit"
value = "100%"
name = "indices.breaker.total.limit"
value = "60%"
}
setting {
name = "xpack.security.audit.logfile.events.include"
Expand All @@ -136,7 +136,7 @@ func checkResourceClusterSettingsDestroy(s *terraform.State) error {
listOfSettings := []string{
"indices.lifecycle.poll_interval",
"indices.recovery.max_bytes_per_sec",
"indices.breaker.accounting.limit",
"indices.breaker.total.limit",
"xpack.security.audit.logfile.events.include",
}

Expand Down
56 changes: 49 additions & 7 deletions internal/elasticsearch/index/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package index
import (
"context"
"encoding/json"
"log"
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
Expand Down Expand Up @@ -48,6 +49,11 @@ func ResourceTemplate() *schema.Resource {
Default: false,
Optional: true,
},
"allow_custom_routing": {
Description: "If `true`, the data stream supports custom routing. Defaults to `false`. Available only in *8.x*",
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand Down Expand Up @@ -195,10 +201,39 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
}
indexTemplate.ComposedOf = compsOf

if v, ok := d.GetOk("data_stream"); ok {
// only one definition of stream allowed
stream := v.([]interface{})[0].(map[string]interface{})
indexTemplate.DataStream = stream
if d.HasChange("data_stream") {
old, new := d.GetChange("data_stream")
log.Printf("[TRACE] old data stream settings <%v> new settings <%v>", old, new)

// 8.x workaround
hasAllowCustomRouting := false
//for _, s := range []interface{}{old} {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, done in ec5e137

if old != nil && len(old.([]interface{})) == 1 {
if old.([]interface{})[0] != nil {
setting := old.([]interface{})[0].(map[string]interface{})
if _, ok := setting["allow_custom_routing"]; ok {
hasAllowCustomRouting = true
}
}
}
// }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup


if v, ok := d.GetOk("data_stream"); ok {
// only one definition of stream allowed
if v.([]interface{})[0] != nil {
stream := v.([]interface{})[0].(map[string]interface{})
dSettings := &models.DataStreamSettings{}
if s, ok := stream["hidden"]; ok {
hidden := s.(bool)
dSettings.Hidden = &hidden
}
if s, ok := stream["allow_custom_routing"]; ok && hasAllowCustomRouting && s.(bool) {
allow := s.(bool)
dSettings.AllowCustomRouting = &allow
}
indexTemplate.DataStream = dSettings
}
}
}

if v, ok := d.GetOk("index_patterns"); ok {
Expand Down Expand Up @@ -298,9 +333,16 @@ func resourceIndexTemplateRead(ctx context.Context, d *schema.ResourceData, meta
if err := d.Set("composed_of", tpl.IndexTemplate.ComposedOf); err != nil {
return diag.FromErr(err)
}
if tpl.IndexTemplate.DataStream != nil {
ds := make([]interface{}, 0)
ds = append(ds, tpl.IndexTemplate.DataStream)
if stream := tpl.IndexTemplate.DataStream; stream != nil {
ds := make([]interface{}, 1)
dSettings := make(map[string]interface{})
if v := stream.Hidden; v != nil {
dSettings["hidden"] = *v
}
if v := stream.AllowCustomRouting; v != nil {
dSettings["allow_custom_routing"] = *v
}
ds[0] = dSettings
if err := d.Set("data_stream", ds); err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ type IndexTemplate struct {
Create bool `json:"-"`
Timeout string `json:"-"`
ComposedOf []string `json:"composed_of"`
DataStream map[string]interface{} `json:"data_stream,omitempty"`
DataStream *DataStreamSettings `json:"data_stream,omitempty"`
IndexPatterns []string `json:"index_patterns"`
Meta map[string]interface{} `json:"_meta,omitempty"`
Priority *int `json:"priority,omitempty"`
Template *Template `json:"template,omitempty"`
Version *int `json:"version,omitempty"`
}

type DataStreamSettings struct {
Hidden *bool `json:"hidden,omitempty"`
AllowCustomRouting *bool `json:"allow_custom_routing,omitempty"`
}

type Template struct {
Aliases map[string]IndexAlias `json:"aliases,omitempty"`
Mappings map[string]interface{} `json:"mappings,omitempty"`
Expand Down