Class: Cisco::Utils
- Inherits:
-
Object
- Object
- Cisco::Utils
- Defined in:
- lib/cisco_node_utils/cisco_cmn_utils.rb
Overview
General utility class
Class Method Summary collapse
- .delta_add_remove(should, current = [], opt = nil) ⇒ Object
-
.depth(a) ⇒ Object
Helper to build a hash of add/remove commands for a nested array.
-
.process_network_mask(network) ⇒ Object
Helper utility method for ip/prefix format networks.
-
.zero_pad_macaddr(mac) ⇒ Object
Helper to 0-pad a mac address.
Class Method Details
.delta_add_remove(should, current = [], opt = nil) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 119 def self.delta_add_remove(should, current=[], opt=nil) # Remove nil entries from array should.each(&:compact!) if depth(should) > 1 delta = { add: should - current, remove: current - should } # Some cli properties cannot be updated, thus must be removed first return delta if opt == :updates_not_allowed # Delete entries from :remove if f1 is an update to an existing command delta[:add].each do |id, _| if depth(should) == 1 delta[:remove].delete_if { |f1| [f1] if f1.to_s == id.to_s } else delta[:remove].delete_if { |f1, f2| [f1, f2] if f1.to_s == id.to_s } end end delta end |
.depth(a) ⇒ Object
Helper to build a hash of add/remove commands for a nested array. Useful for network, redistribute, etc.
should: an array of expected cmds (manifest/recipe)
current: an array of existing cmds on the device
114 115 116 117 |
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 114 def self.depth(a) return 0 unless a.is_a?(Array) 1 + depth(a[0]) end |
.process_network_mask(network) ⇒ Object
Helper utility method for ip/prefix format networks. For ip/prefix format ‘1.1.1.1/24’ or ‘2000:123:38::34/64’, we need to mask the address using the prefix length so that they are converted to ‘1.1.1.0/24’ or ‘2000:123:38::/64’
103 104 105 106 107 108 |
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 103 def self.process_network_mask(network) mask = network.split('/')[1] address = IPAddr.new(network).to_s network = address + '/' + mask unless mask.nil? network end |
.zero_pad_macaddr(mac) ⇒ Object
Helper to 0-pad a mac address.
139 140 141 142 143 |
# File 'lib/cisco_node_utils/cisco_cmn_utils.rb', line 139 def self.zero_pad_macaddr(mac) return nil if mac.nil? || mac.empty? o1, o2, o3 = mac.split('.').map { |o| o.to_i(16).to_s(10) } sprintf('%04x.%04x.%04x', o1, o2, o3) end |