Skip to content

Commit

Permalink
plugins/ism: Fix error notification types
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <[email protected]>
  • Loading branch information
Jakob3xD committed Sep 11, 2024
1 parent 096d633 commit d8b583b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions plugins/ism/api_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
81 changes: 65 additions & 16 deletions plugins/ism/api_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand Down Expand Up @@ -113,7 +120,7 @@ func TestPoliciesClient(t *testing.T) {
},
Template: []ism.Template{
ism.Template{
IndexPatterns: []string{"*test*"},
IndexPatterns: []string{"test"},
Priority: 20,
},
},
Expand All @@ -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,
Expand All @@ -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{})
},
},
Expand All @@ -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)
},
},
Expand All @@ -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{})
},
},
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions plugins/ism/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestClient(t *testing.T) {
Template: []ism.Template{
ism.Template{
IndexPatterns: []string{"test"},
Priority: 20,
Priority: 22,
},
},
},
Expand Down

0 comments on commit d8b583b

Please sign in to comment.