Class: Etcd::Client
- Inherits:
-
Object
- Object
- Etcd::Client
- Extended by:
- Forwardable
- 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
Defined Under Namespace
Classes: Config
Constant Summary collapse
- HTTP_REDIRECT =
->(r) { r.is_a? Net::HTTPRedirection }
- HTTP_SUCCESS =
->(r) { r.is_a? Net::HTTPSuccess }
- HTTP_CLIENT_ERROR =
->(r) { r.is_a? Net::HTTPClientError }
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#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, options = {}) ⇒ Object
This method sends api request to etcd server.
-
#build_http_request(klass, path, params = nil, body = nil) ⇒ Object
rubocop:enable MethodLength.
-
#initialize(opts = {}) {|@config| ... } ⇒ Client
constructor
Creates an Etcd::Client object.
-
#leader ⇒ Object
Get the current leader.
-
#process_http_request(res) ⇒ Object
need to have original request to process the response when it redirects.
- #setup_https(http) ⇒ Object
-
#version ⇒ Object
Returns the etcd daemon version.
-
#version_prefix ⇒ Object
Returns the etcd api version that will be used for across API methods.
Methods included from Keys
#compare_and_swap, #create, #create_in_order, #delete, #eternal_watch, #exists?, #get, #key_endpoint, #set, #update, #watch
Methods included from Stats
Constructor Details
#initialize(opts = {}) {|@config| ... } ⇒ Client
Creates an Etcd::Client object. It accepts a hash opts as argument
rubocop:disable CyclomaticComplexity
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/etcd/client.rb', line 52 def initialize(opts = {}) @host = opts[:host] || '127.0.0.1' @port = opts[:port] || 4001 @config = Config.new @config.read_timeout = opts[:read_timeout] || 60 @config.use_ssl = opts[:use_ssl] || false @config.verify_mode = opts.key?(:verify_mode) ? opts[:verify_mode] : OpenSSL::SSL::VERIFY_PEER @config.user_name = opts[:user_name] || nil @config.password = opts[:password] || nil @config.ca_file = opts.key?(:ca_file) ? opts[:ca_file] : nil # Provide a OpenSSL X509 cert here and not the path. See README @config.ssl_cert = opts.key?(:ssl_cert) ? opts[:ssl_cert] : nil # Provide the key (content) and not just the filename here. @config.ssl_key = opts.key?(:ssl_key) ? opts[:ssl_key] : nil yield @config if block_given? end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
42 43 44 |
# File 'lib/etcd/client.rb', line 42 def config @config end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
42 43 44 |
# File 'lib/etcd/client.rb', line 42 def host @host end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
42 43 44 |
# File 'lib/etcd/client.rb', line 42 def http @http end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
42 43 44 |
# File 'lib/etcd/client.rb', line 42 def port @port end |
Instance Method Details
#api_execute(path, method, options = {}) ⇒ 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
-
options - any additional parameters used by request method (optional)
rubocop:disable MethodLength, CyclomaticComplexity
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/etcd/client.rb', line 92 def api_execute(path, method, = {}) params = [:params] case method when :get req = build_http_request(Net::HTTP::Get, path, params) when :post req = build_http_request(Net::HTTP::Post, path, nil, params) when :put req = build_http_request(Net::HTTP::Put, path, nil, params) when :delete req = build_http_request(Net::HTTP::Delete, path, params) else fail "Unknown http action: #{method}" end http = Net::HTTP.new(host, port) http.read_timeout = [:timeout] || read_timeout setup_https(http) req.basic_auth(user_name, password) if [user_name, password].all? Log.debug("Invoking: '#{req.class}' against '#{path}") res = http.request(req) Log.debug("Response code: #{res.code}") Log.debug("Response body: #{res.body}") process_http_request(res) end |
#build_http_request(klass, path, params = nil, body = nil) ⇒ Object
rubocop:enable MethodLength
150 151 152 153 154 155 156 |
# File 'lib/etcd/client.rb', line 150 def build_http_request(klass, path, params = nil, body = nil) path += '?' + URI.encode_www_form(params) unless params.nil? req = klass.new(path) req.body = URI.encode_www_form(body) unless body.nil? Etcd::Log.debug("Built #{klass} path:'#{path}' body:'#{req.body}'") req end |
#leader ⇒ Object
Get the current leader
81 82 83 |
# File 'lib/etcd/client.rb', line 81 def leader api_execute(version_prefix + '/stats/leader', :get).body.strip end |
#process_http_request(res) ⇒ Object
need to have original request to process the response when it redirects
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/etcd/client.rb', line 135 def process_http_request(res) case res when HTTP_SUCCESS Log.debug('Http success') res when HTTP_CLIENT_ERROR fail Error.from_http_response(res) else Log.debug('Http error') Log.debug(res.body) res.error! end end |
#setup_https(http) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/etcd/client.rb', line 117 def setup_https(http) http.use_ssl = use_ssl http.verify_mode = verify_mode if config.ssl_cert Log.debug('Setting up ssl cert') http.cert = config.ssl_cert end if config.ssl_key Log.debug('Setting up ssl key') http.key = config.ssl_key end if config.ca_file Log.debug('Setting up ssl ca file to :' + config.ca_file) http.ca_file = config.ca_file end end |
#version ⇒ Object
Returns the etcd daemon version
76 77 78 |
# File 'lib/etcd/client.rb', line 76 def version api_execute('/version', :get).body end |
#version_prefix ⇒ Object
Returns the etcd api version that will be used for across API methods
71 72 73 |
# File 'lib/etcd/client.rb', line 71 def version_prefix '/v2' end |