imperium 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -2
- data/Dockerfile.ci +1 -1
- data/lib/imperium.rb +1 -0
- data/lib/imperium/client.rb +8 -1
- data/lib/imperium/error.rb +1 -0
- data/lib/imperium/http_client.rb +2 -1
- data/lib/imperium/kv.rb +43 -10
- data/lib/imperium/kv_put_response.rb +37 -0
- data/lib/imperium/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70b5342210c6ee5d4e7876d0899b224b36092bbb
|
4
|
+
data.tar.gz: 660c0bba85f85516ead3f5c8b00fe3ad7bb437fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 699233998e7e26e9036f2362b4a5ca6eff394e258935966609dde0215b6283dada530510c4ca537ca1a3d474c3739234e69133b7117095b3eb5722edbb8eef7a
|
7
|
+
data.tar.gz: f52e244277ad058e270ab111ebd287a6959ff9c2a65168d4bf399ebfee05c0ef0d93df5f26950400ac85acd985431b544a845d8a8b5cbd4af076097e3c2e0178
|
data/.travis.yml
CHANGED
data/Dockerfile.ci
CHANGED
data/lib/imperium.rb
CHANGED
data/lib/imperium/client.rb
CHANGED
@@ -3,6 +3,9 @@ require 'json'
|
|
3
3
|
|
4
4
|
module Imperium
|
5
5
|
class Client
|
6
|
+
# Options that are allowed for all API endpoints
|
7
|
+
UNIVERSAL_API_OPTIONS = %i{dc}.freeze
|
8
|
+
|
6
9
|
class << self
|
7
10
|
attr_reader :subclasses
|
8
11
|
attr_accessor :path_prefix
|
@@ -42,7 +45,11 @@ module Imperium
|
|
42
45
|
if full_options.key?(:consistent) && full_options.key?(:stale)
|
43
46
|
raise InvalidConsistencySpecification, 'Both consistency modes (consistent, stale) supplied, this is not allowed by the HTTP API'
|
44
47
|
end
|
45
|
-
allowed_params == :all ?
|
48
|
+
allowed_params == :all ?
|
49
|
+
full_options :
|
50
|
+
full_options.select { |k, _|
|
51
|
+
allowed_params.include?(k.to_sym) || UNIVERSAL_API_OPTIONS.include?(k.to_sym)
|
52
|
+
}
|
46
53
|
end
|
47
54
|
|
48
55
|
def hashify_options(options_array)
|
data/lib/imperium/error.rb
CHANGED
data/lib/imperium/http_client.rb
CHANGED
data/lib/imperium/kv.rb
CHANGED
@@ -9,6 +9,12 @@ module Imperium
|
|
9
9
|
default_client.get(key, *options)
|
10
10
|
end
|
11
11
|
|
12
|
+
# {#put Create or update} a key using the {.default_client}
|
13
|
+
# @see #put
|
14
|
+
def self.put(key, value, *options)
|
15
|
+
default_client.put(key, value, *options)
|
16
|
+
end
|
17
|
+
|
12
18
|
# Delete the specified key
|
13
19
|
# @note This is really a stub of this method, it will delete the key but
|
14
20
|
# you'll get back a raw
|
@@ -27,6 +33,10 @@ module Imperium
|
|
27
33
|
|
28
34
|
GET_ALLOWED_OPTIONS = %i{consistent stale recurse keys separator raw}.freeze
|
29
35
|
private_constant :GET_ALLOWED_OPTIONS
|
36
|
+
|
37
|
+
PUT_ALLOWED_OPTIONS = %i{flags cas acquire release}.freeze
|
38
|
+
private_constant :PUT_ALLOWED_OPTIONS
|
39
|
+
|
30
40
|
# Get the specified key/prefix using the supplied options.
|
31
41
|
#
|
32
42
|
# @example Fetching a key that is allowed to be stale.
|
@@ -40,6 +50,7 @@ module Imperium
|
|
40
50
|
# @param [String] key The key/prefix to be fetched from Consul.
|
41
51
|
# @param [Array<Symbol,String,Hash>] options The options for constructing
|
42
52
|
# the request
|
53
|
+
# @option options [String] :dc Specify the datacenter to use for the request
|
43
54
|
# @option options [Symbol] :consistent Specify the consistent option to the
|
44
55
|
# API resulting in the most up to date value possible at the expense of a
|
45
56
|
# bit of latency and the requirement of a validly elected leader. See
|
@@ -62,20 +73,42 @@ module Imperium
|
|
62
73
|
end
|
63
74
|
|
64
75
|
# Update or create the specified key
|
65
|
-
# @note This is really a stub of this method, it will put the key but
|
66
|
-
# you'll get back a raw
|
67
|
-
# {http://www.rubydoc.info/gems/httpclient/HTTP/Message HTTP::Message}
|
68
|
-
# object. If you're really serious about using this we'll probably want
|
69
|
-
# to build a wrapper around the response with some logic to simplify
|
70
|
-
# interpreting the response.
|
71
76
|
#
|
72
77
|
# @param key [String] The key to be created or updated.
|
73
78
|
# @param value [String] The value to be set on the key.
|
74
|
-
# @param
|
75
|
-
#
|
76
|
-
# @
|
79
|
+
# @param [Array<Symbol,String,Hash>] options The options for constructing
|
80
|
+
# the request
|
81
|
+
# @option options [String] :dc Specify the datacenter to use for the request
|
82
|
+
# @option options [Integer] :flags Specifies an unsigned value
|
83
|
+
# between 0 and (2^64)-1. Clients can choose to use this however makes
|
84
|
+
# sense for their application.
|
85
|
+
# @option options [Integer] :cas Specifies to use a Check-And-Set operation.
|
86
|
+
# This is very useful as a building block for more complex synchronization
|
87
|
+
# primitives. If the index is 0, Consul will only put the key if it does
|
88
|
+
# not already exist. If the index is non-zero, the key is only set if the
|
89
|
+
# index matches the ModifyIndex of that key.
|
90
|
+
# @option options [String] :acquire Supply a session key for use in
|
91
|
+
# acquiring lock on the key. From the Consul docs: Specifies to use a lock
|
92
|
+
# acquisition operation. This is useful as it allows leader election to be
|
93
|
+
# built on top of Consul. If the lock is not held and the session is
|
94
|
+
# valid, this increments the LockIndex and sets the Session value of the
|
95
|
+
# key in addition to updating the key contents. A key does not need to
|
96
|
+
# exist to be acquired. If the lock is already held by the given session,
|
97
|
+
# then the LockIndex is not incremented but the key contents are updated.
|
98
|
+
# This lets the current lock holder update the key contents without having
|
99
|
+
# to give up the lock and reacquire it.
|
100
|
+
# @option options [String] :release Supply a session key for releasing a
|
101
|
+
# lock. From the Consul docs: Specifies to use a lock release operation.
|
102
|
+
# This is useful when paired with ?acquire= as it allows clients to yield
|
103
|
+
# a lock. This will leave the LockIndex unmodified but will clear the
|
104
|
+
# associated Session of the key. The key must be held by this session to
|
105
|
+
# be unlocked.
|
106
|
+
# @return [KVPUTResponse]
|
77
107
|
def put(key, value, *options)
|
78
|
-
|
108
|
+
expanded_options = hashify_options(options)
|
109
|
+
query_params = extract_query_params(expanded_options, allowed_params: PUT_ALLOWED_OPTIONS)
|
110
|
+
response = @http_client.put(prefix_path(key), value, query: query_params)
|
111
|
+
KVPUTResponse.new(response, options: expanded_options)
|
79
112
|
end
|
80
113
|
|
81
114
|
private
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'response'
|
2
|
+
|
3
|
+
module Imperium
|
4
|
+
# KVPUTResponse is a wrapper for the raw HTTP::Message response from the API
|
5
|
+
#
|
6
|
+
# @note This class doesn't really make sense to be instantiated outside of
|
7
|
+
# {KV#put}
|
8
|
+
#
|
9
|
+
# @!attribute [rw] options
|
10
|
+
# @return [Hash<Symbol, Object>] The options for the get request after being
|
11
|
+
# coerced from an array to hash.
|
12
|
+
# @attribute [rw] prefix
|
13
|
+
# @return [String] The key prefix requested from the api, used to coerce the
|
14
|
+
# returned values from the API into their various shapes.
|
15
|
+
class KVPUTResponse < Response
|
16
|
+
attr_accessor :options
|
17
|
+
|
18
|
+
def initialize(message, options: {})
|
19
|
+
super message
|
20
|
+
@options = options
|
21
|
+
end
|
22
|
+
|
23
|
+
if RUBY_VERSION < "2.4"
|
24
|
+
def success?
|
25
|
+
return @success if defined? @success
|
26
|
+
@success = (body.chomp == "true")
|
27
|
+
end
|
28
|
+
else
|
29
|
+
def success?
|
30
|
+
return @success if defined? @success
|
31
|
+
@success = JSON.parse(body)
|
32
|
+
rescue JSON::ParserError
|
33
|
+
body.empty? ? @success = false : raise
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/imperium/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: imperium
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler Pickett
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|
@@ -164,6 +164,7 @@ files:
|
|
164
164
|
- lib/imperium/kv.rb
|
165
165
|
- lib/imperium/kv_get_response.rb
|
166
166
|
- lib/imperium/kv_pair.rb
|
167
|
+
- lib/imperium/kv_put_response.rb
|
167
168
|
- lib/imperium/response.rb
|
168
169
|
- lib/imperium/testing.rb
|
169
170
|
- lib/imperium/version.rb
|
@@ -187,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
187
188
|
version: '0'
|
188
189
|
requirements: []
|
189
190
|
rubyforge_project:
|
190
|
-
rubygems_version: 2.
|
191
|
+
rubygems_version: 2.6.13
|
191
192
|
signing_key:
|
192
193
|
specification_version: 4
|
193
194
|
summary: A powerful, easy to use, Consul client
|