Class: Diplomat::Kv
- Inherits:
-
RestClient
- Object
- RestClient
- Diplomat::Kv
- Defined in:
- lib/diplomat/kv.rb
Overview
Methods for interacting with the Consul KV API endpoint
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#delete(key, options = {}) ⇒ OpenStruct
Delete a value by its key.
-
#get(key, options = {}, not_found = :reject, found = :return) ⇒ String
Get a value by its key, potentially blocking for the first or next value rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Layout/LineLength, Metrics/CyclomaticComplexity.
-
#get_all(key, options = {}, not_found = :reject, found = :return) ⇒ List
Get all keys recursively, potentially blocking for the first or next value rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Layout/LineLength, Metrics/CyclomaticComplexity.
-
#put(key, value, options = {}) ⇒ Bool
Associate a value with a key.
-
#txn(value, options = {}) ⇒ OpenStruct
Perform a key/value store transaction.
Methods inherited from RestClient
access_method?, #concat_url, #configuration, #initialize, method_missing, respond_to?, respond_to_missing?, #use_named_parameter
Constructor Details
This class inherits a constructor from Diplomat::RestClient
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/diplomat/kv.rb', line 7 def key @key end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
7 8 9 |
# File 'lib/diplomat/kv.rb', line 7 def raw @raw end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/diplomat/kv.rb', line 7 def value @value end |
Instance Method Details
#delete(key, options = {}) ⇒ OpenStruct
Delete a value by its key
177 178 179 180 181 182 183 184 185 |
# File 'lib/diplomat/kv.rb', line 177 def delete(key, = {}) key = normalize_key_for_uri(key) @key = key @options = custom_params = [] custom_params << recurse_get(@options) custom_params << dc(@options) @raw = send_delete_request(@conn, ["/v1/kv/#{@key}"], , custom_params) end |
#get(key, options = {}, not_found = :reject, found = :return) ⇒ String
When trying to access a key, there are two possibilites:
-
The key doesn’t (yet) exist
-
The key exists. This may be its first value, there is no way to tell
The combination of not_found and found behaviour gives maximum possible flexibility. For X: reject, R: return, W: wait
-
X X - meaningless; never return a value
-
X R - “normal” non-blocking get operation. Default
-
X W - get the next value only (must have a current value)
-
R X - meaningless; never return a meaningful value
-
R R - “safe” non-blocking, non-throwing get-or-default operation
-
R W - get the next value or a default
-
W X - get the first value only (must not have a current value)
-
W R - get the first or current value; always return something, but
block only when necessary
-
W W - get the first or next value; wait until there is an update
Get a value by its key, potentially blocking for the first or next value rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Layout/LineLength, Metrics/CyclomaticComplexity
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 77 78 79 80 81 82 83 84 85 |
# File 'lib/diplomat/kv.rb', line 46 def get(key, = {}, not_found = :reject, found = :return) @options = return_nil_values = @options && @options[:nil_values] transformation = @options && @options[:transformation] && @options[:transformation].methods.find_index(:call) ? @options[:transformation] : nil raw = get_raw(key, ) if raw.status == 404 case not_found when :reject raise Diplomat::KeyNotFound, key when :return return @value = '' when :wait index = raw.headers['x-consul-index'] end elsif raw.status == 200 case found when :reject raise Diplomat::KeyAlreadyExists, key when :return @raw = raw @raw = parse_body return @raw.first['ModifyIndex'] if @options && @options[:modify_index] return @raw.first['Session'] if @options && @options[:session] return decode_values if @options && @options[:decode_values] return convert_to_hash(return_value(return_nil_values, transformation, true)) if @options && @options[:convert_to_hash] return return_value(return_nil_values, transformation) when :wait index = raw.headers['x-consul-index'] end else raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}" end # Wait for first/next value @raw = wait_for_value(index, ) @raw = parse_body return_value(return_nil_values, transformation) end |
#get_all(key, options = {}, not_found = :reject, found = :return) ⇒ List
Get all keys recursively, potentially blocking for the first or next value rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength, Layout/LineLength, Metrics/CyclomaticComplexity
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/diplomat/kv.rb', line 106 def get_all(key, = {}, not_found = :reject, found = :return) @options = @options[:recurse] = true return_nil_values = @options && @options[:nil_values] transformation = @options && @options[:transformation] && @options[:transformation].methods.find_index(:call) ? @options[:transformation] : nil raw = get_raw(key, ) if raw.status == 404 case not_found when :reject raise Diplomat::KeyNotFound, key when :return return @value = [] when :wait index = raw.headers['x-consul-index'] end elsif raw.status == 200 case found when :reject raise Diplomat::KeyAlreadyExists, key when :return @raw = raw @raw = parse_body return decode_values if @options && @options[:decode_values] return convert_to_hash(return_value(return_nil_values, transformation, true)) if @options && @options[:convert_to_hash] return return_value(return_nil_values, transformation, true) when :wait index = raw.headers['x-consul-index'] end else raise Diplomat::UnknownStatus, "status #{raw.status}: #{raw.body}" end # Wait for first/next value @raw = wait_for_value(index, ) @raw = parse_body return_value(return_nil_values, transformation, true) end |
#put(key, value, options = {}) ⇒ Bool
Associate a value with a key
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/diplomat/kv.rb', line 155 def put(key, value, = {}) key = normalize_key_for_uri(key) @options = custom_params = [] custom_params << use_cas(@options) custom_params << dc(@options) custom_params << acquire(@options) @raw = send_put_request(@conn, ["/v1/kv/#{key}"], , value, custom_params, 'application/x-www-form-urlencoded') if @raw.body.chomp == 'true' @key = key @value = value end @raw.body.chomp == 'true' end |
#txn(value, options = {}) ⇒ OpenStruct
Perform a key/value store transaction.
208 209 210 211 212 213 214 215 216 217 |
# File 'lib/diplomat/kv.rb', line 208 def txn(value, = {}) # Verify the given value for the transaction transaction_verification(value) # Will return 409 if transaction was rolled back custom_params = [] custom_params << dc() custom_params << transaction_consistency() raw = send_put_request(@conn_no_err, ['/v1/txn'], , value, custom_params) transaction_return JSON.parse(raw.body), end |