class Rbeapi::Api::BgpNeighbors

The BgpNeighbors class implements BGP neighbor configuration

Public Instance Methods

create(name) click to toggle source

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

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

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/bgp.rb, line 716
def create(name)
  set_shutdown(name, enable: false)
end
delete(name) click to toggle source

delete will delete the BGP neighbor from the node.

Commands

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

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

@return [Boolean] Returns true if the command completed successfully.

# 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) click to toggle source

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

@example

{
  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>
}

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

@return [nil, Hash<Symbol, Object>] Returns the BGP neighbor

resource as a Hash.
# 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
getall() click to toggle source

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

@example

{
  <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>
  },
  ...
}

@return [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.
# 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) click to toggle source

neigh_command_builder for neighbors which calls command_builder.

@param name [String] The name of the BGP neighbor to manage.

@param cmd [String] The command portion of the neighbor command.

@param opts [hash] Optional keyword arguments.

@option opts value [String] Value being set.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the command using

the default keyword.

@return [String] Returns built command string.

# 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 = {}) click to toggle source

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

Commands

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

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts value [String] The description string.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts enable [String] True enables the feature. False

disables the feature.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

set_peer_group creates a BGP static peer group name.

Commands

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

@param name [String] The IP address of the neighbor.

@param opts [hash] Optional keyword arguments.

@option opts value [String] The group name.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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>

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts value [String] The remote as-id.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts value [String] Name of a route map.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts value [String] Name of a route map.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts enable [String] True enables the feature. False

disables the feature.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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 = {}) click to toggle source

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

@param name [String] The IP address or name of the peer group.

@param opts [hash] Optional keyword arguments.

@option opts enable [String] True enables the specified neighbor.

False disables the specified neighbor.

@option opts default [Boolean] Configure the peer group using

the default keyword.

@return [Boolean] Returns true if the command complete successfully.

# 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

Private Instance Methods

configure_bgp(cmd) click to toggle source

configure_bgp adds the command to go to BGP config mode. Then it adds the passed in command. The commands are then passed on to configure.

@api private

@param cmd [String] Command to run under BGP mode.

@return [Boolean] Returns true if the command complete successfully.

# File lib/rbeapi/api/bgp.rb, line 698
def configure_bgp(cmd)
  config = get_block('^router bgp .*')
  raise 'BGP router is not configured' unless config
  bgp_as = Bgp.parse_bgp_as(config)
  cmds = ["router bgp #{bgp_as[:bgp_as]}", cmd]
  configure(cmds)
end
parse_description(config, name) click to toggle source

parse_description scans the BGP neighbor entries for the description.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Returns the resource hash

attribute.
# File lib/rbeapi/api/bgp.rb, line 619
def parse_description(config, name)
  value = config.scan(/neighbor #{name} description (.*)$/)
  description = value[0] ? value[0][0] : nil
  { description: description }
end
parse_next_hop_self(config, name) click to toggle source

parse_next_hop_self scans the BGP neighbor entries for the next hop self.

@api private

@param config [String] The switch config. @param 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).

@return [Hash<Symbol, Object>] Returns the resource hash

attribute.
# File lib/rbeapi/api/bgp.rb, line 639
def parse_next_hop_self(config, name)
  value = config.scan(/no neighbor #{name} next-hop-self/)
  enabled = value[0] ? false : true
  { next_hop_self: enabled }
end
parse_peer_group(config, name) click to toggle source

parse_peer_group scans the BGP neighbor entries for the peer group.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Returns the resource hash attribute.

# File lib/rbeapi/api/bgp.rb, line 537
def parse_peer_group(config, name)
  value = config.scan(/neighbor #{name} peer-group ([^\s]+)/)
  peer_group = value[0] ? value[0][0] : nil
  { peer_group: peer_group }
end
parse_remote_as(config, name) click to toggle source

parse_remote_as scans the BGP neighbor entries for the remote AS.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Returns the resource hash attribute

# File lib/rbeapi/api/bgp.rb, line 557
def parse_remote_as(config, name)
  value = config.scan(/neighbor #{name} remote-as (\d+)/)
  remote_as = value[0] ? value[0][0] : nil
  { remote_as: remote_as }
end
parse_route_map_in(config, name) click to toggle source

parse_route_map_in scans the BGP neighbor entries for the route map in.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Returns the resource hash

attribute.
# File lib/rbeapi/api/bgp.rb, line 660
def parse_route_map_in(config, name)
  value = config.scan(/neighbor #{name} route-map ([^\s]+) in/)
  route_map_in = value[0] ? value[0][0] : nil
  { route_map_in: route_map_in }
end
parse_route_map_out(config, name) click to toggle source

parse_route_map_out scans the BGP neighbor entries for the route map in.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Returns the resource hash

attribute.
# File lib/rbeapi/api/bgp.rb, line 681
def parse_route_map_out(config, name)
  value = config.scan(/neighbor #{name} route-map ([^\s]+) out/)
  route_map_out = value[0] ? value[0][0] : nil
  { route_map_out: route_map_out }
end
parse_send_community(config, name) click to toggle source

parse_send_community scans the BGP neighbor entries for the remote AS.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Returns the resource hash attribute.

# File lib/rbeapi/api/bgp.rb, line 577
def parse_send_community(config, name)
  value = config.scan(/no neighbor #{name} send-community/)
  enabled = value[0] ? false : true
  { send_community: enabled }
end
parse_shutdown(config, name) click to toggle source

parse_shutdown scans the BGP neighbor entries for the remote AS.

@api private

@param config [String] The switch config.

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

@return [Hash<Symbol, Object>] Resource hash attribute. Returns

true if shutdown, false otherwise.
# File lib/rbeapi/api/bgp.rb, line 598
def parse_shutdown(config, name)
  value = config.scan(/no neighbor #{name} shutdown/)
  shutdown = value[0] ? false : true
  { shutdown: shutdown }
end