brocade_vrouter 0.2.4 → 0.3.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: 8a9e6f6daa97e9d2323e40e618f41207b7a5a017
4
- data.tar.gz: 9a726c4cda9ff5d28704658e0bff8fb4ec649beb
3
+ metadata.gz: 9ebdaf31f73486d7a464dca110e6479a1815f62c
4
+ data.tar.gz: 49caa01e9e6198e5822578be281148dee31a4564
5
5
  SHA512:
6
- metadata.gz: 47a137fcd068a19e6759c843a5d9a2b59b3c517ba80f5e86f5b9ea2ae11bcbb11f53a44b1ab81c0cb118eda12cdf03a39f4cf2be6d38bee649117695104aca05
7
- data.tar.gz: 00bd2ce3da7b1d6ecd38c3fc9c8da4ae1d06b8f36dd4ae699697931b2717bb137a84070b0e63b4480f809e65e49c6f8fc92fc37cb38c02189a941a366ebd2329
6
+ metadata.gz: 908a44936e03163368a5b72694be50c839740d1a449eec4afe71239bd41797dddbb305a16385e6f0ab13b8051dd07141963c08c7cbfdaf84eaad69e423b21e9f
7
+ data.tar.gz: 692f9bd9506ae9c95e3de77c90ea82a2a1a7b382535b22f592f93c4054eb0632061f2b9dc8a0a6712b753935f1cb59a93fc4a74668f2a6a6f834e5eb16ad371f
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.2
4
- before_install: gem install bundler -v 1.11.2
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.12.5
@@ -5,6 +5,7 @@ module BrocadeVRouter
5
5
  autoload 'Client', 'brocade_vrouter/client'
6
6
  autoload 'Config', 'brocade_vrouter/config'
7
7
  autoload 'Configuration', 'brocade_vrouter/configuration'
8
+ autoload 'Configurator', 'brocade_vrouter/configurator'
8
9
  autoload 'PathBuilder', 'brocade_vrouter/path_builder'
9
10
 
10
11
  RequestError = Class.new StandardError
@@ -18,7 +18,7 @@ module BrocadeVRouter
18
18
 
19
19
  def configuration(raise_on_fail: nil, &blk)
20
20
  raise_on_fail = BrocadeVRouter.config.raise_on_fail if raise_on_fail.nil?
21
- @current_conf ||= Configuration.new connection, logger: logger
21
+ @current_conf ||= Configurator.new connection, logger: logger
22
22
  @current_conf.raise_on_fail = raise_on_fail
23
23
  block_given? ? @current_conf.perform(&blk) : @current_conf
24
24
  end
@@ -1,131 +1,57 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BrocadeVRouter
2
4
  class Configuration
3
- CONF_PATH = 'rest/conf'.freeze
4
-
5
- attr_reader :connection, :last_response, :logger
6
- attr_accessor :raise_on_fail
7
-
8
- def initialize(connection, base_path: nil, logger: nil, raise_on_fail: true)
9
- @connection = connection
10
- @base_path = base_path
11
- @logger = logger
12
- @raise_on_fail = raise_on_fail
13
- end
14
-
15
- def base_path
16
- @base_path ||=
17
- begin
18
- handle_request :post, CONF_PATH
19
- handle_response(connection.post "/#{CONF_PATH}").headers['location']
20
- end
21
- end
22
-
23
- def perform(&blk)
24
- @context = eval 'self', blk.binding
25
- instance_eval(&blk) if block_given?
26
- end
27
-
28
- def set(paths = nil, &blk)
29
- res = build_paths(paths, &blk).map do |p|
30
- path = "#{base_path}/set/#{p}"
31
- handle_request :put, path
32
- handle_response connection.put path
33
- end
34
-
35
- res.size > 1 ? res : res.first
36
- end
37
-
38
- def get(paths = nil, &blk)
39
- all_res = build_paths(paths, &blk).map do |p|
40
- path = "#{base_path}/#{p}"
41
- handle_request :get, path
42
- res = handle_response connection.get path
43
- res.success? ? MultiJson.load(res.body) : nil
44
- end
45
-
46
- all_res.size > 1 ? all_res : all_res.first
47
- end
48
-
49
- def delete(paths = nil, &blk)
50
- res = build_paths(paths, &blk).map do |p|
51
- path = "#{base_path}/delete/#{p}"
52
- handle_request :put, path
53
- handle_response connection.put path
54
- end
5
+ LTSEP = ' '.freeze
6
+ OBRKT = '{'.freeze
7
+ CBRKT = '}'.freeze
8
+ ARRAY_FIELDS = {
9
+ 'address' => 'address-group',
10
+ 'port' => 'port-group'
11
+ }.freeze
55
12
 
