diplomat 0.14.0 → 0.15.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd8fd2de1577af47ba483ec661d17065898e0347
4
- data.tar.gz: 67472f8f9b059e34acb2da7565416545a16a4150
3
+ metadata.gz: ce6e11c99998e8ccde4674d2f25b1c65a7dadcb6
4
+ data.tar.gz: 0ed7b4a14cedf79aed1f0856b60f8eb96744adbe
5
5
  SHA512:
6
- metadata.gz: fc1c47f0f2a6fd0ef1d8d368ff476c2943b7ac28d0ea8baa69cedd18e22ba78131196b8b6867c0ab8debfcd6b5ee84d5e26b9065a994bb11148e17c6bc91d435
7
- data.tar.gz: c6c453c1bebd64a5354304820e3946abb3f98ff3974e0ba15cb9c4c9939b5016a2dcc6bda78bbaf17f7d2c3d547f1db1ac0b3de1d07d3e04304256645137f45f
6
+ metadata.gz: dae2bbf4adfdcda941ce8d9ad5b09868dfe7abbd957122e217159fc27d15f585201edfd8de4ac570fb0e28baf284352ba9c4e4a750808b9e95b8149e074eaf80
7
+ data.tar.gz: 4a5b3fb941c350acc1a2272727fb4eaefe63df2c5814bca82fd9dc0f889acf6709aa85b1fb531431d2c092b96cff766d48ae5fe392c6f6c84a8d81e659d191e8
data/README.md CHANGED
@@ -82,6 +82,24 @@ Diplomat::Kv.get('foo/', recurse: true)
82
82
  # => [{:key=>"foo/a", :value=>"lorem"}, {:key=>"foo/b", :value=>"ipsum"}, {:key=>"foo/c", :value=>"dolor"}]
