Class: Rbeapi::Api::Vlans

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/vlans.rb

Overview

The Vlan class provides a class implementation for working with the collection of Vlans on the node. This class presents an abstraction of the nodes configured vlan id's from the running configuration.

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#configure, #get_block, #initialize, instance

Constructor Details

This class inherits a constructor from Rbeapi::Api::Entity

Instance Method Details

#add_trunk_group(id, value) ⇒ Boolean

add_trunk_group adds a new trunk group value to the specified vlan id in the nodes running configuration. The trunk group name value accepts a-z 0-9 and _

Parameters:

  • :id (String, Integer)

    The vlan id to apply the configuration to. the id value should be in the range of 1 to 4094

  • :value (String)

    The value to add to the vlan id configuration on the node.

Returns:

  • (Boolean)

    returns true if the command completed successfully

Version:

  • 4.13.7M


312
313
314
# File 'lib/rbeapi/api/vlans.rb', line 312

def add_trunk_group(id, value)
  configure(["vlan #{id}", "trunk group #{value}"])
end

#create(id) ⇒ Boolean

create will create a new vlan resource in the nodes current configuration with the specified vlan id. If the create method is called and the vlan id already exists, this method will still return true.

Parameters:

  • :id (String, Integer)

    The vlan id to create on the node. The vlan id must be in the valid range of 1 to 4094

Returns:

  • (Boolean)

    returns true if the command completed successfully


160
161
162
# File 'lib/rbeapi/api/vlans.rb', line 160

def create(id)
  configure("vlan #{id}")
end

#default(id) ⇒ Boolean

default will configure the vlan using the default keyword. This command has the same effect as deleting the vlan from the nodes running configuration.

Parameters:

  • :id (String, Integer)

    The vlan id to default in the nodes configuration. Ths vid value should be in the valid range of 1 to 4094

Returns:

  • (Boolean)

    returns true if the command complete successfully


197
198
199
# File 'lib/rbeapi/api/vlans.rb', line 197

def default(id)
  configure("default vlan #{id}")
end

#delete(id) ⇒ Boolean

delete will delete an existing vlan resource from the nodes current running configuration. If the delete method is called and the vlan id does not exist, this method will succeed.

Parameters:

  • :id (String, Integer)

    The vlan id to delete from the node. The id value should be in the valid range of 1 to 4094

Returns:

  • (Boolean)

    returns true if the command completed successfully


178
179
180
# File 'lib/rbeapi/api/vlans.rb', line 178

def delete(id)
  configure("no vlan #{id}")
end

#get(id) ⇒ nil, Hash<Symbol, Object>

get returns the specified vlan resource Hash that represents the nodes current vlan configuration.

Examples:

{
  name: <string>
  state: <string>
  trunk_groups: array[<string]
}

Parameters:

  • id (String)

    The vlan id to return a resource for from the nodes configuration

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the vlan resource as a Hash. If the specified vlan id is not found in the nodes current configuration a nil object is returned


65
66
67
68
69
70
71
72
73
# File 'lib/rbeapi/api/vlans.rb', line 65

def get(id)
  config = get_block("vlan #{id}")
  return nil unless config
  response = {}
  response.merge!(parse_name(config))
  response.merge!(parse_state(config))
  response.merge!(parse_trunk_groups(config))
  response
end

#getallHash<Symbol, Object>

getall returns the collection of vlan resources from the nodes running configuration as a hash. The vlan resource collection hash is keyed by the unique vlan id

Examples:

{
  <vlanid>: {...}
}

Returns:

  • (Hash<Symbol, Object>)

    returns a hash that represents the entire vlan collection from the nodes running configuration. If there are no vlans configured, this method will return an empty hash

See Also:


91
92
93
94
95
96
97
# File 'lib/rbeapi/api/vlans.rb', line 91

def getall
  vlans = config.scan(/(?<=^vlan\s)\d+$/)
  vlans.each_with_object({}) do |vid, hsh|
    resource = get vid
    hsh[vid] = resource if resource
  end
end

#remove_trunk_group(id, value) ⇒ Object

remove_trunk_group removes the specified trunk group value from the specified vlan id in the node's configuration. If the trunk group name does not exist, this method will return success

Parameters:

  • :id (String, Integer)

    The vlan id to apply the configuration to. the id value should be in the range of 1 to 4094

  • :value (String)

    The value to remove from the list of trunk group names configured for the specified vlan


333
334
335
# File 'lib/rbeapi/api/vlans.rb', line 333

def remove_trunk_group(id, value)
  configure(["vlan #{id}", "no trunk group #{value}"])
end

#set_name(id, opts = {}) ⇒ Boolean

set_name configures the name value for the specified vlan id in the nodes running configuration. If the value is not provided in the opts keyword Hash then the name value is negated using the no keyword. If the default keyword is set to true, then the name value is defaulted using the default keyword. The default keyword takes precedence over the value keyword

Parameters:

  • :id (String, Integer)

    The vlan id to apply the configuration to. The id value should be in the valid range of 1 to 4094

  • :opts (Hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command completed successfully


230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rbeapi/api/vlans.rb', line 230

def set_name(id, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  cmds = ["vlan #{id}"]
  case default
  when true
    cmds << 'default name'
  when false
    cmds << (value.nil? ?  'no name' : "name #{value}")
  end
  configure(cmds)
end

#set_state(id, opts = {}) ⇒ Boolean

set_state configures the state value for the specified vlan id in the nodes running configuration. If the value is not provided in the opts keyword Hash then the state value is negated using the no keyword. If the default keyword is set to true, then the state value is defaulted using the default keyword. The default keyword takes precedence over the value keyword

Parameters:

  • :id (String, Integer)

    The vlan id to apply the configuration to. The id value should be in the valid range of 1 to 4094

  • :opts (Hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command completed successfully

Raises:

  • (ArgumentError)

    if the value is not in the accept list of values


276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rbeapi/api/vlans.rb', line 276

def set_state(id, opts = {})
  value = opts[:value]
  default = opts[:default] || false

  unless ['active', 'suspend', nil].include?(value)
    raise ArgumentError, 'state must be active, suspend or nil'
  end

  cmds = ["vlan #{id}"]
  case default
  when true
    cmds << 'default state'
  when false
    cmds << (value.nil? ? 'no state' : "state #{value}")
  end
  configure(cmds)
end