Class: PuppetX::NetDev::EosApi
- Inherits:
-
Object
- Object
- PuppetX::NetDev::EosApi
- Includes:
- CommonMethods, SnmpMethods
- Defined in:
- lib/puppet_x/net_dev/eos_api.rb,
lib/puppet_x/net_dev/eos_api/version.rb,
lib/puppet_x/net_dev/eos_api/snmp_methods.rb,
lib/puppet_x/net_dev/eos_api/common_methods.rb
Overview
Eapi class
Defined Under Namespace
Modules: CommonMethods, SnmpMethods
Constant Summary collapse
- VERSION =
'0.0.1'
Constants included from SnmpMethods
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
IP address or hostname of the REST api.
-
#password ⇒ Object
readonly
API password.
-
#port ⇒ Object
readonly
TCP port of the REST api.
-
#username ⇒ Object
readonly
API username.
Instance Method Summary collapse
-
#all_interfaces ⇒ Hash<String,Hash>
all_interfaces returns a hash of all interfaces.
-
#all_portchannel_modes ⇒ Hash<String,Hash>
private
all_portchannel_modes returns a hash of each of the port channel LACP modes.
-
#all_portchannels ⇒ Hash<String,Hash>
all_portchannels returns a hash of all port channels based on multiple sources of data from the API.
-
#all_portchannels_detailed ⇒ Hash<String,Hash>
private
all_portchannels_detailed returns a hash of all port channels based on the ‘show etherchannel detailed` command.
-
#all_vlans ⇒ Hash<String,Hash>
all_vlans returns a hash of all vlans.
-
#channel_group_create(name, opts) ⇒ Object
channel_group_create creates a channel group and associated port channel interface if the interface does not already exist.
-
#channel_group_destroy(name) ⇒ Object
channel_group_destroy destroys a port channel group.
-
#format_error(data) ⇒ String
private
format_error takes the value of the ‘error’ key from the EOS API response and formats the error strings into a string suitable for error messages.
-
#get_flowcontrol(name) ⇒ Hash<Symbol,String>
get_flowcontrol obtains the configured flow_control send and receive values from the target device.
-
#http ⇒ NetX::HttpUnix
http returns a memoized HTTP client instance conforming to the Net::HTTP interface.
-
#initialize(opts = {}) ⇒ PuppetX::NetDev::EosApi
constructor
initialize an API instance.
-
#interface_set_channel_group(interface, opts) ⇒ Object
interface_set_channel_group configures an interface to be a member of a specified channel group ID.
-
#interface_unset_channel_group(interface) ⇒ Object
interface_unset_channel_group removes a specific interface from all channel groups.
-
#parse_flowcontrol_single(text) ⇒ Hash<Symbol,String>
private
parse_flowcontrol_single parses the text output of the ‘show flowcontrol <interface>` command where there is a single entry for the named interface.
-
#parse_min_links(text) ⇒ Fixnum
private
parse_min_links takes the text from the ‘show running-config interfaces Port-ChannelX` API command and parses out the currently configured number of minimum links.
-
#parse_portchannel_active_ports(group_lines) ⇒ Array<String>
private
parse_portchannel_active_ports takes a portchannel section from ‘show port-channel detailed` and parses all of the active ports from the section.
-
#parse_portchannel_configured_ports(group_lines) ⇒ Array<String>
private
parse_portchannel_configured_ports takes a portchannel section from ‘show port-channel detailed` and parses all of the active ports from the section.
-
#parse_portchannel_modes(text) ⇒ Hash<String,Hash>
private
Parse the portchannel modes from the text of the ‘show port-channel summary` command.
-
#port_channel_destroy(name) ⇒ Object
port_channel_destroy destroys a port channel interface and removes all interfaces from the channel group.
-
#portchannel_min_links(name) ⇒ Fixnum
private
portchannel_min_links takes the name of a Port Channel interface and obtains the currently configured min-links value by parsing the text of the running configuration.
-
#set_flowcontrol_recv(name, value) ⇒ Object
set_flowcontrol_recv Configures a specific interface’s flow control receive value.
-
#set_flowcontrol_send(name, value) ⇒ Object
set_flowcontrol_send Configures a specific interface’s flow control send value.
-
#set_interface_description(name, description) ⇒ Object
set_interface_description configures the description string for an interface.
-
#set_interface_mtu(name, mtu) ⇒ Object
set_interface_mtu configures the interface MTU.
-
#set_interface_speed(name, speed) ⇒ Object
set_interface_speed enable a network interface.
-
#set_interface_state(name, state) ⇒ Object
set_interface_state enables or disables a network interface.
-
#set_portchannel_min_links(name, min_links) ⇒ Object
set_portchannel_min_links Configures the minimum links value for a channel group.
-
#set_vlan_name(id, name) ⇒ Object
set_vlan_name assigns a name to a vlan.
-
#set_vlan_state(id, state) ⇒ Object
set_vlan_state set a vlan to the state specified.
-
#uri ⇒ URI
The URI of the server.
-
#vlan(id) ⇒ nil, Hash<String,Hash>
vlan returns data about a specific VLAN identified by the VLAN ID number.
-
#vlan_create(id) ⇒ Boolean
vlan_create creates a VLAN that does not yet exist on the target device.
-
#vlan_destroy(id) ⇒ Object
vlan_destroy destroys a vlan.
Methods included from SnmpMethods
#parse_snmp_communities, #parse_snmp_enable, #parse_snmp_hosts, #parse_snmp_traps, #parse_snmp_users, #snmp_attributes, #snmp_communities, #snmp_community_destroy, #snmp_community_set, #snmp_contact, #snmp_contact=, #snmp_enable, #snmp_enable=, #snmp_location, #snmp_location=, #snmp_notification_receiver_remove, #snmp_notification_receiver_set, #snmp_notification_receivers, #snmp_notification_set, #snmp_notifications, #snmp_user_destroy, #snmp_user_password_hash, #snmp_user_set, #snmp_users
Methods included from CommonMethods
Constructor Details
#initialize(opts = {}) ⇒ PuppetX::NetDev::EosApi
initialize an API instance. The API will communicate with the HTTP server over TCP or a Unix Domain Socket. If a unix domain socket is being used then the address parameter should be set to the socket path. The port, username, and password are not necessary.
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
65 66 67 68 69 70 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 65 def initialize(opts = {}) @address = opts[:address] || ENV['EOS_HOSTNAME'] || 'unix:///var/run/command-api.sock' @port = opts[:port] || ENV['EOS_PORT'] || 80 @username = opts[:username] || ENV['EOS_USERNAME'] || 'admin' @password = opts[:password] || ENV['EOS_PASSWORD'] || 'puppet' end |
Instance Attribute Details
#address ⇒ Object (readonly)
IP address or hostname of the REST api
29 30 31 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 29 def address @address end |
#password ⇒ Object (readonly)
API password
35 36 37 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 35 def password @password end |
#port ⇒ Object (readonly)
TCP port of the REST api
31 32 33 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 31 def port @port end |
#username ⇒ Object (readonly)
API username
33 34 35 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 33 def username @username end |
Instance Method Details
#all_interfaces ⇒ Hash<String,Hash>
all_interfaces returns a hash of all interfaces
508 509 510 511 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 508 def all_interfaces result = eapi_action('show interfaces', 'list all interfaces') result.first['interfaces'] end |
#all_portchannel_modes ⇒ Hash<String,Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
all_portchannel_modes returns a hash of each of the port channel LACP modes. This method could be merged with the data from the all_portchannels method.
372 373 374 375 376 377 378 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 372 def all_portchannel_modes # JSON format is not supported in EOS 4.13.7M so use text format result = eapi_action('show port-channel summary', 'get lag modes', format: 'text') text = result.first['output'] parse_portchannel_modes(text) end |
#all_portchannels ⇒ Hash<String,Hash>
all_portchannels returns a hash of all port channels based on multiple sources of data from the API.
281 282 283 284 285 286 287 288 289 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 281 def all_portchannels detailed = all_portchannels_detailed modes = all_portchannel_modes # Merge the two detailed.each_with_object(Hash.new) do |(name, attr), hsh| hsh[name] = modes[name] ? attr.merge(modes[name]) : attr hsh[name]['minimum_links'] = portchannel_min_links(name) end end |
#all_portchannels_detailed ⇒ Hash<String,Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
all_portchannels_detailed returns a hash of all port channels based on the ‘show etherchannel detailed` command.
355 356 357 358 359 360 361 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 355 def all_portchannels_detailed # JSON format is not supported in EOS 4.13.7M so use text format result = eapi_action('show etherchannel detailed', 'list port channels', format: 'text') text = result.first['output'] parse_portchannels(text) end |
#all_vlans ⇒ Hash<String,Hash>
all_vlans returns a hash of all vlans
268 269 270 271 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 268 def all_vlans result = eapi_action('show vlan', 'list all vlans') result.first['vlans'] end |
#channel_group_create(name, opts) ⇒ Object
channel_group_create creates a channel group and associated port channel interface if the interface does not already exist.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 205 def channel_group_create(name, opts) channel_group = name.scan(/\d+/).first.to_i interfaces = [*opts[:interfaces]] if interfaces.empty? fail ArgumentError, 'Cannot create a channel group with no interfaces' end interfaces.each do |interface| set_opts = { mode: opts[:mode], group: channel_group } interface_set_channel_group(interface, set_opts) end end |
#channel_group_destroy(name) ⇒ Object
channel_group_destroy destroys a port channel group.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 120 def channel_group_destroy(name) # Need to remove all interfaces from the channel group. port_channels = all_portchannels_detailed channel_group = port_channels[name] unless channel_group msg = "#{name} is not in #{port_channels.keys.inspect}" fail ArgumentError, msg end interfaces = channel_group['ports'] interfaces.each { |iface| interface_unset_channel_group(iface) } end |
#format_error(data) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
format_error takes the value of the ‘error’ key from the EOS API response and formats the error strings into a string suitable for error messages.
582 583 584 585 586 587 588 589 590 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 582 def format_error(data) if data data.each_with_object([]) do |i, ary| ary.push(*i['errors']) if i['errors'] end.join(', ') else 'unknown error' end end |
#get_flowcontrol(name) ⇒ Hash<Symbol,String>
get_flowcontrol obtains the configured flow_control send and receive values from the target device.
437 438 439 440 441 442 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 437 def get_flowcontrol(name) cmd = "show flowcontrol interface #{name}" result = eapi_action(cmd, 'get flowcontrol config', format: 'text') text = result.first['output'] parse_flowcontrol_single(text) end |
#http ⇒ NetX::HttpUnix
http returns a memoized HTTP client instance conforming to the Net::HTTP interface.
599 600 601 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 599 def http @http ||= NetX::HTTPUnix.new(address, port) end |
#interface_set_channel_group(interface, opts) ⇒ Object
interface_set_channel_group configures an interface to be a member of a specified channel group ID.
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 162 def interface_set_channel_group(interface, opts) channel_group = opts[:group] mode = case opts[:mode] when :active, :passive then opts[:mode] when :disabled then :on else fail ArgumentError, "Unknown LACP mode #{opts[:mode]}" end cmd = %w(enable configure) << "interface #{interface}" cmd << "channel-group #{channel_group} mode #{mode}" msg = "join #{interface} to channel group #{channel_group}" eapi_action(cmd, msg) end |
#interface_unset_channel_group(interface) ⇒ Object
interface_unset_channel_group removes a specific interface from all channel groups.
140 141 142 143 144 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 140 def interface_unset_channel_group(interface) cmds = %w(enable configure) << "interface #{interface}" cmds << 'no channel-group' eapi_action(cmds, "remove #{interface} from channel group") end |
#parse_flowcontrol_single(text) ⇒ Hash<Symbol,String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
parse_flowcontrol_single parses the text output of the ‘show flowcontrol <interface>` command where there is a single entry for the named interface.
Port Send FlowControl Receive FlowControl RxPause TxPause
admin oper admin oper
---------- -------- -------- -------- -------- ------------- -------------
Et1 off unknown off unknown 0 0
459 460 461 462 463 464 465 466 467 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 459 def parse_flowcontrol_single(text) re = /----\n(.*?)\s+(.*?)\s+.*?\s+(.*?)\s+.*\n/m mdata = re.match(text) if mdata { send: mdata[2], receive: mdata[3] } else fail ArgumentError, 'could not parse flowcontrol' end end |
#parse_min_links(text) ⇒ Fixnum
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
parse_min_links takes the text from the ‘show running-config interfaces Port-ChannelX` API command and parses out the currently configured number of minimum links. If there is no min-links value we (safely) assume it is configured to 0. Example output is:
interface Port-Channel4
description Office Backbone
port-channel min-links 2
341 342 343 344 345 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 341 def parse_min_links(text) re = /min-links\s+(\d+)/m mdata = re.match(text) mdata ? mdata[1].to_i : 0 end |
#parse_portchannel_active_ports(group_lines) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
parse_portchannel_active_ports takes a portchannel section from ‘show port-channel detailed` and parses all of the active ports from the section.
672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 672 def parse_portchannel_active_ports(group_lines) lines = group_lines.dup # Check if there are no active ports mdata = /(No)? Active Ports/.match(lines.shift) return [] if mdata[1] # return if there are none lines.shift until /^\s*Port /.match(lines.first) || lines.empty? lines.shift(2) # heading line and ---- line # Read interfaces until the first blank line lines.each_with_object([]) do |l, a| l.chomp! break a if l.empty? a << l.split.first end end |
#parse_portchannel_configured_ports(group_lines) ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
parse_portchannel_configured_ports takes a portchannel section from ‘show port-channel detailed` and parses all of the active ports from the section.
698 699 700 701 702 703 704 705 706 707 708 709 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 698 def parse_portchannel_configured_ports(group_lines) lines = group_lines.dup # Check if there are no active ports lines.shift until /inactive ports/.match(lines.first) || lines.empty? return [] if lines.empty? lines.shift(3) lines.each_with_object([]) do |l, a| l.chomp! break a if l.empty? a << l.split.first end end |
#parse_portchannel_modes(text) ⇒ Hash<String,Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parse the portchannel modes from the text of the ‘show port-channel summary` command. The following is an example of two channel groups, one static, one active.
rubocop:disable Metrics/LineLength, Metrics/MethodLength, Style/TrailingWhitespace
Flags
------------------------ ---------------------------- -------------------------
a - LACP Active p - LACP Passive * - static fallback
F - Fallback enabled f - Fallback configured ^ - individual fallback
U - In Use D - Down
+ - In-Sync - - Out-of-Sync i - incompatible with agg
P - bundled in Po s - suspended G - Aggregable
I - Individual S - ShortTimeout w - wait for agg
Number of channels in use: 1
Number of aggregators:1
Port-Channel Protocol Ports
------------------ -------------- ----------------
Po3(U) Static Et1(D) Et2(P)
Po4(D) LACP(a) Et3(G-) Et4(G-)
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 408 def parse_portchannel_modes(text) lines = text.lines.each_with_object(Array.new) do |v, ary| ary << v.chomp if /^\s*Po\d/.match(v) end lines.each_with_object(Hash.new) do |line, hsh| mdata = /^\s+Po(\d+).*?\s+([a-zA-Z()0-9_-]+)/.match(line) idx = mdata[1] protocol = mdata[2] mode = case protocol when 'Static' then :disabled when /LACP/ flags = /\((.*?)\)/.match(protocol)[1] if flags.include? 'p' then :passive elsif flags.include? 'a' then :active end end hsh["Port-Channel#{idx}"] = { 'mode' => mode } end end |
#port_channel_destroy(name) ⇒ Object
port_channel_destroy destroys a port channel interface and removes all interfaces from the channel group.
184 185 186 187 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 184 def port_channel_destroy(name) cmds = %w(enable configure) << "no interface #{name}" eapi_action(cmds, "remove #{name}") end |
#portchannel_min_links(name) ⇒ Fixnum
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
portchannel_min_links takes the name of a Port Channel interface and obtains the currently configured min-links value by parsing the text of the running configuration.
300 301 302 303 304 305 306 307 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 300 def portchannel_min_links(name) api_commands = ['enable', "show running-config interfaces #{name}"] result = eapi_action(api_commands, 'obtain port channel min links value', format: 'text') text = result[1]['output'] # skip over the enable command output. parse_min_links(text) end |
#set_flowcontrol_recv(name, value) ⇒ Object
set_flowcontrol_recv Configures a specific interface’s flow control
receive value.
495 496 497 498 499 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 495 def set_flowcontrol_recv(name, value) cmd = %w(enable configure) << "interface #{name}" cmd << "flowcontrol receive #{value}" eapi_action(cmd, 'configure flowcontrol receive') end |
#set_flowcontrol_send(name, value) ⇒ Object
set_flowcontrol_send Configures a specific interface’s flow control
send value.
479 480 481 482 483 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 479 def set_flowcontrol_send(name, value) cmd = %w(enable configure) << "interface #{name}" cmd << "flowcontrol send #{value}" eapi_action(cmd, 'configure flowcontrol send') end |
#set_interface_description(name, description) ⇒ Object
set_interface_description configures the description string for an interface.
536 537 538 539 540 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 536 def set_interface_description(name, description) cmd = %w(enable configure) << "interface #{name}" cmd << "description #{description}" eapi_action(cmd, "set interface #{name} description to #{description}") end |
#set_interface_mtu(name, mtu) ⇒ Object
set_interface_mtu configures the interface MTU
565 566 567 568 569 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 565 def set_interface_mtu(name, mtu) cmd = %w(enable configure) << "interface #{name}" cmd << "mtu #{mtu}" eapi_action(cmd, "set interface #{name} mtu to #{mtu}") end |
#set_interface_speed(name, speed) ⇒ Object
set_interface_speed enable a network interface
551 552 553 554 555 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 551 def set_interface_speed(name, speed) cmd = %w(enable configure) << "interface #{name}" cmd << "speed forced #{speed}" eapi_action(cmd, "set interface #{name} speed to #{speed}") end |
#set_interface_state(name, state) ⇒ Object
set_interface_state enables or disables a network interface
522 523 524 525 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 522 def set_interface_state(name, state) cmd = %w(enable configure) << "interface #{name}" << state eapi_action(cmd, "set interface #{name} state to #{state}") end |
#set_portchannel_min_links(name, min_links) ⇒ Object
set_portchannel_min_links Configures the minimum links value for a channel group.
319 320 321 322 323 324 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 319 def set_portchannel_min_links(name, min_links) cmd = %w(enable configure) cmd << "interface #{name}" cmd << "port-channel min-links #{min_links}" eapi_action(cmd, 'set port-channel min links') end |
#set_vlan_name(id, name) ⇒ Object
set_vlan_name assigns a name to a vlan
225 226 227 228 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 225 def set_vlan_name(id, name) cmds = ['enable', 'configure', "vlan #{id}"] << "name #{name}" eapi_action(cmds, "set vlan #{id} name to #{name}") end |
#set_vlan_state(id, state) ⇒ Object
set_vlan_state set a vlan to the state specified
239 240 241 242 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 239 def set_vlan_state(id, state) cmds = ['enable', 'configure', "vlan #{id}"] << "state #{state}" eapi_action(cmds, "set vlan #{id} state to #{state}") end |
#uri ⇒ URI
Returns the URI of the server.
825 826 827 828 829 830 831 832 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 825 def uri return @uri if @uri if username && password @uri = URI("http://#{username}:#{password}@#{address}:#{port}") else @uri = URI("http://#{address}:#{port}") end end |
#vlan(id) ⇒ nil, Hash<String,Hash>
vlan returns data about a specific VLAN identified by the VLAN ID number. This API call maps roughly to the ‘show vlan <id>` command. This method returns nil if no VLAN was found matching the ID provided.
84 85 86 87 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 84 def vlan(id) result = eapi_action("show vlan #{id}", 'list vlans') result.first['vlans'] if result end |
#vlan_create(id) ⇒ Boolean
vlan_create creates a VLAN that does not yet exist on the target device.
98 99 100 101 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 98 def vlan_create(id) cmds = ['enable', 'configure', "vlan #{id}"] eapi_action(cmds, "create vlan #{id}") end |
#vlan_destroy(id) ⇒ Object
vlan_destroy destroys a vlan
109 110 111 112 |
# File 'lib/puppet_x/net_dev/eos_api.rb', line 109 def vlan_destroy(id) cmds = ['enable', 'configure', "no vlan #{id}"] eapi_action(cmds, "destroy vlan #{id}") end |