Skip to content

Commit

Permalink
add in_vrf flag to check for vrf context and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruchip16 committed Oct 20, 2024
1 parent 74d31a7 commit 991ce19
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ def _state_replaced(self, want, have):
if "." in item or ":" in item or "/" in item:
cmd += " {0}".format(item)
else:
cmd += " vrf {0}".format(item)
if "vrf" in want:
cmd += " vrf {0}".format(item)
else:
cmd += " {0}".format(item)
update_commands.append(cmd)

for key, value in iteritems(rotated_want_next_hops):
Expand All @@ -251,6 +254,7 @@ def _state_replaced(self, want, have):
dest=want_route["dest"],
next_hop=key,
updates=updates,
in_vrf="vrf" in want,
),
)

Expand Down Expand Up @@ -421,6 +425,7 @@ def _state_merged(self, want, have):
rotated_have_next_hops.get(key, {}),
updates,
),
in_vrf="vrf" in want,
),
)

Expand Down Expand Up @@ -476,7 +481,10 @@ def _static_route_popper(self, want_afi, have_afi):
if "." in item or ":" in item or "/" in item:
cmd += " {0}".format(item)
else:
cmd += " vrf {0}".format(item)
if "vrf" in want_afi:
cmd += " vrf {0}".format(item)
else:
cmd += " {0}".format(item)
update_commands.append(cmd)
if update_commands:
update_commands.insert(
Expand Down Expand Up @@ -580,7 +588,7 @@ def rotate_next_hops(self, next_hops):

return next_hops_dict

def _compute_commands(self, dest, next_hop, updates=None):
def _compute_commands(self, dest, next_hop, updates=None, in_vrf=False):
"""This method computes a static route entry command
from the specified `dest`, `next_hop` and `updates`
Expand All @@ -593,7 +601,14 @@ def _compute_commands(self, dest, next_hop, updates=None):
command = dest

for x in next_hop:
command += " {0}".format(x)
if "." in x or ":" in x or "/" in x:
command += " {0}".format(x)
else:
# Only add 'vrf' if in VRF context and not in normal context
if in_vrf:
command += " vrf {0}".format(x)
else:
command += " {0}".format(x)

for key in sorted(updates):
if key == "admin_distance":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
- forward_router_address: 192.0.2.11
admin_distance: 100

- dest: 192.168.17.0/24
next_hops:
- interface: Loopback0

- afi: ipv6
safi: unicast
routes:
Expand All @@ -39,6 +43,9 @@

- interface: FastEthernet0/0/0/8
forward_router_address: 2001:db8:2000:2::1
- dest: 2001:db8::/64
next_hops:
- interface: Loopback0

- vrf: DEV_SITE
address_families:
Expand All @@ -58,6 +65,14 @@
dest_vrf: test_1
track: ip_sla_2
vrflabel: 124
- vrf: TEST_VRF
address_families:
- afi: ipv4
safi: unicast
routes:
- dest: 192.1.0.0/24
next_hops:
- interface: Loopback1
state: merged

- name: Assert that before dicts were correctly generated
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/targets/iosxr_static_routes/vars/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ merged:
10
- 192.0.2.16/28 FastEthernet0/0/0/5 track ip_sla_1
- 192.0.2.32/28 192.0.2.11 100
- 192.168.17.0/24 Loopback0
- address-family ipv6 unicast
- 2001:db8:1000::/36 FastEthernet0/0/0/7 description DC
- 2001:db8:1000::/36 2001:db8:2000:2::1 FastEthernet0/0/0/8
- 2001:db8::/64 Loopback0
- vrf DEV_SITE
- address-family ipv4 unicast
- 192.0.2.48/28 vrf test_1 192.0.2.12 description DEV
Expand All @@ -23,6 +25,9 @@ merged:
- 192.0.2.48/28 vrf test_1 192.0.2.12 description DEV vrflabel 2301
- 192.0.2.80/28 vrf test_1 192.0.2.14 FastEthernet0/0/0/2 description rt_test_1
track ip_sla_2 vrflabel 124
- vrf TEST_VRF
- address-family ipv4 unicast
- 192.1.0.0/24 Loopback1
after:
- address_families:
- afi: ipv4
Expand All @@ -40,6 +45,9 @@ merged:
next_hops:
- admin_distance: 100
forward_router_address: 192.0.2.11
- dest: 192.168.17.0/24
next_hops:
- interface: Loopback0
safi: unicast
- afi: ipv6
routes:
Expand All @@ -49,6 +57,9 @@ merged:
interface: FastEthernet0/0/0/7
- forward_router_address: 2001:db8:2000:2::1
interface: FastEthernet0/0/0/8
- dest: 2001:db8::/64
next_hops:
- interface: Loopback0
safi: unicast
- address_families:
- afi: ipv4
Expand All @@ -67,6 +78,14 @@ merged:
vrflabel: 124
safi: unicast
vrf: DEV_SITE
- address_families:
- afi: ipv4
routes:
- dest: 192.1.0.0/24
next_hops:
- interface: Loopback1
safi: unicast
vrf: TEST_VRF
update_after:
- address_families:
- afi: ipv4
Expand Down

0 comments on commit 991ce19

Please sign in to comment.