diplomat 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/diplomat.rb +1 -1
- data/lib/diplomat/error.rb +5 -0
- data/lib/diplomat/kv.rb +74 -6
- data/lib/diplomat/rest_client.rb +12 -2
- data/lib/diplomat/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13812b43787dfad96eb90581712105f0c628d79b
|
4
|
+
data.tar.gz: 6a1243327e6ac77df41c96d9d00b6a6aaeb8e5c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbccfdcb4107ff7c5fe8a515207f93ab162c5f3a469e932ee1fbd71596302e2fdc0aa7a67b056139abba39029b781702f3c854a23a50e39fa70a53b7fee7442e
|
7
|
+
data.tar.gz: 50e25171fab1b59b069f180d4ca50ea5084795852bc0c4a4a18fe522e325da26c5922968508d7c69c92b8db9e4c753bdb3b757da7ea2da57e1b5aee714916c14
|
data/lib/diplomat.rb
CHANGED
@@ -20,7 +20,7 @@ module Diplomat
|
|
20
20
|
self.root_path = File.expand_path "..", __FILE__
|
21
21
|
self.lib_path = File.expand_path "../diplomat", __FILE__
|
22
22
|
|
23
|
-
require_libs "configuration", "rest_client", "kv", "service", "members", "check", "health", "session", "lock"
|
23
|
+
require_libs "configuration", "rest_client", "kv", "service", "members", "check", "health", "session", "lock", "error"
|
24
24
|
self.configuration ||= Diplomat::Configuration.new
|
25
25
|
|
26
26
|
class << self
|
data/lib/diplomat/kv.rb
CHANGED
@@ -6,14 +6,69 @@ module Diplomat
|
|
6
6
|
|
7
7
|
attr_reader :key, :value, :raw
|
8
8
|
|
9
|
-
# Get a value by its key
|
9
|
+
# Get a value by its key, potentially blocking for the first or next value
|
10
10
|
# @param key [String] the key
|
11
|
+
# @param options [Hash] the query params
|
12
|
+
# @option options [String] :consistency The read consistency type
|
13
|
+
# @param not_found [Symbol] behaviour if the key doesn't exist;
|
14
|
+
# :reject with exception, or :wait for it to appear
|
15
|
+
# @param found [Symbol] behaviour if the key does exist;
|
16
|
+
# :reject with exception, :wait for its next value, or :return its current value
|
11
17
|
# @return [String] The base64-decoded value associated with the key
|
12
|
-
|
18
|
+
# @note
|
19
|
+
# When trying to access a key, there are two possibilites:
|
20
|
+
# - The key doesn't (yet) exist
|
21
|
+
# - The key exists. This may be its first value, there is no way to tell
|
22
|
+
# The combination of not_found and found behaviour gives maximum possible
|
23
|
+
# flexibility. For X: reject, R: return, W: wait
|
24
|
+
# - X X - meaningless; never return a value
|
25
|
+
# - X R - "normal" non-blocking get operation. Default
|
26
|
+
# - X W - get the next value only (must have a current value)
|
27
|
+
# - W X - get the first value only (must not have a current value)
|
28
|
+
# - W R - get the first or current value; always return something, but
|
29
|
+
# block only when necessary
|
30
|
+
# - W W - get the first or next value; wait until there is an update
|
31
|
+
def get key, options=nil, not_found=:reject, found=:return
|
13
32
|
@key = key
|
33
|
+
@options = options
|
34
|
+
|
14
35
|
url = ["/v1/kv/#{@key}"]
|
15
36
|
url += check_acl_token unless check_acl_token.nil?
|
16
|
-
@
|
37
|
+
url += use_consistency(@options) unless use_consistency(@options).nil?
|
38
|
+
|
39
|
+
# 404s OK using this connection
|
40
|
+
raw = @conn_no_err.get concat_url url
|
41
|
+
if raw.status == 404
|
42
|
+
case not_found
|
43
|
+
when :reject
|
44
|
+
raise Diplomat::KeyNotFound, key
|
45
|
+
when :wait
|
46
|
+
index = raw.headers["x-consul-index"]
|
47
|
+
end
|
48
|
+
elsif raw.status == 200
|
49
|
+
case found
|
50
|
+
when :reject
|
51
|
+
raise Diplomat::KeyAlreadyExists, key
|
52
|
+
when :return
|
53
|
+
@raw = raw
|
54
|
+
parse_body
|
55
|
+
return return_value
|
56
|
+
when :wait
|
57
|
+
index = raw.headers["x-consul-index"]
|
58
|
+
end
|
59
|
+
else
|
60
|
+
raise Diplomat::UnknownStatus, "status #{raw.status}"
|
61
|
+
end
|
62
|
+
|
63
|
+
# Wait for first/next value
|
64
|
+
url = ["/v1/kv/#{@key}"]
|
65
|
+
url += check_acl_token unless check_acl_token.nil?
|
66
|
+
url += use_consistency(@options) unless use_consistency(@options).nil?
|
67
|
+
url += ["index=#{index}"]
|
68
|
+
@raw = @conn.get do |req|
|
69
|
+
req.url concat_url url
|
70
|
+
req.options.timeout = 86400
|
71
|
+
end
|
17
72
|
parse_body
|
18
73
|
return_value
|
19
74
|
end
|
@@ -69,7 +124,7 @@ module Diplomat
|
|
69
124
|
|
70
125
|
# Parse the body, apply it to the raw attribute
|
71
126
|
def parse_body
|
72
|
-
@raw = JSON.parse(@raw.body)
|
127
|
+
@raw = JSON.parse(@raw.body)
|
73
128
|
end
|
74
129
|
|
75
130
|
# Get the key from the raw output
|
@@ -79,8 +134,17 @@ module Diplomat
|
|
79
134
|
|
80
135
|
# Get the value from the raw output
|
81
136
|
def return_value
|
82
|
-
@
|
83
|
-
|
137
|
+
if @raw.count == 1
|
138
|
+
@value = @raw.first["Value"]
|
139
|
+
@value = Base64.decode64(@value) unless @value.nil?
|
140
|
+
else
|
141
|
+
@value = @raw.map do |e|
|
142
|
+
{
|
143
|
+
key: e["Key"],
|
144
|
+
value: e["Value"].nil? ? e["Value"] : Base64.decode64(e["Value"])
|
145
|
+
}
|
146
|
+
end
|
147
|
+
end
|
84
148
|
end
|
85
149
|
|
86
150
|
def check_acl_token
|
@@ -90,5 +154,9 @@ module Diplomat
|
|
90
154
|
def use_cas(options)
|
91
155
|
["cas=#{options[:cas]}"] if options && options[:cas]
|
92
156
|
end
|
157
|
+
|
158
|
+
def use_consistency(options)
|
159
|
+
["#{options[:consistency]}"] if options && options[:consistency]
|
160
|
+
end
|
93
161
|
end
|
94
162
|
end
|
data/lib/diplomat/rest_client.rb
CHANGED
@@ -21,13 +21,23 @@ module Diplomat
|
|
21
21
|
# Build the API Client
|
22
22
|
# @param api_connection [Faraday::Connection,nil] supply mock API Connection
|
23
23
|
def start_connection api_connection=nil
|
24
|
-
@conn = api_connection
|
24
|
+
@conn = api_connection || Faraday.new(:url => Diplomat.configuration.url) do |faraday|
|
25
|
+
faraday.adapter Faraday.default_adapter
|
25
26
|
faraday.request :url_encoded
|
27
|
+
faraday.response :raise_error
|
28
|
+
|
26
29
|
Diplomat.configuration.middleware.each do |middleware|
|
27
30
|
faraday.use middleware
|
28
31
|
end
|
32
|
+
end
|
33
|
+
|
34
|
+
@conn_no_err = api_connection || Faraday.new(:url => Diplomat.configuration.url) do |faraday|
|
29
35
|
faraday.adapter Faraday.default_adapter
|
30
|
-
faraday.
|
36
|
+
faraday.request :url_encoded
|
37
|
+
|
38
|
+
Diplomat.configuration.middleware.each do |middleware|
|
39
|
+
faraday.use middleware
|
40
|
+
end
|
31
41
|
end
|
32
42
|
end
|
33
43
|
|
data/lib/diplomat/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diplomat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Hamelink
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -179,6 +179,7 @@ files:
|
|
179
179
|
- lib/diplomat.rb
|
180
180
|
- lib/diplomat/check.rb
|
181
181
|
- lib/diplomat/configuration.rb
|
182
|
+
- lib/diplomat/error.rb
|
182
183
|
- lib/diplomat/health.rb
|
183
184
|
- lib/diplomat/kv.rb
|
184
185
|
- lib/diplomat/lock.rb
|