diplomat 0.8.2 → 0.8.3
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/event.rb +30 -23
- data/lib/diplomat/rest_client.rb +6 -10
- data/lib/diplomat/service.rb +8 -20
- data/lib/diplomat/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbf402f2ee123f9bd5695a8faa1d1419cf504c7e
|
4
|
+
data.tar.gz: 183ea346004b5658c96dfdd9c7a023bd4a39a685
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dfd0b88b712187fe1ad8f025b410e687c6b655c60ded8e448f43d0a586ce0ea030387355f3c45f9b569b15956bc595843ca63a90c2a9fb67d0352aad245b6cf
|
7
|
+
data.tar.gz: e92caa5c03cf316657b98b3c431077e456326d574715f183ba91c084cfcc542dc81cc260b2532193e5c419b522b87facb3dc4cf1c3cd55dac92efe77a871a917
|
data/lib/diplomat/event.rb
CHANGED
@@ -52,15 +52,14 @@ module Diplomat
|
|
52
52
|
def get_all name=nil, not_found=:reject, found=:return
|
53
53
|
url = ["/v1/event/list"]
|
54
54
|
url += use_named_parameter("name", name)
|
55
|
+
url = concat_url url
|
55
56
|
|
56
57
|
# Event list never returns 404 or blocks, but may return an empty list
|
57
|
-
@raw = @conn.get
|
58
|
+
@raw = @conn.get url
|
58
59
|
if JSON.parse(@raw.body).count == 0
|
59
60
|
case not_found
|
60
61
|
when :reject
|
61
62
|
raise Diplomat::EventNotFound, name
|
62
|
-
when :wait
|
63
|
-
index = @raw.headers["x-consul-index"]
|
64
63
|
end
|
65
64
|
else
|
66
65
|
case found
|
@@ -69,17 +68,10 @@ module Diplomat
|
|
69
68
|
when :return
|
70
69
|
parse_body
|
71
70
|
return return_payload
|
72
|
-
when :wait
|
73
|
-
index = @raw.headers["x-consul-index"]
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|
77
|
-
|
78
|
-
url += use_named_parameter("index", index)
|
79
|
-
@raw = @conn.get do |req|
|
80
|
-
req.url concat_url url
|
81
|
-
req.options.timeout = 86400
|
82
|
-
end
|
74
|
+
@raw = wait_for_next_event(url)
|
83
75
|
parse_body
|
84
76
|
return_payload
|
85
77
|
end
|
@@ -107,7 +99,8 @@ module Diplomat
|
|
107
99
|
def get name=nil, token=:last, not_found=:wait, found=:return
|
108
100
|
url = ["/v1/event/list"]
|
109
101
|
url += use_named_parameter("name", name)
|
110
|
-
|
102
|
+
url = concat_url url
|
103
|
+
@raw = @conn.get url
|
111
104
|
body = JSON.parse(@raw.body)
|
112
105
|
# TODO: deal with unknown symbols, invalid indices (find_index will return nil)
|
113
106
|
idx = case token
|
@@ -121,15 +114,11 @@ module Diplomat
|
|
121
114
|
when :reject
|
122
115
|
raise Diplomat::EventNotFound, name
|
123
116
|
when :wait
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
req.options.timeout = 86400
|
130
|
-
end
|
131
|
-
body = JSON.parse(@raw.body)
|
132
|
-
event = body.last # If it's possible for two events to arrive at once, this needs to #find again
|
117
|
+
@raw = wait_for_next_event(url)
|
118
|
+
parse_body
|
119
|
+
# If it's possible for two events to arrive at once,
|
120
|
+
# this needs to #find again:
|
121
|
+
event = @raw.last
|
133
122
|
end
|
134
123
|
else
|
135
124
|
case found
|
@@ -140,8 +129,14 @@ module Diplomat
|
|
140
129
|
end
|
141
130
|
end
|
142
131
|
|
143
|
-
{
|
144
|
-
:
|
132
|
+
{
|
133
|
+
:value => {
|
134
|
+
:name => event["Name"],
|
135
|
+
:payload => Base64.decode64(event["Payload"])
|
136
|
+
},
|
137
|
+
:token => event["ID"]
|
138
|
+
}
|
139
|
+
|
145
140
|
end
|
146
141
|
|
147
142
|
|
@@ -159,5 +154,17 @@ module Diplomat
|
|
159
154
|
def self.get *args
|
160
155
|
Diplomat::Event.new.get *args
|
161
156
|
end
|
157
|
+
|
158
|
+
private
|
159
|
+
|
160
|
+
def wait_for_next_event url
|
161
|
+
index = @raw.headers["x-consul-index"]
|
162
|
+
url = [url, use_named_parameter("index", index)].join("&")
|
163
|
+
return @conn.get do |req|
|
164
|
+
req.url concat_url url
|
165
|
+
req.options.timeout = 86400
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
162
169
|
end
|
163
170
|
end
|
data/lib/diplomat/rest_client.rb
CHANGED
@@ -25,19 +25,15 @@ module Diplomat
|
|
25
25
|
# Build the API Client
|
26
26
|
# @param api_connection [Faraday::Connection,nil] supply mock API Connection
|
27
27
|
def start_connection api_connection=nil
|
28
|
-
@conn = api_connection
|
29
|
-
|
30
|
-
|
31
|
-
faraday.response :raise_error
|
32
|
-
|
33
|
-
Diplomat.configuration.middleware.each do |middleware|
|
34
|
-
faraday.use middleware
|
35
|
-
end
|
36
|
-
end
|
28
|
+
@conn = build_connection(api_connection)
|
29
|
+
@conn_no_err = build_connection(api_connection, true)
|
30
|
+
end
|
37
31
|
|
38
|
-
|
32
|
+
def build_connection(api_connection, raise_error=false)
|
33
|
+
return api_connection || Faraday.new(:url => Diplomat.configuration.url) do |faraday|
|
39
34
|
faraday.adapter Faraday.default_adapter
|
40
35
|
faraday.request :url_encoded
|
36
|
+
faraday.response :raise_error if raise_error
|
41
37
|
|
42
38
|
Diplomat.configuration.middleware.each do |middleware|
|
43
39
|
faraday.use middleware
|
data/lib/diplomat/service.rb
CHANGED
@@ -3,8 +3,6 @@ require 'faraday'
|
|
3
3
|
|
4
4
|
module Diplomat
|
5
5
|
class Service < Diplomat::RestClient
|
6
|
-
REGISTER_URL = '/v1/agent/service/register'
|
7
|
-
DEREGISTER_URL = '/v1/agent/service/deregister'
|
8
6
|
|
9
7
|
# Get a service by it's key
|
10
8
|
# @param key [String] the key
|
@@ -14,23 +12,12 @@ module Diplomat
|
|
14
12
|
# @return [OpenStruct] all data associated with the service
|
15
13
|
def get key, scope=:first, options=nil, meta=nil
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
if options and options[:
|
20
|
-
|
21
|
-
sep = "&"
|
22
|
-
end
|
23
|
-
if options and options[:index]
|
24
|
-
qs = "#{qs}#{sep}index=#{options[:index]}"
|
25
|
-
sep = "&"
|
26
|
-
end
|
27
|
-
if options and options[:dc]
|
28
|
-
qs = "#{qs}#{sep}dc=#{options[:dc]}"
|
29
|
-
sep = "&"
|
30
|
-
end
|
15
|
+
url = ["/v1/catalog/service/#{key}"]
|
16
|
+
url << use_named_parameter('wait', options[:wait]) if options and options[:wait]
|
17
|
+
url << use_named_parameter('index', options[:index]) if options and options[:index]
|
18
|
+
url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
|
31
19
|
|
32
|
-
|
33
|
-
ret = @conn.get "/v1/catalog/service/#{key}#{qs}"
|
20
|
+
ret = @conn.get concat_url url
|
34
21
|
|
35
22
|
if meta and ret.headers
|
36
23
|
meta[:index] = ret.headers["x-consul-index"]
|
@@ -41,6 +28,7 @@ module Diplomat
|
|
41
28
|
if scope == :all
|
42
29
|
return JSON.parse(ret.body).map { |service| OpenStruct.new service }
|
43
30
|
end
|
31
|
+
|
44
32
|
return OpenStruct.new JSON.parse(ret.body).first
|
45
33
|
end
|
46
34
|
|
@@ -49,7 +37,7 @@ module Diplomat
|
|
49
37
|
# @return [Boolean]
|
50
38
|
def register(definition)
|
51
39
|
json_definition = JSON.dump(definition)
|
52
|
-
register = @conn.put
|
40
|
+
register = @conn.put '/v1/agent/service/register', json_definition
|
53
41
|
return register.status == 200
|
54
42
|
end
|
55
43
|
|
@@ -57,7 +45,7 @@ module Diplomat
|
|
57
45
|
# @param service_name [String] Service name to de-register
|
58
46
|
# @return [Boolean]
|
59
47
|
def deregister(service_name)
|
60
|
-
deregister = @conn.get "
|
48
|
+
deregister = @conn.get "/v1/agent/service/deregister/#{service_name}"
|
61
49
|
return deregister.status == 200
|
62
50
|
end
|
63
51
|
|
data/lib/diplomat/version.rb
CHANGED