Class: Rbeapi::Api::Interfaces

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

Constant Summary collapse

METHODS =
[:create, :delete, :default]

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#configure, #get_block, instance

Constructor Details

#initialize(node) ⇒ Interfaces

Returns a new instance of Interfaces.


43
44
45
46
# File 'lib/rbeapi/api/interfaces.rb', line 43

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


80
81
82
83
84
85
# File 'lib/rbeapi/api/interfaces.rb', line 80

def method_missing(method_name, *args, &block)
  if method_name.to_s =~ /set_(.*)/ || METHODS.include?(method_name)
    instance = get_instance(args[0])
    instance.send(method_name.to_sym, *args, &block)
  end
end

Instance Method Details

#get(name) ⇒ Object


48
49
50
# File 'lib/rbeapi/api/interfaces.rb', line 48

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

#get_instance(name) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rbeapi/api/interfaces.rb', line 61

def get_instance(name)
  name = name[0,2].upcase
  case name
  when 'ET'
    cls = 'Rbeapi::Api::EthernetInterface'
  when 'PO'
    cls = 'Rbeapi::Api::PortchannelInterface'
  when 'VX'
    cls = 'Rbeapi::Api::VxlanInterface'
  else
    cls = '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

#getallObject


52
53
54
55
56
57
58
59
# File 'lib/rbeapi/api/interfaces.rb', line 52

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

Returns:

  • (Boolean)

87
88
89
90
91
# File 'lib/rbeapi/api/interfaces.rb', line 87

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