Class: Cisco::ConfigParser::Configuration

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

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



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

def initialize(config_str)
  raise ArgumentError, "Argument is not a String." unless config_str.kind_of? String

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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



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

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



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

def Configuration.build_min_config_hash(current, must, min_config={})
  return {} if must.empty?   # base case
  must.each { |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
  }
  min_config
end

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

build_min_config_hash



81
82
83
84
85
86
87
88
# File 'lib/cisco_node_utils/configparser_lib.rb', line 81

def Configuration.config_hash_to_str(cmd_hash, str="")
  return "" if cmd_hash.empty?
  cmd_hash.each { |k, v|
    str += k + "\n"
    str += config_hash_to_str(v.configuration, "")
  }
  str
end

Instance Method Details

#compare_with(config) ⇒ String

Compare ConfigParser::Configuration objects

Parameters:

Returns:

  • (String)

    containing match, empty if no match found.



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

def compare_with(config)
  return nil if config.nil?
  existing = ""
  @ordered_keys.each { |config_line|
    command = config_line.strip
    submode = @configuration[command]
    raise 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

    prefix, base = base_commands(command)
    if prefix != "" and !config.include_command?(base)
      existing << config_line
      next
    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



145
146
147
148
# File 'lib/cisco_node_utils/configparser_lib.rb', line 145

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.



137
138
139
# File 'lib/cisco_node_utils/configparser_lib.rb', line 137

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:



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

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