Class: Cisco::Vlan

Inherits:
Object
  • Object
show all
Defined in:
lib/cisco_node_utils/vlan.rb

Constant Summary collapse

@@node =
Node.instance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vlan_id, instantiate = true) ⇒ Vlan

Returns a new instance of Vlan.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/cisco_node_utils/vlan.rb', line 31

def initialize(vlan_id, instantiate=true)
  @vlan_id = vlan_id.to_s
  raise ArgumentError,
    "Invalid value(non-numeric Vlan id)" unless @vlan_id[/^\d+$/]

  create if instantiate
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/cisco_node_utils/vlan.rb', line 26

def name
  @name
end

#vlan_idObject (readonly)

Returns the value of attribute vlan_id.



26
27
28
# File 'lib/cisco_node_utils/vlan.rb', line 26

def vlan_id
  @vlan_id
end

Class Method Details

.vlansObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/cisco_node_utils/vlan.rb', line 39

def Vlan.vlans
  hash = {}
  vlan_list = @@node.config_get("vlan", "all_vlans")
  return hash if vlan_list.nil?

  vlan_list.each do |id|
    hash[id] = Vlan.new(id, false)
  end
  hash
end

Instance Method Details

#add_interface(interface) ⇒ Object



134
135
136
# File 'lib/cisco_node_utils/vlan.rb', line 134

def add_interface(interface)
  interface.access_vlan = @vlan_id
end

#cli_error_check(result) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/cisco_node_utils/vlan.rb', line 58

def cli_error_check(result)
  # The NXOS vlan cli does not raise an exception in some conditions and
  # instead just displays a STDOUT error message; thus NXAPI does not detect
  # the failure and we must catch it by inspecting the "body" hash entry
  # returned by NXAPI. This vlan cli behavior is unlikely to change.
  raise result[2]["body"] unless result[2]["body"].empty?
end

#createObject



50
51
52
# File 'lib/cisco_node_utils/vlan.rb', line 50

def create
  @@node.config_set("vlan", "create", @vlan_id)
end

#default_shutdownObject



130
131
132
# File 'lib/cisco_node_utils/vlan.rb', line 130

def default_shutdown
  @@node.config_get_default("vlan", "shutdown")
end

#default_stateObject



111
112
113
# File 'lib/cisco_node_utils/vlan.rb', line 111

def default_state
  @@node.config_get_default("vlan", "state")
end

#default_vlan_nameObject



84
85
86
# File 'lib/cisco_node_utils/vlan.rb', line 84

def default_vlan_name
  "VLAN%04d" % @vlan_id
end

#destroyObject



54
55
56
# File 'lib/cisco_node_utils/vlan.rb', line 54

def destroy
  @@node.config_set("vlan", "destroy", @vlan_id)
end

#interfacesObject



142
143
144
145
146
147
148
149
150
151
# File 'lib/cisco_node_utils/vlan.rb', line 142

def interfaces
  all_interfaces = Interface.interfaces
  interfaces = {}
  all_interfaces.each do |name, i|
    next unless i.switchport_mode == :access
    next unless i.access_vlan == @vlan_id
    interfaces[name] = i
  end
  interfaces
end

#remove_interface(interface) ⇒ Object



138
139
140
# File 'lib/cisco_node_utils/vlan.rb', line 138

def remove_interface(interface)
  interface.access_vlan = interface.default_access_vlan
end

#shutdownObject



115
116
117
118
119
120
# File 'lib/cisco_node_utils/vlan.rb', line 115

def shutdown
  result = @@node.config_get("vlan", "shutdown", @vlan_id)
  return default_shutdown if result.nil?
  # valid result is either: "active"(aka no shutdown) or "shutdown"
  result.first[/shut/] ? true : false
end

#shutdown=(val) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/cisco_node_utils/vlan.rb', line 122

def shutdown=(val)
  no_cmd = (val) ? "" : "no"
  result = @@node.config_set("vlan", "shutdown", @vlan_id, no_cmd)
  cli_error_check(result)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end

#stateObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/cisco_node_utils/vlan.rb', line 88

def state
  result = @@node.config_get("vlan", "state", @vlan_id)
  return default_state if result.nil?
  case result.first
  when /act/
    return "active"
  when /sus/
    return "suspend"
  end
end

#state=(str) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cisco_node_utils/vlan.rb', line 99

def state=(str)
  str = str.to_s
  if str.empty?
    result = @@node.config_set("vlan", "state", @vlan_id, "no", "")
  else
    result = @@node.config_set("vlan", "state", @vlan_id, "", str)
  end
  cli_error_check(result)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end

#vlan_nameObject



66
67
68
69
70
# File 'lib/cisco_node_utils/vlan.rb', line 66

def vlan_name
  result = @@node.config_get("vlan", "name", @vlan_id)
  return default_vlan_name if result.nil?
  result.shift
end

#vlan_name=(str) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cisco_node_utils/vlan.rb', line 72

def vlan_name=(str)
  raise TypeError unless str.is_a?(String)
  if str.empty?
    result = @@node.config_set("vlan", "name", @vlan_id, "no", "")
  else
    result = @@node.config_set("vlan", "name", @vlan_id, "", str)
  end
  cli_error_check(result)
rescue CliError => e
  raise "[vlan #{@vlan_id}] '#{e.command}' : #{e.clierror}"
end