Class: Rbeapi::Api::Staticroutes

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

Overview

The Staticroutes class provides a configuration instance for working with static routes in EOS.

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(destination, nexthop, opts = {}) ⇒ Boolean

Creates a static route in EOS. May add or overwrite an existing route.

Commands

ip route <destination> <nexthop> [router_ip] [distance] [tag <tag>]
  [name <name>]

Parameters:

  • destination (String)

    The destination and prefix matching the route(s). Ex ‘192.168.0.2/24’.

  • nexthop (String)

    The nexthop for this entry, which may an IP address or interface name.

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

    Additional options for the route entry.

Options Hash (opts):

  • router_ip (String)

    If nexthop is an egress interface, router_ip specifies the router to which traffic will be forwarded.

  • distance (String)

    The administrative distance (metric).

  • tag (String)

    The route tag.

  • name (String)

    A route name.

Returns:

  • (Boolean)

    Returns True on success, otherwise False.



113
114
115
116
117
118
119
120
# File 'lib/rbeapi/api/staticroutes.rb', line 113

def create(destination, nexthop, opts = {})
  cmd = "ip route #{destination} #{nexthop}"
  cmd << " #{opts[:router_ip]}" if opts[:router_ip]
  cmd << " #{opts[:distance]}" if opts[:distance]
  cmd << " tag #{opts[:tag]}" if opts[:tag]
  cmd << " name #{opts[:name]}" if opts[:name]
  configure cmd
end

#delete(destination, nexthop = nil) ⇒ Boolean

Removes a given route from EOS. May remove multiple routes if nexthop is not specified.

Commands

no ip route <destination> [nexthop]

Parameters:

  • destination (String)

    The destination and prefix matching the route(s). Ex ‘192.168.0.2/24’.

  • nexthop (String) (defaults to: nil)

    The nexthop for this entry, which may an IP address or interface name.

Returns:

  • (Boolean)

    Returns True on success, otherwise False.



136
137
138
139
140
# File 'lib/rbeapi/api/staticroutes.rb', line 136

def delete(destination, nexthop = nil)
  cmd = "no ip route #{destination}"
  cmd << " #{nexthop}" if nexthop
  configure cmd
end

#getallArray<Hash, Hash>

Returns the static routes configured on the node.

Examples:

{
  [
    {
      destination: <route_dest/masklen>,
      nexthop: next_hop>,
      distance: <integer>,
      tag: <integer, nil>,
      name: <string, nil>
    },
    ...
  ]
}

Returns:

  • (Array<Hash, Hash>)

    The method will return all of the configured static routes on the node as a Ruby array object containing a list of hashes with each hash describing a route. If there are no static routes configured, this method will return an empty array.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbeapi/api/staticroutes.rb', line 67

def getall
  regex = /
    (?<=^ip\sroute\s)
    ([^\s]+)\s                # capture destination
    ([^\s$]+)                 # capture next hop IP or egress interface
    [\s|$](\d+)               # capture metric (distance)
    [\s|$]{1}(?:tag\s(\d+))?  # catpure route tag
    [\s|$]{1}(?:name\s(.+))?  # capture route name
  /x

  routes = config.scan(regex)

  routes.each_with_object([]) do |route, arry|
    arry << { destination: route[0],
              nexthop: route[1],
              distance: route[2],
              tag: route[3],
              name: route[4] }
  end
end