From 5afffd4fc71e40ceafb306b0e9166d643ad6429b Mon Sep 17 00:00:00 2001 From: Jake Groves Date: Fri, 24 Jan 2020 17:48:17 +0000 Subject: [PATCH] Added support for configuring BGP maximum paths on a BGP neighbor --- lib/rbeapi/api/bgp.rb | 73 ++++++++++++++++--- spec/system/rbeapi/api/bgp_neighbors_spec.rb | 36 +++++++-- .../unit/rbeapi/api/bgp/bgp_neighbors_spec.rb | 20 ++++- spec/unit/rbeapi/api/bgp/bgp_spec.rb | 6 +- 4 files changed, 109 insertions(+), 26 deletions(-) diff --git a/lib/rbeapi/api/bgp.rb b/lib/rbeapi/api/bgp.rb index 09208be..8bfd4eb 100644 --- a/lib/rbeapi/api/bgp.rb +++ b/lib/rbeapi/api/bgp.rb @@ -449,10 +449,11 @@ class BgpNeighbors < Entity # remote_as: , # send_community: , # shutdown: , - # description: - # next_hop_self: - # route_map_in: - # route_map_out: + # description: , + # next_hop_self: , + # route_map_in: , + # route_map_out: , + # maximum_routes: # } # # @param name [String] The name of the BGP neighbor to manage. @@ -473,6 +474,7 @@ def get(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.merge!(parse_maximum_routes(config, name)) response end @@ -487,20 +489,22 @@ def get(name) # remote_as: , # send_community: , # shutdown: , - # description: - # next_hop_self: - # route_map_in: - # route_map_out: + # description: , + # next_hop_self: , + # route_map_in: , + # route_map_out: , + # maximum_routes: # }, # : { # peer_group: , # remote_as: , # send_community: , # shutdown: , - # description: - # next_hop_self: - # route_map_in: - # route_map_out: + # description: , + # next_hop_self: , + # route_map_in: , + # route_map_out: , + # maximum_routes: # }, # ... # } @@ -685,6 +689,26 @@ def parse_route_map_out(config, name) end private :parse_route_map_out + ## + # parse_maximum_routes scans the BGP neighbor entries for the + # maximum routes. + # + # @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] Returns the resource hash attribute + def parse_maximum_routes(config, name) + value = config.scan(/neighbor #{name} maximum-routes (\d+)/) + maximum_routes = value[0] ? value[0][0] : nil + { maximum_routes: maximum_routes } + end + private :parse_maximum_routes + ## # configure_bgp adds the command to go to BGP config mode. # Then it adds the passed in command. The commands are then @@ -965,6 +989,31 @@ def set_route_map_out(name, opts = {}) def set_description(name, opts = {}) configure_bgp(neigh_command_builder(name, 'description', opts)) end + + ## + # set_maximum_routes configures the maximum routes number for a neighbor + # (peer). + # + # ===Commands + # router bgp + # {no | default} neighbor maximum-routes + # + # @param name [String] The IP address or name of the peer group. + # + # @param opts [hash] Optional keyword arguments. + # + # @option opts value [Integer] The maximum routes to receive from the neighbor. + # + # @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. + def set_maximum_routes(name, opts = {}) + configure_bgp(neigh_command_builder(name, 'maximum-routes', opts)) + end end end end diff --git a/spec/system/rbeapi/api/bgp_neighbors_spec.rb b/spec/system/rbeapi/api/bgp_neighbors_spec.rb index 9a54027..1431928 100644 --- a/spec/system/rbeapi/api/bgp_neighbors_spec.rb +++ b/spec/system/rbeapi/api/bgp_neighbors_spec.rb @@ -51,7 +51,8 @@ description: nil, next_hop_self: false, route_map_in: nil, - route_map_out: nil } + route_map_out: nil, + maximum_routes: nil } end before do @@ -70,17 +71,17 @@ 'eBGP_GROUP' => { peer_group: nil, remote_as: nil, send_community: false, shutdown: false, description: nil, next_hop_self: false, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: '12000' }, '192.168.255.1' => { peer_group: 'eBGP_GROUP', remote_as: '65000', send_community: true, shutdown: true, description: nil, next_hop_self: true, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: nil }, '192.168.255.3' => { peer_group: 'eBGP_GROUP', remote_as: '65001', send_community: true, shutdown: true, description: nil, next_hop_self: true, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: nil } } end @@ -115,7 +116,8 @@ description: nil, next_hop_self: true, route_map_in: nil, - route_map_out: nil } + route_map_out: nil, + maximum_routes: nil } end let(:after) do @@ -126,7 +128,8 @@ description: nil, next_hop_self: false, route_map_in: nil, - route_map_out: nil } + route_map_out: nil, + maximum_routes: nil } end before { node.config(['no router bgp 64600', 'router bgp 64600']) } @@ -148,7 +151,8 @@ description: nil, next_hop_self: false, route_map_in: nil, - route_map_out: nil } + route_map_out: nil, + maximum_routes: nil } end let(:after) do @@ -159,7 +163,8 @@ description: nil, next_hop_self: true, route_map_in: nil, - route_map_out: nil } + route_map_out: nil, + maximum_routes: nil } end it 'delete a BGP resource' do @@ -351,4 +356,19 @@ expect(subject.get('eng')[:description]).to eq(nil) end end + + describe '#set_maximum_routes' do + it 'set the maximum routes value' do + expect(subject.get('eng')[:maximum_routes]).to eq('12000') + expect(subject.set_maximum_routes('eng', value: '10')).to be_truthy + expect(subject.get('eng')[:maximum_routes]).to eq('0') + end + + it 'defaults the maximum routes value' do + expect(subject.set_maximum_routes('eng', value: '0')).to be_truthy + expect(subject.set_maximum_routes('eng', default: true)) + .to be_truthy + expect(subject.get('eng')[:maximum_routes]).to eq('12000') + end + end end diff --git a/spec/unit/rbeapi/api/bgp/bgp_neighbors_spec.rb b/spec/unit/rbeapi/api/bgp/bgp_neighbors_spec.rb index df73537..a851bf6 100644 --- a/spec/unit/rbeapi/api/bgp/bgp_neighbors_spec.rb +++ b/spec/unit/rbeapi/api/bgp/bgp_neighbors_spec.rb @@ -55,17 +55,17 @@ 'eBGP_GROUP' => { peer_group: nil, remote_as: nil, send_community: false, shutdown: false, description: nil, next_hop_self: false, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: '12000' }, '192.168.255.1' => { peer_group: 'eBGP_GROUP', remote_as: '65000', send_community: true, shutdown: true, description: nil, next_hop_self: true, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: nil }, '192.168.255.3' => { peer_group: 'eBGP_GROUP', remote_as: '65001', send_community: true, shutdown: true, description: nil, next_hop_self: true, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: nil } } } end @@ -287,4 +287,18 @@ def bgp expect(subject.set_description('eng', default: true)).to be_truthy end end + + describe '#set_maximum_routes' do + it 'set the maximum routes value' do + expect(node).to receive(:config).with(['router bgp 64600', + 'neighbor eng maximum-routes 0']) + expect(subject.set_maximum_routes('eng', value: 0)).to be_truthy + end + + it 'defaults the maximum routes value' do + expect(node).to receive(:config) + .with(['router bgp 64600', 'default neighbor eng maximum-routes']) + expect(subject.set_maximum_routes('eng', default: true)).to be_truthy + end + end end diff --git a/spec/unit/rbeapi/api/bgp/bgp_spec.rb b/spec/unit/rbeapi/api/bgp/bgp_spec.rb index 5e6ad5d..e54a926 100644 --- a/spec/unit/rbeapi/api/bgp/bgp_spec.rb +++ b/spec/unit/rbeapi/api/bgp/bgp_spec.rb @@ -55,17 +55,17 @@ 'eBGP_GROUP' => { peer_group: nil, remote_as: nil, send_community: false, shutdown: false, description: nil, next_hop_self: false, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: '12000' }, '192.168.255.1' => { peer_group: 'eBGP_GROUP', remote_as: '65000', send_community: true, shutdown: true, description: nil, next_hop_self: true, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: nil }, '192.168.255.3' => { peer_group: 'eBGP_GROUP', remote_as: '65001', send_community: true, shutdown: true, description: nil, next_hop_self: true, - route_map_in: nil, route_map_out: nil + route_map_in: nil, route_map_out: nil, maximum_routes: nil } } } end