83
83
  ```
84
84
 
85
+ ### Nodes
86
+
87
+ #### Getting
88
+
89
+ Look up a node:
90
+
91
+ ```ruby
92
+ foo_service = Diplomat::Node.get('foo')
93
+ # => {"Node"=>{"Node"=>"foobar", "Address"=>"10.1.10.12"}, "Services"=>{"consul"=>{"ID"=>"consul", "Service"=>"consul", "Tags"=>nil, "Port"=>8300}, "redis"=>{"ID"=>"redis", "Service"=>"redis", "Tags"=>["v1"], "Port"=>8000}}}
94
+ ```
95
+
96
+ Get all nodes:
97
+
98
+ ```ruby
99
+ nodes = Diplomat::Node.get_all
100
+ # => [#<OpenStruct Address="10.1.10.12", Node="foo">, #<OpenStruct Address="10.1.10.13", Node="bar">]
101
+ ```
102
+
85
103
  ### Services
86
104
 
87
105
  #### Getting
@@ -0,0 +1,100 @@
1
+ require 'base64'
2
+ require 'faraday'
3
+
4
+ module Diplomat
5
+ class Acl < Diplomat::RestClient
6
+
7
+ include ApiOptions
8
+
9
+ @access_methods = [ :list, :info, :create, :destroy, :update ]
10
+ attr_reader :id, :type, :acl
11
+
12
+ # Get Acl info by ID
13
+ # @param id [String] ID of the Acl to get
14
+ # @return [Hash]
15
+ def info id, options=nil, not_found=:reject, found=:return
16
+ @id = id
17
+ @options = options
18
+
19
+ url = ["/v1/acl/info/#{@id}"]
20
+ url += check_acl_token
21
+ url += use_consistency(@options)
22
+
23
+ raw = @conn_no_err.get concat_url url
24
+ if raw.status == 200 and raw.body
25
+ case found
26
+ when :reject
27
+ raise Diplomat::AclAlreadyExists, @id
28
+ when :return
29
+ @raw = raw
30
+ return parse_body
31
+ end
32
+ elsif raw.status == 200 and !raw.body
33
+ case not_found
34
+ when :reject
35
+ raise Diplomat::AclNotFound, @id
36
+ when :return
37
+ return nil
38
+ end
39
+ else
40
+ raise Diplomat::UnknownStatus, "status #{raw.status}"
41
+ end
42
+ end
43
+
44
+ # List all Acls
45
+ # @return [List] list of [Hash] of Acls
46
+ def list
47
+ url = ["/v1/acl/list"]
48
+ url += check_acl_token
49
+ raw = @conn_no_err.get concat_url url
50
+
51
+ if raw.status == 200
52
+ @raw = raw
53
+ return parse_body
54
+ else
55
+ raise Diplomat::UnknownStatus, "status #{raw.status}"
56
+ end
57
+ end
58
+
59
+ # Update an Acl definition, create if not present
60
+ # @param value [Hash] Acl definition, ID field is mandatory
61
+ # @return [Hash] The result Acl
62
+ def update value
63
+ raise Diplomat::IdParameterRequired unless value['ID']
64
+
65
+ @raw = @conn.put do |req|
66
+ url = ["/v1/acl/update"]
67
+ url += check_acl_token
68
+ url += use_cas(@options)
69
+ req.url concat_url url
70
+ req.body = value.to_json
71
+ end
72
+ JSON.parse(@raw.body)
73
+ end
74
+
75
+ # Create an Acl definition
76
+ # @param value [Hash] Acl definition, ID field is mandatory
77
+ # @return [Hash] The result Acl
78
+ def create value
79
+ @raw = @conn.put do |req|
80
+ url = ["/v1/acl/create"]
81
+ url += check_acl_token
82
+ url += use_cas(@options)
83
+ req.url concat_url url
84
+ req.body = value.to_json
85
+ end
86
+ JSON.parse(@raw.body)
87
+ end
88
+
89
+ # Destroy an ACl token by its id
90
+ # @param ID [String] the Acl ID
91
+ # @return [Bool]
92
+ def destroy id
93
+ @id = id
94
+ url = ["/v1/acl/destroy/#{@id}"]
95
+ url += check_acl_token
96
+ @raw = @conn.put concat_url url
97
+ @raw.body == "true"
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,15 @@
1
+ module Diplomat
2
+ module ApiOptions
3
+ def check_acl_token
4
+ use_named_parameter("token", Diplomat.configuration.acl_token)
5
+ end
6
+
7
+ def use_cas(options)
8
+ if options then use_named_parameter("cas", options[:cas]) else [] end
9
+ end
10
+
11
+ def use_consistency(options)
12
+ if options && options[:consistency] then ["#{options[:consistency]}"] else [] end
13
+ end
14
+ end
15
+ end
@@ -2,7 +2,10 @@ module Diplomat
2
2
  class KeyNotFound < StandardError; end
3
3
  class PathNotFound < StandardError; end
4
4
  class KeyAlreadyExists < StandardError; end
5
+ class AclNotFound < StandardError; end
6
+ class AclAlreadyExists < StandardError; end
5
7
  class EventNotFound < StandardError; end
6
8
  class EventAlreadyExists < StandardError; end
7
9
  class UnknownStatus < StandardError; end
10
+ class IdParameterRequired < StandardError; end
8
11
  end
data/lib/diplomat/kv.rb CHANGED
@@ -3,6 +3,9 @@ require 'faraday'
3
3
 
4
4
  module Diplomat
5
5
  class Kv < Diplomat::RestClient
6
+
7
+ include ApiOptions
8
+
6
9
  @access_methods = [ :get, :put, :delete ]
7
10
  attr_reader :key, :value, :raw
8
11
 
@@ -12,6 +15,8 @@ module Diplomat
12
15
  # @option options [Boolean] :recurse If to make recursive get or not
13
16
  # @option options [String] :consistency The read consistency type
14
17
  # @option options [String] :dc Target datacenter
18
+ # @option options [Boolean] :keys Only return key names.
19
+ # @option options [String] :separator List only up to a given separator. Only applies when combined with :keys option.
15
20
  # @option options [Boolean] :nil_values If to return keys/dirs with nil values
16
21
  # @param not_found [Symbol] behaviour if the key doesn't exist;
17
22
  # :reject with exception, :return degenerate value, or :wait for it to appear
@@ -43,6 +48,8 @@ module Diplomat
43
48
  url += check_acl_token
44
49
  url += use_consistency(@options)
45
50
  url += dc(@options)
51
+ url += keys(@options)
52
+ url += separator(@options)
46
53
 
47
54
  return_nil_values = (@options and @options[:nil_values])
48
55
 
@@ -122,24 +129,20 @@ module Diplomat
122
129
 
123
130
  private
124
131
 
125
- def check_acl_token
126
- use_named_parameter("token", Diplomat.configuration.acl_token)
127
- end
128
-
129
- def use_cas(options)
130
- if options then use_named_parameter("cas", options[:cas]) else [] end
131
- end
132
-
133
- def use_consistency(options)
134
- if options && options[:consistency] then ["#{options[:consistency]}"] else [] end
135
- end
136
-
137
132
  def recurse_get(options)
138
- if options && options[:recurse] then ['recurse'] else [] end
133
+ if options && options[:recurse] == true then ['recurse'] else [] end
139
134
  end
140
135
 
141
136
  def dc(options)
142
137
  if options && options[:dc] then use_named_parameter("dc", options[:dc]) else [] end
143
138
  end
139
+
140
+ def keys(options)
141
+ if options && options[:keys] == true then ['keys'] else [] end
142
+ end
143
+
144
+ def separator(options)
145
+ if options && options[:separator] then use_named_parameter("separator", options[:separator]) else [] end
146
+ end
144
147
  end
145
148
  end
@@ -0,0 +1,41 @@
1
+ require 'base64'
2
+ require 'faraday'
3
+
4
+ module Diplomat
5
+ class Node < Diplomat::RestClient
6
+
7
+ @access_methods = [ :get, :get_all ]
8
+
9
+ # Get a node by it's key
10
+ # @param key [String] the key
11
+ # @param options [Hash] :dc string for dc specific query
12
+ # @return [OpenStruct] all data associated with the node
13
+ def get key, options=nil
14
+
15
+ url = ["/v1/catalog/node/#{key}"]
16
+ url << use_named_parameter('dc', options[:dc]) if options and options[:dc]
17
+
18
+ # If the request fails, it's probably due to a bad path
19
+ # so return a PathNotFound error.
20
+ begin
21
+ ret = @conn.get concat_url url
22
+ rescue Faraday::ClientError
23
+ raise Diplomat::PathNotFound
24
+ end
25
+ return JSON.parse(ret.body)
26
+ end
27
+
28
+ # Get all the nodes
29
+ # @return [OpenStruct] the list of all nodes
30
+ def get_all
31
+ url = "/v1/catalog/nodes"
32
+ begin
33
+ ret = @conn.get url
34
+ rescue Faraday::ClientError
35
+ raise Diplomat::PathNotFound
36
+ end
37
+
38
+ return JSON.parse(ret.body).map { |service| OpenStruct.new service }
39
+ end
40
+ end
41
+ end
@@ -6,6 +6,7 @@ module Diplomat
6
6
  @access_methods = [ :get ]
7
7
 
8
8
  # Get all nodes
9
+ # @deprecated Please use Diplomat::Node instead.
9
10
  # @return [OpenStruct] all data associated with the nodes in catalog
10
11
  def get
11
12
  ret = @conn.get "/v1/catalog/nodes"
@@ -76,6 +76,7 @@ module Diplomat
76
76
 
77
77
  # Get the key/value(s) from the raw output
78
78
  def return_value(nil_values=false)
79
+ return @value = @raw if @raw.first.is_a? String
79
80
  if @raw.count == 1
80
81
  @value = @raw.first["Value"]
81
82
  @value = Base64.decode64(@value) unless @value.nil?
@@ -1,3 +1,3 @@
1
1
  module Diplomat
2
- VERSION = "0.14.0"
2
+ VERSION = "0.15.0"
3
3
  end
data/lib/diplomat.rb CHANGED
@@ -21,7 +21,9 @@ 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", "datacenter", "service", "members", "nodes", "check", "health", "session", "lock", "error", "event"
24
+ require_libs "configuration", "rest_client", "api_options", "kv", "datacenter",
25
+ "service", "members", "node", "nodes", "check", "health", "session", "lock",
26
+ "error", "event", "acl"
25
27
  self.configuration ||= Diplomat::Configuration.new
26
28
 
27
29
  class << self
@@ -45,4 +47,4 @@ module Diplomat
45
47
  Diplomat::Kv.new.send(name, *args, &block)
46
48
  end
47
49
  end
48
- end
50
+ end
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.14.0
4
+ version: 0.15.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-10-05 00:00:00.000000000 Z
11
+ date: 2015-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -177,6 +177,8 @@ files:
177
177
  - features/step_definitions/setup_diplomat.rb
178
178
  - features/step_definitions/test_key_value.rb
179
179
  - lib/diplomat.rb
180
+ - lib/diplomat/acl.rb
181
+ - lib/diplomat/api_options.rb
180
182
  - lib/diplomat/check.rb
181
183
  - lib/diplomat/configuration.rb
182
184
  - lib/diplomat/datacenter.rb
@@ -186,6 +188,7 @@ files:
186
188
  - lib/diplomat/kv.rb
187
189
  - lib/diplomat/lock.rb
188
190
  - lib/diplomat/members.rb
191
+ - lib/diplomat/node.rb
189
192
  - lib/diplomat/nodes.rb
190
193
  - lib/diplomat/rest_client.rb
191
194
  - lib/diplomat/service.rb