Skip to content

Commit

Permalink
plugins/ism: add alias action (#615)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD authored Sep 11, 2024
1 parent 64630c6 commit bd5d233
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased]

### Added
- Adds ISM Alias action ([#615](https://github.com/opensearch-project/opensearch-go/pull/615))

### Changed

Expand Down
19 changes: 18 additions & 1 deletion plugins/ism/api_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"`
}
65 changes: 56 additions & 9 deletions plugins/ism/api_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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, 4, "policy with alias action")
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,
},
},
Expand All @@ -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,
Expand Down Expand Up @@ -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})
},
},
{
Expand All @@ -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})
},
},
{
Expand Down

0 comments on commit bd5d233

Please sign in to comment.