56
- res.size > 1 ? res : res.first
13
+ def initialize(data)
14
+ @conf = parse_lines! data.lines, []
57
15
  end
58
16
 
59
- def commit(path = nil)
60
- req_path = "#{path || base_path}/commit"
61
- handle_request :post, req_path
62
- handle_response connection.post req_path
63
- rescue RequestError => e
64
- raise unless e.message['No changes to commit']
65
- end
66
-
67
- def save(path = nil)
68
- req_path = "#{path || base_path}/save"
69
- handle_request :post, req_path
70
- handle_response connection.post req_path
71
- end
72
-
73
- def close(path = nil)
74
- handle_request :delete, path || base_path
75
- handle_response connection.delete(path || base_path)
76
- end
77
-
78
- def close_all_sessions!(commit: false, save: false)
79
- sessions.each do |session|
80
- path = "#{CONF_PATH}/#{session['id']}"
81
- commit path if commit
82
- save path if save
83
- close path
84
- end
85
- end
86
-
87
- def sessions
88
- (MultiJson.load (handle_response connection.get CONF_PATH).body)
89
- .fetch('session', [])
90
- end
91
-
92
- def raise_on_fail?
93
- @raise_on_fail
17
+ def to_h
18
+ @conf
94
19
  end
95
20
 
96
21
  private
97
22
 
98
- def handle_request(method, path)
99
- return unless logger
100
- logger.info "> #{method.to_s.upcase} #{connection.url_prefix}#{path}"
101
- end
102
-
103
- def handle_response(resp)
104
- @last_response = resp
105
-
106
- message = "< #{resp.env.method.upcase} #{resp.env.url} #{resp.env.status}\n#{resp.body}"
107
-
108
- if !resp.success?
109
- logger.warn message if logger
110
- raise RequestError.new message if raise_on_fail?
111
- else
112
- logger.info message if logger
23
+ def parse_lines!(lines, path)
24
+ conf = {}
25
+ while lines.any?
26
+ literals = lines.shift.split(LTSEP)
27
+ name = literals.shift
28
+ name = literals.shift if name == path.last
29
+
30
+ if name == CBRKT
31
+ return conf
32
+ elsif literals.last == OBRKT
33
+ if literals.first != OBRKT
34
+ conf[name] ||= {}
35
+ conf[name][literals.first] = parse_lines! lines, path + [name, literals.first]
36
+ else
37
+ conf[name] = parse_lines! lines, path + [name]
38
+ end
39
+ else
40
+ value = literals.join(LTSEP).tr('\'"', '')
41
+ if array_field?(path, name)
42
+ conf[name] ||= []
43
+ conf[name] << value
44
+ else
45
+ conf[name] = value
46
+ end
47
+ end
113
48
  end
114
49
 
115
- resp
116
- end
117
-
118
- def method_missing(name, value = nil, exec = false, &blk)
119
- PathBuilder.new(@context) { public_send name, value, exec, &blk }.to_a
50
+ conf
120
51
  end
121
52
 
122
- def build_paths(paths, &blk)
123
- paths = Array(paths)
124
- if block_given?
125
- paths.map { |p| PathBuilder.new(@context, p, &blk).to_a }.flatten
126
- else
127
- paths
128
- end
53
+ def array_field?(path, name)
54
+ ARRAY_FIELDS[name] && path.include?(ARRAY_FIELDS[name])
129
55
  end
130
56
  end
131
57
  end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrocadeVRouter
