lita-consul 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lita/handlers/consul.rb +38 -0
- data/lita-consul.gemspec +1 -1
- data/spec/lita/handlers/consul_spec.rb +24 -0
- 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: 4d9d08f566174b54aff99adc47f1b5ed4005f3ae
|
4
|
+
data.tar.gz: 7c38b9852453a70228a1883847ef46f442733ba9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0742652f2f71c7b74fc220251efc2d28a1300799399f41fd7fef900c1f2b68ba71f9504e4ad6f443a3db7ac836b06567c2ea580f4d0576a001396087b9c072fb
|
7
|
+
data.tar.gz: 8cdfe7ec5ffb5bd7f851a2ff7e0de3771ef6716541407b62918ab8d69434b2c35678ecb29ff69f2111423635b9857ad373b02da91237996ac16aa1015c5ae63f
|
data/lib/lita/handlers/consul.rb
CHANGED
@@ -11,9 +11,17 @@ module Lita
|
|
11
11
|
"consul get <key>" => "Return value for <key>"
|
12
12
|
}
|
13
13
|
|
14
|
+
route /^consul set ([a-zA-Z0-9\-\/_]+) ([a-zA-Z0-9\-\/_]+)/, :consul_set, command: true, help: {
|
15
|
+
"consul set <key> <value>" => "Set <value> for <key>"
|
16
|
+
}
|
17
|
+
|
14
18
|
def consul_get(response)
|
15
19
|
key = response.matches.first.first
|
16
20
|
|
21
|
+
value = get_key_value(key)
|
22
|
+
response.reply "#{key} = #{value}"
|
23
|
+
end
|
24
|
+
=begin
|
17
25
|
begin
|
18
26
|
resp = http.get("#{api_url}/kv/#{key}")
|
19
27
|
obj = MultiJson.load(resp.body)
|
@@ -27,8 +35,38 @@ module Lita
|
|
27
35
|
response.reply e.to_s
|
28
36
|
end
|
29
37
|
end
|
38
|
+
=end
|
39
|
+
|
40
|
+
def consul_set(response)
|
41
|
+
key = response.matches.first.first
|
42
|
+
value = response.matches.first.last
|
43
|
+
begin
|
44
|
+
resp = http.put("#{api_url}/kv/#{key}", value)
|
45
|
+
if resp.status == 200
|
46
|
+
value = get_key_value(key)
|
47
|
+
response.reply "#{key} = #{value}"
|
48
|
+
else
|
49
|
+
response.reply resp.body
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
30
53
|
|
31
54
|
private
|
55
|
+
|
56
|
+
def get_key_value(key)
|
57
|
+
begin
|
58
|
+
resp = http.get("#{api_url}/kv/#{key}")
|
59
|
+
obj = MultiJson.load(resp.body)
|
60
|
+
unless obj[0]["Value"].nil?
|
61
|
+
value = Base64.decode64(obj[0]["Value"])
|
62
|
+
"#{value}"
|
63
|
+
else
|
64
|
+
"null"
|
65
|
+
end
|
66
|
+
rescue Faraday::ConnectionFailed=> e
|
67
|
+
e.to_s
|
68
|
+
end
|
69
|
+
end
|
32
70
|
|
33
71
|
def api_url
|
34
72
|
host = "http://127.0.0.1"
|
data/lita-consul.gemspec
CHANGED
@@ -3,6 +3,7 @@ require "spec_helper"
|
|
3
3
|
describe Lita::Handlers::Consul, lita_handler: true do
|
4
4
|
describe 'lita routes' do
|
5
5
|
it { is_expected.to route_command('consul get mykey').to(:consul_get) }
|
6
|
+
it { is_expected.to route_command('consul set mykey myvalue').to(:consul_set) }
|
6
7
|
end
|
7
8
|
|
8
9
|
before do
|
@@ -40,6 +41,21 @@ describe Lita::Handlers::Consul, lita_handler: true do
|
|
40
41
|
}
|
41
42
|
}
|
42
43
|
|
44
|
+
let(:new_key_response) {
|
45
|
+
%{
|
46
|
+
[
|
47
|
+
{
|
48
|
+
"CreateIndex":67,
|
49
|
+
"ModifyIndex":67,
|
50
|
+
"LockIndex":0,
|
51
|
+
"Key":"myapp/config/url",
|
52
|
+
"Flags":0,
|
53
|
+
"Value":"d3d3LnRlc3QuY29t"
|
54
|
+
}
|
55
|
+
]
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
43
59
|
describe '#consul get' do
|
44
60
|
it 'return value for key' do
|
45
61
|
allow(response).to receive(:body).and_return(single_key_response)
|
@@ -52,4 +68,12 @@ describe Lita::Handlers::Consul, lita_handler: true do
|
|
52
68
|
expect(replies.last).to eq("mykey = null")
|
53
69
|
end
|
54
70
|
end
|
71
|
+
|
72
|
+
describe '#consul set' do
|
73
|
+
it 'set and return value for key' do
|
74
|
+
allow(response).to receive(:body).and_return(new_key_response)
|
75
|
+
send_command('consul set myapp/config/url www.test.com')
|
76
|
+
expect(replies.last).to eq("myapp/config/url = www.test.com")
|
77
|
+
end
|
78
|
+
end
|
55
79
|
end
|