Skip to content

Commit 76af119

Browse files
authored
Merge pull request #325 from github/jherns/auto-follow
Add auto Follow
2 parents 586f77e + 1a4dcd8 commit 76af119

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

lib/elastomer_client/client/ccr.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,60 @@ def follow(follower_index, body, params = {})
3636
response = client.put "/#{follower_index}/_ccr/follow", params.merge(body:, action: "follow", rest_api: "ccr")
3737
response.body
3838
end
39+
40+
# Creates a new auto-follow pattern for the provided remote cluster.
41+
#
42+
# pattern_name - String name of the auto-follow pattern to create
43+
# body - Hash of the request body
44+
# :remote_cluster - String name of the remote cluster. Required.
45+
# :leader_index_patterns - An array of simple index patterns to match against indices in the remote cluster
46+
# :leader_index_exclusion_patterns - An array of simple index patterns that can be used to exclude indices from being auto-followed.
47+
# :follow_index_pattern - The name of follower index. The template {{leader_index}} can be used to derive
48+
# the name of the follower index from the name of the leader index.
49+
# params - Hash of query parameters
50+
51+
# Examples
52+
53+
# ccr.auto_follow("follower_pattern", { remote_cluster: "leader", leader_index_patterns: ["leader_index*"],
54+
# follow_index_pattern: "{{leader_index}}-follower" })
55+
56+
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-put-auto-follow-pattern.html
57+
58+
def auto_follow(pattern_name, body, params = {})
59+
response = client.put "/_ccr/auto_follow/#{pattern_name}", params.merge(body:, action: "create_auto_follow_pattern", rest_api: "ccr")
60+
response.body
61+
end
62+
63+
# Deletes the auto-follow pattern for the provided remote cluster.
64+
#
65+
# pattern_name - String name of the auto-follow pattern to delete
66+
# params - Hash of query parameters
67+
#
68+
# Examples
69+
#
70+
# ccr.delete_auto_follow("follower_pattern")
71+
#
72+
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-delete-auto-follow-pattern.html
73+
74+
def delete_auto_follow(pattern_name, params = {})
75+
response = client.delete "/_ccr/auto_follow/#{pattern_name}", params.merge(action: "delete_auto_follow_pattern", rest_api: "ccr")
76+
response.body
77+
end
78+
79+
# Gets cross-cluster replication auto-follow patterns
80+
#
81+
# params - Hash of query parameters
82+
# :pattern_name - (Optional) String name of the auto-follow pattern. Returns all patterns if not specified
83+
# Examples
84+
#
85+
# ccr.get_auto_follow
86+
#
87+
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-get-auto-follow-pattern.html
88+
89+
def get_auto_follow(params = {})
90+
response = client.get "/_ccr/auto_follow{/pattern_name}", params.merge(action: "get_auto_follow_pattern", rest_api: "ccr")
91+
response.body
92+
end
3993
end
4094
end
4195
end

test/client/ccr_test.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,46 @@
88

99
@leader_index = $client.index("leader_index")
1010
@follower_index = $replica_client.index("follower_index")
11+
@auto_followed_index = $client.index("followed_index")
12+
@auto_follower_index = $replica_client.index("followed_index-follower")
13+
1114
if @leader_index.exists?
1215
@leader_index.delete
1316
end
17+
if @auto_followed_index.exists?
18+
@auto_followed_index.delete
19+
end
1420
if @follower_index.exists?
1521
@follower_index.delete
1622
end
23+
if @auto_follower_index.exists?
24+
@auto_follower_index.delete
25+
end
26+
1727
@leader_index.create(default_index_settings)
1828
wait_for_index(@leader_index.name, "green")
1929

2030
@leader_index.docs.index(document_wrapper("book", { _id: 1, title: "Book 1" }))
2131
@leader_index.refresh
32+
33+
begin
34+
ccr.delete_auto_follow("follower_pattern")
35+
rescue StandardError
36+
puts "No auto-follow pattern to delete"
37+
end
2238
end
2339

2440
after do
2541
@leader_index.delete if @leader_index.exists?
2642
@follower_index.delete if @follower_index.exists?
43+
@auto_followed_index.delete if @auto_followed_index.exists?
44+
@auto_follower_index.delete if @auto_follower_index.exists?
45+
46+
begin
47+
ccr.delete_auto_follow("follower_pattern")
48+
rescue StandardError
49+
puts "No auto-follow pattern to delete"
50+
end
2751
end
2852

2953
it "successfully follows a leader index" do
@@ -36,4 +60,22 @@
3660
assert_equal "Book 1", doc["_source"]["title"]
3761
end
3862

63+
it "successfully implements an auto-follow policy" do
64+
ccr = $replica_client.ccr
65+
66+
ccr.auto_follow("follower_pattern", { remote_cluster: "leader", leader_index_patterns: ["*"], follow_index_pattern: "{{leader_index}}-follower" })
67+
68+
@auto_followed_index.create(default_index_settings)
69+
wait_for_index(@auto_followed_index.name, "green")
70+
71+
@auto_follower_index = $replica_client.index("followed_index-follower")
72+
wait_for_index(@auto_follower_index.name, "green")
73+
74+
resp = ccr.get_auto_follow(pattern_name: "follower_pattern")
75+
76+
assert_equal "follower_pattern", resp["patterns"].first["name"]
77+
78+
assert_predicate @auto_follower_index, :exists?
79+
end
80+
3981
end

0 commit comments

Comments
 (0)