diff --git a/CHANGELOG.md b/CHANGELOG.md index f6c786c45..b5f6b552c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix secret handling `elasticstack_fleet_integration_policy` resource. ([#821](https://github.com/elastic/terraform-provider-elasticstack/pull/821)) - Fix merge values for `elasticstack_kibana_synthetics_monitor` monitor locations ([#823](https://github.com/elastic/terraform-provider-elasticstack/pull/823) +- Add `elasticstack_elasticsearch_index_template` data source ([#828](https://github.com/elastic/terraform-provider-elasticstack/pull/828)) ## [0.11.8] - 2024-10-02 diff --git a/docs/data-sources/elasticsearch_index_template.md b/docs/data-sources/elasticsearch_index_template.md new file mode 100644 index 000000000..2a8cd4ec6 --- /dev/null +++ b/docs/data-sources/elasticsearch_index_template.md @@ -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 + +### 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. + + +### 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. + + + +### Nested Schema for `data_stream` + +Read-Only: + +- `allow_custom_routing` (Boolean) +- `hidden` (Boolean) + + + +### 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) + + +### 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) + + + +### Nested Schema for `template.lifecycle` + +Read-Only: + +- `data_retention` (String) diff --git a/examples/data-sources/elasticstack_elasticsearch_index_template/data-source.tf b/examples/data-sources/elasticstack_elasticsearch_index_template/data-source.tf new file mode 100644 index 000000000..7942f9d5b --- /dev/null +++ b/examples/data-sources/elasticstack_elasticsearch_index_template/data-source.tf @@ -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 +} diff --git a/internal/elasticsearch/index/template_data_source.go b/internal/elasticsearch/index/template_data_source.go new file mode 100644 index 000000000..8d1630268 --- /dev/null +++ b/internal/elasticsearch/index/template_data_source.go @@ -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) +} diff --git a/internal/elasticsearch/index/template_data_source_test.go b/internal/elasticsearch/index/template_data_source_test.go new file mode 100644 index 000000000..c37ee5797 --- /dev/null +++ b/internal/elasticsearch/index/template_data_source_test.go @@ -0,0 +1,49 @@ +package index_test + +import ( + "fmt" + "testing" + + "github.com/elastic/terraform-provider-elasticstack/internal/acctest" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccIndexTemplateDataSource(t *testing.T) { + // generate a random role name + templateName := "test-template-" + sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + ProtoV6ProviderFactories: acctest.Providers, + Steps: []resource.TestStep{ + { + Config: testAccIndexTemplateDataSourceConfig(templateName), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "name", templateName), + resource.TestCheckTypeSetElemAttr("data.elasticstack_elasticsearch_index_template.test", "index_patterns.*", fmt.Sprintf("tf-acc-%s-*", templateName)), + resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_index_template.test", "priority", "100"), + ), + }, + }, + }) +} + +func testAccIndexTemplateDataSourceConfig(templateName string) string { + return fmt.Sprintf(` +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_index_template" "test" { + name = "%s" + + priority = 100 + index_patterns = ["tf-acc-%s-*"] +} + +data "elasticstack_elasticsearch_index_template" "test" { + name = elasticstack_elasticsearch_index_template.test.name +} + `, templateName, templateName) +} diff --git a/provider/provider.go b/provider/provider.go index b86ae0ab1..4a7c9c69f 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -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(), diff --git a/templates/data-sources/elasticsearch_index_template.md.tmpl b/templates/data-sources/elasticsearch_index_template.md.tmpl new file mode 100644 index 000000000..2c45cd386 --- /dev/null +++ b/templates/data-sources/elasticsearch_index_template.md.tmpl @@ -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 }}