Class: Consul::Client::KeyValue

Inherits:
Base
  • Object
show all
Defined in:
lib/consul/client/key_value.rb

Instance Method Summary collapse

Methods inherited from Base

#is_reachable

Constructor Details

#initialize(options = nil) ⇒ KeyValue

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



20
21
22
# File 'lib/consul/client/key_value.rb', line 20

def initialize(options = nil)
  super(options)
end

Instance Method Details

#build_url(suffix) ⇒ Object



109
110
111
# File 'lib/consul/client/key_value.rb', line 109

def build_url(suffix)
  "#{base_versioned_url}/kv/#{suffix}"
end

#delete(key, params = {}) ⇒ Object

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.



94
95
96
97
98
99
# 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

#get(key, params = {}) ⇒ Object

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

#namespaceObject

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.



105
106
107
# File 'lib/consul/client/key_value.rb', line 105

def namespace
  @namespace ||= options[:namespace] || ''
end

#put(key, value, params = {}) ⇒ Object

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.



76
77
78
79
80
81
82
83
84
85
# 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