Class: Cisco::ConfigParser::Configuration
- Inherits:
-
Object
- Object
- Cisco::ConfigParser::Configuration
- Defined in:
- lib/cisco_node_utils/configparser_lib.rb
Overview
Configuration class - helper for dealing with config CLI
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
Class Method Summary collapse
-
.build_min_config_hash(current, must, min_config = {}) ⇒ Object
build_min_config_hash.
-
.config_hash_to_str(cmd_hash, str = '') ⇒ Object
build_min_config_hash.
Instance Method Summary collapse
-
#compare_with(config) ⇒ String
Compare ConfigParser::Configuration objects.
-
#include_command?(command) ⇒ Boolean
Check command [Array] for test command.
-
#initialize(config_str) ⇒ Configuration
constructor
Constructor for Configuration.
-
#mode_configuration ⇒ Array
Containing command with leading/trailing whitespace removed.
-
#submode_config(config_line) ⇒ ConfigParser::Configuration
Fetch ConfigParser::Configuration object containing config_line.
Constructor Details
#initialize(config_str) ⇒ Configuration
Constructor for Configuration
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
#configuration ⇒ Object
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.
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
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
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_configuration ⇒ Array
Returns 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
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 |