class Consul::Client::KeyValue
Public Class Methods
Public: Constructor with options hash
Optional Parameters:
options[:namespace] - The KeyValue Store namespace. options[:data_center] - The consul data center. options[:api_host] - The api host to request against. options[:api_port] - The api port the api host is listening to. options[:version] - The Consul API version to use. options[:logger] - The default logging mechanism.
Return: This instance
Consul::Client::Base::new
# File lib/consul/client/key_value.rb, line 20 def initialize(options = nil) super(options) end
Public Instance Methods
# File lib/consul/client/key_value.rb, line 109 def build_url(suffix) "#{base_versioned_url}/kv/#{suffix}" end
Public: Delete the Key Value pair in consul.
key - Key params - Parameter Hash params - Existence of key notifies that all sub keys will be deleted params - Modify index for Check and set operation.
# File lib/consul/client/key_value.rb, line 94 def delete(key, params = {}) key = sanitize(key) params = {} params[:recurse] = nil if params.has_key?(:recurse) RestClient.delete build_url(compose_key(key)), {:params => params} end
Public: Gets the value associated with a given key.
Reference: www.consul.io/docs/agent/http/kv.html
Params: key - Key to get value for params - Parameter hash for Consul
params - existence of any value with tell consul to remove all keys
with the same prefix
params - Existence of :index, indicates to use a blocking call params - Existence of :keys, indicates to only return keys params - List only up to a given separator
Returns: An array of Consul::Model::KeyValue
objects, if only
# File lib/consul/client/key_value.rb, line 38 def get(key, params = {}) key = sanitize(key) params = {} if params.nil? params[:recurse] = nil if params.has_key?(:recurse) params[:index] = nil if params.has_key?(:index) params[:keys] = nil if params.has_key?(:keys) begin resp = _get build_url(compose_key(key)), params rescue Exception => e logger.warn("Unable to get value for #{key} due to: #{e}") return nil end return nil if resp.code == 404 json = JSON.parse(resp) return json if params.has_key?(:keys) json.map { |kv| kv = Consul::Model::KeyValue.new.extend(Consul::Model::KeyValue::Representer).from_hash(kv) kv.value = Base64.decode64(kv.value) unless kv.value.nil? kv } end
Public: Returns the name space of this KeyValue
Store. This allows you to identify what root namespace all keys will be placed under.
Returns: Namespace String.
# File lib/consul/client/key_value.rb, line 105 def namespace @namespace ||= options[:namespace] || '' end
Public: Put the Key Value pair in consul.
Low level put key value implementation.
Reference: www.consul.io/docs/agent/http/kv.html
key - Key value - Value to assign for Key params - Consul
Parameter Hash params - Unsigned value between 0 and 2^(64-1). General purpose parameter. params - Modify index for Check and set operation. params - session id to use to lock. params - session id to use to unlock.
Returns: True on success, False on failure Throws: IOError: Unable to contact Consul
Agent
.
# File lib/consul/client/key_value.rb, line 76 def put(key, value, params = {}) key = sanitize(key) params = {} if params.nil? begin value = JSON.generate(value) rescue JSON::GeneratorError logger.debug("Using non-JSON value: #{value} for key #{key}") end _put build_url(compose_key(key)), value, params end
Private Instance Methods
# File lib/consul/client/key_value.rb, line 128 def compose_key(key) ns = sanitize(namespace.strip) return "#{key}" if ns.empty? "#{ns}/#{key}" end
# File lib/consul/client/key_value.rb, line 115 def sanitize(key) unless key.nil? or !key.respond_to?(:to_s) key = key.to_s while !key.empty? and key[0] == '/' do key[0] = '' end while !key.empty? and key[key.length - 1] == '/' do key[key.length - 1] = '' end end key end