Class: PuppetX::Eos::Switchport

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/eos/modules/switchport.rb

Overview

The Switchport class provides a base class instance for working with logical layer-2 interfaces.

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ PuppetX::Eos::Switchport

Initialize instance of Switchport

Parameters:



50
51
52
# File 'lib/puppet_x/eos/modules/switchport.rb', line 50

def initialize(api)
  @api = api
end

Instance Method Details

#create(name) ⇒ Boolean

Creates a new logical switchport interface in EOS

Parameters:

  • name (String)

    The name of the logical interface

Returns:

  • (Boolean)

    True if it succeeds otherwise False



102
103
104
105
# File 'lib/puppet_x/eos/modules/switchport.rb', line 102

def create(name)
  @api.config(["interface #{name}", 'no ip address',
               'switchport']) == [{}, {}, {}]
end

#default(name) ⇒ Boolean

Defaults a logical switchport interface in the running-config

Parameters:

  • name (String)

    The name of the logical interface

Returns:

  • (Boolean)

    True if it succeeds otherwise False



123
124
125
126
# File 'lib/puppet_x/eos/modules/switchport.rb', line 123

def default(name)
  @api.config(["interface #{name}",
               'default switchport']) == [{}, {}]
end

#delete(name) ⇒ Boolean

Deletes a logical switchport interface from the running-config

Parameters:

  • name (String)

    The name of the logical interface

Returns:

  • (Boolean)

    True if it succeeds otherwise False



113
114
115
# File 'lib/puppet_x/eos/modules/switchport.rb', line 113

def delete(name)
  @api.config(["interface #{name}", 'no switchport']) == [{}, {}]
end

#get(name) ⇒ Hash

Retrieves the properies for a logical switchport from the running-config using eAPI

Example

{
  "name": <String>,
  "mode": [access, trunk],
  "trunk_allowed_vlans": [],
  "trunk_native_vlan": <Integer>,
  "access_vlan": <Integer>
}

Parameters:

  • name (String)

    The full name of the interface to get. The interface name must be the full interface (ie Ethernet, not Et)

Returns:

  • (Hash)

    a hash that includes the switchport properties



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/puppet_x/eos/modules/switchport.rb', line 71

def get(name)
  result = @api.enable("show interfaces #{name} switchport",
                       format: 'text')
  output = result.first['output']
  attr_hash = { name: name }
  attr_hash[:mode] = mode_to_value output
  attr_hash[:trunk_allowed_vlans] = trunk_vlans_to_value output
  attr_hash[:trunk_native_vlan] = trunk_native_to_value output
  attr_hash[:access_vlan] = access_vlan_to_value output
  attr_hash
end

#getallArray

Retrieves all switchport interfaces from the running-config

Returns:

  • (Array)

    an array of switchport hashes



87
88
89
90
91
92
93
94
# File 'lib/puppet_x/eos/modules/switchport.rb', line 87

def getall
  result = @api.enable('show interfaces')
  switchports = []
  result.first['interfaces'].map do |name, attrs|
    switchports << get(name) if attrs['forwardingModel'] == 'bridged'
  end
  switchports
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.

Parameters:

  • name (String)

    The name of the interface to configure

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface

Options Hash (opts):

  • :value (string)

    The value of the access vlan

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/puppet_x/eos/modules/switchport.rb', line 216

def set_access_vlan(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport access vlan'
  when false
    cmds << (value.nil? ? 'no switchport access vlan' : \
                          "switchport access vlan #{value}")
  end
  @api.config(cmds) == [{}, {}]
end

#set_mode(name, opts = {}) ⇒ Boolean

Configures the switchport mode for the specified interafce. Valid modes are access (default) or trunk

Parameters:

  • name (String)

    The name of the interface to configure

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface

Options Hash (opts):

  • :value (string)

    The value to set the mode to

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet_x/eos/modules/switchport.rb', line 138

def set_mode(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport mode'
  when false
    cmds << (value.nil? ? 'no switchport mode' : \
                          "switchport mode #{value}")
  end
  @api.config(cmds) == [{}, {}]
end

#set_trunk_allowed_vlans(name, opts = {}) ⇒ Boolean

Configures the trunk port allowed vlans for the specified interface. This value is only valid if the switchport mode is configure as trunk.

Parameters:

  • name (String)

    The name of the interface to configure

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface

Options Hash (opts):

  • :value (string)

    The list of vlans to allow

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/puppet_x/eos/modules/switchport.rb', line 164

def set_trunk_allowed_vlans(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport trunk allowed vlan'
  when false
    cmds << (value.nil? ? 'no switchport trunk allowed vlan' : \
                          "switchport trunk allowed vlan #{value}")
  end
  @api.config(cmds) == [{}, {}]
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.

Parameters:

  • name (String)

    The name of the interface to configure

  • opts (Hash) (defaults to: {})

    The configuration parameters for the interface

Options Hash (opts):

  • :value (string)

    The value of the trunk native vlan

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/puppet_x/eos/modules/switchport.rb', line 190

def set_trunk_native_vlan(name, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default switchport trunk native vlan'
  when false
    cmds << (value.nil? ? 'no switchport trunk native vlan' : \
                          "switchport trunk native vlan #{value}")
  end
  @api.config(cmds) == [{}, {}]
end