From c1f0b753eefca733d22b1867369061d4d379fbaf Mon Sep 17 00:00:00 2001 From: Jakob Hahn Date: Tue, 10 Sep 2024 16:55:49 +0200 Subject: [PATCH] plugins/ism: Fix error notification types Signed-off-by: Jakob Hahn --- CHANGELOG.md | 1 + plugins/ism/api_policies.go | 9 ++++-- plugins/ism/api_policies_test.go | 47 ++++++++++++++++++++++++++++++-- plugins/ism/api_test.go | 4 +-- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c2a00c2..abb427d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Fixed - Fix ISM Transition to omitempty Conditions field ([#609](https://github.com/opensearch-project/opensearch-go/pull/609)) - Fix ISM Allocation field types ([#609](https://github.com/opensearch-project/opensearch-go/pull/609)) +- Fix ISM Error Notification types ([#612](https://github.com/opensearch-project/opensearch-go/pull/612)) ### Security diff --git a/plugins/ism/api_policies.go b/plugins/ism/api_policies.go index 0b7787dd..1508b6d7 100644 --- a/plugins/ism/api_policies.go +++ b/plugins/ism/api_policies.go @@ -80,11 +80,16 @@ type PolicyBody struct { // PolicyErrorNotification is a sub type of PolicyBody containing information about error notification type PolicyErrorNotification struct { - Channel string `json:"channel,omitempty"` - Destination NotificationDestination `json:"destination,omitempty"` + Channel *NotificationChannel `json:"channel,omitempty"` + Destination *NotificationDestination `json:"destination,omitempty"` MessageTemplate NotificationMessageTemplate `json:"message_template"` } +// NotificationChannel is a sub type of PolicyErrorNotification containg the channel id +type NotificationChannel struct { + ID string `json:"id"` +} + // NotificationDestination is a sub type of PolicyErrorNotification containing information about notification destinations type NotificationDestination struct { Chime *NotificationDestinationURL `json:"chime,omitempty"` diff --git a/plugins/ism/api_policies_test.go b/plugins/ism/api_policies_test.go index 2de7ffb2..7caf25d0 100644 --- a/plugins/ism/api_policies_test.go +++ b/plugins/ism/api_policies_test.go @@ -27,6 +27,8 @@ func TestPoliciesClient(t *testing.T) { failingClient, err := osismtest.CreateFailingClient() require.Nil(t, err) + t.Cleanup(func() { client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: "test2"}) }) + var putResp ism.PoliciesPutResp type policiesTests struct { @@ -52,7 +54,7 @@ func TestPoliciesClient(t *testing.T) { Policy: ism.PolicyBody{ Description: "test", ErrorNotification: &ism.PolicyErrorNotification{ - Destination: ism.NotificationDestination{ + Destination: &ism.NotificationDestination{ CustomWebhook: &ism.NotificationDestinationCustomWebhook{ Host: "exmaple.com", Scheme: "https", @@ -113,7 +115,7 @@ func TestPoliciesClient(t *testing.T) { }, Template: []ism.Template{ ism.Template{ - IndexPatterns: []string{"*test*"}, + IndexPatterns: []string{"test"}, Priority: 20, }, }, @@ -124,6 +126,47 @@ func TestPoliciesClient(t *testing.T) { return putResp, err }, }, + { + Name: "Create with Channel", + Results: func() (osismtest.Response, error) { + return client.Policies.Put( + nil, + ism.PoliciesPutReq{ + Policy: "test2", + Body: ism.PoliciesPutBody{ + Policy: ism.PolicyBody{ + Description: "test", + ErrorNotification: &ism.PolicyErrorNotification{ + Channel: &ism.NotificationChannel{ + ID: "test", + }, + MessageTemplate: ism.NotificationMessageTemplate{ + Source: "The index {{ctx.index}} failed during policy execution.", + }, + }, + DefaultState: "delete", + States: []ism.PolicyState{ + ism.PolicyState{ + Name: "delete", + Actions: []ism.PolicyStateAction{ + ism.PolicyStateAction{ + Delete: &ism.PolicyStateDelete{}, + }, + }, + }, + }, + Template: []ism.Template{ + ism.Template{ + IndexPatterns: []string{"test2"}, + Priority: 21, + }, + }, + }, + }, + }, + ) + }, + }, { Name: "Update", Results: func() (osismtest.Response, error) { diff --git a/plugins/ism/api_test.go b/plugins/ism/api_test.go index 6c366a1c..3aa38915 100644 --- a/plugins/ism/api_test.go +++ b/plugins/ism/api_test.go @@ -45,7 +45,7 @@ func TestClient(t *testing.T) { Policy: ism.PolicyBody{ Description: "test", ErrorNotification: &ism.PolicyErrorNotification{ - Destination: ism.NotificationDestination{ + Destination: &ism.NotificationDestination{ CustomWebhook: &ism.NotificationDestinationCustomWebhook{ Host: "exmaple.com", Scheme: "https", @@ -76,7 +76,7 @@ func TestClient(t *testing.T) { Template: []ism.Template{ ism.Template{ IndexPatterns: []string{"test"}, - Priority: 20, + Priority: 22, }, }, },