Class: Cisco::ConfigParser::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/cisco_node_utils/configparser_lib.rb

Overview

Configuration class - helper for dealing with config CLI

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_str) ⇒ Configuration

Constructor for Configuration

Parameters:

  • config_str (String)

    to parse

Raises:

  • (ArgumentError)

    if config_str is not a String



41
42
43
44
45
46
47
48
49
50
# File 'lib/cisco_node_utils/configparser_lib.rb', line 41

def initialize(config_str)
  unless config_str.kind_of? String
    fail ArgumentError, 'Argument is not a String.'
  end

  @configuration = {}
  @ordered_keys = []
  @indent = ''
  parse(config_str)
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



35
36
37
# File 'lib/cisco_node_utils/configparser_lib.rb', line 35

def configuration
  @configuration
end

Class Method Details

.build_min_config_hash(current, must, min_config = {}) ⇒ Object

build_min_config_hash

Build a config hash of the minimum keys that would be needed to update current config to all of the changes in the “must” config. Each hash key is a configuration command; some keys have subconfigs which must be checked before dismissing top-level keys as present. This method is used primarily by the free-form command_config providers.

Parameters:

  • current (Hash)

    superset of running-config & must config

  • must (Hash)

    pending config from recipe, manifest, etc

  • min_config (Hash) (defaults to: {})

    in-progress recursion-built minimum config



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cisco_node_utils/configparser_lib.rb', line 65

def self.build_min_config_hash(current, must, min_config={})
  return {} if must.empty? # base case
  must.each do |k, v| # check each must{k} is present in current{}
    if current.key?(k) # if cmd is in current then compare subconfig
      min_config[k] = Configuration.new('')
      min_config[k].configuration =
        build_min_config_hash(current[k].configuration,
                              v.configuration, {})
      if min_config[k].configuration.empty?
        # no differing subconfigs, so empty hash is returned
        min_config.delete(k)
      end
    else # command NOT in current, apply it + all subcommands
      min_config[k] = v
    end
  end
  min_config
end

.config_hash_to_str(cmd_hash, str = '') ⇒ Object

build_min_config_hash



84
85
86
87
88
89
90
91
# File 'lib/cisco_node_utils/configparser_lib.rb', line 84

def self.config_hash_to_str(cmd_hash, str='')
  return '' if cmd_hash.empty?
  cmd_hash.each do |k, v|
    str += k + "\n"
    str += config_hash_to_str(v.configuration, '')
  end
  str
end

Instance Method Details

#compare_with(config) ⇒ String

Compare ConfigParser::Configuration objects

Parameters:

Returns:

  • (String)

    containing match, empty if no match found.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cisco_node_utils/configparser_lib.rb', line 109

def compare_with(config)
  return nil if config.nil?
  existing = ''
  @ordered_keys.each do |config_line|
    command = config_line.strip
    submode = @configuration[command]
    fail StopIteration, 'Could not find submode.' if submode.nil?

    if special_command?(command)
      # match special exit/end command
      existing << config_line
      break
    elsif config.include_command?(command)
      # match whole command
      existing << config_line
      config_submode = config.submode_config(command)
      existing << submode.compare_with(config_submode)
      next
    end # if
  end
  existing
end

#include_command?(command) ⇒ Boolean

Check command [Array] for test command

Parameters:

  • command (String)

    test command

Returns:

  • (Boolean)

    true if command found, else false



142
143
144
145
# File 'lib/cisco_node_utils/configparser_lib.rb', line 142

def include_command?(command)
  commands = mode_configuration
  commands.include?(command)
end

#mode_configurationArray

Returns containing command with leading/trailing whitespace removed.

Returns:

  • (Array)

    containing command with leading/trailing whitespace removed.



134
135
136
# File 'lib/cisco_node_utils/configparser_lib.rb', line 134

def mode_configuration
  @ordered_keys.collect(&:strip)
end

#submode_config(config_line) ⇒ ConfigParser::Configuration

Fetch ConfigParser::Configuration object containing config_line

Parameters:

  • config_line (String)

Returns:



187
188
189
190
# File 'lib/cisco_node_utils/configparser_lib.rb', line 187

def submode_config(config_line)
  command = config_line.strip
  @configuration[command]
end