From 9d14bb8f4f524822a9c6dcfbf2f912d6f1289177 Mon Sep 17 00:00:00 2001 From: Oleksandr Kylymnychenko Date: Thu, 3 Feb 2022 08:05:23 +0100 Subject: [PATCH 1/5] Add resource to manage ingest pipelines --- .../elasticsearch_ingest_pipeline.md | 82 +++++++ .../import.sh | 1 + .../resource.tf | 28 +++ internal/clients/index.go | 60 +++++ internal/elasticsearch/ingest/pipeline.go | 223 ++++++++++++++++++ .../elasticsearch/ingest/pipeline_test.go | 1 + internal/models/models.go | 8 + internal/provider/provider.go | 2 + .../elasticsearch_ingest_pipeline.md.tmpl | 23 ++ 9 files changed, 428 insertions(+) create mode 100644 docs/resources/elasticsearch_ingest_pipeline.md create mode 100644 examples/resources/elasticstack_elasticsearch_ingest_pipeline/import.sh create mode 100644 examples/resources/elasticstack_elasticsearch_ingest_pipeline/resource.tf create mode 100644 internal/elasticsearch/ingest/pipeline.go create mode 100644 internal/elasticsearch/ingest/pipeline_test.go create mode 100644 templates/resources/elasticsearch_ingest_pipeline.md.tmpl diff --git a/docs/resources/elasticsearch_ingest_pipeline.md b/docs/resources/elasticsearch_ingest_pipeline.md new file mode 100644 index 000000000..c20ed74bf --- /dev/null +++ b/docs/resources/elasticsearch_ingest_pipeline.md @@ -0,0 +1,82 @@ +--- +subcategory: "Ingest" +layout: "" +page_title: "Elasticstack: elasticstack_elasticsearch_ingest_pipeline Resource" +description: |- + Manages Ingest Pipelines +--- + +# Resource: elasticstack_elasticsearch_ingest_pipeline + +Use ingest APIs to manage tasks and resources related to ingest pipelines and processors. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-apis.html + +## Example Usage + +```terraform +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_ingest_pipeline" "my_ingest_pipeline" { + name = "my_ingest_pipeline" + description = "My first ingest pipeline managed by Terraform" + + // processors can be defined in different way + processors = [ + // using the jsonencode function, which is the recomended way if you want to provide JSON object by yourself + jsonencode({ + set = { + description = "My set processor descirption" + field = "_meta" + value = "indexed" + } + }), + // or use the HERE DOC construct to provide the processor definition + < +## Schema + +### Required + +- **name** (String) The name of the ingest pipeline. +- **processors** (List of String) Processors used to perform transformations on documents before indexing. Processors run sequentially in the order specified. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/processors.html. Each record must be a valid JSON document. + +### Optional + +- **description** (String) Description of the ingest pipeline. +- **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 index template. +- **on_failure** (List of String) Processors to run immediately after a processor failure. Each processor supports a processor-level `on_failure` value. If a processor without an `on_failure` value fails, Elasticsearch uses this pipeline-level parameter as a fallback. The processors in this parameter run sequentially in the order specified. Elasticsearch will not attempt to run the pipeline’s remaining processors. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/processors.html. Each record must be a valid JSON document + +### Read-Only + +- **id** (String) Internal identifier of the resource + + +### 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_ingest_pipeline.my_ingest_pipeline / +``` diff --git a/examples/resources/elasticstack_elasticsearch_ingest_pipeline/import.sh b/examples/resources/elasticstack_elasticsearch_ingest_pipeline/import.sh new file mode 100644 index 000000000..77374a73c --- /dev/null +++ b/examples/resources/elasticstack_elasticsearch_ingest_pipeline/import.sh @@ -0,0 +1 @@ +terraform import elasticstack_elasticsearch_ingest_pipeline.my_ingest_pipeline / diff --git a/examples/resources/elasticstack_elasticsearch_ingest_pipeline/resource.tf b/examples/resources/elasticstack_elasticsearch_ingest_pipeline/resource.tf new file mode 100644 index 000000000..ea3f12147 --- /dev/null +++ b/examples/resources/elasticstack_elasticsearch_ingest_pipeline/resource.tf @@ -0,0 +1,28 @@ +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_ingest_pipeline" "my_ingest_pipeline" { + name = "my_ingest_pipeline" + description = "My first ingest pipeline managed by Terraform" + + // processors can be defined in different way + processors = [ + // using the jsonencode function, which is the recomended way if you want to provide JSON object by yourself + jsonencode({ + set = { + description = "My set processor descirption" + field = "_meta" + value = "indexed" + } + }), + // or use the HERE DOC construct to provide the processor definition + < Date: Thu, 3 Feb 2022 08:07:36 +0100 Subject: [PATCH 2/5] Add record to CHANGELOG about new ingest_pipeline resource --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5043f8b3..aa66b44a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased] ### 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)) ### Fixed - Update only changed index settings ([#52](https://github.com/elastic/terraform-provider-elasticstack/issues/52)) From df981a65427bde576953bf0dd8a74592e0f7ec03 Mon Sep 17 00:00:00 2001 From: Oleksandr Kylymnychenko Date: Fri, 4 Feb 2022 08:09:37 +0100 Subject: [PATCH 3/5] Add test for ingest pipeline resource --- .../elasticsearch/ingest/pipeline_test.go | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/internal/elasticsearch/ingest/pipeline_test.go b/internal/elasticsearch/ingest/pipeline_test.go index fd3c69ab8..4914ded0b 100644 --- a/internal/elasticsearch/ingest/pipeline_test.go +++ b/internal/elasticsearch/ingest/pipeline_test.go @@ -1 +1,114 @@ package ingest_test + +import ( + "fmt" + "testing" + + "github.com/elastic/terraform-provider-elasticstack/internal/acctest" + "github.com/elastic/terraform-provider-elasticstack/internal/clients" + sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccResourceIngestPipeline(t *testing.T) { + pipelineName := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum) + + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(t) }, + CheckDestroy: checkResourceIngestPipelineDestroy, + ProviderFactories: acctest.Providers, + Steps: []resource.TestStep{ + { + Config: testAccResourceIngestPipelineCreate(pipelineName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("elasticstack_elasticsearch_ingest_pipeline.test_pipeline", "name", pipelineName), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_ingest_pipeline.test_pipeline", "description", "Test Pipeline"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_ingest_pipeline.test_pipeline", "processors.#", "2"), + ), + }, + { + Config: testAccResourceIngestPipelineUpdate(pipelineName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("elasticstack_elasticsearch_ingest_pipeline.test_pipeline", "name", pipelineName), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_ingest_pipeline.test_pipeline", "description", "Test Pipeline"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_ingest_pipeline.test_pipeline", "processors.#", "1"), + ), + }, + }, + }) +} + +func testAccResourceIngestPipelineCreate(name string) string { + return fmt.Sprintf(` +provider "elasticstack" { + elasticsearch {} +} + +resource "elasticstack_elasticsearch_ingest_pipeline" "test_pipeline" { + name = "%s" + description = "Test Pipeline" + + processors = [ + jsonencode({ + set = { + description = "My set processor descirption" + field = "_meta" + value = "indexed" + } + }), + < Date: Fri, 4 Feb 2022 12:01:12 +0100 Subject: [PATCH 4/5] Update internal/elasticsearch/ingest/pipeline_test.go Co-authored-by: Michael Russell --- internal/elasticsearch/ingest/pipeline_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/elasticsearch/ingest/pipeline_test.go b/internal/elasticsearch/ingest/pipeline_test.go index 4914ded0b..477f7d140 100644 --- a/internal/elasticsearch/ingest/pipeline_test.go +++ b/internal/elasticsearch/ingest/pipeline_test.go @@ -52,7 +52,7 @@ resource "elasticstack_elasticsearch_ingest_pipeline" "test_pipeline" { processors = [ jsonencode({ set = { - description = "My set processor descirption" + description = "My set processor description" field = "_meta" value = "indexed" } From 0fa5ee0ae3875837d3441318264e68b83964fe3e Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Fri, 4 Feb 2022 12:01:17 +0100 Subject: [PATCH 5/5] Update internal/elasticsearch/ingest/pipeline_test.go Co-authored-by: Michael Russell --- internal/elasticsearch/ingest/pipeline_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/elasticsearch/ingest/pipeline_test.go b/internal/elasticsearch/ingest/pipeline_test.go index 477f7d140..994536ebe 100644 --- a/internal/elasticsearch/ingest/pipeline_test.go +++ b/internal/elasticsearch/ingest/pipeline_test.go @@ -82,7 +82,7 @@ resource "elasticstack_elasticsearch_ingest_pipeline" "test_pipeline" { processors = [ jsonencode({ set = { - description = "My set processor descirption" + description = "My set processor description" field = "_meta" value = "indexed" }