Skip to content

Commit

Permalink
add support for multi/single-line prefix list output
Browse files Browse the repository at this point in the history
	- update 'get' and 'getall' to be able to handle multi/single-line
	- add single-line prefix list to unit test fixture
	- update system spec tests to use also single-line
  • Loading branch information
Florin Vinti committed Sep 30, 2016
1 parent 96da647 commit 01beff9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
16 changes: 11 additions & 5 deletions lib/rbeapi/api/prefixlists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@ class Prefixlists < Entity
# array of hashes, where each prefix is a hash object.
# If the prefix list is not found, a nil object is returned.
def get(name)
config = get_block("ip prefix-list #{name}")
return nil unless config
return nil unless config =~ /ip prefix-list #{name}/
# single-line prefix list
if config =~ /ip prefix-list #{name}\sseq/
entries = config.scan(/^(?:ip prefix-list #{name} seq\s)(\d+)\s(permit|deny)\s(.+)$/)
# or multi-line
else
prefix_list = get_block("ip prefix-list #{name}")
entries = prefix_list.scan(/^\s{3}(?:seq\s)(\d+)\s(permit|deny)\s(.+)$/)
end

entries = config.scan(/^\s{3}(?:seq\s)(\d+)\s(permit|deny)\s(.+)$/)
entries.each_with_object([]) do |entry, arry|
arry << { 'seq' => entry[0], 'action' => entry[1],
'prefix' => entry[2] }
Expand Down Expand Up @@ -116,8 +122,8 @@ def get(name)
# If there are no prefix lists configured, an empty hash will
# be returned.
def getall
lists = config.scan(/(?<=^ip\sprefix-list\s).+/)
lists.each_with_object({}) do |name, hsh|
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
Expand Down
28 changes: 12 additions & 16 deletions spec/system/rbeapi/api/prefixlists_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@
describe '#get' do
before do
node.config(['no ip prefix-list test1',
'ip prefix-list test1',
'seq 10 permit 1.2.3.0/24',
'seq 20 permit 2.3.4.0/24 le 30',
'seq 30 deny 3.4.5.0/24 ge 26 le 30',
'permit 5.6.7.16/28 eq 29'])
'ip prefix-list test1 seq 10 permit 1.2.3.0/24',
'ip prefix-list test1 seq 20 permit 2.3.4.0/24 le 30',
'ip prefix-list test1 seq 30 deny 3.4.5.0/24 ge 26 le 30',
'ip prefix-list test1 permit 5.6.7.16/28 eq 29'])
end

let(:prefixlist) { subject.get('test1') }
Expand Down Expand Up @@ -107,17 +106,14 @@

before do
node.config(del_pref_lists +
['ip prefix-list test1',
'seq 10 permit 1.2.3.0/24',
'seq 20 permit 2.3.4.0/24 le 30',
'seq 30 deny 3.4.5.0/24 ge 26 le 30',
'permit 5.6.7.8/28',
'ip prefix-list test2',
'seq 10 permit 10.11.0.0/16',
'seq 20 permit 10.12.0.0/16 le 24',
'ip prefix-list test3'])
['ip prefix-list test1 seq 10 permit 1.2.3.0/24',
'ip prefix-list test1 seq 20 permit 2.3.4.0/24 le 30',
'ip prefix-list test1 seq 30 deny 3.4.5.0/24 ge 26 le 30',
'ip prefix-list test1 permit 5.6.7.8/28',
'ip prefix-list test2 seq 10 permit 10.11.0.0/16',
'ip prefix-list test2 seq 20 permit 10.12.0.0/16 le 24',
'ip prefix-list test3 permit 10.13.0.0/16'])
end

let(:prefixlists) { subject.getall }

it 'returns the collection as hash' do
Expand All @@ -129,7 +125,7 @@
end

it 'has three prefix lists' do
expect(prefixlists.size).to eq(3)
expect(prefixlists.size).to eq(3)
end
end

Expand Down
13 changes: 10 additions & 3 deletions spec/unit/rbeapi/api/prefixlists/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ def prefixlists
"prefix" => "10.12.0.0/16 le 24"
}
],
"test3" => []
"test3" => [],
"test4" => [
{
"seq" => "10",
"action" => "permit",
"prefix" => "10.14.0.0/16 le 20"
}
]
}
end

Expand All @@ -143,8 +150,8 @@ def prefixlists
expect(resource).to be_a_kind_of(Hash)
end

it 'has three prefix lists' do
expect(resource.size).to eq(3)
it 'has four prefix lists' do
expect(resource.size).to eq(4)
end
end

Expand Down
2 changes: 2 additions & 0 deletions spec/unit/rbeapi/api/prefixlists/fixture_prefixlists.text
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ ip prefix-list test2
seq 20 permit 10.12.0.0/16 le 24
!
ip prefix-list test3
!
ip prefix-list test4 seq 10 permit 10.14.0.0/16 le 20
!

0 comments on commit 01beff9

Please sign in to comment.