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
Expand Up @@ -2,6 +2,7 @@
### Added
- New resource `elasticstack_elasticsearch_data_stream` to manage Elasticsearch [data streams](https://www.elastic.co/guide/en/elasticsearch/reference/current/data-streams.html) ([#45](https://github.com/elastic/terraform-provider-elasticstack/pull/45))
- New resource `elasticstack_elasticsearch_ingest_pipeline` to manage Elasticsearch [ingest pipelines](https://www.elastic.co/guide/en/elasticsearch/reference/7.16/ingest.html) ([#56](https://github.com/elastic/terraform-provider-elasticstack/issues/56))
- New resource `elasticstack_elasticsearch_component_template` to manage Elasticsearch [component templates](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-component-template.html) ([#39](https://github.com/elastic/terraform-provider-elasticstack/pull/39))

### Fixed
- Update only changed index settings ([#52](https://github.com/elastic/terraform-provider-elasticstack/issues/52))
Expand Down
104 changes: 104 additions & 0 deletions docs/resources/elasticsearch_component_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

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

All the documentation in the docs/ folder is generated and should not be changed manually. See how it's done for template resource:

---
subcategory: "Index"
layout: ""
page_title: "Elasticstack: elasticstack_elasticsearch_index_template Resource"
description: |-
Creates or updates an index template.
---
# Resource: elasticstack_elasticsearch_index_template
Creates or updates an index template. Index templates define settings, mappings, and aliases that can be applied automatically to new indices. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-template.html

you could copy this template and change the specific parts in names and the paths to the examples and run make gen to generate the required files, which you then can add into this PR as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used make docs-generate to generate this file, but without adding the template file. I'll add it and regenete it the new documentation just after

subcategory: "Index"
layout: ""
page_title: "Elasticstack: elasticstack_elasticsearch_component_template Resource"
description: |-
Creates or updates a component template.
---

# Resource: elasticstack_elasticsearch_component_template

Creates or updates a component template. Component templates are building blocks for constructing index templates that specify index mappings, settings, and aliases. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-component-template.html

## Example Usage

```terraform
provider "elasticstack" {
elasticsearch {}
}

resource "elasticstack_elasticsearch_component_template" "my_template" {
name = "my_template"

template {
aliases {
name = "my_template_test"
}

settings = jsonencode({
number_of_shards = "3"
})
}
}

resource "elasticstack_elasticsearch_index_template" "my_template" {
name = "my_data_stream"

index_patterns = ["stream*"]
composed_of = [elasticstack_elasticsearch_component_template.my_template.name]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) Name of the component template to create.
- **template** (Block List, Min: 1, Max: 1) Template to be applied. It may optionally include an aliases, mappings, or settings configuration. (see [below for nested schema](#nestedblock--template))

### Optional

- **elasticsearch_connection** (Block List, Max: 1) Used to establish connection to Elasticsearch server. Overrides environment variables if present. (see [below for nested schema](#nestedblock--elasticsearch_connection))
- **metadata** (String) Optional user metadata about the component template.
- **version** (Number) Version number used to manage component templates externally.

### Read-Only

- **id** (String) Internal identifier of the resource

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

Optional:

- **alias** (Block Set) Alias to add. (see [below for nested schema](#nestedblock--template--alias))
- **mappings** (String) Mapping for fields in the index.
- **settings** (String) Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

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

Required:

- **name** (String) The alias name. Index alias names support date math. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html

Optional:

- **filter** (String) Query used to limit documents the alias can access.
- **index_routing** (String) Value used to route indexing operations to a specific shard. If specified, this overwrites the routing value for indexing operations.
- **is_hidden** (Boolean) If true, the alias is hidden.
- **is_write_index** (Boolean) If true, the index is the write index for the alias.
- **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--elasticsearch_connection"></a>
### Nested Schema for `elasticsearch_connection`

Optional:

- **ca_file** (String) Path to a custom Certificate Authority certificate
- **endpoints** (List of String, Sensitive) A list of endpoints the Terraform provider will point to. They must include the http(s) schema and port number.
- **insecure** (Boolean) Disable TLS certificate validation
- **password** (String, Sensitive) A password to use for API authentication to Elasticsearch.
- **username** (String) A username to use for API authentication to Elasticsearch.

## Import

Import is supported using the following syntax:

```shell
terraform import elasticstack_elasticsearch_component_template.my_template <cluster_uuid>/<component_name>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
terraform import elasticstack_elasticsearch_component_template.my_template <cluster_uuid>/<component_name>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
provider "elasticstack" {
elasticsearch {}
}

resource "elasticstack_elasticsearch_component_template" "my_template" {
name = "my_template"

template {
aliases {
name = "my_template_test"
}

settings = jsonencode({
number_of_shards = "3"
})
}
}

resource "elasticstack_elasticsearch_index_template" "my_template" {
name = "my_data_stream"

index_patterns = ["stream*"]
composed_of = [elasticstack_elasticsearch_component_template.my_template.name]
}
66 changes: 66 additions & 0 deletions internal/clients/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,72 @@ func (a *ApiClient) DeleteElasticsearchIlm(policyName string) diag.Diagnostics {
return diags
}

func (a *ApiClient) PutElasticsearchComponentTemplate(template *models.ComponentTemplate) diag.Diagnostics {
var diags diag.Diagnostics
templateBytes, err := json.Marshal(template)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[TRACE] sending request to ES: %s to create component template '%s' ", templateBytes, template.Name)

res, err := a.es.Cluster.PutComponentTemplate(template.Name, bytes.NewReader(templateBytes))
if err != nil {
return diag.FromErr(err)
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to create component template"); diags.HasError() {
return diags
}

return diags
}

func (a *ApiClient) GetElasticsearchComponentTemplate(templateName string) (*models.ComponentTemplateResponse, diag.Diagnostics) {
var diags diag.Diagnostics
req := a.es.Cluster.GetComponentTemplate.WithName(templateName)
res, err := a.es.Cluster.GetComponentTemplate(req)
if err != nil {
return nil, diag.FromErr(err)
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
return nil, nil
}
if diags := utils.CheckError(res, "Unable to request index template."); diags.HasError() {
return nil, diags
}

var componentTemplates models.ComponentTemplatesResponse
if err := json.NewDecoder(res.Body).Decode(&componentTemplates); err != nil {
return nil, diag.FromErr(err)
}

// we requested only 1 template
if len(componentTemplates.ComponentTemplates) != 1 {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Wrong number of templates returned",
Detail: fmt.Sprintf("Elasticsearch API returned %d when requested '%s' component template.", len(componentTemplates.ComponentTemplates), templateName),
})
return nil, diags
}
tpl := componentTemplates.ComponentTemplates[0]
return &tpl, diags
}

func (a *ApiClient) DeleteElasticsearchComponentTemplate(templateName string) diag.Diagnostics {
var diags diag.Diagnostics
res, err := a.es.Cluster.DeleteComponentTemplate(templateName)
if err != nil {
return diag.FromErr(err)
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to delete component template"); diags.HasError() {
return diags
}
return diags
}

func (a *ApiClient) PutElasticsearchIndexTemplate(template *models.IndexTemplate) diag.Diagnostics {
var diags diag.Diagnostics
templateBytes, err := json.Marshal(template)
Expand Down
Loading