diplomat 1.0.0 → 1.1.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 +13 -2
- data/lib/diplomat/health.rb +2 -0
- data/lib/diplomat/kv.rb +6 -0
- data/lib/diplomat/node.rb +1 -1
- data/lib/diplomat/rest_client.rb +65 -1
- data/lib/diplomat/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 781ef66fd58770259245d9d3624499232db1cdbe
|
4
|
+
data.tar.gz: cffd83536d1936f5f1189129fbf1d238a1555c8b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5b01504b9b8c7f8aac4f08e13837a36018ea7005a198e4d0dfba97568ff65f3417ed8ef33d061f2a2a2ba5adb421ad7ef691018f0e7883a714a798e0a823f1d
|
7
|
+
data.tar.gz: 38034ad0d2add4cbb6368ba65af19c6ee066daa56b39fa8a3f12674d269240527e75765fd4648c17b2ca3b4de556299ed556e55d27475e6e72dc59a30b26e21e
|
data/README.md
CHANGED
@@ -39,10 +39,12 @@ production:
|
|
39
39
|
|
40
40
|
[See here](http://www.consul.io/intro/). I managed to roll it out on my production machines with the help of [Ansible](http://www.ansible.com/) in one working day.
|
41
41
|
|
42
|
-
### Which versions of Ruby does Diplomat support?
|
42
|
+
### Which versions of Ruby does Diplomat support? Where did my ruby 1.9 compatibility go?
|
43
43
|
|
44
44
|
Check out [Travis](https://travis-ci.org/WeAreFarmGeek/diplomat) to see which versions of ruby we currently test when we're making builds.
|
45
45
|
|
46
|
+
We've dropped ruby 1.9 support. You can still depend on Diplomat by directly using the `ruby-1.9-compatible` branch on github, although be advised it's not actively maintained anymore.
|
47
|
+
|
46
48
|
## Usage
|
47
49
|
|
48
50
|
[The most up to date place to read about the API is here.](http://rubydoc.info/github/WeAreFarmGeek/diplomat)
|
@@ -76,7 +78,6 @@ foo = Diplomat::Kv.get('foo', :dc => 'dc-west')
|
|
76
78
|
```
|
77
79
|
|
78
80
|
You can also retrieve values recursively:
|
79
|
-
|
80
81
|
```ruby
|
81
82
|
Diplomat::Kv.put('foo/a', 'lorem')
|
82
83
|
Diplomat::Kv.put('foo/b', 'ipsum')
|
@@ -86,11 +87,21 @@ Diplomat::Kv.get('foo/', recurse: true)
|
|
86
87
|
# => [{:key=>"foo/a", :value=>"lorem"}, {:key=>"foo/b", :value=>"ipsum"}, {:key=>"foo/c", :value=>"dolor"}]
|
87
88
|
```
|
88
89
|
|
90
|
+
|
89
91
|
Or list all available keys:
|
90
92
|
|
91
93
|
```ruby
|
92
94
|
Diplomat::Kv.get('/', :keys => true) # => ['foo/a', 'foo/b']
|
93
95
|
```
|
96
|
+
You can convert the consul data to a ruby hash
|
97
|
+
```ruby
|
98
|
+
Diplomat::Kv.put('foo/a', 'lorem')
|
99
|
+
Diplomat::Kv.put('foo/b', 'ipsum')
|
100
|
+
Diplomat::Kv.put('foo/c', 'dolor')
|
101
|
+
|
102
|
+
Diplomat::Kv.get('foo/', recurse: true, convert_to_hash: true)
|
103
|
+
# => {"foo"=>{"a"=>"lorem", "b"=>"ipsum", "c"=>"dolor"}}
|
104
|
+
```
|
94
105
|
|
95
106
|
### Nodes
|
96
107
|
|
data/lib/diplomat/health.rb
CHANGED
@@ -46,11 +46,13 @@ module Diplomat
|
|
46
46
|
# @param s [String] the service
|
47
47
|
# @param options [Hash] :dc string for dc specific query
|
48
48
|
# @param options [Hash] :state string for specific service state
|
49
|
+
# @param options [Hash] :tag string for specific tag
|
49
50
|
# @return [OpenStruct] all data associated with the node
|
50
51
|
def service s, options=nil
|
51
52
|
url = ["/v1/health/service/#{s}"]
|
52
53
|
url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
|
53
54
|
url << options[:state] if options and options[:state]
|
55
|
+
url << use_named_parameter('tag', options[:tag]) if options and options[:tag]
|
54
56
|
|
55
57
|
# If the request fails, it's probably due to a bad path
|
56
58
|
# so return a PathNotFound error.
|
data/lib/diplomat/kv.rb
CHANGED
@@ -20,6 +20,7 @@ module Diplomat
|
|
20
20
|
# @option options [Boolean] :decode_values Return consul response with decoded values.
|
21
21
|
# @option options [String] :separator List only up to a given separator. Only applies when combined with :keys option.
|
22
22
|
# @option options [Boolean] :nil_values If to return keys/dirs with nil values
|
23
|
+
# @option options [Boolean] :convert_to_hash Take the data returned from consul and build a hash
|
23
24
|
# @option options [Callable] :transformation funnction to invoke on keys values
|
24
25
|
# @param not_found [Symbol] behaviour if the key doesn't exist;
|
25
26
|
# :reject with exception, :return degenerate value, or :wait for it to appear
|
@@ -81,6 +82,9 @@ module Diplomat
|
|
81
82
|
if @options and @options[:decode_values]
|
82
83
|
return decode_values
|
83
84
|
end
|
85
|
+
if @options and @options[:convert_to_hash]
|
86
|
+
return convert_to_hash(return_value(return_nil_values, transformation))
|
87
|
+
end
|
84
88
|
return return_value(return_nil_values, transformation)
|
85
89
|
when :wait
|
86
90
|
index = raw.headers["x-consul-index"]
|
@@ -127,11 +131,13 @@ module Diplomat
|
|
127
131
|
# @param key [String] the key
|
128
132
|
# @param options [Hash] the query params
|
129
133
|
# @option options [String] :dc Target datacenter
|
134
|
+
# @option options [Boolean] :recurse If to make recursive get or not
|
130
135
|
# @return [OpenStruct]
|
131
136
|
def delete key, options=nil
|
132
137
|
@key = key
|
133
138
|
@options = options
|
134
139
|
url = ["/v1/kv/#{@key}"]
|
140
|
+
url += recurse_get(@options)
|
135
141
|
url += check_acl_token
|
136
142
|
url += dc(@options)
|
137
143
|
@raw = @conn.delete concat_url url
|
data/lib/diplomat/node.rb
CHANGED
data/lib/diplomat/rest_client.rb
CHANGED
@@ -43,9 +43,35 @@ module Diplomat
|
|
43
43
|
# @param *args Arguments list
|
44
44
|
# @return [Boolean]
|
45
45
|
def method_missing(meth_id, *args)
|
46
|
-
access_method?(meth_id)
|
46
|
+
if access_method?(meth_id)
|
47
|
+
new.send(meth_id, *args)
|
48
|
+
else
|
49
|
+
|
50
|
+
# See https://bugs.ruby-lang.org/issues/10969
|
51
|
+
begin
|
52
|
+
super
|
53
|
+
rescue NameError => err
|
54
|
+
raise NoMethodError, err
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# Make `respond_to?` aware of method short-cuts.
|
60
|
+
#
|
61
|
+
# @param meth_id [Symbol] the tested method
|
62
|
+
# @oaram with_private if private methods should be tested too
|
63
|
+
def respond_to?(meth_id, with_private = false)
|
64
|
+
access_method?(meth_id) || super
|
47
65
|
end
|
48
66
|
|
67
|
+
# Make `respond_to_missing` aware of method short-cuts. This is needed for
|
68
|
+
# {#method} to work on these, which is helpful for testing purposes.
|
69
|
+
#
|
70
|
+
# @param meth_id [Symbol] the tested method
|
71
|
+
# @oaram with_private if private methods should be tested too
|
72
|
+
def respond_to_missing?(meth_id, with_private = false)
|
73
|
+
access_method?(meth_id) || super
|
74
|
+
end
|
49
75
|
end
|
50
76
|
|
51
77
|
private
|
@@ -69,6 +95,44 @@ module Diplomat
|
|
69
95
|
end
|
70
96
|
end
|
71
97
|
|
98
|
+
#Converts k/v data into ruby hash
|
99
|
+
def convert_to_hash(data)
|
100
|
+
collection = []
|
101
|
+
master = {}
|
102
|
+
data.each do |item|
|
103
|
+
split_up = item[:key].split ?/
|
104
|
+
sub_hash = {}
|
105
|
+
temp = nil
|
106
|
+
real_size = split_up.size - 1
|
107
|
+
for i in 0..real_size do
|
108
|
+
if i == 0
|
109
|
+
temp = {}
|
110
|
+
sub_hash[split_up[i]] = temp
|
111
|
+
next
|
112
|
+
end
|
113
|
+
if i == real_size
|
114
|
+
temp[split_up[i]] = item[:value]
|
115
|
+
else
|
116
|
+
new_h = {}
|
117
|
+
temp[split_up[i]] = new_h
|
118
|
+
temp = new_h
|
119
|
+
end
|
120
|
+
end
|
121
|
+
collection << sub_hash
|
122
|
+
end
|
123
|
+
|
124
|
+
collection.each do |h|
|
125
|
+
n = deep_merge(master, h)
|
126
|
+
master = n
|
127
|
+
end
|
128
|
+
master
|
129
|
+
end
|
130
|
+
|
131
|
+
def deep_merge(first, second)
|
132
|
+
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
133
|
+
first.merge(second, &merger)
|
134
|
+
end
|
135
|
+
|
72
136
|
# Parse the body, apply it to the raw attribute
|
73
137
|
def parse_body
|
74
138
|
return JSON.parse(@raw.body) if @raw.status == 200
|
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: 1.
|
4
|
+
version: 1.1.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: 2016-
|
11
|
+
date: 2016-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|