Class: Etcd::Client
- Inherits:
-
Object
- Object
- Etcd::Client
- Defined in:
- lib/etcd/client.rb
Overview
This is the central ruby class for Etcd. It provides methods for all Etcd api calls. It also provides few additional methods beyond the core Etcd api, like Etcd::Client#lock and Etcd::Client#eternal_watch, they are defined in separate modules and included in this class
Instance Attribute Summary collapse
-
#allow_redirect ⇒ Object
readonly
Returns the value of attribute allow_redirect.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#api_execute(path, method, params = nil) ⇒ Object
This method sends api request to etcd server.
-
#delete(key) ⇒ Object
Deletes a key along with all associated data.
-
#get(key) ⇒ Object
Retrives a key with its associated data, if key is not present it will return with message “Key Not Found”.
-
#initialize(opts = {}) ⇒ Client
constructor
Creates a new instance of Etcd::Client.
-
#key_endpoint ⇒ Object
Lists all the data (keys, dir etc) present in etcd store.
-
#leader ⇒ Object
Get the current leader in a cluster.
-
#machines ⇒ Object
Lists all machines in the cluster.
-
#set(key, value, ttl = nil) ⇒ Object
Adds a new key with specified value and ttl, overwrites old values if exists.
-
#test_and_set(key, value, prevValue, ttl = nil) ⇒ Object
Set a new value for key if previous value of key is matched.
-
#version_prefix ⇒ Object
Currently use ‘v1’ as version for etcd store.
-
#watch(key, index = nil) ⇒ Object
Gives a notification when specified key changes.
-
#watch_endpoint ⇒ Object
Watches all keys and notifies if anyone changes.
Methods included from Lockable
Methods included from Helpers
Constructor Details
#initialize(opts = {}) ⇒ Client
Creates a new instance of Etcd::Client. It accepts a hash opts as argument
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/etcd/client.rb', line 29 def initialize(opts={}) @host = opts[:host] || '127.0.0.1' @port = opts[:port] || 4001 @read_timeout = opts[:read_timeout] || 60 if opts.has_key?(:allow_redirect) @allow_redirect = opts[:allow_redirect] else @allow_redirect = true end end |
Instance Attribute Details
#allow_redirect ⇒ Object (readonly)
Returns the value of attribute allow_redirect.
20 21 22 |
# File 'lib/etcd/client.rb', line 20 def allow_redirect @allow_redirect end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
20 21 22 |
# File 'lib/etcd/client.rb', line 20 def host @host end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
20 21 22 |
# File 'lib/etcd/client.rb', line 20 def http @http end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
20 21 22 |
# File 'lib/etcd/client.rb', line 20 def port @port end |
Instance Method Details
#api_execute(path, method, params = nil) ⇒ Object
This method sends api request to etcd server.
This method has following parameters as argument
-
path - etcd server path (etcd server end point)
-
method - the request method used
-
params - any additional parameters used by request method (optional)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/etcd/client.rb', line 132 def api_execute(path, method, params=nil) http = if path=~/^http/ uri = URI.parse(path) path = uri.path Net::HTTP.new(uri.host, uri.port) else Net::HTTP.new(host, port) end http.read_timeout = @read_timeout case method when :get unless params.nil? encoded_params = URI.encode_www_form(params) path+= "?" + encoded_params end req = Net::HTTP::Get.new(path) when :post encoded_params = URI.encode_www_form(params) req = Net::HTTP::Post.new(path) req.body= encoded_params Log.debug("Setting body for post '#{encoded_params}'") when :delete unless params.nil? encoded_params = URI.encode_www_form(params) path+= "?" + encoded_params end req = Net::HTTP::Delete.new(path) end Log.debug("Invoking: '#{req.class}' against '#{path}") res = http.request(req) Log.debug("Response code: #{res.code}") if res.is_a?(Net::HTTPSuccess) Log.debug("Http success") res.body elsif redirect?(res.code.to_i) and allow_redirect Log.debug("Http redirect, following") api_execute(res['location'], method, params) else Log.debug("Http error") Log.debug(res.body) res.error! end end |
#delete(key) ⇒ Object
Deletes a key along with all associated data
This method has following parameters as argument
-
key - key to be deleted
98 99 100 101 |
# File 'lib/etcd/client.rb', line 98 def delete(key) response = api_execute(key_endpoint + key, :delete) json2obj(response) end |
#get(key) ⇒ Object
Retrives a key with its associated data, if key is not present it will return with message “Key Not Found”
This method has following parameters as argument
-
key - whose data to be retrive
107 108 109 110 |
# File 'lib/etcd/client.rb', line 107 def get(key) response = api_execute(key_endpoint + key, :get) json2obj(response) end |
#key_endpoint ⇒ Object
Lists all the data (keys, dir etc) present in etcd store
56 57 58 |
# File 'lib/etcd/client.rb', line 56 def key_endpoint version_prefix + '/keys' end |
#leader ⇒ Object
Get the current leader in a cluster
51 52 53 |
# File 'lib/etcd/client.rb', line 51 def leader api_execute( version_prefix + '/leader', :get) end |
#machines ⇒ Object
Lists all machines in the cluster
46 47 48 |
# File 'lib/etcd/client.rb', line 46 def machines api_execute( version_prefix + '/machines', :get).split(",").map(&:strip) end |
#set(key, value, ttl = nil) ⇒ Object
Adds a new key with specified value and ttl, overwrites old values if exists
This method has following parameters as argument
-
key - whose value to be set
-
value - value to be set for specified key
-
ttl - shelf life of a key (in secsonds) (optional)
86 87 88 89 90 91 92 |
# File 'lib/etcd/client.rb', line 86 def set(key, value, ttl=nil) path = key_endpoint + key payload = {'value' => value} payload['ttl'] = ttl unless ttl.nil? response = api_execute(path, :post, payload) json2obj(response) end |
#test_and_set(key, value, prevValue, ttl = nil) ⇒ Object
Set a new value for key if previous value of key is matched
This method takes following parameters as argument
-
key - whose value is going to change if previous value is matched
-
value - new value to be set for specified key
-
prevValue - value of a key to compare with existing value of key
-
ttl - shelf life of a key (in secsonds) (optional)
72 73 74 75 76 77 78 |
# File 'lib/etcd/client.rb', line 72 def test_and_set(key, value, prevValue, ttl = nil) path = key_endpoint + key payload = {'value' => value, 'prevValue' => prevValue } payload['ttl'] = ttl unless ttl.nil? response = api_execute(path, :post, payload) json2obj(response) end |
#version_prefix ⇒ Object
Currently use ‘v1’ as version for etcd store
41 42 43 |
# File 'lib/etcd/client.rb', line 41 def version_prefix '/v1' end |
#watch(key, index = nil) ⇒ Object
Gives a notification when specified key changes
This method has following parameters as argument
-
key - key to be watched
-
index - etcd server index of specified key (optional)
117 118 119 120 121 122 123 124 |
# File 'lib/etcd/client.rb', line 117 def watch(key, index=nil) response = if index.nil? api_execute(watch_endpoint + key, :get) else api_execute(watch_endpoint + key, :post, {'index' => index}) end json2obj(response) end |
#watch_endpoint ⇒ Object
Watches all keys and notifies if anyone changes
61 62 63 |
# File 'lib/etcd/client.rb', line 61 def watch_endpoint version_prefix + '/watch' end |