Class: PuppetX::Eos::Portchannel

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

Overview

The Portchannel class provides a base class instance for working with logical link aggregation interfaces.

Instance Method Summary collapse

Constructor Details

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

Initialize innstance of Portchannel

Parameters:



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

def initialize(api)
  @api = api
end

Instance Method Details

#add_member(name, member) ⇒ Boolean

Adds a new member interface to the channel group

Parameters:

  • name (String)

    The name of the port channel to add the interface

  • member (String)

    The name of the interface to add

Returns:

  • (Boolean)

    True if the create succeeds otherwise False



147
148
149
150
151
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 147

def add_member(name, member)
  id = name.match(/\d+/)
  @api.config(["interface #{member}",
               "channel-group #{id} mode on"]) == [{}, {}]
end

#create(name) ⇒ Boolean

Create a logical port-channel interface

Parameters:

  • name (String)

    The name of the interface to create

Returns:

  • (Boolean)

    True if the create succeeds otherwise False



116
117
118
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 116

def create(name)
  @api.config("interface #{name}") == [{}]
end

#default(name) ⇒ Boolean

Defaults a logical port-channel interface

Parameters:

  • name (String)

    The name of the interface to create

Returns:

  • (Boolean)

    True if the create succeeds otherwise False



136
137
138
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 136

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

#delete(name) ⇒ Boolean

Deletes a logical port-channel interface

Parameters:

  • name (String)

    The name of the interface to create

Returns:

  • (Boolean)

    True if the create succeeds otherwise False



126
127
128
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 126

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

#get(name) ⇒ Hash

Retrievess a port channel interface from the running-configuration

Example

{
  "name": <String>,
  "lacp_mode": [active, passive, off],
  "members": [Array],
  "lacp_fallback": [static, individual],
  "lacp_timeout": <0-900>
}

Parameters:

  • name (String)

    The name of the port-channel interface

Returns:

  • (Hash)

    A hash of the port channel attributes and properties



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

def get(name)
  members = get_members name
  result = @api.enable("show interfaces #{name}")
  interface = result.first['interfaces']

  attr_hash = { name: name }
  attr_hash[:members] = members
  attr_hash[:lacp_mode] = get_lacp_mode members
  attr_hash[:lacp_fallback] = get_lacp_fallback interface
  attr_hash[:lacp_timeout] = interface['fallbackTimeout']
  attr_hash
end

#get_members(name) ⇒ Array

Retreives the member interfaces for the specified channel group

Parameters:

  • name (String)

    The name of the port-channel interface to return members for

Returns:

  • (Array)

    An array of interface names that are members of the specified channel group id



104
105
106
107
108
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 104

def get_members(name)
  result = @api.enable("show #{name} all-ports",
                       format: 'text')
  result.first['output'].scan(/Ethernet\d+/)
end

#getallArray

Retrieves all logical port-channel interfaces from the running config

Returns:

  • (Array)

    an array of port-channel attribute hashes



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

def getall
  result = @api.enable('show interfaces')
  interfaces = result.first['interfaces']
  resp = []
  interfaces.each do |key, value|
    resp << get(key) if value['hardware'] == 'portChannel'
  end
  resp
end

#remove_member(_name, member) ⇒ Boolean

Removes a member interface from the channel group

Parameters:

  • name (String)

    The name of the port-channel to add the interface

  • member (String)

    The name of the interface to remove

Returns:

  • (Boolean)

    True if the create succeeds otherwise False



160
161
162
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 160

def remove_member(_name, member)
  @api.config(["interface #{member}", 'no channel-group']) == [{}, {}]
end

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

Configures the lacp fallback value

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 value to

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 213

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

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

#set_lacp_mode(name, mode) ⇒ Boolean

Configures the lacp mode for the interface

Parameters:

  • name (String)

    The name of the port-channel interface

  • mode (String)

    The LACP mode to configure

Returns:

  • (Boolean)

    True if the create succeeds otherwise False



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

def set_lacp_mode(name, mode)
  id = name.match(/\d+/)
  members = get_members name

  commands = []
  config = []

  members.each do |member|
    commands << "interface #{member}" << 'no channel-group'
    config << "interface #{member}" << "channel-group #{id} mode #{mode}"
  end

  config.unshift(*commands)
  result =  @api.config(config)
  config.size == result.size
end

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

Configures the lacp fallback timeout value

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 timeout to

  • :default (Boolean)

    The value should be set to default

Returns:

  • (Boolean)

    True if the commands succeed otherwise False



237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 237

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

  cmds = ["interface #{name}"]
  case default
  when true
    cmds << 'default port-channel lacp fallback timeout'
  when false
    cmds << (value.nil? ? 'no port-channel lacp fallback timeout' : \
                          "port-channel lacp fallback timeout #{value}")
  end
  @api.config(cmds) == [{}, {}]
end

#set_members(name, members) ⇒ Object

Configures the member interfaces for the port-channel interface

Parameters:

  • name (String)

    The name of the port-channel to assign the the members to

  • members (Array)

    The array of members to add to the port-channel



170
171
172
173
174
175
176
177
178
# File 'lib/puppet_x/eos/modules/portchannel.rb', line 170

def set_members(name, members)
  current = get_members(name)
  (current - members).each do |member|
    remove_member(name, member)
  end
  (members - current).each do |member|
    add_member(name, member)
  end
end