diplomat 0.12.0 → 0.13.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/README.md +25 -2
- data/lib/diplomat.rb +2 -2
- data/lib/diplomat/datacenter.rb +27 -0
- data/lib/diplomat/error.rb +1 -0
- data/lib/diplomat/kv.rb +13 -1
- data/lib/diplomat/rest_client.rb +5 -4
- data/lib/diplomat/service.rb +8 -2
- data/lib/diplomat/session.rb +1 -1
- data/lib/diplomat/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: aa373c2ee15ea4f0c80e90974a66f6dad2e76988
|
4
|
+
data.tar.gz: e890be6e209330f3c4363dc7645c67fbe5fbbca2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5105013dcf052dbd3041b5e3aeb8a5b5c8c94e3f8e75b89c5735a0e1ef1d0425c7eb535ad77c28995ede0ca0a45154f9606d090dcec8e76a9cf67b5f65c9348c
|
7
|
+
data.tar.gz: 438f5e8dc09e5b5010b044169757d0bb1ce685fc15ae32d90e6ae037504715faab0afa1e2a0564449bd7b276c1885ed04f6c0fbc5ec74eedfb18d92041ebe51a
|
data/README.md
CHANGED
@@ -65,6 +65,12 @@ foo = Diplomat::Kv.get('foo')
|
|
65
65
|
# => "bar"
|
66
66
|
```
|
67
67
|
|
68
|
+
Or retrieve a value from another datacenter:
|
69
|
+
```ruby
|
70
|
+
foo = Diplomat::Kv.get('foo', :dc => 'dc-west')
|
71
|
+
# => "baz"
|
72
|
+
```
|
73
|
+
|
68
74
|
You can also retrieve values recursively:
|
69
75
|
|
70
76
|
```ruby
|
@@ -84,7 +90,7 @@ Looking up a service is easy as pie:
|
|
84
90
|
|
85
91
|
```ruby
|
86
92
|
foo_service = Diplomat::Service.get('foo')
|
87
|
-
# => #<OpenStruct Node="hotel", Address="1.2.3.4", ServiceID="hotel_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>
|
93
|
+
# => #<OpenStruct Node="hotel", Address="1.2.3.4", ServiceID="hotel_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>
|
88
94
|
```
|
89
95
|
Or if you have multiple nodes per service:
|
90
96
|
|
@@ -93,6 +99,23 @@ foo_service = Diplomat::Service.get('foo', :all)
|
|
93
99
|
# => [#<OpenStruct Node="hotel", Address="1.2.3.4", ServiceID="hotel_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>,#<OpenStruct Node="indigo", Address="1.2.3.5", ServiceID="indigo_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>]
|
94
100
|
```
|
95
101
|
|
102
|
+
Or if you want to find services for a particular datacenter
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
foo_service = Diplomat::Service.get('foo', :all, { :dc => 'My_Datacenter'})
|
106
|
+
# => [#<OpenStruct Node="hotel", Address="1.2.3.4", ServiceID="hotel_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>,#<OpenStruct Node="indigo", Address="1.2.3.5", ServiceID="indigo_foo", ServiceName="foo", ServiceTags=["foo"], ServicePort=5432>]
|
107
|
+
```
|
108
|
+
|
109
|
+
### Datacenters
|
110
|
+
|
111
|
+
Getting a list of datacenters is quite simple and gives you the option to extract all services out of
|
112
|
+
all accessible datacenters if you need to.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
datacenters = Diplomat::Datacenter.get()
|
116
|
+
# => ["DC1", "DC2"]
|
117
|
+
```
|
118
|
+
|
96
119
|
### Sessions
|
97
120
|
|
98
121
|
Creating a session:
|
@@ -179,7 +202,7 @@ You can create a custom configuration using the following syntax:
|
|
179
202
|
```ruby
|
180
203
|
Diplomat.configure do |config|
|
181
204
|
# Set up a custom Consul URL
|
182
|
-
config.url = "localhost:8888"
|
205
|
+
config.url = "http://localhost:8888"
|
183
206
|
# Set up a custom Faraday Middleware
|
184
207
|
config.middleware = MyCustomMiddleware
|
185
208
|
# Connect into consul with custom access token (ACL)
|
data/lib/diplomat.rb
CHANGED
@@ -21,7 +21,7 @@ module Diplomat
|
|
21
21
|
self.root_path = File.expand_path "..", __FILE__
|
22
22
|
self.lib_path = File.expand_path "../diplomat", __FILE__
|
23
23
|
|
24
|
-
require_libs "configuration", "rest_client", "kv", "service", "members", "check", "health", "session", "lock", "error", "event"
|
24
|
+
require_libs "configuration", "rest_client", "kv", "datacenter", "service", "members", "check", "health", "session", "lock", "error", "event"
|
25
25
|
self.configuration ||= Diplomat::Configuration.new
|
26
26
|
|
27
27
|
class << self
|
@@ -45,4 +45,4 @@ module Diplomat
|
|
45
45
|
Diplomat::Kv.new.send(name, *args, &block)
|
46
46
|
end
|
47
47
|
end
|
48
|
-
end
|
48
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
module Diplomat
|
5
|
+
class Datacenter < Diplomat::RestClient
|
6
|
+
|
7
|
+
@access_methods = [ :get ]
|
8
|
+
|
9
|
+
# Get an array of all avaliable datacenters accessible by the local consul agent
|
10
|
+
# @param meta [Hash] output structure containing header information about the request (index)
|
11
|
+
# @return [OpenStruct] all datacenters avaliable to this consul agent
|
12
|
+
def get meta=nil
|
13
|
+
|
14
|
+
url = ["/v1/catalog/datacenters"]
|
15
|
+
|
16
|
+
ret = @conn.get concat_url url
|
17
|
+
|
18
|
+
if meta and ret.headers
|
19
|
+
meta[:index] = ret.headers["x-consul-index"]
|
20
|
+
meta[:knownleader] = ret.headers["x-consul-knownleader"]
|
21
|
+
meta[:lastcontact] = ret.headers["x-consul-lastcontact"]
|
22
|
+
end
|
23
|
+
return JSON.parse(ret.body)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/diplomat/error.rb
CHANGED
data/lib/diplomat/kv.rb
CHANGED
@@ -10,6 +10,7 @@ module Diplomat
|
|
10
10
|
# @param key [String] the key
|
11
11
|
# @param options [Hash] the query params
|
12
12
|
# @option options [String] :consistency The read consistency type
|
13
|
+
# @option options [String] :dc Target datacenter
|
13
14
|
# @param not_found [Symbol] behaviour if the key doesn't exist;
|
14
15
|
# :reject with exception, :return degenerate value, or :wait for it to appear
|
15
16
|
# @param found [Symbol] behaviour if the key does exist;
|
@@ -39,6 +40,7 @@ module Diplomat
|
|
39
40
|
url += recurse_get(@options)
|
40
41
|
url += check_acl_token
|
41
42
|
url += use_consistency(@options)
|
43
|
+
url += dc(@options)
|
42
44
|
|
43
45
|
# 404s OK using this connection
|
44
46
|
raw = @conn_no_err.get concat_url url
|
@@ -81,6 +83,7 @@ module Diplomat
|
|
81
83
|
# @param value [String] the value
|
82
84
|
# @param options [Hash] the query params
|
83
85
|
# @option options [Integer] :cas The modify index
|
86
|
+
# @option options [String] :dc Target datacenter
|
84
87
|
# @return [Bool] Success or failure of the write (can fail in c-a-s mode)
|
85
88
|
def put key, value, options=nil
|
86
89
|
@options = options
|
@@ -88,6 +91,7 @@ module Diplomat
|
|
88
91
|
url = ["/v1/kv/#{key}"]
|
89
92
|
url += check_acl_token
|
90
93
|
url += use_cas(@options)
|
94
|
+
url += dc(@options)
|
91
95
|
req.url concat_url url
|
92
96
|
req.body = value
|
93
97
|
end
|
@@ -100,11 +104,15 @@ module Diplomat
|
|
100
104
|
|
101
105
|
# Delete a value by its key
|
102
106
|
# @param key [String] the key
|
107
|
+
# @param options [Hash] the query params
|
108
|
+
# @option options [String] :dc Target datacenter
|
103
109
|
# @return [OpenStruct]
|
104
|
-
def delete key
|
110
|
+
def delete key, options=nil
|
105
111
|
@key = key
|
112
|
+
@options = options
|
106
113
|
url = ["/v1/kv/#{@key}"]
|
107
114
|
url += check_acl_token
|
115
|
+
url += dc(@options)
|
108
116
|
@raw = @conn.delete concat_url url
|
109
117
|
end
|
110
118
|
|
@@ -125,5 +133,9 @@ module Diplomat
|
|
125
133
|
def recurse_get(options)
|
126
134
|
if options && options[:recurse] then ['recurse'] else [] end
|
127
135
|
end
|
136
|
+
|
137
|
+
def dc(options)
|
138
|
+
if options && options[:dc] then use_named_parameter("dc", options[:dc]) else [] end
|
139
|
+
end
|
128
140
|
end
|
129
141
|
end
|
data/lib/diplomat/rest_client.rb
CHANGED
@@ -80,11 +80,12 @@ module Diplomat
|
|
80
80
|
@value = @raw.first["Value"]
|
81
81
|
@value = Base64.decode64(@value) unless @value.nil?
|
82
82
|
else
|
83
|
-
@value = @raw.
|
84
|
-
{
|
83
|
+
@value = @raw.reduce([]) do |acc, e|
|
84
|
+
acc << {
|
85
85
|
:key => e["Key"],
|
86
|
-
:value =>
|
87
|
-
}
|
86
|
+
:value => Base64.decode64(e["Value"])
|
87
|
+
} unless e["Value"].nil?
|
88
|
+
acc
|
88
89
|
end
|
89
90
|
end
|
90
91
|
end
|
data/lib/diplomat/service.rb
CHANGED
@@ -3,7 +3,7 @@ require 'faraday'
|
|
3
3
|
|
4
4
|
module Diplomat
|
5
5
|
class Service < Diplomat::RestClient
|
6
|
-
|
6
|
+
|
7
7
|
@access_methods = [ :get, :register, :deregister ]
|
8
8
|
|
9
9
|
# Get a service by it's key
|
@@ -19,7 +19,13 @@ module Diplomat
|
|
19
19
|
url << use_named_parameter('index', options[:index]) if options and options[:index]
|
20
20
|
url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
|
21
21
|
|
22
|
-
|
22
|
+
# If the request fails, it's probably due to a bad path
|
23
|
+
# so return a PathNotFound error.
|
24
|
+
begin
|
25
|
+
ret = @conn.get concat_url url
|
26
|
+
rescue Faraday::ClientError
|
27
|
+
raise Diplomat::PathNotFound
|
28
|
+
end
|
23
29
|
|
24
30
|
if meta and ret.headers
|
25
31
|
meta[:index] = ret.headers["x-consul-index"]
|
data/lib/diplomat/session.rb
CHANGED
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.13.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-07-30 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/datacenter.rb
|
182
183
|
- lib/diplomat/error.rb
|
183
184
|
- lib/diplomat/event.rb
|
184
185
|
- lib/diplomat/health.rb
|
@@ -209,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
209
210
|
version: '0'
|
210
211
|
requirements: []
|
211
212
|
rubyforge_project:
|
212
|
-
rubygems_version: 2.4.
|
213
|
+
rubygems_version: 2.4.8
|
213
214
|
signing_key:
|
214
215
|
specification_version: 4
|
215
216
|
summary: Diplomat is a simple wrapper for Consul
|