Skip to content
Merged
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
54 changes: 54 additions & 0 deletions lib/elastomer_client/client/ccr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(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
42 changes: 42 additions & 0 deletions test/client/ccr_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Loading