Class: Rbeapi::Api::BgpNeighbors

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

Overview

The BgpNeighbors class implements BGP neighbor configuration

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

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

create will create a new instance of a BGP neighbor on the node. The neighbor is created in the shutdown state and then enabled.

Parameters:

  • :name (String)

    The name of the BGP neighbor to manage. This value can be either an IPv4 address or string (in the case of managing a peer group).

Returns:

  • (Boolean)

    returns true if the command completed successfully



526
527
528
# File 'lib/rbeapi/api/bgp.rb', line 526

def create(name)
  set_shutdown(name, enable: false)
end

#delete(name) ⇒ Boolean

delete will delete the BGP neighbor from the node.

Parameters:

  • :name (String)

    The name of the BGP neighbor to manage. This value can be either an IPv4 address or string (in the case of managing a peer group).

Returns:

  • (Boolean)

    returns true if the command completed successfully



543
544
545
546
547
548
549
550
551
# File 'lib/rbeapi/api/bgp.rb', line 543

def delete(name)
  cmd = "no neighbor #{name}"
  response = configure_bgp(cmd)
  unless response
    cmd = "no neighbor #{name} peer-group"
    response = configure_bgp(cmd)
  end
  response
end

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

get returns a single BGP neighbor entry from the nodes current configuration.

Parameters:

  • :name (String)

    The name of the BGP neighbor to manage. This value can be either an IPv4 address or string (in the case of managing a peer group).

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the BGP neighbor resource as a Hash.



310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rbeapi/api/bgp.rb', line 310

def get(name)
  config = get_block('^router bgp .*')
  return nil unless config

  response = parse_peer_group(config, name)
  response.merge!(parse_remote_as(config, name))
  response.merge!(parse_send_community(config, name))
  response.merge!(parse_shutdown(config, name))
  response.merge!(parse_description(config, name))
  response.merge!(parse_next_hop_self(config, name))
  response.merge!(parse_route_map_in(config, name))
  response.merge!(parse_route_map_out(config, name))
  response
end

#getallnil, Hash<Symbol, Object>

getall returns the collection of all neighbor entries for the BGP router instance.

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns a hash that represents the entire BGP neighbor collection from the nodes running configuration. If there a BGP router is not configured or contains no neighbor entries then this method will return an empty hash.



334
335
336
337
338
339
340
341
342
343
# File 'lib/rbeapi/api/bgp.rb', line 334

def getall
  config = get_block('^router bgp .*')
  return nil unless config

  entries = config.scan(/neighbor ([^\s]+)/)
  entries.uniq.each_with_object({}) do |name, hsh|
    resource = get(name[0])
    hsh[name[0]] = resource if resource
  end
end

#neigh_command_builder(name, cmd, opts) ⇒ String

neigh_command_builder for neighbors which calls command_builder

Parameters:

  • :name (String)

    The name of the BGP neighbor to manage.

  • :cmd (String)

    The command portion of the neighbor command.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (String)

    Returns built command string



569
570
571
# File 'lib/rbeapi/api/bgp.rb', line 569

def neigh_command_builder(name, cmd, opts)
  command_builder("neighbor #{name} #{cmd}", opts)
end

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

set_description associates descriptive text with the specified peer or peer group.

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



765
766
767
# File 'lib/rbeapi/api/bgp.rb', line 765

def set_description(name, opts = {})
  configure_bgp(neigh_command_builder(name, 'description', opts))
end

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

set_next_hop_self configures the switch to list its address as the next hop in routes that it advertises to the specified BGP-speaking neighbor or neighbors in the specified peer group. The value option is not used by this method.

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



690
691
692
693
# File 'lib/rbeapi/api/bgp.rb', line 690

def set_next_hop_self(name, opts = {})
  fail 'set_next_hop_self has the value option set' if opts[:value]
  configure_bgp(neigh_command_builder(name, 'next-hop-self', opts))
end

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

set_peer_group creates a BGP static peer group name.

Parameters:

  • :name (String)

    The IP address of the neighbor

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



592
593
594
# File 'lib/rbeapi/api/bgp.rb', line 592

def set_peer_group(name, opts = {})
  configure_bgp(neigh_command_builder(name, 'peer-group', opts))
end

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

set_remote_as configures the expected AS number for a neighbor (peer).

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



616
617
618
# File 'lib/rbeapi/api/bgp.rb', line 616

def set_remote_as(name, opts = {})
  configure_bgp(neigh_command_builder(name, 'remote-as', opts))
end

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

set_route_map_in command applies a route map to inbound BGP routes.

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



715
716
717
718
# File 'lib/rbeapi/api/bgp.rb', line 715

def set_route_map_in(name, opts = {})
  cmd = neigh_command_builder(name, 'route-map', opts) + ' in'
  configure_bgp(cmd)
end

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

set_route_map_out command applies a route map to outbound BGP routes.

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



740
741
742
743
# File 'lib/rbeapi/api/bgp.rb', line 740

def set_route_map_out(name, opts = {})
  cmd = neigh_command_builder(name, 'route-map', opts) + ' out'
  configure_bgp(cmd)
end

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

set_send_community configures the switch to send community attributes to the specified BGP neighbor. The value option is not used by this method.

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



665
666
667
668
# File 'lib/rbeapi/api/bgp.rb', line 665

def set_send_community(name, opts = {})
  fail 'send_community has the value option set' if opts[:value]
  configure_bgp(neigh_command_builder(name, 'send-community', opts))
end

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

set_shutdown disables the specified neighbor. The value option is not used by this method.

Parameters:

  • :name (String)

    The IP address or name of the peer group.

  • :opts (hash)

    Optional keyword arguments

Returns:

  • (Boolean)

    returns true if the command complete successfully



638
639
640
641
642
643
644
# File 'lib/rbeapi/api/bgp.rb', line 638

def set_shutdown(name, opts = {})
  fail 'set_shutdown has value option set' if opts[:value]
  # Shutdown semantics are opposite of enable semantics so invert enable
  value = !opts[:enable]
  opts.merge!(enable: value)
  configure_bgp(neigh_command_builder(name, 'shutdown', opts))
end