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.



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

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

#delete(name) ⇒ Boolean

delete will delete the BGP neighbor from the node.

Commands

no neighbor <name>
  or
no neighbor <name> peer-group

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.



733
734
735
736
737
738
739
740
741
# File 'lib/rbeapi/api/bgp.rb', line 733

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.

Examples:

{
  peer_group: <string>,
  remote_as: <string>,
  send_community: <string>,
  shutdown: <boolean>,
  description: <integer>
  next_hop_self: <boolean>
  route_map_in: <string>
  route_map_out: <string>
}

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.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/rbeapi/api/bgp.rb', line 464

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.

Examples:

{
  <name>: {
    peer_group: <string>,
    remote_as: <string>,
    send_community: <string>,
    shutdown: <boolean>,
    description: <integer>
    next_hop_self: <boolean>
    route_map_in: <string>
    route_map_out: <string>
  },
  <name>: {
    peer_group: <string>,
    remote_as: <string>,
    send_community: <string>,
    shutdown: <boolean>,
    description: <integer>
    next_hop_self: <boolean>
    route_map_in: <string>
    route_map_out: <string>
  },
  ...
}

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.



513
514
515
516
517
518
519
520
521
522
# File 'lib/rbeapi/api/bgp.rb', line 513

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.

Options Hash (opts):

  • value (String)

    Value being set.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the command using the default keyword.

Returns:

  • (String)

    Returns built command string.



761
762
763
# File 'lib/rbeapi/api/bgp.rb', line 761

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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> description <string>

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    The description string.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



965
966
967
# File 'lib/rbeapi/api/bgp.rb', line 965

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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> next-hop-self

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • enable (String)

    True enables the feature. False disables the feature.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



887
888
889
890
# File 'lib/rbeapi/api/bgp.rb', line 887

def set_next_hop_self(name, opts = {})
  raise '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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> peer-group <group-name>

Parameters:

  • name (String)

    The IP address of the neighbor.

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

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    The group name.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



785
786
787
# File 'lib/rbeapi/api/bgp.rb', line 785

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).

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> remote-as <as-id>

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    The remote as-id.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



810
811
812
# File 'lib/rbeapi/api/bgp.rb', line 810

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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> route-map <name> in

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    Name of a route map.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



913
914
915
916
# File 'lib/rbeapi/api/bgp.rb', line 913

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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> route-map <name> out

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    Name of a route map.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



939
940
941
942
# File 'lib/rbeapi/api/bgp.rb', line 939

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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> send-community

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • enable (String)

    True enables the feature. False disables the feature.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



861
862
863
864
# File 'lib/rbeapi/api/bgp.rb', line 861

def set_send_community(name, opts = {})
  raise '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.

Commands

router bgp <bgp_as>
  {no | default} neighbor <name> shutdown

Parameters:

  • name (String)

    The IP address or name of the peer group.

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

    Optional keyword arguments.

Options Hash (opts):

  • enable (String)

    True enables the specified neighbor. False disables the specified neighbor.

  • default (Boolean)

    Configure the peer group using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command complete successfully.



833
834
835
836
837
838
839
# File 'lib/rbeapi/api/bgp.rb', line 833

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