diplomat 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,27 +1,28 @@
1
- require 'base64'
2
- require 'faraday'
3
-
4
1
  module Diplomat
2
+ # Methods for interacting with the Consul serivce API endpoint.
5
3
  class Service < Diplomat::RestClient
6
-
7
4
  include ApiOptions
8
5
 
9
- @access_methods = [ :get, :get_all, :register, :deregister, :register_external, :deregister_external ]
6
+ @access_methods = [:get, :get_all, :register, :deregister, :register_external, :deregister_external]
10
7
 
11
8
  # Get a service by it's key
12
9
  # @param key [String] the key
13
10
  # @param scope [Symbol] :first or :all results
14
- # @param options [Hash] :wait string for wait time and :index for index of last query
11
+ # @param options [Hash] options parameter hash
12
+ # @option wait [Integer] :wait string for wait time
13
+ # @option index [String] :index for index of last query
14
+ # @option dc [String] :dc data center to make request for
15
+ # @option tag [String] :tag service tag to get
15
16
  # @param meta [Hash] output structure containing header information about the request (index)
16
17
  # @return [OpenStruct] all data associated with the service
17
- def get key, scope=:first, options=nil, meta=nil
18
-
18
+ # rubocop:disable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
19
+ def get(key, scope = :first, options = nil, meta = nil)
19
20
  url = ["/v1/catalog/service/#{key}"]
20
21
  url += check_acl_token
21
- url << use_named_parameter('wait', options[:wait]) if options and options[:wait]
22
- url << use_named_parameter('index', options[:index]) if options and options[:index]
23
- url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
24
- url << use_named_parameter('tag', options[:tag]) if options and options[:tag]
22
+ url << use_named_parameter('wait', options[:wait]) if options && options[:wait]
23
+ url << use_named_parameter('index', options[:index]) if options && options[:index]
24
+ url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
25
+ url << use_named_parameter('tag', options[:tag]) if options && options[:tag]
25
26
 
26
27
  # If the request fails, it's probably due to a bad path
27
28
  # so return a PathNotFound error.
@@ -31,42 +32,43 @@ module Diplomat
31
32
  raise Diplomat::PathNotFound, e
32
33
  end
33
34
 
34
- if meta and ret.headers
35
- meta[:index] = ret.headers["x-consul-index"]
36
- meta[:knownleader] = ret.headers["x-consul-knownleader"]
37
- meta[:lastcontact] = ret.headers["x-consul-lastcontact"]
35
+ if meta && ret.headers
36
+ meta[:index] = ret.headers['x-consul-index']
37
+ meta[:knownleader] = ret.headers['x-consul-knownleader']
38
+ meta[:lastcontact] = ret.headers['x-consul-lastcontact']
38
39
  end
39
40
 
40
41
  if scope == :all
41
- return JSON.parse(ret.body).map { |service| OpenStruct.new service }
42
+ JSON.parse(ret.body).map { |service| OpenStruct.new service }
43
+ else
44
+ OpenStruct.new JSON.parse(ret.body).first
42
45
  end
43
-
44
- return OpenStruct.new JSON.parse(ret.body).first
45
46
  end
47
+ # rubocop:enable PerceivedComplexity, MethodLength, CyclomaticComplexity, AbcSize
46
48
 
47
49
  # Get all the services
48
50
  # @param options [Hash] :dc Consul datacenter to query
49
51
  # @return [OpenStruct] the list of all services
50
- def get_all options=nil
51
- url = ["/v1/catalog/services"]
52
+ def get_all(options = nil)
53
+ url = ['/v1/catalog/services']
52
54
  url += check_acl_token
53
- url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
55
+ url << use_named_parameter('dc', options[:dc]) if options && options[:dc]
54
56
  begin
55
57
  ret = @conn.get concat_url url
56
58
  rescue Faraday::ClientError
57
59
  raise Diplomat::PathNotFound
58
60
  end
59
61
 
60
- return OpenStruct.new JSON.parse(ret.body)
62
+ OpenStruct.new JSON.parse(ret.body)
61
63
  end
