class BrocadeVRouter::Configurator
Constants
- CONF_PATH
- SHOW_CONF_CMD
Attributes
connection[R]
last_response[R]
logger[R]
raise_on_fail[RW]
Public Class Methods
new(connection, base_path: nil, logger: nil, raise_on_fail: true)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 11 def initialize(connection, base_path: nil, logger: nil, raise_on_fail: true) @connection = connection @base_path = base_path @logger = logger @raise_on_fail = raise_on_fail end
Public Instance Methods
base_path()
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 18 def base_path @base_path ||= begin handle_request :post, CONF_PATH handle_response(connection.post "/#{CONF_PATH}").headers['location'] end end
close(path = nil)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 79 def close(path = nil) handle_request :delete, path || base_path handle_response connection.delete(path || base_path) end
close_all_sessions!(commit: false, save: false)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 84 def close_all_sessions!(commit: false, save: false) sessions.each do |session| path = "#{CONF_PATH}/#{session['id']}" commit path if commit save path if save close path end end
commit(path = nil)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 65 def commit(path = nil) req_path = "#{path || base_path}/commit" handle_request :post, req_path handle_response connection.post req_path rescue RequestError => e raise unless e.message['No changes to commit'] end
delete(paths = nil, &blk)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 55 def delete(paths = nil, &blk) res = build_paths(paths, &blk).map do |p| path = "#{base_path}/delete/#{p}" handle_request :put, path handle_response connection.put path end res.size > 1 ? res : res.first end
get(cmd = SHOW_CONF_CMD)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 41 def get(cmd = SHOW_CONF_CMD) path = "rest/op/show/#{cmd}" handle_request :post, path resp = handle_response connection.post "/#{path}" return unless resp.success? path = "#{resp.headers['location']}" handle_request :get, path resp = handle_response connection.get "/#{path}" resp.success? && cmd == SHOW_CONF_CMD ? Configuration.new(resp.body).to_h : resp.body end
perform(&blk)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 26 def perform(&blk) @context = eval 'self', blk.binding instance_eval(&blk) if block_given? end
raise_on_fail?()
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 98 def raise_on_fail? @raise_on_fail end
save(path = nil)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 73 def save(path = nil) req_path = "#{path || base_path}/save" handle_request :post, req_path handle_response connection.post req_path end
sessions()
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 93 def sessions (MultiJson.load (handle_response connection.get CONF_PATH).body) .fetch('session', []) end
set(paths = nil, &blk)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 31 def set(paths = nil, &blk) res = build_paths(paths, &blk).map do |p| path = "#{base_path}/set/#{p}" handle_request :put, path handle_response connection.put path end res.size > 1 ? res : res.first end
Private Instance Methods
build_paths(paths, &blk)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 128 def build_paths(paths, &blk) paths = Array(paths) return paths unless block_given? paths.map { |p| PathBuilder.new(@context, p, &blk).to_a }.flatten end
handle_request(method, path)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 104 def handle_request(method, path) return unless logger logger.info "> #{method.to_s.upcase} #{connection.url_prefix}#{path}" end
handle_response(resp)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 109 def handle_response(resp) @last_response = resp message = "< #{resp.env.method.upcase} #{resp.env.url} #{resp.env.status}\n#{resp.body}" if resp.success? logger.info message if logger else logger.warn message if logger raise RequestError.new message if raise_on_fail? end resp end
method_missing(name, value = nil, exec = false, &blk)
click to toggle source
# File lib/brocade_vrouter/configurator.rb, line 124 def method_missing(name, value = nil, exec = false, &blk) PathBuilder.new(@context) { public_send name, value, exec, &blk }.to_a end