From d8b583bcb483c75cc74370561cacf9befda45c54 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 | 81 +++++++++++++++++++++++++------- plugins/ism/api_test.go | 4 +- 4 files changed, 75 insertions(+), 20 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..02724945 100644 --- a/plugins/ism/api_policies_test.go +++ b/plugins/ism/api_policies_test.go @@ -24,14 +24,21 @@ func TestPoliciesClient(t *testing.T) { client, err := osismtest.NewClient() require.Nil(t, err) + osClient, err := ostest.NewClient() + require.Nil(t, err) + failingClient, err := osismtest.CreateFailingClient() require.Nil(t, err) + testPolicy1 := "test" + testPolicy2 := "test2" + t.Cleanup(func() { client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicy2}) }) + var putResp ism.PoliciesPutResp type policiesTests struct { Name string - Results func() (osismtest.Response, error) + Results func(*testing.T) (osismtest.Response, error) } testCases := []struct { @@ -43,16 +50,16 @@ func TestPoliciesClient(t *testing.T) { Tests: []policiesTests{ { Name: "Create", - Results: func() (osismtest.Response, error) { + Results: func(t *testing.T) (osismtest.Response, error) { putResp, err = client.Policies.Put( nil, ism.PoliciesPutReq{ - Policy: "test", + Policy: testPolicy1, Body: ism.PoliciesPutBody{ Policy: ism.PolicyBody{ Description: "test", ErrorNotification: &ism.PolicyErrorNotification{ - Destination: ism.NotificationDestination{ + Destination: &ism.NotificationDestination{ CustomWebhook: &ism.NotificationDestinationCustomWebhook{ Host: "exmaple.com", Scheme: "https", @@ -113,7 +120,7 @@ func TestPoliciesClient(t *testing.T) { }, Template: []ism.Template{ ism.Template{ - IndexPatterns: []string{"*test*"}, + IndexPatterns: []string{"test"}, Priority: 20, }, }, @@ -124,15 +131,57 @@ func TestPoliciesClient(t *testing.T) { return putResp, err }, }, + { + Name: "Create with Channel", + Results: func(t *testing.T) (osismtest.Response, error) { + ostest.SkipIfBelowVersion(t, osClient, 2, 0, "policy with error notification channel") + return client.Policies.Put( + nil, + ism.PoliciesPutReq{ + Policy: testPolicy2, + 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) { + Results: func(t *testing.T) (osismtest.Response, error) { putResp.Policy.Policy.ErrorNotification.Destination.CustomWebhook = nil putResp.Policy.Policy.ErrorNotification.Destination.Slack = &ism.NotificationDestinationURL{URL: "https://example.com"} return client.Policies.Put( nil, ism.PoliciesPutReq{ - Policy: "test", + Policy: testPolicy1, Params: ism.PoliciesPutParams{IfSeqNo: opensearch.ToPointer(putResp.SeqNo), IfPrimaryTerm: opensearch.ToPointer(putResp.PrimaryTerm)}, Body: ism.PoliciesPutBody{ Policy: putResp.Policy.Policy, @@ -143,7 +192,7 @@ func TestPoliciesClient(t *testing.T) { }, { Name: "inspect", - Results: func() (osismtest.Response, error) { + Results: func(t *testing.T) (osismtest.Response, error) { return failingClient.Policies.Put(nil, ism.PoliciesPutReq{}) }, }, @@ -154,19 +203,19 @@ func TestPoliciesClient(t *testing.T) { Tests: []policiesTests{ { Name: "without request", - Results: func() (osismtest.Response, error) { + Results: func(t *testing.T) (osismtest.Response, error) { return client.Policies.Get(nil, nil) }, }, { Name: "with request", - Results: func() (osismtest.Response, error) { - return client.Policies.Get(nil, &ism.PoliciesGetReq{Policy: "test"}) + Results: func(t *testing.T) (osismtest.Response, error) { + return client.Policies.Get(nil, &ism.PoliciesGetReq{Policy: testPolicy1}) }, }, { Name: "inspect", - Results: func() (osismtest.Response, error) { + Results: func(t *testing.T) (osismtest.Response, error) { return failingClient.Policies.Get(nil, nil) }, }, @@ -177,13 +226,13 @@ func TestPoliciesClient(t *testing.T) { Tests: []policiesTests{ { Name: "with request", - Results: func() (osismtest.Response, error) { - return client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: "test"}) + Results: func(t *testing.T) (osismtest.Response, error) { + return client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicy1}) }, }, { Name: "inspect", - Results: func() (osismtest.Response, error) { + Results: func(t *testing.T) (osismtest.Response, error) { return failingClient.Policies.Delete(nil, ism.PoliciesDeleteReq{}) }, }, @@ -194,7 +243,7 @@ func TestPoliciesClient(t *testing.T) { t.Run(value.Name, func(t *testing.T) { for _, testCase := range value.Tests { t.Run(testCase.Name, func(t *testing.T) { - res, err := testCase.Results() + res, err := testCase.Results(t) if testCase.Name == "inspect" { assert.NotNil(t, err) assert.NotNil(t, res) 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, }, }, },