62
64
 
63
65
  # Register a service
64
66
  # @param definition [Hash] Hash containing definition of service
65
67
  # @return [Boolean]
66
- def register(definition, path='/v1/agent/service/register')
68
+ def register(definition, path = '/v1/agent/service/register')
67
69
  json_definition = JSON.dump(definition)
68
70
  register = @conn.put path, json_definition
69
- return register.status == 200
71
+ register.status == 200
70
72
  end
71
73
 
72
74
  # De-register a service
@@ -74,7 +76,7 @@ module Diplomat
74
76
  # @return [Boolean]
75
77
  def deregister(service_name)
76
78
  deregister = @conn.get "/v1/agent/service/deregister/#{service_name}"
77
- return deregister.status == 200
79
+ deregister.status == 200
78
80
  end
79
81
 
80
82
  # Register an external service
@@ -90,7 +92,7 @@ module Diplomat
90
92
  def deregister_external(definition)
91
93
  json_definition = JSON.dump(definition)
92
94
  deregister = @conn.put '/v1/catalog/deregister', json_definition
93
- return deregister.status == 200
95
+ deregister.status == 200
94
96
  end
95
97
  end
96
98
  end
@@ -1,52 +1,99 @@
1
- require 'faraday'
2
-
3
1
  module Diplomat
2
+ # Methods for interacting with the Consul session API endpoint
4
3
  class Session < Diplomat::RestClient
5
-
6
- @access_methods = [ :create, :destroy, :list, :renew ]
4
+ @access_methods = [:create, :destroy, :list, :renew, :info, :node]
7
5
 
8
6
  # Create a new session
9
7
  # @param value [Object] hash or json representation of the session arguments
8
+ # @param options [Hash] session options
9
+ # @param options [String] :dc datacenter to create session for
10
10
  # @return [String] The sesssion id
11
- def create value=nil
11
+ def create(value = nil, options = nil)
12
12
  # TODO: only certain keys are recognised in a session create request,
13
13
  # should raise an error on others.
14
14
  raw = @conn.put do |req|
15
- req.url "/v1/session/create"
16
- req.body = (if value.kind_of?(String) then value else JSON.generate(value) end) unless value.nil?
15
+ url = ['/v1/session/create']
16
+ url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
17
+
18
+ req.url concat_url url
19
+ req.body = (value.is_a?(String) ? value : JSON.generate(value)) unless value.nil?
17
20
  end
18
21
  body = JSON.parse(raw.body)
19
- return body["ID"]
22
+ body['ID']
20
23
  end
21
24
 
22
25
  # Destroy a session
23
26
  # @param id [String] session id
24
- # @return [nil]
25
- def destroy id
27
+ # @param options [Hash] session options
28
+ # @param options [String] :dc datacenter to destroy session for
29
+ # @return [String] Success or failure of the session destruction
30
+ def destroy(id, options = nil)
26
31
  raw = @conn.put do |req|
27
- req.url "/v1/session/destroy/#{id}"
32
+ url = ["/v1/session/destroy/#{id}"]
33
+ url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
34
+
35
+ req.url concat_url url
28
36
  end
29
- return raw.body
37
+ raw.body
30
38
  end
31
-
39
+
32
40
  # List sessions
33
- # @return [Struct]
34
- def list
41
+ # @param options [Hash] session options
42
+ # @param options [String] :dc datacenter to list sessions
43
+ # @return [OpenStruct]
44
+ def list(options = nil)
35
45
  raw = @conn.get do |req|
36
- req.url "/v1/session/list"
46
+ url = ['/v1/session/list']
47
+ url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
48
+
49
+ req.url concat_url url
37
50
  end
38
- JSON.parse(raw.body)
51
+ JSON.parse(raw.body).map { |session| OpenStruct.new session }
39
52
  end
40
-
53
+
41
54
  # Renew session
42
55
  # @param id [String] session id
