Module: Etcd::Keys

Included in:
Client
Defined in:
lib/etcd/keys.rb

Overview

Keys module provides the basic key value operations against etcd /keys namespace

Instance Method Summary collapse

Instance Method Details

#compare_and_swap(key, opts = {}) ⇒ Object Also known as: test_and_set

Set a new value for key if previous value of key is matched

This method takes the following parameters as arguments

  • 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)



59
60
61
62
63
# File 'lib/etcd/keys.rb', line 59

def compare_and_swap(key, opts =  {})
  fail ArgumentError, 'Second argument must be a hash' unless opts.is_a?(Hash)
  fail ArgumentError, 'You must pass prevValue' unless opts.key?(:prevValue)
  set(key, opts)
end

#create(key, opts = {}) ⇒ Object



110
111
112
# File 'lib/etcd/keys.rb', line 110

def create(key, opts = {})
  set(key, opts.merge(prevExist: false))
end

#create_in_order(dir, opts = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/etcd/keys.rb', line 90

def create_in_order(dir, opts = {})
  path  = key_endpoint + dir
  fail ArgumentError, 'Second argument must be a hash' unless opts.is_a?(Hash)
  payload = {}
  [:ttl, :value].each do |k|
    payload[k] = opts[k] if opts.key?(k)
  end
  response = api_execute(path, :post, params: payload)
  Response.from_http_response(response)
end

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

Deletes a key (and its content)

This method takes the following parameters as arguments

  • key - key to be deleted



47
48
49
50
# File 'lib/etcd/keys.rb', line 47

def delete(key, opts = {})
  response = api_execute(key_endpoint + key, :delete, params: opts)
  Response.from_http_response(response)
end

#eternal_watch(key, index = nil) ⇒ Object



118
119
120
121
122
123
# File 'lib/etcd/keys.rb', line 118

def eternal_watch(key, index = nil)
  loop do
    response = watch(key, index)
    yield response
  end
end

#exists?(key) ⇒ Boolean Also known as: key?, exist?, has_key?

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
# File 'lib/etcd/keys.rb', line 101

def exists?(key)
  Etcd::Log.debug("Checking if key:' #{key}' exists")
  get(key)
  true
rescue KeyNotFound => e
  Etcd::Log.debug("Key does not exist #{e}")
  false
end

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

Retrives a key with its associated data, if key is not present it will return with message “Key Not Found”

This method takes the following parameters as arguments

  • key - whose data is to be retrieved



21
22
23
24
# File 'lib/etcd/keys.rb', line 21

def get(key, opts = {})
  response = api_execute(key_endpoint + key, :get, params: opts)
  Response.from_http_response(response)
end

#key_endpointObject

return etcd endpoint that is reserved for key/value store



12
13
14
# File 'lib/etcd/keys.rb', line 12

def key_endpoint
  version_prefix + '/keys'
end

#set(key, opts = nil) ⇒ Object

Create or update a new key

This method takes the following parameters as arguments

  • key - whose value to be set

  • value - value to be set for specified key

  • ttl - shelf life of a key (in seconds) (optional)



32
33
34
35
36
37
38
39
40
41
# File 'lib/etcd/keys.rb', line 32

def set(key, opts = nil)
  fail ArgumentError, 'Second argument must be a hash' unless opts.is_a?(Hash)
  path  = key_endpoint + key
  payload = {}
  [:ttl, :value, :dir, :prevExist, :prevValue, :prevIndex].each do |k|
    payload[k] = opts[k] if opts.key?(k)
  end
  response = api_execute(path, :put, params: payload)
  Response.from_http_response(response)
end

#update(key, opts = {}) ⇒ Object



114
115
116
# File 'lib/etcd/keys.rb', line 114

def update(key, opts = {})
  set(key, opts.merge(prevExist: true))
end

#watch(key, opts = {}) ⇒ Object

Gives a notification when specified key changes

This method takes the following parameters as arguments @ key - key to be watched



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/etcd/keys.rb', line 72

def watch(key, opts = {})
  params = { wait: true }
  fail ArgumentError, 'Second argument must be a hash' unless opts.is_a?(Hash)
  timeout = opts[:timeout] || @read_timeout
  index = opts[:waitIndex] || opts[:index]
  params[:waitIndex] = index unless index.nil?
  params[:consistent] = opts[:consistent] if opts.key?(:consistent)
  params[:recursive] = opts[:recursive] if opts.key?(:recursive)

  response = api_execute(
    key_endpoint + key,
    :get,
    timeout: timeout,
    params: params
  )
  Response.from_http_response(response)
end