class Rbeapi::SwitchConfig::SwitchConfig
SwitchConfig
class
Attributes
Public Class Methods
The SwitchConfig
class will parse a string containing a switch configuration and return an instance of a SwitchConfig
. The SwitchConfig
contains the global section which contains references to all sub-sections (children).
{
global: <Section>,
}
@param config [String] A string containing the switch configuration.
@return [Section] Returns an instance of Section
# File lib/rbeapi/switchconfig.rb, line 213 def initialize(config) @indent = 3 @multiline_cmds = ['^banner', '^\s*ssl key', '^\s*ssl certificate', '^\s*protocol https certificate'] chk_format(config) parse(config) end
Public Instance Methods
Campare the current SwitchConfig
class with another SwitchConfig
class.
@param switch_config [SwitchConfig] An instance of a SwitchConfig
class to compare with the current instance.
@return [Array<Sections>] Returns an array of 2 Section
objects. The
first Section object contains the portion of the current SwitchConfig instance that is not in the passed in switch_config. The second Section object is the portion of the passed in switch_config that is not in the current SwitchConfig instance.
# File lib/rbeapi/switchconfig.rb, line 324 def compare(switch_config) @global.compare(switch_config.global) end
Private Instance Methods
Check format on a switch configuration string.
Verify that the indentation is correct on the switch configuration.
@param config [String] A string containing the switch configuration.
@return [boolean] Returns true if format is good, otherwise raises
an argument error.
# File lib/rbeapi/switchconfig.rb, line 230 def chk_format(config) skip = false config.each_line do |line| skip = true if @multiline_cmds.any? { |cmd| line =~ /#{cmd}/ } if skip if line =~ /^\s*EOF$/ # rubocop:disable Style/GuardClause skip = false else next end end ind = line[/\A */].size if (ind % @indent).nonzero? # rubocop:disable Style/Next raise ArgumentError, 'SwitchConfig indentation must be multiple '\ "of #{@indent} improper indent #{ind}: "\ "#{line}" end end true end
Parse a switch configuration into sections.
Parse a switch configuration and return a Config class. A switch configuration consists of the global section that contains a reference to all switch configuration sub-sections (children). Lines starting with '!' (comments) are ignored
@param config [String] A string containing the switch configuration. rubocop:disable Metrics/MethodLength
# File lib/rbeapi/switchconfig.rb, line 262 def parse(config) # Setup global section section = Section.new('', nil) @global = section prev_indent = 0 prev_line = '' combine = false longline = [] config.each_line do |line| if @multiline_cmds.any? { |cmd| line =~ /#{cmd}/ } longline = [] combine = true end if combine longline << line if line =~ /^\s*EOF$/ # rubocop:disable Style/GuardClause line = longline.join combine = false else next end end # Ignore comment lines and the end statement if there # XXX Fix parsing end next if line.start_with?('!', 'end') line.chomp! next if line.empty? indent_level = line[/\A */].size / @indent if indent_level > prev_indent # New section section = Section.new(prev_line, section) section.parent.add_child(section) prev_indent = indent_level elsif indent_level < prev_indent # XXX This has a bug if we pop more than one section # XXX Bug if we have 2 subsections with intervening commands # End of current section section = section.parent prev_indent = indent_level end # Add the line to the current section section.add_cmd(line) prev_line = line end end