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
109 changes: 109 additions & 0 deletions docs/data-sources/elasticsearch_index_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
subcategory: "Index"
layout: ""
page_title: "Elasticstack: elasticstack_elasticsearch_index_template Data Source"
description: |-
Retrieves index template.
---

# Data Source: elasticstack_elasticsearch_index_template

Use this data source to retrieve information about existing Elasticsearch index templates. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-template.html

## Example Usage

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

data "elasticstack_elasticsearch_index_template" "ilm-history-7" {
name = "ilm-history-7"
}

output "template" {
value = data.elasticstack_elasticsearch_index_template.ilm-history-7
}
```

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

### Required

- `name` (String) The name of the index template.

### Optional

- `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))

### Read-Only

- `composed_of` (List of String) An ordered list of component template names.
- `data_stream` (List of Object) If this object is included, the template is used to create data streams and their backing indices. Supports an empty object. (see [below for nested schema](#nestedatt--data_stream))
- `id` (String) Internal identifier of the resource
- `index_patterns` (Set of String) Array of wildcard (*) expressions used to match the names of data streams and indices during creation.
- `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` (List of Object) Template to be applied. It may optionally include an aliases, mappings, lifecycle, or settings configuration. (see [below for nested schema](#nestedatt--template))
- `version` (Number) Version number used to manage index templates externally.

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

Optional:

- `api_key` (String, Sensitive) API Key to use for authentication to Elasticsearch
- `bearer_token` (String, Sensitive) Bearer Token to use for authentication to Elasticsearch
- `ca_data` (String) PEM-encoded custom Certificate Authority certificate
- `ca_file` (String) Path to a custom Certificate Authority certificate
- `cert_data` (String) PEM encoded certificate for client auth
- `cert_file` (String) Path to a file containing the PEM encoded certificate for client auth
- `endpoints` (List of String, Sensitive) A list of endpoints where the terraform provider will point to, this must include the http(s) schema and port number.
- `es_client_authentication` (String, Sensitive) ES Client Authentication field to be used with the bearer token
- `insecure` (Boolean) Disable TLS certificate validation
- `key_data` (String, Sensitive) PEM encoded private key for client auth
- `key_file` (String) Path to a file containing the PEM encoded private key for client auth
- `password` (String, Sensitive) Password to use for API authentication to Elasticsearch.
- `username` (String) Username to use for API authentication to Elasticsearch.


<a id="nestedatt--data_stream"></a>
### Nested Schema for `data_stream`

Read-Only:

- `allow_custom_routing` (Boolean)
- `hidden` (Boolean)


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

Read-Only:

- `alias` (Set of Object) (see [below for nested schema](#nestedobjatt--template--alias))
- `lifecycle` (Set of Object) (see [below for nested schema](#nestedobjatt--template--lifecycle))
- `mappings` (String)
- `settings` (String)

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

Read-Only:

- `filter` (String)
- `index_routing` (String)
- `is_hidden` (Boolean)
- `is_write_index` (Boolean)
- `name` (String)
- `routing` (String)
- `search_routing` (String)


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

Read-Only:

- `data_retention` (String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
provider "elasticstack" {
elasticsearch {}
}

data "elasticstack_elasticsearch_index_template" "ilm-history-7" {
name = "ilm-history-7"
}

output "template" {
value = data.elasticstack_elasticsearch_index_template.ilm-history-7
}
176 changes: 176 additions & 0 deletions internal/elasticsearch/index/template_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package index

import (
"context"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceTemplate() *schema.Resource {
templateSchema := map[string]*schema.Schema{
"id": {
Description: "Internal identifier of the resource",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "The name of the index template.",
Type: schema.TypeString,
Required: true,
},
"composed_of": {
Description: "An ordered list of component template names.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"data_stream": {
Description: "If this object is included, the template is used to create data streams and their backing indices. Supports an empty object.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hidden": {
Description: "If true, the data stream is hidden.",
Type: schema.TypeBool,
Computed: true,
},
"allow_custom_routing": {
Description: "If `true`, the data stream supports custom routing. Defaults to `false`. Available only in **8.x**",
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"index_patterns": {
Description: "Array of wildcard (*) expressions used to match the names of data streams and indices during creation.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"metadata": {
Description: "Optional user metadata about the index template.",
Type: schema.TypeString,
Computed: true,
},
"priority": {
Description: "Priority to determine index template precedence when a new data stream or index is created.",
Type: schema.TypeInt,
Computed: true,
},
"template": {
Description: "Template to be applied. It may optionally include an aliases, mappings, lifecycle, or settings configuration.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"alias": {
Description: "Alias to add.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Description: "The alias name.",
Type: schema.TypeString,
Computed: true,
},
"filter": {
Description: "Query used to limit documents the alias can access.",
Type: schema.TypeString,
Computed: true,
},
"index_routing": {
Description: "Value used to route indexing operations to a specific shard. If specified, this overwrites the `routing` value for indexing operations.",
Type: schema.TypeString,
Computed: true,
},
"is_hidden": {
Description: "If true, the alias is hidden.",
Type: schema.TypeBool,
Computed: true,
},
"is_write_index": {
Description: "If true, the index is the write index for the alias.",
Type: schema.TypeBool,
Computed: true,
},
"routing": {
Description: "Value used to route indexing and search operations to a specific shard.",
Type: schema.TypeString,
Computed: true,
},
"search_routing": {
Description: "Value used to route search operations to a specific shard. If specified, this overwrites the routing value for search operations.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"mappings": {
Description: "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",
Type: schema.TypeString,
Computed: true,
},
"settings": {
Description: "Configuration options for the index. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings",
Type: schema.TypeString,
Computed: true,
},
"lifecycle": {
Description: "Lifecycle of data stream. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/data-stream-lifecycle.html",
Type: schema.TypeSet,
Computed: true,
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,
Computed: true,
},
},
},
},
},
},
},
"version": {
Description: "Version number used to manage index templates externally.",
Type: schema.TypeInt,
Computed: true,
},
}

utils.AddConnectionSchema(templateSchema)

return &schema.Resource{
Description: "Retrieves index template definition. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-template.html",
ReadContext: dataSourceIndexTemplateRead,
Schema: templateSchema,
}
}

func dataSourceIndexTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, diags := clients.NewApiClientFromSDKResource(d, meta)
if diags.HasError() {
return diags
}

templateName := d.Get("name").(string)
id, diags := client.ID(ctx, templateName)
if diags.HasError() {
return diags
}
d.SetId(id.String())

return resourceIndexTemplateRead(ctx, d, meta)
}
35 changes: 35 additions & 0 deletions internal/elasticsearch/index/template_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package index_test

import (
"testing"

"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIndexTemplateDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProtoV6ProviderFactories: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccIndexTemplateDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.logs", "name", "logs"),
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.logs", "index_patterns.0", "logs-*-*"),
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.logs", "priority", "100"),
),
},
},
})
}

