Class: Rbeapi::Client::Config

Inherits:
IniFile
  • Object
show all
Defined in:
lib/rbeapi/client.rb

Overview

The Config class holds the loaded configuration file data. It is a subclass of IniFile.

Constant Summary collapse

CONFIG_SEARCH_PATH =
['/mnt/flash/eapi.conf']

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

The Config class will automatically search for a filename to load (if none provided) and load the data when the object is instantiated.

Parameters:

  • :filename (String)

    The full path to the filename to load. If the filename is not provided, then this class will attempt to find a valid conf file using the CONFIG_SEARCH_PATH.



168
169
170
171
172
# File 'lib/rbeapi/client.rb', line 168

def initialize(opts = {})
  super(parameter: ':')
  @filename = opts.fetch(:filename, nil)
  autoload
end

Instance Method Details

#add_connection(name, values) ⇒ Object

Adds a new connection section to the current configuration

Parameters:

  • :name (String)

    The name of the connection to add to the configuration.

  • :values (Hash)

    The properties for the connection



260
261
262
# File 'lib/rbeapi/client.rb', line 260

def add_connection(name, values)
  self["connection:#{name}"] = values
end

#get_connection(name) ⇒ nil, Hash<String, String> Returns a hash of the connection properties from the loaded config. This method will return nil if the connection name is not found.

Returns the configuration for the connection specified. If a connection is not found matching the name and if a default connection has been specified then return the default connection.

Parameters:

  • :name (String)

    The name of the connection to return from the configuration. This should be the string right of the : in the config section header

Returns:

  • (nil, Hash<String, String> Returns a hash of the connection properties from the loaded config. This method will return nil if the connection name is not found.)

    nil, Hash<String, String> Returns a hash of the connection properties from the loaded config. This method will return nil if the connection name is not found.



247
248
249
250
251
252
# File 'lib/rbeapi/client.rb', line 247

def get_connection(name)
  return self["connection:#{name}"] \
    if sections.include? "connection:#{name}"
  return self['connection:*'] if sections.include? 'connection:*'
  nil
end

#read(filename) ⇒ Object

This method will read the specified filename and load its contents into the instance. It will also add the default localhost entry if it doesn’t exist in the conf file

Parameters:

  • :filename (String)

    The full path to the filename to load



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rbeapi/client.rb', line 207

def read(filename)
  super(filename: filename)

  # For each section, if the host parameter is omitted then the
  # connection name is used
  sections.each do |name|
    if name.start_with?('connection:')
      conn = self["#{name}"]
      conn['host'] = name.split(':')[1] unless conn['host']
    end
  end

  return if get_connection 'localhost'
  add_connection('localhost', transport: 'socket')
end

#reload(opts = {}) ⇒ Object

This method will cause the config to be loaded. The process of finding the configuration will be repeated so it is possible a different conf file could be chosen if the original file was removed or a new file added higher on the search priority list

Parameters:

  • :opts (Hash)

    The options for specifying the message



231
232
233
# File 'lib/rbeapi/client.rb', line 231

def reload(opts = {})
  autoload opts
end