From 9a3750955e4233c21a223cf26c462c776d378ddc Mon Sep 17 00:00:00 2001 From: Hakan Uyumaz Date: Mon, 21 Nov 2022 15:39:06 +0300 Subject: [PATCH 1/2] Removing ILM Policy from Index is added --- es.go | 12 ++++++++++++ es_test.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/es.go b/es.go index eb7a51c..ab389dc 100644 --- a/es.go +++ b/es.go @@ -1673,3 +1673,15 @@ func (c *Client) AllocateStalePrimaryShard(node, index string, shard int) error return nil } + +// RemoveIndexILMPolicy removes the ILM policy from the index +func (c *Client) RemoveIndexILMPolicy(index string) error { + agent := c.buildPostRequest(fmt.Sprintf("%s/_ilm/remove", index)) + + _, err := handleErrWithBytes(agent) + if err != nil { + return err + } + + return nil +} diff --git a/es_test.go b/es_test.go index 4d7bbde..03a70ad 100644 --- a/es_test.go +++ b/es_test.go @@ -2212,3 +2212,19 @@ func TestAllocateStalePrimaryShard(t *testing.T) { t.Fatalf("Unexpected error. expected nil, got %s", err) } } + +func TestRemoveIndexILMPolicy(t *testing.T) { + testSetup := &ServerSetup{ + Method: "POST", + Path: "/test-index/_ilm/remove", + } + + host, port, ts := setupTestServers(t, []*ServerSetup{testSetup}) + defer ts.Close() + client := NewClient(host, port) + + err := client.RemoveIndexILMPolicy("test-index") + if err != nil { + t.Fatalf("Unexpected error. expected nil, got %s", err) + } +} From b0aeb1f7efa9b6a290f50d27dee6abea33dc5207 Mon Sep 17 00:00:00 2001 From: Hakan Uyumaz Date: Wed, 23 Nov 2022 20:53:16 +0300 Subject: [PATCH 2/2] ILM history indices are removed during policy removal --- es.go | 24 ++++++++++++++++++++++++ es_test.go | 9 +++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/es.go b/es.go index ab389dc..dd3d495 100644 --- a/es.go +++ b/es.go @@ -710,6 +710,18 @@ func (c *Client) GetIndices(index string) ([]Index, error) { return indices, nil } +// Get a subset of indices including hidden ones +func (c *Client) GetHiddenIndices(index string) ([]Index, error) { + var indices []Index + err := handleErrWithStruct(c.buildGetRequest(fmt.Sprintf("_cat/indices/%s?h=health,status,index,pri,rep,store.size,docs.count&expand_wildcards=open,closed,hidden", index)), &indices) + + if err != nil { + return nil, err + } + + return indices, nil +} + // Get all the aliases in the cluster. // // Use case: You want to see some basic info on all the aliases of the cluster @@ -1683,5 +1695,17 @@ func (c *Client) RemoveIndexILMPolicy(index string) error { return err } + ilmHistoryIndices, err := c.GetHiddenIndices(fmt.Sprintf("%s*.ds-ilm-history-*", index)) + if err != nil { + return err + } + + for _, ilmHistoryIndex := range ilmHistoryIndices { + err = c.DeleteIndex(ilmHistoryIndex.Name) + if err != nil { + return err + } + } + return nil } diff --git a/es_test.go b/es_test.go index 03a70ad..4b4c96d 100644 --- a/es_test.go +++ b/es_test.go @@ -2214,12 +2214,17 @@ func TestAllocateStalePrimaryShard(t *testing.T) { } func TestRemoveIndexILMPolicy(t *testing.T) { - testSetup := &ServerSetup{ + ilmRemoveTestSetup := &ServerSetup{ Method: "POST", Path: "/test-index/_ilm/remove", } + getIndicesTestSetup := &ServerSetup{ + Method: "GET", + Path: "/_cat/indices/test-index*.ds-ilm-history-*", + Response: "[]", + } - host, port, ts := setupTestServers(t, []*ServerSetup{testSetup}) + host, port, ts := setupTestServers(t, []*ServerSetup{ilmRemoveTestSetup, getIndicesTestSetup}) defer ts.Close() client := NewClient(host, port)