Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]

- Add the `alert_delay` field to the Create Rule API ([#715](https://github.com/elastic/terraform-provider-elasticstack/pull/715))
- Add support for data_stream `lifecycle` template settings ([#724](https://github.com/elastic/terraform-provider-elasticstack/pull/724))

## [0.11.6] - 2024-08-20

Expand Down
11 changes: 10 additions & 1 deletion docs/resources/elasticsearch_index_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ resource "elasticstack_elasticsearch_index_template" "my_data_stream" {
- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))
- `metadata` (String) Optional user metadata about the index template.
- `priority` (Number) Priority to determine index template precedence when a new data stream or index is created.
- `template` (Block List, Max: 1) Template to be applied. It may optionally include an aliases, mappings, or settings configuration. (see [below for nested schema](#nestedblock--template))
- `template` (Block List, Max: 1) Template to be applied. It may optionally include an aliases, mappings, lifecycle, or settings configuration. (see [below for nested schema](#nestedblock--template))
- `version` (Number) Version number used to manage index templates externally.

### Read-Only
Expand Down Expand Up @@ -102,6 +102,7 @@ Optional:
Optional:

- `alias` (Block Set) Alias to add. (see [below for nested schema](#nestedblock--template--alias))
- `lifecycle` (Block Set, Max: 1) Lifecycle of data stream. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/data-stream-lifecycle.html (see [below for nested schema](#nestedblock--template--lifecycle))
- `mappings` (String) Mapping for fields in the index. Should be specified as a JSON object of field mappings. See the documentation (https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html) for more details
- `settings` (String) Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

Expand All @@ -121,6 +122,14 @@ Optional:
- `routing` (String) Value used to route indexing and search operations to a specific shard.
- `search_routing` (String) Value used to route search operations to a specific shard. If specified, this overwrites the routing value for search operations.


<a id="nestedblock--template--lifecycle"></a>
### Nested Schema for `template.lifecycle`

Required:

- `data_retention` (String) The retention period of the data indexed in this data stream.

## Import

Import is supported using the following syntax:
Expand Down
25 changes: 25 additions & 0 deletions internal/elasticsearch/index/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,28 @@ func FlattenIndexAlias(name string, alias models.IndexAlias) (interface{}, diag.

return a, diags
}

func ExpandLifecycle(definedLifecycle *schema.Set) *models.LifecycleSettings {
if definedLifecycle.Len() == 0 {
return nil
}
lifecycleMap := definedLifecycle.List()[0].(map[string]interface{})
if lifecycleMap != nil {
lifecycle := &models.LifecycleSettings{}
if s, ok := lifecycleMap["data_retention"]; ok {
lifecycle.DataRetention = s.(string)
}
return lifecycle
}
return nil
}

func FlattenLifecycle(lifecycle *models.LifecycleSettings) interface{} {
lf := make([]interface{}, 1)
lfSettings := make(map[string]interface{})
lfSettings["data_retention"] = lifecycle.DataRetention

lf[0] = lfSettings

return lf
}
29 changes: 28 additions & 1 deletion internal/elasticsearch/index/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func ResourceTemplate() *schema.Resource {
Optional: true,
},
"template": {
Description: "Template to be applied. It may optionally include an aliases, mappings, or settings configuration.",
Description: "Template to be applied. It may optionally include an aliases, mappings, lifecycle, or settings configuration.",
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Expand Down Expand Up @@ -157,6 +157,21 @@ func ResourceTemplate() *schema.Resource {
validation.StringIsJSON, stringIsJSONObject,
),
},
"lifecycle": {
Description: "Lifecycle of data stream. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/data-stream-lifecycle.html",
Type: schema.TypeSet,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"data_retention": {
Description: "The retention period of the data indexed in this data stream.",
Type: schema.TypeString,
Required: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -298,6 +313,13 @@ func expandTemplate(config interface{}) (models.Template, bool, diag.Diagnostics
}
templ.Aliases = aliases

if lc, ok := definedTempl["lifecycle"]; ok {
lifecycle := ExpandLifecycle(lc.(*schema.Set))
if lifecycle != nil {
templ.Lifecycle = lifecycle
}
}

if mappings, ok := definedTempl["mappings"]; ok {
if mappings.(string) != "" {
maps := make(map[string]interface{})
Expand Down Expand Up @@ -422,6 +444,11 @@ func flattenTemplateData(template *models.Template) ([]interface{}, diag.Diagnos
tmpl["alias"] = aliases
}

if template.Lifecycle != nil {
lifecycle := FlattenLifecycle(template.Lifecycle)
tmpl["lifecycle"] = lifecycle
}

return []interface{}{tmpl}, diags
}

Expand Down
11 changes: 8 additions & 3 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ type DataStreamSettings struct {
}

type Template struct {
Aliases map[string]IndexAlias `json:"aliases,omitempty"`
Mappings map[string]interface{} `json:"mappings,omitempty"`
Settings map[string]interface{} `json:"settings,omitempty"`
Aliases map[string]IndexAlias `json:"aliases,omitempty"`
Mappings map[string]interface{} `json:"mappings,omitempty"`
Settings map[string]interface{} `json:"settings,omitempty"`
Lifecycle *LifecycleSettings `json:"lifecycle,omitempty"`
}

type IndexTemplatesResponse struct {
Expand Down Expand Up @@ -291,6 +292,10 @@ type IndexAlias struct {
SearchRouting string `json:"search_routing,omitempty"`
}

type LifecycleSettings struct {
DataRetention string `json:"data_retention,omitempty"`
}

type DataStream struct {
Name string `json:"name"`
TimestampField TimestampField `json:"timestamp_field"`
Expand Down