Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause Follow and Unfollow endpoints #324

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/elastomer_client/client/ccr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ def follow(follower_index, body, params = {})
response = client.put "/#{follower_index}/_ccr/follow", params.merge(body:, action: "follow", rest_api: "ccr")
response.body
end

# Pauses a follower index.
#
# follower_index - String name of the follower index pause
# params - Hash of the request body
#
# Examples
# ccr.pause_follow("follower_index")
#
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-pause-follow.html
def pause_follow(follower_index, params = {})
response = client.post "/#{follower_index}/_ccr/pause_follow", params.merge(action: "pause_follow", rest_api: "ccr")
response.body
end

# Unfollows a leader index given a follower index.
# The follower index must be paused and closed before unfollowing.
#
# follower_index - String name of the follower index to create
Copy link
Preview

Copilot AI Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment inaccurately states 'to create'; consider revising it to 'follower_index - String name of the follower index to unfollow'.

Suggested change
# follower_index - String name of the follower index to create
# follower_index - String name of the follower index to unfollow

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
# params - Hash of the request body
#
# Examples
# ccr.unfollow("follower_index")
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/ccr-post-unfollow.html
def unfollow(follower_index, params = {})
response = client.post "/#{follower_index}/_ccr/unfollow", params.merge(action: "unfollow", rest_api: "ccr")
response.body
end
end
end
end
76 changes: 68 additions & 8 deletions test/client/ccr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "../test_helper"

describe ElastomerClient::Client::Ccr do
before do
before :each do
skip "Cannot test Ccr API without a replica cluster" unless $replica_client.available?

@leader_index = $client.index("leader_index")
Expand All @@ -16,24 +16,84 @@
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
end

after do
after :each do
@leader_index.delete if @leader_index.exists?
@follower_index.delete if @follower_index.exists?
end

it "successfully follows a leader index" do
def follow_index(follower_index, leader_index)
ccr = $replica_client.ccr
response = ccr.follow(follower_index.name, { leader_index: leader_index.name, remote_cluster: "leader" })
wait_for_index(follower_index.name, "green")
response
end

def pause_follow(follower_index)
ccr = $replica_client.ccr
response = ccr.pause_follow(follower_index.name)
wait_for_index(follower_index.name, "green")
response
end

def unfollow_index(follower_index)
ccr = $replica_client.ccr
response = ccr.unfollow(follower_index.name)
wait_for_index(follower_index.name, "green")
response
end

def create_document(index, type, document)
response = index.docs.index(document_wrapper(type, document))
index.refresh
response
end

it "successfully follows a leader index" do
create_document(@leader_index, "book", { _id: 1, title: "Book 1" })

follow_index(@follower_index, @leader_index)

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

assert_equal "Book 1", doc["_source"]["title"]
end

it "should successfully pauses a follower index" do
follow_index(@follower_index, @leader_index)

response = pause_follow(@follower_index)

assert response["acknowledged"]

create_document(@leader_index, "book", { _id: 2, title: "Book 2" })

doc = @follower_index.docs.get(id: 2, type: "book")

refute doc["found"]
end

it "should successfully unfollows a leader index" do
follow_index(@follower_index, @leader_index)

pause_follow(@follower_index)

@follower_index.close

response = unfollow_index(@follower_index)

assert response["acknowledged"]

@follower_index.open

wait_for_index(@follower_index.name, "green")

create_document(@leader_index, "book", { _id: 2, title: "Book 2" })

doc = @follower_index.docs.get(id: 2, type: "book")

refute doc["found"]
end

end