Class: BrocadeVRouter::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/brocade_vrouter/configurator.rb

Constant Summary collapse

CONF_PATH =
'rest/conf'.freeze
SHOW_CONF_CMD =
'configuration'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, base_path: nil, logger: nil, raise_on_fail: true) ⇒ Configurator

Returns a new instance of Configurator.



11
12
13
14
15
16
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, value = nil, exec = false, &blk) ⇒ Object (private)



124
125
126
# 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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/brocade_vrouter/configurator.rb', line 8

def connection
  @connection
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



8
9
10
# File 'lib/brocade_vrouter/configurator.rb', line 8

def last_response
  @last_response
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/brocade_vrouter/configurator.rb', line 8

def logger
  @logger
end

#raise_on_failObject

Returns the value of attribute raise_on_fail.



9
10
11
# File 'lib/brocade_vrouter/configurator.rb', line 9

def raise_on_fail
  @raise_on_fail
end

Instance Method Details

#base_pathObject



18
19
20
21
22
23
24
# 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) ⇒ Object



79
80
81
82
# 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) ⇒ Object



84
85
86
87
88
89
90
91
# 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) ⇒ Object



65
66
67
68
69
70
71
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object



26
27
28
29
# 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?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/brocade_vrouter/configurator.rb', line 98

def raise_on_fail?
  @raise_on_fail
end

#save(path = nil) ⇒ Object



73
74
75
76
77
# 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

#sessionsObject



93
94
95
96
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
# 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