From 787af80203d905cf7cd2a36bbe0c1cf232888266 Mon Sep 17 00:00:00 2001 From: Robson Sutton Date: Mon, 29 May 2023 12:22:32 +0100 Subject: [PATCH 1/4] Add transform field support --- docs/resources/elasticsearch_watch.md | 4 +++ .../resource.tf | 3 +++ internal/elasticsearch/watcher/watch.go | 25 +++++++++++++++++++ internal/elasticsearch/watcher/watch_test.go | 18 +++++++++++++ internal/models/models.go | 1 + 5 files changed, 51 insertions(+) diff --git a/docs/resources/elasticsearch_watch.md b/docs/resources/elasticsearch_watch.md index 3545fbd75..1b3f3eedd 100644 --- a/docs/resources/elasticsearch_watch.md +++ b/docs/resources/elasticsearch_watch.md @@ -36,6 +36,9 @@ resource "elasticstack_elasticsearch_watch" "example" { metadata = jsonencode({ "example_key" = "example_value" }) + transform = jsonencode({ + "script" = "return [ 'time' : ctx.trigger.scheduled_time ]" + }) throttle_period_in_millis = 10000 } @@ -60,6 +63,7 @@ output "watch" { - `input` (String) The input that defines the input that loads the data for the watch. - `metadata` (String) Metadata json that will be copied into the history entries. - `throttle_period_in_millis` (Number) Minimum time in milliseconds between actions being run. Defaults to 5000. +- `transform` (String) Processes the watch payload to prepare it for the watch actions. ### Read-Only diff --git a/examples/resources/elasticstack_elasticsearch_watch/resource.tf b/examples/resources/elasticstack_elasticsearch_watch/resource.tf index 7e0150975..65758cb26 100644 --- a/examples/resources/elasticstack_elasticsearch_watch/resource.tf +++ b/examples/resources/elasticstack_elasticsearch_watch/resource.tf @@ -21,6 +21,9 @@ resource "elasticstack_elasticsearch_watch" "example" { metadata = jsonencode({ "example_key" = "example_value" }) + transform = jsonencode({ + "script" = "return [ 'time' : ctx.trigger.scheduled_time ]" + }) throttle_period_in_millis = 10000 } diff --git a/internal/elasticsearch/watcher/watch.go b/internal/elasticsearch/watcher/watch.go index c30f05bbe..b8a4dd0d8 100644 --- a/internal/elasticsearch/watcher/watch.go +++ b/internal/elasticsearch/watcher/watch.go @@ -73,6 +73,13 @@ func ResourceWatch() *schema.Resource { Optional: true, Default: "{}", }, + "transform": { + Description: "Processes the watch payload to prepare it for the watch actions.", + Type: schema.TypeString, + ValidateFunc: validation.StringIsJSON, + DiffSuppressFunc: utils.DiffJsonSuppress, + Optional: true, + }, "throttle_period_in_millis": { Description: "Minimum time in milliseconds between actions being run. Defaults to 5000.", Type: schema.TypeInt, @@ -143,6 +150,14 @@ func resourceWatchPut(ctx context.Context, d *schema.ResourceData, meta interfac } watch.Body.Metadata = metadata + if transformJSON, ok := d.GetOk("transform"); ok { + var transform map[string]interface{} + if err := json.Unmarshal([]byte(transformJSON.(string)), &transform); err != nil { + return diag.FromErr(err) + } + watch.Body.Transform = transform + } + watch.Body.Throttle_period_in_millis = d.Get("throttle_period_in_millis").(int) if diags := elasticsearch.PutWatch(ctx, client, &watch); diags.HasError() { @@ -220,6 +235,16 @@ func resourceWatchRead(ctx context.Context, d *schema.ResourceData, meta interfa return diag.FromErr(err) } + if watch.Body.Transform != nil { + transform, err := json.Marshal(watch.Body.Transform) + if err != nil { + return diag.FromErr(err) + } + if err := d.Set("transform", string(transform)); err != nil { + return diag.FromErr(err) + } + } + if err := d.Set("throttle_period_in_millis", watch.Body.Throttle_period_in_millis); err != nil { return diag.FromErr(err) } diff --git a/internal/elasticsearch/watcher/watch_test.go b/internal/elasticsearch/watcher/watch_test.go index 1169ba9e3..b3300d88c 100644 --- a/internal/elasticsearch/watcher/watch_test.go +++ b/internal/elasticsearch/watcher/watch_test.go @@ -41,6 +41,7 @@ func TestResourceWatch(t *testing.T) { resource.TestCheckResourceAttr("elasticstack_elasticsearch_watch.test", "condition", `{"never":{}}`), resource.TestCheckResourceAttr("elasticstack_elasticsearch_watch.test", "actions", `{"log":{"logging":{"level":"info","text":"example logging text"}}}`), resource.TestCheckResourceAttr("elasticstack_elasticsearch_watch.test", "metadata", `{"example_key":"example_value"}`), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_watch.test", "transform", `{"search":{"request":{"body":{"query":{"match_all":{}}},"indices":[],"rest_total_hits_as_int":true,"search_type":"query_then_fetch"}}}`), resource.TestCheckResourceAttr("elasticstack_elasticsearch_watch.test", "throttle_period_in_millis", "10000"), ), }, @@ -114,6 +115,23 @@ EOF } EOF + transform = < Date: Mon, 29 May 2023 12:35:52 +0100 Subject: [PATCH 2/4] Add migration instructions surrounding logstash_pipelines breaking change to changelog --- CHANGELOG.md | 9 +++++++++ internal/elasticsearch/watcher/watch_test.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d915f7572..95e8dab61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - Add `path_style_access` setting to `elasticstack_elasticsearch_snapshot_repository` on s3 repositories to enable path style access pattern ([#331](https://github.com/elastic/terraform-provider-elasticstack/pull/331)) +- Add `transform` field to `elasticstack_elasticsearch_watch` to allow for payload transforms to be defined ([#340](https://github.com/elastic/terraform-provider-elasticstack/pull/340)) ## [0.6.0] - 2023-05-24 @@ -58,6 +59,14 @@ }) } ``` + - If migrating existing resources in state from a previous version of the provider, then you will need to remove the reference to the resources in state before reapplying / reimporting: + - Run `terraform state rm` against your logstash pipelines (https://developer.hashicorp.com/terraform/cli/commands/state/rm) + - Ensure any definitions of the `pipeline_metadata` field in your resource definitions have been encapsulated with `jsonencode()` as mentioned above. + - EITHER + - run `terraform plan` + - run `terraform apply` + - OR + - reimport the resources into state using `terraform import` (https://developer.hashicorp.com/terraform/cli/import) - Fix order of `indices` field in SLM ([#326](https://github.com/elastic/terraform-provider-elasticstack/pull/326)) ## [0.5.0] - 2022-12-07 diff --git a/internal/elasticsearch/watcher/watch_test.go b/internal/elasticsearch/watcher/watch_test.go index b3300d88c..52b662336 100644 --- a/internal/elasticsearch/watcher/watch_test.go +++ b/internal/elasticsearch/watcher/watch_test.go @@ -118,8 +118,8 @@ EOF transform = < Date: Mon, 29 May 2023 14:28:11 +0100 Subject: [PATCH 3/4] resolve error when using disabled_features field in elasticstack_kibana_space --- CHANGELOG.md | 3 +++ docs/resources/kibana_space.md | 9 +++++---- examples/resources/elasticstack_kibana_space/resource.tf | 9 +++++---- internal/kibana/space.go | 9 +++++++-- internal/kibana/space_test.go | 9 ++++++--- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95e8dab61..169246aab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - Add `path_style_access` setting to `elasticstack_elasticsearch_snapshot_repository` on s3 repositories to enable path style access pattern ([#331](https://github.com/elastic/terraform-provider-elasticstack/pull/331)) - Add `transform` field to `elasticstack_elasticsearch_watch` to allow for payload transforms to be defined ([#340](https://github.com/elastic/terraform-provider-elasticstack/pull/340)) +### Fixed +- Fix error presented by incorrect handling of `disabled_features` field in `elasticstack_kibana_space` resource ([#340](https://github.com/elastic/terraform-provider-elasticstack/pull/340)) + ## [0.6.0] - 2023-05-24 ### Added diff --git a/docs/resources/kibana_space.md b/docs/resources/kibana_space.md index ca65af39f..2acb4486a 100644 --- a/docs/resources/kibana_space.md +++ b/docs/resources/kibana_space.md @@ -18,10 +18,11 @@ provider "elasticstack" { } resource "elasticstack_kibana_space" "example" { - space_id = "test_space" - name = "Test Space" - description = "A fresh space for testing visualisations" - initials = "ts" + space_id = "test_space" + name = "Test Space" + description = "A fresh space for testing visualisations" + disabled_features = ["ingestManager", "enterpriseSearch"] + initials = "ts" } ``` diff --git a/examples/resources/elasticstack_kibana_space/resource.tf b/examples/resources/elasticstack_kibana_space/resource.tf index c42be10f5..0633a8eb2 100644 --- a/examples/resources/elasticstack_kibana_space/resource.tf +++ b/examples/resources/elasticstack_kibana_space/resource.tf @@ -3,8 +3,9 @@ provider "elasticstack" { } resource "elasticstack_kibana_space" "example" { - space_id = "test_space" - name = "Test Space" - description = "A fresh space for testing visualisations" - initials = "ts" + space_id = "test_space" + name = "Test Space" + description = "A fresh space for testing visualisations" + disabled_features = ["ingestManager", "enterpriseSearch"] + initials = "ts" } diff --git a/internal/kibana/space.go b/internal/kibana/space.go index 5ead41040..716b1f172 100644 --- a/internal/kibana/space.go +++ b/internal/kibana/space.go @@ -90,9 +90,14 @@ func resourceSpaceUpsert(ctx context.Context, d *schema.ResourceData, meta inter space.Description = description.(string) } - if disabledFeatures, ok := d.GetOk("disabled_features"); ok { - space.DisabledFeatures = disabledFeatures.([]string) + features := make([]string, 0) + if v, ok := d.GetOk("disabled_features"); ok { + p := v.(*schema.Set) + for _, e := range p.List() { + features = append(features, e.(string)) + } } + space.DisabledFeatures = features if initials, ok := d.GetOk("initials"); ok { space.Initials = initials.(string) diff --git a/internal/kibana/space_test.go b/internal/kibana/space_test.go index 0ee16901a..43f728d1b 100644 --- a/internal/kibana/space_test.go +++ b/internal/kibana/space_test.go @@ -33,6 +33,8 @@ func TestAccResourceSpace(t *testing.T) { resource.TestCheckResourceAttr("elasticstack_kibana_space.test_space", "space_id", spaceId), resource.TestCheckResourceAttr("elasticstack_kibana_space.test_space", "name", fmt.Sprintf("Updated %s", spaceId)), resource.TestCheckResourceAttr("elasticstack_kibana_space.test_space", "description", "Updated space description"), + resource.TestCheckTypeSetElemAttr("elasticstack_kibana_space.test_space", "disabled_features.*", "ingestManager"), + resource.TestCheckTypeSetElemAttr("elasticstack_kibana_space.test_space", "disabled_features.*", "enterpriseSearch"), ), }, }, @@ -62,9 +64,10 @@ provider "elasticstack" { } resource "elasticstack_kibana_space" "test_space" { - space_id = "%s" - name = "%s" - description = "Updated space description" + space_id = "%s" + name = "%s" + description = "Updated space description" + disabled_features = ["ingestManager", "enterpriseSearch"] } `, id, fmt.Sprintf("Updated %s", id)) } From 6520b47a8cb4fc1a8de2a10b39c90f92c79e308d Mon Sep 17 00:00:00 2001 From: Robson Sutton Date: Mon, 29 May 2023 14:45:25 +0100 Subject: [PATCH 4/4] fix formatting - spaces vs tabs --- internal/elasticsearch/watcher/watch_test.go | 4 ++-- internal/kibana/space_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/elasticsearch/watcher/watch_test.go b/internal/elasticsearch/watcher/watch_test.go index 52b662336..e9d8b92cf 100644 --- a/internal/elasticsearch/watcher/watch_test.go +++ b/internal/elasticsearch/watcher/watch_test.go @@ -127,8 +127,8 @@ EOF "indices": [], "rest_total_hits_as_int" : true, "search_type": "query_then_fetch" - } - } + } + } } EOF diff --git a/internal/kibana/space_test.go b/internal/kibana/space_test.go index 43f728d1b..5cef8b750 100644 --- a/internal/kibana/space_test.go +++ b/internal/kibana/space_test.go @@ -67,7 +67,7 @@ resource "elasticstack_kibana_space" "test_space" { space_id = "%s" name = "%s" description = "Updated space description" - disabled_features = ["ingestManager", "enterpriseSearch"] + disabled_features = ["ingestManager", "enterpriseSearch"] } `, id, fmt.Sprintf("Updated %s", id)) }