class Rbeapi::Client::Config
The Config
class holds the loaded configuration file data. It is a subclass of IniFile.
Constants
- CONFIG_SEARCH_PATH
Public Class Methods
The Config
class will automatically search for a filename to load (if none provided) and load the data when the object is instantiated.
@param opts [Hash] The initialization parameters.
@option opts 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.
# File lib/rbeapi/client.rb, line 180 def initialize(opts = {}) super(parameter: ':') @filename = opts.fetch(:filename, nil) autoload end
Public Instance Methods
Adds a new connection section to the current configuration.
@param name [String] The name of the connection to add to the
configuration.
@param values [Hash] The properties for the connection.
# File lib/rbeapi/client.rb, line 283 def add_connection(name, values) self["connection:#{name}"] = values nil end
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.
@param 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.
@return [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.
# File lib/rbeapi/client.rb, line 269 def get_connection(name) return self["connection:#{name}"] \ if sections.include? "connection:#{name}" return self['connection:*'] if sections.include? 'connection:*' nil end
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.
@param filename [String] The full path to the filename to load.
# File lib/rbeapi/client.rb, line 220 def read(filename) begin super(filename: filename) rescue IniFile::Error => exc Rbeapi::Utils.syslog_warning("#{exc}: in eapi conf file: #{filename}") return end # For each section, if the host parameter is omitted then the # connection name is used. has_default = has_section?('DEFAULT') sections.each do |name| next unless name.start_with?('connection:') conn = self[name.to_s] conn['host'] = name.split(':')[1] unless conn['host'] # Merge in the default values into the connections conn.merge!(self['DEFAULT']) { |_key, v1, _v2| v1 } if has_default end return if get_connection 'localhost' add_connection('localhost', transport: 'socket') end
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.
@param opts [Hash] The options for specifying the message.
@option opts filename [String] The full path to the file to load.
# File lib/rbeapi/client.rb, line 253 def reload(opts = {}) autoload opts end
Private Instance Methods
This private method automatically finds and loads the conf file into the instance using the class variable CONFIG_SEARCH_PATH
. The connections should be retrieved using the get_connection
method.
@param opts [Hash] The options for specifying the message.
@option opts filename [String] The full path to the filename
to load. Using this option eliminates the use of the search path.
# File lib/rbeapi/client.rb, line 196 def autoload(opts = {}) search_path = CONFIG_SEARCH_PATH.dup # Add the home directory path if the HOME environement var is defined. search_path.insert(0, '~/.eapi.conf') if ENV.key?('HOME') search_path.insert(0, ENV['EAPI_CONF']) if ENV.key?('EAPI_CONF') path = opts[:filename] || search_path path.each do |fn| fn = File.expand_path(fn) return read(fn) if File.exist?(fn) end return if get_connection 'localhost' add_connection('localhost', transport: 'socket') end