Skip to content

Commit e46cbbc

Browse files
committed
Elasticsearch indices data source
1 parent d928605 commit e46cbbc

File tree

11 files changed

+885
-3
lines changed

11 files changed

+885
-3
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
subcategory: "Index"
3+
layout: ""
4+
page_title: "Elasticstack: elasticstack_elasticsearch_indices Data Source"
5+
description: |-
6+
Retrieves indices.
7+
---
8+
9+
# Data Source: elasticstack_elasticsearch_indices
10+
11+
Use this data source to retrieve and get information about existing Elasticsearch indices. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-index.html
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider "elasticstack" {
17+
elasticsearch {}
18+
kibana {}
19+
}
20+
21+
data "elasticstack_elasticsearch_indices" "logs" {
22+
search = "log*"
23+
}
24+
```
25+
26+
<!-- schema generated by tfplugindocs -->
27+
## Schema
28+
29+
### Optional
30+
31+
- `search` (String) Comma-separated list of indices to resolve by their name. Supports wildcards `*`.
32+
33+
### Read-Only
34+
35+
- `id` (String) Generated ID for the indices.
36+
- `indices` (Attributes List) The list of indices. (see [below for nested schema](#nestedatt--indices))
37+
38+
<a id="nestedatt--indices"></a>
39+
### Nested Schema for `indices`
40+
41+
Required:
42+
43+
- `name` (String) Name of the index.
44+
45+
Optional:
46+
47+
- `alias` (Attributes Set) Aliases for the index. (see [below for nested schema](#nestedatt--indices--alias))
48+
- `analysis_analyzer` (String) A JSON string describing the analyzers applied to the index.
49+
- `analysis_char_filter` (String) A JSON string describing the char_filters applied to the index.
50+
- `analysis_filter` (String) A JSON string describing the filters applied to the index.
51+
- `analysis_normalizer` (String) A JSON string describing the normalizers applied to the index.
52+
- `analysis_tokenizer` (String) A JSON string describing the tokenizers applied to the index.
53+
- `analyze_max_token_count` (Number) The maximum number of tokens that can be produced using _analyze API.
54+
- `auto_expand_replicas` (String) The number of replicas to the node count in the cluster. Set to a dash delimited lower and upper bound (e.g. 0-5) or use all for the upper bound (e.g. 0-all)
55+
- `blocks_metadata` (Boolean) Set to `true` to disable index metadata reads and writes.
56+
- `blocks_read` (Boolean) Set to `true` to disable read operations against the index.
57+
- `blocks_read_only` (Boolean) Set to `true` to make the index and index metadata read only, `false` to allow writes and metadata changes.
58+
- `blocks_read_only_allow_delete` (Boolean) Identical to `index.blocks.read_only` but allows deleting the index to free up resources.
59+
- `blocks_write` (Boolean) Set to `true` to disable data write operations against the index. This setting does not affect metadata.
60+
- `codec` (String) The `default` value compresses stored data with LZ4 compression, but this can be set to `best_compression` which uses DEFLATE for a higher compression ratio.
61+
- `default_pipeline` (String) The default ingest node pipeline for this index. Index requests will fail if the default pipeline is set and the pipeline does not exist.
62+
- `deletion_protection` (Boolean) Whether to allow Terraform to destroy the index. Unless this field is set to false in Terraform state, a terraform destroy or terraform apply command that deletes the instance will fail.
63+
- `final_pipeline` (String) Final ingest pipeline for the index. Indexing requests will fail if the final pipeline is set and the pipeline does not exist. The final pipeline always runs after the request pipeline (if specified) and the default pipeline (if it exists). The special pipeline name _none indicates no ingest pipeline will run.
64+
- `gc_deletes` (String) The length of time that a deleted document's version number remains available for further versioned operations.
65+
- `highlight_max_analyzed_offset` (Number) The maximum number of characters that will be analyzed for a highlight request.
66+
- `include_type_name` (Boolean) If true, a mapping type is expected in the body of mappings. Defaults to false. Supported for Elasticsearch 7.x.
67+
- `indexing_slowlog_level` (String) Set which logging level to use for the search slow log, can be: `warn`, `info`, `debug`, `trace`
68+
- `indexing_slowlog_source` (String) Set the number of characters of the `_source` to include in the slowlog lines, `false` or `0` will skip logging the source entirely and setting it to `true` will log the entire source regardless of size. The original `_source` is reformatted by default to make sure that it fits on a single log line.
69+
- `indexing_slowlog_threshold_index_debug` (String) Set the cutoff for shard level slow search logging of slow searches for indexing queries, in time units, e.g. `2s`
70+
- `indexing_slowlog_threshold_index_info` (String) Set the cutoff for shard level slow search logging of slow searches for indexing queries, in time units, e.g. `5s`
71+
- `indexing_slowlog_threshold_index_trace` (String) Set the cutoff for shard level slow search logging of slow searches for indexing queries, in time units, e.g. `500ms`
72+
- `indexing_slowlog_threshold_index_warn` (String) Set the cutoff for shard level slow search logging of slow searches for indexing queries, in time units, e.g. `10s`
73+
- `load_fixed_bitset_filters_eagerly` (Boolean) Indicates whether cached filters are pre-loaded for nested queries.
74+
- `mapping_coerce` (Boolean) The index level coercion setting that is applied to all mapping types.
75+
- `mappings` (String) Mapping for fields in the index.
76+
If specified, this mapping can include: field names, [field data types](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html), [mapping parameters](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html).
77+
**NOTE:**
78+
- Changing datatypes in the existing _mappings_ will force index to be re-created.
79+
- Removing field will be ignored by default same as elasticsearch. You need to recreate the index to remove field completely.
80+
- `master_timeout` (String) Period to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`. This value is ignored when running against Serverless projects.
81+
- `max_docvalue_fields_search` (Number) The maximum number of `docvalue_fields` that are allowed in a query.
82+
- `max_inner_result_window` (Number) The maximum value of `from + size` for inner hits definition and top hits aggregations to this index.
83+
- `max_ngram_diff` (Number) The maximum allowed difference between min_gram and max_gram for NGramTokenizer and NGramTokenFilter.
84+
- `max_refresh_listeners` (Number) Maximum number of refresh listeners available on each shard of the index.
85+
- `max_regex_length` (Number) The maximum length of regex that can be used in Regexp Query.
86+
- `max_rescore_window` (Number) The maximum value of `window_size` for `rescore` requests in searches of this index.
87+
- `max_result_window` (Number) The maximum value of `from + size` for searches to this index.
88+
- `max_script_fields` (Number) The maximum number of `script_fields` that are allowed in a query.
89+
- `max_shingle_diff` (Number) The maximum allowed difference between max_shingle_size and min_shingle_size for ShingleTokenFilter.
90+
- `max_terms_count` (Number) The maximum number of terms that can be used in Terms Query.
91+
- `number_of_routing_shards` (Number) Value used with number_of_shards to route documents to a primary shard.
92+
- `number_of_shards` (Number) Number of shards for the index.
93+
- `query_default_field` (Set of String) Wildcard (*) patterns matching one or more fields. Defaults to '*', which matches all fields eligible for term-level queries, excluding metadata fields.
94+
- `refresh_interval` (String) How often to perform a refresh operation, which makes recent changes to the index visible to search. Can be set to `-1` to disable refresh.
95+
- `routing_allocation_enable` (String) Controls shard allocation for this index. It can be set to: `all` , `primaries` , `new_primaries` , `none`.
96+
- `routing_partition_size` (Number) The number of shards a custom routing value can go to.
97+
- `routing_rebalance_enable` (String) Enables shard rebalancing for this index. It can be set to: `all`, `primaries` , `replicas` , `none`.
98+
- `search_idle_after` (String) How long a shard can not receive a search or get request until it’s considered search idle.
99+
- `search_slowlog_level` (String) Set which logging level to use for the search slow log, can be: `warn`, `info`, `debug`, `trace`
100+
- `search_slowlog_threshold_fetch_debug` (String) Set the cutoff for shard level slow search logging of slow searches in the fetch phase, in time units, e.g. `2s`
101+
- `search_slowlog_threshold_fetch_info` (String) Set the cutoff for shard level slow search logging of slow searches in the fetch phase, in time units, e.g. `5s`
102+
- `search_slowlog_threshold_fetch_trace` (String) Set the cutoff for shard level slow search logging of slow searches in the fetch phase, in time units, e.g. `500ms`
103+
- `search_slowlog_threshold_fetch_warn` (String) Set the cutoff for shard level slow search logging of slow searches in the fetch phase, in time units, e.g. `10s`
104+
- `search_slowlog_threshold_query_debug` (String) Set the cutoff for shard level slow search logging of slow searches in the query phase, in time units, e.g. `2s`
105+
- `search_slowlog_threshold_query_info` (String) Set the cutoff for shard level slow search logging of slow searches in the query phase, in time units, e.g. `5s`
106+
- `search_slowlog_threshold_query_trace` (String) Set the cutoff for shard level slow search logging of slow searches in the query phase, in time units, e.g. `500ms`
107+
- `search_slowlog_threshold_query_warn` (String) Set the cutoff for shard level slow search logging of slow searches in the query phase, in time units, e.g. `10s`
108+
- `shard_check_on_startup` (String) Whether or not shards should be checked for corruption before opening. When corruption is detected, it will prevent the shard from being opened. Accepts `false`, `true`, `checksum`.
109+
- `sort_field` (Set of String) The field to sort shards in this index by.
110+
- `sort_order` (List of String) The direction to sort shards in. Accepts `asc`, `desc`.
111+
- `timeout` (String) Period to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`.
112+
- `unassigned_node_left_delayed_timeout` (String) Time to delay the allocation of replica shards which become unassigned because a node has left, in time units, e.g. `10s`
113+
- `wait_for_active_shards` (String) The number of shard copies that must be active before proceeding with the operation. Set to `all` or any positive integer up to the total number of shards in the index (number_of_replicas+1). Default: `1`, the primary shard. This value is ignored when running against Serverless projects.
114+
115+
Read-Only:
116+
117+
- `id` (String) Internal identifier of the resource.
118+
- `number_of_replicas` (Number) Number of shard replicas.
119+
- `settings_raw` (String) All raw settings fetched from the cluster.
120+
121+
<a id="nestedatt--indices--alias"></a>
122+
### Nested Schema for `indices.alias`
123+
124+
Required:
125+
126+
- `name` (String) Index alias name.
127+
128+
Optional:
129+
130+
- `filter` (String) Query used to limit documents the alias can access.
131+
- `index_routing` (String) Value used to route indexing operations to a specific shard. If specified, this overwrites the `routing` value for indexing operations.
132+
- `is_hidden` (Boolean) If true, the alias is hidden.
133+
- `is_write_index` (Boolean) If true, the index is the write index for the alias.
134+
- `routing` (String) Value used to route indexing and search operations to a specific shard.
135+
- `search_routing` (String) Value used to route search operations to a specific shard. If specified, this overwrites the routing value for search operations.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
provider "elasticstack" {
3+
elasticsearch {}
4+
kibana {}
5+
}
6+
7+
data "elasticstack_elasticsearch_indices" "logs" {
8+
search = "log*"
9+
}

internal/clients/elasticsearch/index.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,16 @@ func DeleteIndex(ctx context.Context, apiClient *clients.ApiClient, name string)
301301
}
302302

303303
func GetIndex(ctx context.Context, apiClient *clients.ApiClient, name string) (*models.Index, diag.Diagnostics) {
304+
indices, diags := GetIndices(ctx, apiClient, name)
305+
if diags.HasError() {
306+
return nil, diags
307+
}
308+
309+
index := indices[name]
310+
return &index, diags
311+
}
312+
313+
func GetIndices(ctx context.Context, apiClient *clients.ApiClient, name string) (map[string]models.Index, diag.Diagnostics) {
304314
var diags diag.Diagnostics
305315

306316
esClient, err := apiClient.GetESClient()
@@ -326,8 +336,7 @@ func GetIndex(ctx context.Context, apiClient *clients.ApiClient, name string) (*
326336
if err := json.NewDecoder(res.Body).Decode(&indices); err != nil {
327337
return nil, diag.FromErr(err)
328338
}
329-
index := indices[name]
330-
return &index, diags
339+
return indices, diags
331340
}
332341

333342
func DeleteIndexAlias(ctx context.Context, apiClient *clients.ApiClient, index string, aliases []string) diag.Diagnostics {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package indices
2+
3+
import (
4+
"context"
5+
6+
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
)
9+
10+
// Ensure the implementation satisfies the expected interfaces.
11+
var (
12+
_ datasource.DataSource = &dataSource{}
13+
_ datasource.DataSource = &dataSource{}
14+
_ datasource.DataSourceWithConfigure = &dataSource{}
15+
)
16+
17+
// NewDataSource is a helper function to simplify the provider implementation.
18+
func NewDataSource() datasource.DataSource {
19+
return &dataSource{}
20+
}
21+
22+
// dataSource is the data source implementation.
23+
type dataSource struct {
24+
client clients.ApiClient
25+
}
26+
27+
// Metadata returns the data source type name.
28+
func (d *dataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
29+
resp.TypeName = req.ProviderTypeName + "_elasticsearch_indices"
30+
}
31+
32+
// Configure adds the provider configured client to the data source.
33+
func (d *dataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
34+
// Add a nil check when handling ProviderData because Terraform
35+
// sets that data after it calls the ConfigureProvider RPC.
36+
if req.ProviderData == nil {
37+
return
38+
}
39+
40+
apiClient, diags := clients.ConvertProviderData(req.ProviderData)
41+
resp.Diagnostics.Append(diags...)
42+
if resp.Diagnostics.HasError() {
43+
return
44+
}
45+
46+
d.client = *apiClient
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package indices_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
)
9+
10+
func TestAccIndicesDataSource(t *testing.T) {
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { acctest.PreCheck(t) },
13+
ProtoV6ProviderFactories: acctest.Providers,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccIndicesDataSourceConfig,
17+
Check: resource.ComposeAggregateTestCheckFunc(
18+
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_indices.all_indices", "indices.0.name", ".internal.alerts-default.alerts-default-000001"),
19+
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_indices.all_indices", "indices.0.number_of_shards", "1"),
20+
resource.TestCheckResourceAttr("data.elasticstack_elasticsearch_indices.all_indices", "indices.0.alias.0.name", ".alerts-default.alerts-default"),
21+
),
22+
},
23+
},
24+
})
25+
}
26+
27+
const testAccIndicesDataSourceConfig = `
28+
provider "elasticstack" {
29+
elasticsearch {}
30+
kibana {}
31+
}
32+
33+
data "elasticstack_elasticsearch_indices" "all_indices" {
34+
search = ".internal.alerts-default.alerts-default-0*"
35+
}
36+
`

0 commit comments

Comments
 (0)