Class: Consul::Client::KeyValue

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

Instance Method Summary collapse

Methods included from Base

#is_reachable

Constructor Details

#initialize(name_space = '', data_center = 'dc1', api_host = '127.0.0.1', api_port = '8500', version = 'v1', logger = Logger.new(STDOUT)) ⇒ KeyValue

Public: Creates an API Endpoint

data_center - The data center to utilize, defaults to bootstrap ‘dc1’ datat center api_host - The host the Consul Agent is running on. Default: 127.0.0.1 api_port - The port the Consul Agent is listening on. Default: 8500 version - The version of the api to use. logger - Logging mechanism. Must conform to Ruby Logger interface



18
19
20
21
22
23
24
25
26
27
# File 'lib/consul/client/key_value.rb', line 18

def initialize(name_space = '', data_center = 'dc1', api_host = '127.0.0.1', api_port = '8500', version = 'v1', logger = Logger.new(STDOUT))
  name_space = sanitize(name_space)
  name_space = "#{name_space}/" unless name_space.nil? or name_space.empty?
  @namespace = sanitize(name_space)
  @dc = data_center
  @host = api_host
  @port = api_port
  @logger = logger
  @version = version
end

Instance Method Details

#build_url(suffix) ⇒ Object



126
127
128
# File 'lib/consul/client/key_value.rb', line 126

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

#delete(key, recurse = false, cas = nil) ⇒ Object

Public: Delete the Key Value pair in consul.

key - Key recurse - Delete all keys as the ‘key’ is a prefix for cas - Check and Set



118
119
120
121
122
123
124
# File 'lib/consul/client/key_value.rb', line 118

def delete(key, recurse = false, cas = nil)
  key = sanitize(key)
  params = {}
  params[:recurse] = nil if recurse
  params[:cas] = cas unless cas.nil?
  RestClient.delete build_url(key), {:params => params}
end

#get(key, recurse = false, index = false, only_keys = false, separator = nil) ⇒ Object

Public: Gets the value associated with a given key.

Reference: www.consul.io/docs/agent/http/kv.html

key - Key to get value for, if recurse = true the key is treated by a prefix recurse - Flag to signify treating the key as a prefix index - Can be used to establish blocking queries by setting only_keys - Flag to return only keys separator - list only up to a given separator

Returns: An array of Consul::Model::KeyValue objects, if only



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/consul/client/key_value.rb', line 44

def get(key,
        recurse = false,
        index = false,
        only_keys = false,
        separator = nil)
  key = sanitize(key)
  params = {}
  params[:recurse] = nil if recurse
  params[:index] = nil if index
  params[:keys] = nil if only_keys
  params[:separator] = separator unless separator.nil?
  # begin
  #   resp = RestClient.get key_url(key), {:params => params}
  # rescue
  #   # TODO need to pass more information back to the client.
  #   logger.warn("Unable to get value for #{key}")
  #   nil
  # end
  begin
    resp = _get key_url(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(_get key_url(key), params)
  return json if only_keys
  json.map { |kv|
    kv = Consul::Model::KeyValue.new.extend(Consul::Model::KeyValue::Representer).from_hash(kv)
    kv.value = Base64.decode64(kv.value)
    kv
  }
end

#name_spaceObject



29
30
31
# File 'lib/consul/client/key_value.rb', line 29

def name_space
  @namespace ||= ''
end

#put(key, value, flags = nil, cas = nil, acquire_session = nil, release_session = nil) ⇒ 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 flags - Client specified value [0, 2e64-1] cas - Check and Set operation acquire - Session id to acquire the lock with a valid session. release - Session id to release the lock with a valid session.

Returns: True on success, False on failure Throws: IOError: Unable to contact Consul Agent.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/consul/client/key_value.rb', line 93

def put(key,
        value,
        flags = nil,
        cas = nil,
        acquire_session = nil,
        release_session = nil)
  key = sanitize(key)
  params = {}
  params[:flags] = flags unless flags.nil?
  params[:cas] = cas unless cas.nil?
  params[:acquire] = acquire_session unless acquire_session.nil?
  params[:release_session] = release_session unless release_session.nil?
  begin
    value = JSON.generate(value)
  rescue JSON::GeneratorError
    @logger.debug("Using non-JSON value for key #{key}")
  end
  _put build_url(key), value, {:params => params}
end