From fb5423c5c9dc785d25ecf7f0d14db42792859714 Mon Sep 17 00:00:00 2001 From: John Hernley Date: Wed, 12 Feb 2025 22:25:29 +0000 Subject: [PATCH 1/3] add auto follow --- lib/elastomer_client/client/ccr.rb | 54 ++++++++++++++++++++++++++++++ test/client/ccr_test.rb | 42 +++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/lib/elastomer_client/client/ccr.rb b/lib/elastomer_client/client/ccr.rb index f42a005c..753c3c76 100644 --- a/lib/elastomer_client/client/ccr.rb +++ b/lib/elastomer_client/client/ccr.rb @@ -36,6 +36,60 @@ def follow(follower_index, body, params = {}) response = client.put "/#{follower_index}/_ccr/follow", params.merge(body:, action: "follow", rest_api: "ccr") response.body end + + # Creates a new auto-follow pattern for the provided remote cluster. + # + # pattern_name - String name of the auto-follow pattern to create + # body - Hash of the request body + # :remote_cluster - String name of the remote cluster. Required. + # :leader_index_patterns - An array of simple index patterns to match against indices in the remote cluster + # :leader_index_exclusion_patterns - An array of simple index patterns that can be used to exclude indices from being auto-followed. + # :follow_index_pattern - The name of follower index. The template {{leader_index}} can be used to derive + # the name of the follower index from the name of the leader index. + # params - Hash of query parameters + + # Examples + + # ccr.auto_follow("follower_pattern", { remote_cluster: "leader", leader_index_patterns: ["leader_index*"], + # follow_index_pattern: "{{leader_index}}-follower" }) + + # See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-put-auto-follow-pattern.html + + def auto_follow(pattern_name, body, params = {}) + response = client.put "/_ccr/auto_follow/#{pattern_name}", params.merge(params, body:, action: "create_auto_follow_pattern", rest_api: "ccr") + response.body + end + + # Deletes the auto-follow pattern for the provided remote cluster. + # + # pattern_name - String name of the auto-follow pattern to delete + # params - Hash of query parameters + # + # Examples + # + # ccr.delete_auto_follow("follower_pattern") + # + # See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-delete-auto-follow-pattern.html + + def delete_auto_follow(pattern_name, params = {}) + response = client.delete "/_ccr/auto_follow/#{pattern_name}", params.merge(action: "delete_auto_follow_pattern", rest_api: "ccr") + response.body + end + + # Gets cross-cluster replication auto-follow patterns + # + # params - Hash of query parameters + # :pattern_name - (Optional) String name of the auto-follow pattern. Returns all patterns if not specified + # Examples + # + # ccr.get_auto_follow + # + # See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-auto-follow-pattern.html + + def get_auto_follow(params = {}) + response = client.get "/_ccr/auto_follow{/pattern_name}", params.merge(action: "get_auto_follow_pattern", rest_api: "ccr") + response.body + end end end end diff --git a/test/client/ccr_test.rb b/test/client/ccr_test.rb index dbee88f1..f66cf6b9 100644 --- a/test/client/ccr_test.rb +++ b/test/client/ccr_test.rb @@ -8,22 +8,46 @@ @leader_index = $client.index("leader_index") @follower_index = $replica_client.index("follower_index") + @auto_followed_index = $client.index("followed_index") + @auto_follower_index = $replica_client.index("followed_index-follower") + if @leader_index.exists? @leader_index.delete end + if @auto_followed_index.exists? + @auto_followed_index.delete + end if @follower_index.exists? @follower_index.delete end + if @auto_follower_index.exists? + @auto_follower_index.delete + end + @leader_index.create(default_index_settings) wait_for_index(@leader_index.name, "green") @leader_index.docs.index(document_wrapper("book", { _id: 1, title: "Book 1" })) @leader_index.refresh + + begin + ccr.delete_auto_follow("follower_pattern") + rescue StandardError + puts "No auto-follow pattern to delete" + end end after do @leader_index.delete if @leader_index.exists? @follower_index.delete if @follower_index.exists? + @auto_followed_index.delete if @auto_followed_index.exists? + @auto_follower_index.delete if @auto_follower_index.exists? + + begin + ccr.delete_auto_follow("follower_pattern") + rescue StandardError + puts "No auto-follow pattern to delete" + end end it "successfully follows a leader index" do @@ -36,4 +60,22 @@ assert_equal "Book 1", doc["_source"]["title"] end + it "successfully implements an auto-follow policy" do + ccr = $replica_client.ccr + + ccr.auto_follow("follower_pattern", { remote_cluster: "leader", leader_index_patterns: ["*"], follow_index_pattern: "{{leader_index}}-follower" }) + + @auto_followed_index.create(default_index_settings) + wait_for_index(@auto_followed_index.name, "green") + + @auto_follower_index = $replica_client.index("followed_index-follower") + wait_for_index(@auto_follower_index.name, "green") + + resp = ccr.get_auto_follow(pattern_name: "follower_pattern") + + assert_equal "follower_pattern", resp["patterns"].first()["name"] + + assert_predicate @auto_follower_index, :exists? + end + end From 09cf672f8311052cfe57bcf21bcb849f71b6b40a Mon Sep 17 00:00:00 2001 From: John Hernley Date: Thu, 13 Feb 2025 11:55:18 -0500 Subject: [PATCH 2/3] Update lib/elastomer_client/client/ccr.rb Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lib/elastomer_client/client/ccr.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elastomer_client/client/ccr.rb b/lib/elastomer_client/client/ccr.rb index 753c3c76..0787d980 100644 --- a/lib/elastomer_client/client/ccr.rb +++ b/lib/elastomer_client/client/ccr.rb @@ -56,7 +56,7 @@ def follow(follower_index, body, params = {}) # See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-put-auto-follow-pattern.html def auto_follow(pattern_name, body, params = {}) - response = client.put "/_ccr/auto_follow/#{pattern_name}", params.merge(params, body:, action: "create_auto_follow_pattern", rest_api: "ccr") + response = client.put "/_ccr/auto_follow/#{pattern_name}", params.merge(body: body, action: "create_auto_follow_pattern", rest_api: "ccr") response.body end From 1a4dcd8701e1c1f4eeeb9915e0d66a6fa1774941 Mon Sep 17 00:00:00 2001 From: John Hernley Date: Thu, 13 Feb 2025 17:02:30 +0000 Subject: [PATCH 3/3] linting --- lib/elastomer_client/client/ccr.rb | 2 +- test/client/ccr_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/elastomer_client/client/ccr.rb b/lib/elastomer_client/client/ccr.rb index 0787d980..050bf6f5 100644 --- a/lib/elastomer_client/client/ccr.rb +++ b/lib/elastomer_client/client/ccr.rb @@ -56,7 +56,7 @@ def follow(follower_index, body, params = {}) # See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-put-auto-follow-pattern.html def auto_follow(pattern_name, body, params = {}) - response = client.put "/_ccr/auto_follow/#{pattern_name}", params.merge(body: body, action: "create_auto_follow_pattern", rest_api: "ccr") + response = client.put "/_ccr/auto_follow/#{pattern_name}", params.merge(body:, action: "create_auto_follow_pattern", rest_api: "ccr") response.body end diff --git a/test/client/ccr_test.rb b/test/client/ccr_test.rb index f66cf6b9..9c0642e6 100644 --- a/test/client/ccr_test.rb +++ b/test/client/ccr_test.rb @@ -73,7 +73,7 @@ resp = ccr.get_auto_follow(pattern_name: "follower_pattern") - assert_equal "follower_pattern", resp["patterns"].first()["name"] + assert_equal "follower_pattern", resp["patterns"].first["name"] assert_predicate @auto_follower_index, :exists? end