From cdf764bbfc0ae211480e55ef6f80aee85f8122fe Mon Sep 17 00:00:00 2001 From: Jakob Hahn Date: Wed, 11 Sep 2024 16:16:14 +0200 Subject: [PATCH] plugins/ism: add alias action Signed-off-by: Jakob Hahn --- CHANGELOG.md | 1 + plugins/ism/api_policies.go | 19 +++++++++- plugins/ism/api_policies_test.go | 65 +++++++++++++++++++++++++++----- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abb427d8..82b45a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ## [Unreleased] ### Added +- Adds ISM Alias action ([#614](https://github.com/opensearch-project/opensearch-go/pull/614)) ### Changed diff --git a/plugins/ism/api_policies.go b/plugins/ism/api_policies.go index 74295340..6380d097 100644 --- a/plugins/ism/api_policies.go +++ b/plugins/ism/api_policies.go @@ -72,7 +72,7 @@ type PolicyBody struct { Description string `json:"description,omitempty"` LastUpdatedTime int64 `json:"last_updated_time,omitempty"` SchemaVersion int `json:"schema_version,omitempty"` - ErrorNotification *PolicyErrorNotification `json:"error_notification,omitempty"` + ErrorNotification *PolicyErrorNotification `json:"error_notification"` DefaultState string `json:"default_state"` States []PolicyState `json:"states"` Template []Template `json:"ism_template,omitempty"` @@ -146,6 +146,7 @@ type PolicyStateAction struct { IndexPriority *PolicyStateIndexPriority `json:"index_priority,omitempty"` Allocation *PolicyStateAllocation `json:"allocation,omitempty"` Rollup *PolicyStateRollup `json:"rollup,omitempty"` + Alias *PolicyStateAlias `json:"alias,omitempty"` } // Template is a sub type of PolicyBody containing information about the ims template @@ -271,3 +272,19 @@ type PolicyStateTransition struct { StateName string `json:"state_name"` Conditions *PolicyStateTransitionCondition `json:"conditions,omitempty"` } + +// PolicyStateAlias represents the Alias action +type PolicyStateAlias struct { + Actions []PolicyStateAliasAction `json:"actions"` +} + +// PolicyStateAliasAction is a sub type of PolicyStateAlias defining to add or remove aliases +type PolicyStateAliasAction struct { + Add *PolicyStateAliasName `json:"add,omitempty"` + Remove *PolicyStateAliasName `json:"remove,omitempty"` +} + +// PolicyStateAliasName is a sub type of PolicyStateAliasAction defining the list of aliases +type PolicyStateAliasName struct { + Aliases []string `json:"aliases"` +} diff --git a/plugins/ism/api_policies_test.go b/plugins/ism/api_policies_test.go index 02724945..34171a01 100644 --- a/plugins/ism/api_policies_test.go +++ b/plugins/ism/api_policies_test.go @@ -30,9 +30,13 @@ func TestPoliciesClient(t *testing.T) { failingClient, err := osismtest.CreateFailingClient() require.Nil(t, err) - testPolicy1 := "test" - testPolicy2 := "test2" - t.Cleanup(func() { client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicy2}) }) + testPolicy := "test" + testPolicyChannel := "test_channel" + testPolicyAlias := "test_alias" + t.Cleanup(func() { + client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicyChannel}) + client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicyAlias}) + }) var putResp ism.PoliciesPutResp @@ -54,7 +58,7 @@ func TestPoliciesClient(t *testing.T) { putResp, err = client.Policies.Put( nil, ism.PoliciesPutReq{ - Policy: testPolicy1, + Policy: testPolicy, Body: ism.PoliciesPutBody{ Policy: ism.PolicyBody{ Description: "test", @@ -138,7 +142,7 @@ func TestPoliciesClient(t *testing.T) { return client.Policies.Put( nil, ism.PoliciesPutReq{ - Policy: testPolicy2, + Policy: testPolicyChannel, Body: ism.PoliciesPutBody{ Policy: ism.PolicyBody{ Description: "test", @@ -163,7 +167,50 @@ func TestPoliciesClient(t *testing.T) { }, Template: []ism.Template{ ism.Template{ - IndexPatterns: []string{"test2"}, + IndexPatterns: []string{testPolicyChannel}, + Priority: 21, + }, + }, + }, + }, + }, + ) + }, + }, + { + Name: "Create with Alias", + 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: testPolicyAlias, + Body: ism.PoliciesPutBody{ + Policy: ism.PolicyBody{ + Description: "test", + DefaultState: "alias", + States: []ism.PolicyState{ + ism.PolicyState{ + Name: "alias", + Actions: []ism.PolicyStateAction{ + ism.PolicyStateAction{ + Alias: &ism.PolicyStateAlias{ + Actions: []ism.PolicyStateAliasAction{ + ism.PolicyStateAliasAction{ + Add: &ism.PolicyStateAliasName{Aliases: []string{"alias-test"}}, + }, + ism.PolicyStateAliasAction{ + Remove: &ism.PolicyStateAliasName{Aliases: []string{"alias-test"}}, + }, + }, + }, + }, + }, + }, + }, + Template: []ism.Template{ + ism.Template{ + IndexPatterns: []string{testPolicyAlias}, Priority: 21, }, }, @@ -181,7 +228,7 @@ func TestPoliciesClient(t *testing.T) { return client.Policies.Put( nil, ism.PoliciesPutReq{ - Policy: testPolicy1, + Policy: testPolicy, Params: ism.PoliciesPutParams{IfSeqNo: opensearch.ToPointer(putResp.SeqNo), IfPrimaryTerm: opensearch.ToPointer(putResp.PrimaryTerm)}, Body: ism.PoliciesPutBody{ Policy: putResp.Policy.Policy, @@ -210,7 +257,7 @@ func TestPoliciesClient(t *testing.T) { { Name: "with request", Results: func(t *testing.T) (osismtest.Response, error) { - return client.Policies.Get(nil, &ism.PoliciesGetReq{Policy: testPolicy1}) + return client.Policies.Get(nil, &ism.PoliciesGetReq{Policy: testPolicy}) }, }, { @@ -227,7 +274,7 @@ func TestPoliciesClient(t *testing.T) { { Name: "with request", Results: func(t *testing.T) (osismtest.Response, error) { - return client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicy1}) + return client.Policies.Delete(nil, ism.PoliciesDeleteReq{Policy: testPolicy}) }, }, {