43
- # @return [Struct]
44
-
45
- def renew id
56
+ # @param options [Hash] session options
57
+ # @param options [String] :dc datacenter to renew session for
58
+ # @return [OpenStruct]
59
+ def renew(id, options = nil)
46
60
  raw = @conn.put do |req|
47
- req.url "/v1/session/renew/#{id}"
61
+ url = ["/v1/session/renew/#{id}"]
62
+ url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
63
+
64
+ req.url concat_url url
65
+ end
66
+ JSON.parse(raw.body).map { |session| OpenStruct.new session }
67
+ end
68
+
69
+ # Session information
70
+ # @param id [String] session id
71
+ # @param options [Hash] session options
72
+ # @param options [String] :dc datacenter to renew session for
73
+ # @return [OpenStruct]
74
+ def info(id, options = nil)
75
+ raw = @conn.get do |req|
76
+ url = ["/v1/session/info/#{id}"]
77
+ url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
78
+
79
+ req.url concat_url url
80
+ end
81
+ JSON.parse(raw.body).map { |session| OpenStruct.new session }
82
+ end
83
+
84
+ # Session information for a given node
85
+ # @param name [String] node name
86
+ # @param options [Hash] session options
87
+ # @param options [String] :dc datacenter to renew session for
88
+ # @return [OpenStruct]
89
+ def node(name, options = nil)
90
+ raw = @conn.get do |req|
91
+ url = ["/v1/session/node/#{name}"]
92
+ url += use_named_parameter('dc', options[:dc]) if options && options[:dc]
93
+
94
+ req.url concat_url url
48
95
  end
49
- JSON.parse(raw.body)
96
+ JSON.parse(raw.body).map { |session| OpenStruct.new session }
50
97
  end
51
98
  end
52
99
  end
@@ -1,3 +1,3 @@
1
1
  module Diplomat
2
- VERSION = "1.1.0"
2
+ VERSION = '1.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diplomat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hamelink
8
+ - Trevor Wood
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2016-11-08 00:00:00.000000000 Z
12
+ date: 2017-01-23 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -28,16 +29,16 @@ dependencies:
28
29
  name: rake
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
- - - "~>"
32
+ - - ">="
32
33
  - !ruby/object:Gem::Version
33
- version: '10.3'
34
+ version: '0'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
- - - "~>"
39
+ - - ">="
39
40
  - !ruby/object:Gem::Version
40
- version: '10.3'
41
+ version: '0'
41
42
  - !ruby/object:Gem::Dependency
42
43
  name: pry
43
44
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +137,20 @@ dependencies:
136
137
  - - "~>"
137
138
  - !ruby/object:Gem::Version
138
139
  version: '2.0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
139
154
  - !ruby/object:Gem::Dependency
140
155
  name: json
141
156
  requirement: !ruby/object:Gem::Requirement
@@ -167,6 +182,7 @@ dependencies:
167
182
  description: Diplomat is a simple wrapper for Consul
168
183
  email:
169
184
  - john@johnhamelink.com
185
+ - trevor.g.wood@gmail.com
170
186
  executables: []
171
187
  extensions: []
172
188
  extra_rdoc_files: []
@@ -191,11 +207,12 @@ files:
191
207
  - lib/diplomat/members.rb
192
208
  - lib/diplomat/node.rb
193
209
  - lib/diplomat/nodes.rb
210
+ - lib/diplomat/query.rb
194
211
  - lib/diplomat/rest_client.rb
195
212
  - lib/diplomat/service.rb
196
213
  - lib/diplomat/session.rb
197
214
  - lib/diplomat/version.rb
198
- homepage: https://github.com/johnhamelink/diplomat
215
+ homepage: https://github.com/WeAreFarmGeek/diplomat
199
216
  licenses:
200
217
  - BSD-3-Clause
201
218
  metadata: {}
@@ -215,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
215
232
  version: '0'
216
233
  requirements: []
217
234
  rubyforge_project:
218
- rubygems_version: 2.5.1
235
+ rubygems_version: 2.6.8
219
236
  signing_key:
220
237
  specification_version: 4
221
238
  summary: Diplomat is a simple wrapper for Consul