4
+ class Configurator
5
+ CONF_PATH = 'rest/conf'.freeze
6
+
7
+ attr_reader :connection, :last_response, :logger
8
+ attr_accessor :raise_on_fail
9
+
10
+ def initialize(connection, base_path: nil, logger: nil, raise_on_fail: true)
11
+ @connection = connection
12
+ @base_path = base_path
13
+ @logger = logger
14
+ @raise_on_fail = raise_on_fail
15
+ end
16
+
17
+ def base_path
18
+ @base_path ||=
19
+ begin
20
+ handle_request :post, CONF_PATH
21
+ handle_response(connection.post "/#{CONF_PATH}").headers['location']
22
+ end
23
+ end
24
+
25
+ def perform(&blk)
26
+ @context = eval 'self', blk.binding
27
+ instance_eval(&blk) if block_given?
28
+ end
29
+
30
+ def set(paths = nil, &blk)
31
+ res = build_paths(paths, &blk).map do |p|
32
+ path = "#{base_path}/set/#{p}"
33
+ handle_request :put, path
34
+ handle_response connection.put path
35
+ end
36
+
37
+ res.size > 1 ? res : res.first
38
+ end
39
+
40
+ def get
41
+ res = handle_response connection.post '/rest/op/show/configuration'
42
+ res = handle_response connection.get "/#{res.headers['location']}" if res.success?
43
+ Configuration.new(res.body).to_h if res.success?
44
+ end
45
+
46
+ def delete(paths = nil, &blk)
47
+ res = build_paths(paths, &blk).map do |p|
48
+ path = "#{base_path}/delete/#{p}"
49
+ handle_request :put, path
50
+ handle_response connection.put path
51
+ end
52
+
53
+ res.size > 1 ? res : res.first
54
+ end
55
+
56
+ def commit(path = nil)
57
+ req_path = "#{path || base_path}/commit"
58
+ handle_request :post, req_path
59
+ handle_response connection.post req_path
60
+ rescue RequestError => e
61
+ raise unless e.message['No changes to commit']
62
+ end
63
+
64
+ def save(path = nil)
65
+ req_path = "#{path || base_path}/save"
66
+ handle_request :post, req_path
67
+ handle_response connection.post req_path
68
+ end
69
+
70
+ def close(path = nil)
71
+ handle_request :delete, path || base_path
72
+ handle_response connection.delete(path || base_path)
73
+ end
74
+
75
+ def close_all_sessions!(commit: false, save: false)
76
+ sessions.each do |session|
77
+ path = "#{CONF_PATH}/#{session['id']}"
78
+ commit path if commit
79
+ save path if save
80
+ close path
81
+ end
82
+ end
83
+
84
+ def sessions
85
+ (MultiJson.load (handle_response connection.get CONF_PATH).body)
86
+ .fetch('session', [])
87
+ end
88
+
89
+ def raise_on_fail?
90
+ @raise_on_fail
91
+ end
92
+
93
+ private
94
+
95
+ def handle_request(method, path)
96
+ return unless logger
97
+ logger.info "> #{method.to_s.upcase} #{connection.url_prefix}#{path}"
98
+ end
99
+
100
+ def handle_response(resp)
101
+ @last_response = resp
102
+
103
+ message = "< #{resp.env.method.upcase} #{resp.env.url} #{resp.env.status}\n#{resp.body}"
104
+
105
+ if !resp.success?
106
+ logger.warn message if logger
107
+ raise RequestError.new message if raise_on_fail?
108
+ else
109
+ logger.info message if logger
110
+ end
111
+
112
+ resp
113
+ end
114
+
115
+ def method_missing(name, value = nil, exec = false, &blk)
116
+ PathBuilder.new(@context) { public_send name, value, exec, &blk }.to_a
117
+ end
118
+
119
+ def build_paths(paths, &blk)
120
+ paths = Array(paths)
121
+ if block_given?
122
+ paths.map { |p| PathBuilder.new(@context, p, &blk).to_a }.flatten
123
+ else
124
+ paths
125
+ end
126
+ end
127
+ end
128
+ end
@@ -1,3 +1,3 @@
1
1
  module BrocadeVRouter
2
- VERSION = '0.2.4'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brocade_vrouter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Kosmatov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-21 00:00:00.000000000 Z
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -146,6 +146,7 @@ files:
146
146
  - lib/brocade_vrouter/client.rb
147
147
  - lib/brocade_vrouter/config.rb
148
148
  - lib/brocade_vrouter/configuration.rb
149
+ - lib/brocade_vrouter/configurator.rb
149
150
  - lib/brocade_vrouter/path_builder.rb
150
151
  - lib/brocade_vrouter/version.rb
151
152
  homepage: https://github.com/serverscom/brocade_vrouter