Class: Rbeapi::Api::Prefixlists

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

Overview

The Prefixlists 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

#add_rule(name, action, prefix, seq = nil) ⇒ Boolean

Creates an ip prefix-list with the designated name,

seqno, action and prefix.

Parameters:

  • name (String)

    The name of the ip prefix-list.

  • seq (String) (defaults to: nil)

    The seq value.

  • action (String)

    The action value.

  • prefix (String)

    The prefix value.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.



159
160
161
162
163
164
# File 'lib/rbeapi/api/prefixlists.rb', line 159

def add_rule(name, action, prefix, seq = nil)
  cmd = "ip prefix-list #{name}"
  cmd << " seq #{seq}" if seq
  cmd << " #{action} #{prefix}"
  configure(cmd)
end

#create(name) ⇒ Boolean

Creates a new ip prefix-list with the designated name.

Parameters:

  • name (String)

    The name of the ip prefix-list.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.



142
143
144
# File 'lib/rbeapi/api/prefixlists.rb', line 142

def create(name)
  configure("ip prefix-list #{name}")
end

#delete(name, seq = nil) ⇒ Boolean

Removes the designated prefix-list.

Parameters:

  • name (String)

    The name of the ip prefix-list.

  • seq (String) (defaults to: nil)

    The seq value.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.



174
175
176
177
178
# File 'lib/rbeapi/api/prefixlists.rb', line 174

def delete(name, seq = nil)
  cmd = "no ip prefix-list #{name}"
  cmd << " seq #{seq}" if seq
  configure(cmd)
end

#get(name) ⇒ Array<Hash>

Returns the prefix list configured on the node.

Examples:

[
  {
    seq: <string>,
    action: <string>,
    prefix: <string>
  },
  ...,
  {
    seq: <string>,
    action: <string>,
    prefix: <string>
  }
]

Parameters:

  • name (String)

    The name of the prefix-list to return.

Returns:

  • (Array<Hash>)

    The method will return the configured prefix list on the node with all its sequences as a Ruby array of hashes, where each prefix is a hash object. If the prefix list is not found, a nil object is returned.



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

def get(name)
  return nil unless config =~ /ip prefix-list #{name}/

  # single-line prefix list
  if config =~ /ip prefix-list #{name}\sseq/
    entries = config.scan(/^(?:ip\sprefix-list\s#{name}\sseq\s)(\d+)\s
                          (permit|deny)\s(.+)$/x)
  # or multi-line
  else
    prefix_list = get_block("ip prefix-list #{name}")
    entries = prefix_list.scan(/^\s+(?:seq\s)(\d+)\s
                               (permit|deny)\s(.+)$/x)
  end

  entries.each_with_object([]) do |entry, arry|
    arry << { 'seq' => entry[0],
              'action' => entry[1],
              'prefix' => entry[2] }
  end
end

#getallHash<String, Array>

Returns all prefix lists configured on the node.

Examples:

{
  <name1>: [
    {
      seq: <string>,
      action: <string>,
      prefix: <string>
    },
    ...
    {
      seq: <string>,
      action: <string>,
      prefix: <string>
    }
  ],
  ...,
  <nameN>: [
    {
      seq: <string>,
      action: <string>,
      prefix: <string>
    },
    ...
    {
      seq: <string>,
      action: <string>,
      prefix: <string>
    }
  ]
}

Returns:

  • (Hash<String, Array>)

    The method will return all the prefix lists configured on the node as a Ruby hash object. If there are no prefix lists configured, an empty hash will be returned.



128
129
130
131
132
133
134
# File 'lib/rbeapi/api/prefixlists.rb', line 128

def getall
  lists = config.scan(/(?<=^ip\sprefix-list\s)[^\s]+(?=\sseq.+)?/)
  lists.uniq.each_with_object({}) do |name, hsh|
    values = get name
    hsh[name] = values if values
  end
end