Class: Cisco::Vlan
- Inherits:
-
Object
- Object
- Cisco::Vlan
- Defined in:
- lib/cisco_node_utils/vlan.rb
Constant Summary collapse
- @@node =
Node.instance
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#vlan_id ⇒ Object
readonly
Returns the value of attribute vlan_id.
Class Method Summary collapse
Instance Method Summary collapse
- #add_interface(interface) ⇒ Object
- #cli_error_check(result) ⇒ Object
- #create ⇒ Object
- #default_shutdown ⇒ Object
- #default_state ⇒ Object
- #default_vlan_name ⇒ Object
- #destroy ⇒ Object
-
#initialize(vlan_id, instantiate = true) ⇒ Vlan
constructor
A new instance of Vlan.
- #interfaces ⇒ Object
- #remove_interface(interface) ⇒ Object
- #shutdown ⇒ Object
- #shutdown=(val) ⇒ Object
- #state ⇒ Object
- #state=(str) ⇒ Object
- #vlan_name ⇒ Object
- #vlan_name=(str) ⇒ Object
Constructor Details
#initialize(vlan_id, instantiate = true) ⇒ Vlan
Returns a new instance of Vlan.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/cisco_node_utils/vlan.rb', line 26 def name @name end |
#vlan_id ⇒ Object (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
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 |
#create ⇒ Object
50 51 52 |
# File 'lib/cisco_node_utils/vlan.rb', line 50 def create @@node.config_set("vlan", "create", @vlan_id) end |
#default_shutdown ⇒ Object
130 131 132 |
# File 'lib/cisco_node_utils/vlan.rb', line 130 def default_shutdown @@node.config_get_default("vlan", "shutdown") end |
#default_state ⇒ Object
111 112 113 |
# File 'lib/cisco_node_utils/vlan.rb', line 111 def default_state @@node.config_get_default("vlan", "state") end |
#default_vlan_name ⇒ Object
84 85 86 |
# File 'lib/cisco_node_utils/vlan.rb', line 84 def default_vlan_name "VLAN%04d" % @vlan_id end |
#destroy ⇒ Object
54 55 56 |
# File 'lib/cisco_node_utils/vlan.rb', line 54 def destroy @@node.config_set("vlan", "destroy", @vlan_id) end |
#interfaces ⇒ Object
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 |
#shutdown ⇒ Object
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 |
#state ⇒ Object
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_name ⇒ Object
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 |