Class: Rbeapi::Api::Switchports
- Defined in:
- lib/rbeapi/api/switchports.rb
Overview
The Switchport class provides a base class instance for working with logical layer-2 interfaces.
Instance Attribute Summary
Attributes inherited from Entity
Instance Method Summary collapse
-
#create(name) ⇒ Boolean
Creates a new logical switchport interface in EOS.
-
#default(name) ⇒ Boolean
Defaults a logical switchport interface in the running-config.
-
#delete(name) ⇒ Boolean
Deletes a logical switchport interface from the running-config.
-
#get(name) ⇒ Hash
Retrieves the properties for a logical switchport from the running-config using eAPI.
-
#getall ⇒ Array
Retrieves all switchport interfaces from the running-config.
-
#set_access_vlan(name, opts = {}) ⇒ Boolean
Configures the access port vlan for the specified interface.
-
#set_mode(name, opts = {}) ⇒ Boolean
Configures the switchport mode for the specified interface.
-
#set_trunk_allowed_vlans(name, opts = {}) ⇒ Boolean
set_trunk_allowed_vlans configures the list of vlan ids that are allowed on the specified trunk port.
-
#set_trunk_groups(name, opts = {}) ⇒ Boolean
Configures the trunk group vlans for the specified interface.
-
#set_trunk_native_vlan(name, opts = {}) ⇒ Boolean
Configures the trunk port native vlan for the specified interface.
Methods inherited from Entity
#command_builder, #configure, #configure_interface, #get_block, #initialize, instance
Constructor Details
This class inherits a constructor from Rbeapi::Api::Entity
Instance Method Details
#create(name) ⇒ Boolean
Creates a new logical switchport interface in EOS
203 204 205 |
# File 'lib/rbeapi/api/switchports.rb', line 203 def create(name) configure ["interface #{name}", 'no ip address', 'switchport'] end |
#default(name) ⇒ Boolean
Defaults a logical switchport interface in the running-config
223 224 225 |
# File 'lib/rbeapi/api/switchports.rb', line 223 def default(name) configure ["interface #{name}", 'default switchport'] end |
#delete(name) ⇒ Boolean
Deletes a logical switchport interface from the running-config
213 214 215 |
# File 'lib/rbeapi/api/switchports.rb', line 213 def delete(name) configure ["interface #{name}", 'no switchport'] end |
#get(name) ⇒ Hash
Retrieves the properties for a logical switchport from the running-config using eAPI
Example
{
"name": <String>,
"mode": [access, trunk],
"trunk_allowed_vlans": array<strings>
"trunk_native_vlan": <Integer>,
"access_vlan": <Integer>
}
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rbeapi/api/switchports.rb', line 62 def get(name) config = get_block("interface #{name}") return nil unless config return nil if /no\sswitchport$/ =~ config response = {} response.merge!(parse_mode(config)) response.merge!(parse_access_vlan(config)) response.merge!(parse_trunk_native_vlan(config)) response.merge!(parse_trunk_allowed_vlans(config)) response.merge!(parse_trunk_groups(config)) response end |
#getall ⇒ Array
Retrieves all switchport interfaces from the running-config
189 190 191 192 193 194 195 |
# File 'lib/rbeapi/api/switchports.rb', line 189 def getall interfaces = config.scan(/(?<=^interface\s)([Et|Po].+)$/) interfaces.each_with_object({}) do |port, hsh| cfg = get port.first hsh[port.first] = cfg if cfg end end |
#set_access_vlan(name, opts = {}) ⇒ Boolean
Configures the access port vlan for the specified interface. This value is only valid if the switchport mode is configure in access mode.
327 328 329 330 |
# File 'lib/rbeapi/api/switchports.rb', line 327 def set_access_vlan(name, opts = {}) cmd = command_builder('switchport access vlan', opts) configure_interface(name, cmd) end |
#set_mode(name, opts = {}) ⇒ Boolean
Configures the switchport mode for the specified interface.
238 239 240 241 |
# File 'lib/rbeapi/api/switchports.rb', line 238 def set_mode(name, opts = {}) cmd = command_builder('switchport mode', opts) configure_interface(name, cmd) end |
#set_trunk_allowed_vlans(name, opts = {}) ⇒ Boolean
set_trunk_allowed_vlans configures the list of vlan ids that are allowed on the specified trunk port. If the enable option is set to false, then the allowed trunks is configured using the no keyword. If the default keyword is provided then the allowed trunks is configured using the default keyword The default option takes precedence over the enable option if both are specified
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/rbeapi/api/switchports.rb', line 270 def set_trunk_allowed_vlans(name, opts = {}) value = opts[:value] enable = opts.fetch(:enable, true) default = opts[:default] || false if value fail ArgumentError, 'value must be an Array' unless value.is_a?(Array) value = value.map(&:inspect).join(',') end case default when true cmds = 'default switchport trunk allowed vlan' when false if !enable cmds = 'no switchport trunk allowed vlan' else cmds = ['switchport trunk allowed vlan none', "switchport trunk allowed vlan #{value}"] end end configure_interface(name, cmds) end |
#set_trunk_groups(name, opts = {}) ⇒ Boolean
Configures the trunk group vlans for the specified interface. Trunk groups not currently set are added and trunk groups currently configured but not in the passed in value array are removed.
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/rbeapi/api/switchports.rb', line 346 def set_trunk_groups(name, opts = {}) default = opts.fetch(:default, false) if default return configure_interface(name, 'default switchport trunk group') end enable = opts.fetch(:enable, true) unless enable return configure_interface(name, 'no switchport trunk group') end value = opts.fetch(:value, []) fail ArgumentError, 'value must be an Array' unless value.is_a?(Array) value = Set.new value current_value = Set.new get(name)[:trunk_groups] cmds = [] # Add trunk groups that are not currently in the list value.difference(current_value).each do |group| cmds << "switchport trunk group #{group}" end # Remove trunk groups that are not in the new list current_value.difference(value).each do |group| cmds << "no switchport trunk group #{group}" end configure_interface(name, cmds) if cmds.length > 0 end |
#set_trunk_native_vlan(name, opts = {}) ⇒ Boolean
Configures the trunk port native vlan for the specified interface. This value is only valid if the switchport mode is configure as trunk.
308 309 310 311 |
# File 'lib/rbeapi/api/switchports.rb', line 308 def set_trunk_native_vlan(name, opts = {}) cmd = command_builder('switchport trunk native vlan', opts) configure_interface(name, cmd) end |