Class: Rbeapi::Api::Interfaces

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/interfaces.rb

Overview

The Interfaces class manages all physical and logical interfaces on an EOS node.

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#command_builder, #configure, #configure_interface, #get_block, instance

Constructor Details

#initialize(node) ⇒ Interfaces

Returns a new instance of Interfaces.



45
46
47
48
# File 'lib/rbeapi/api/interfaces.rb', line 45

def initialize(node)
  super(node)
  @instances = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

rubocop:disable Style/MethodMissing



136
137
138
139
# File 'lib/rbeapi/api/interfaces.rb', line 136

def method_missing(method_name, *args, &block)
  instance = get_instance(args[0])
  instance.send(method_name.to_sym, *args, &block)
end

Instance Method Details

#get(name) ⇒ nil, Hash<Symbol, Object>

get returns a hash of interface configurations for the given name.

Examples:

{
  name: <string>,
  type: <string>,
  description: <string>,
  encapsulation: <integer>,
  shutdown: <boolean>
}

Parameters:

  • name (String)

    The interface name to return a resource for from the nodes configuration.

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the interface resource as a Hash. If the specified name is not found in the nodes current configuration a nil object is returned.



68
69
70
# File 'lib/rbeapi/api/interfaces.rb', line 68

def get(name)
  get_instance(name).get(name)
end

#get_instance(name) ⇒ Object

get_instance returns an interface instance for the given name.

Parameters:

  • name (String)

    The interface name to return an instance for.

Returns:

  • (Object)

    Returns the interface instance as an Object.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rbeapi/api/interfaces.rb', line 114

def get_instance(name)
  name = name[0, 2].upcase
  cls = case name
        when 'ET'
          'Rbeapi::Api::EthernetInterface'
        when 'PO'
          'Rbeapi::Api::PortchannelInterface'
        when 'VX'
          'Rbeapi::Api::VxlanInterface'
        when 'VL'
          'Rbeapi::Api::VlanInterface'
        else
          'Rbeapi::Api::BaseInterface'
        end

  return @instances[name] if @instances.include?(cls)
  instance = Rbeapi::Utils.class_from_string(cls).new(@node)
  @instances[name] = instance
  instance
end

#getallHash<Symbol, Object>

getall returns a hash of interface configurations.

Examples:

{
  <name>: {
    name: <string>,
    type: <string>,
    description: <string>,
    encapsulation: <integer>,
    shutdown: <boolean>,
    ...
  },
  <name>: {
    name: <string>,
    type: <string>,
    description: <string>,
    encapsulation: <integer>,
    shutdown: <boolean>,
    ...
  },
  ...
}

Returns:

  • (Hash<Symbol, Object>)

    Returns the interface resources as a Hash. If none exist in the nodes current configuration an empty hash is returned.



99
100
101
102
103
104
105
106
# File 'lib/rbeapi/api/interfaces.rb', line 99

def getall
  interfaces = config.scan(/(?<=^interface\s).+$/)

  interfaces.each_with_object({}) do |name, hsh|
    data = get(name)
    hsh[name] = data if data
  end
end

#respond_to?(method_name, name = nil) ⇒ Boolean

rubocop:enable Style/MethodMissing

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/rbeapi/api/interfaces.rb', line 142

def respond_to?(method_name, name = nil)
  return super unless name
  instance = get_instance(name)
  instance.respond_to?(method_name) || super
end