Skip to content

Commit 283b68a

Browse files
authored
Merge pull request #324 from github/sarwaan001/pause_follow_endpoint
Pause Follow and Unfollow endpoints
2 parents 76af119 + f4727d1 commit 283b68a

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

lib/elastomer_client/client/ccr.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,34 @@ def get_auto_follow(params = {})
9090
response = client.get "/_ccr/auto_follow{/pattern_name}", params.merge(action: "get_auto_follow_pattern", rest_api: "ccr")
9191
response.body
9292
end
93+
94+
# Pauses a follower index.
95+
#
96+
# follower_index - String name of the follower index to pause
97+
# params - Hash of the request body
98+
#
99+
# Examples
100+
# ccr.pause_follow("follower_index")
101+
#
102+
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-pause-follow.html
103+
def pause_follow(follower_index, params = {})
104+
response = client.post "/#{follower_index}/_ccr/pause_follow", params.merge(action: "pause_follow", rest_api: "ccr")
105+
response.body
106+
end
107+
108+
# Unfollows a leader index given a follower index.
109+
# The follower index must be paused and closed before unfollowing.
110+
#
111+
# follower_index - String name of the follower index to unfollow
112+
# params - Hash of the request body
113+
#
114+
# Examples
115+
# ccr.unfollow("follower_index")
116+
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-unfollow.html
117+
def unfollow(follower_index, params = {})
118+
response = client.post "/#{follower_index}/_ccr/unfollow", params.merge(action: "unfollow", rest_api: "ccr")
119+
response.body
120+
end
93121
end
94122
end
95123
end

test/client/ccr_test.rb

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_relative "../test_helper"
44

55
describe ElastomerClient::Client::Ccr do
6-
before do
6+
before :each do
77
skip "Cannot test Ccr API without a replica cluster" unless $replica_client.available?
88

99
@leader_index = $client.index("leader_index")
@@ -27,17 +27,14 @@
2727
@leader_index.create(default_index_settings)
2828
wait_for_index(@leader_index.name, "green")
2929

30-
@leader_index.docs.index(document_wrapper("book", { _id: 1, title: "Book 1" }))
31-
@leader_index.refresh
32-
3330
begin
3431
ccr.delete_auto_follow("follower_pattern")
3532
rescue StandardError
3633
puts "No auto-follow pattern to delete"
3734
end
3835
end
3936

40-
after do
37+
after :each do
4138
@leader_index.delete if @leader_index.exists?
4239
@follower_index.delete if @follower_index.exists?
4340
@auto_followed_index.delete if @auto_followed_index.exists?
@@ -50,16 +47,79 @@
5047
end
5148
end
5249

53-
it "successfully follows a leader index" do
50+
def follow_index(follower_index_name, leader_index_name)
5451
ccr = $replica_client.ccr
52+
response = ccr.follow(follower_index_name, { leader_index: leader_index_name, remote_cluster: "leader" })
53+
wait_for_index(follower_index_name, "green")
54+
response
55+
end
56+
57+
def pause_follow(follower_index_name)
58+
ccr = $replica_client.ccr
59+
response = ccr.pause_follow(follower_index_name)
60+
wait_for_index(follower_index_name, "green")
61+
response
62+
end
63+
64+
def unfollow_index(follower_index_name)
65+
ccr = $replica_client.ccr
66+
response = ccr.unfollow(follower_index_name)
67+
wait_for_index(follower_index_name, "green")
68+
response
69+
end
70+
71+
def create_document(index, type, document)
72+
response = index.docs.index(document_wrapper(type, document))
73+
index.refresh
74+
response
75+
end
76+
77+
it "successfully follows a leader index" do
78+
create_document(@leader_index, "book", { _id: 1, title: "Book 1" })
79+
80+
follow_index(@follower_index, @leader_index)
5581

56-
ccr.follow(@follower_index.name, { leader_index: @leader_index.name, remote_cluster: "leader" })
57-
wait_for_index(@follower_index.name, "green")
5882
doc = @follower_index.docs.get(id: 1, type: "book")
5983

6084
assert_equal "Book 1", doc["_source"]["title"]
6185
end
6286

87+
it "successfully pauses a follower index" do
88+
follow_index(@follower_index, @leader_index)
89+
90+
response = pause_follow(@follower_index)
91+
92+
assert response["acknowledged"]
93+
94+
create_document(@leader_index, "book", { _id: 2, title: "Book 2" })
95+
96+
doc = @follower_index.docs.get(id: 2, type: "book")
97+
98+
refute doc["found"]
99+
end
100+
101+
it "successfully unfollow a leader index" do
102+
follow_index(@follower_index, @leader_index)
103+
104+
pause_follow(@follower_index)
105+
106+
@follower_index.close
107+
108+
response = unfollow_index(@follower_index)
109+
110+
assert response["acknowledged"]
111+
112+
@follower_index.open
113+
114+
wait_for_index(@follower_index.name, "green")
115+
116+
create_document(@leader_index, "book", { _id: 2, title: "Book 2" })
117+
118+
doc = @follower_index.docs.get(id: 2, type: "book")
119+
120+
refute doc["found"]
121+
end
122+
63123
it "successfully implements an auto-follow policy" do
64124
ccr = $replica_client.ccr
65125

0 commit comments

Comments
 (0)