const testAccIndexTemplateDataSourceConfig = `
provider "elasticstack" {
elasticsearch {}
kibana {}
}
data "elasticstack_elasticsearch_index_template" "logs" {
name = "logs"
}
`
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func New(version string) *schema.Provider {
fleetKeyName: providerSchema.GetFleetConnectionSchema(),
},
DataSourcesMap: map[string]*schema.Resource{
"elasticstack_elasticsearch_index_template": index.DataSourceTemplate(),
"elasticstack_elasticsearch_ingest_processor_append": ingest.DataSourceProcessorAppend(),
"elasticstack_elasticsearch_ingest_processor_bytes": ingest.DataSourceProcessorBytes(),
"elasticstack_elasticsearch_ingest_processor_circle": ingest.DataSourceProcessorCircle(),
Expand Down
17 changes: 17 additions & 0 deletions templates/data-sources/elasticsearch_index_template.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
subcategory: "Index"
layout: ""
page_title: "Elasticstack: elasticstack_elasticsearch_index_template Data Source"
description: |-
Retrieves index template.
---

# Data Source: elasticstack_elasticsearch_index_template

Use this data source to retrieve information about existing Elasticsearch index templates. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-template.html

## Example Usage

{{ tffile "examples/data-sources/elasticstack_elasticsearch_index_template/data-source.tf" }}

{{ .SchemaMarkdown | trimspace }}
Loading