Class: Rbeapi::Client::Config
- Inherits:
-
IniFile
- Object
- IniFile
- Rbeapi::Client::Config
- 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'].freeze
Instance Method Summary collapse
-
#add_connection(name, values) ⇒ Object
Adds a new connection section to the current configuration.
-
#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.
-
#initialize(opts = {}) ⇒ Config
constructor
The Config class will automatically search for a filename to load (if none provided) and load the data when the object is instantiated.
-
#read(filename) ⇒ Object
This method will read the specified filename and load its contents into the instance.
-
#reload(opts = {}) ⇒ Object
This method will cause the config to be loaded.
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.
180 181 182 183 184 |
# File 'lib/rbeapi/client.rb', line 180 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.
283 284 285 286 |
# File 'lib/rbeapi/client.rb', line 283 def add_connection(name, values) self["connection:#{name}"] = values nil 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.
269 270 271 272 273 274 |
# 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 |
#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.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# 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 |
#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.
253 254 255 |
# File 'lib/rbeapi/client.rb', line 253 def reload(opts = {}) autoload opts end |