Skip to content

Commit

Permalink
handle additional new changes in route modules
Browse files Browse the repository at this point in the history
Signed-off-by: vivekpandeyibm <[email protected]>
  • Loading branch information
vivekpandeyibm committed Jan 4, 2025
1 parent c9a2832 commit 338b2b2
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 20 deletions.
104 changes: 95 additions & 9 deletions playbooks/demo_route.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,49 @@
- name: Add a route with numeric mode
ibm.power_aix.route:
action: add
destination: "192.168.2.0"
destination: "192.168.4.1"
gateway: "192.168.1.1"
numeric: true
flags: "net"
- name: delete a route with numeric mode
ibm.power_aix.route:
action: delete
destination: "192.168.2.0"
destination: "192.168.4.1"
gateway: "192.168.1.1"
numeric: true
flags: "net"
- name: Add a route with numeric mode
ibm.power_aix.route:
action: add
destination: "192.168.4.1"
gateway: "192.168.1.1"
numeric: true
prefixlen: 24
flags: "net"
- name: delete a route with numeric mode
ibm.power_aix.route:
action: delete
destination: "192.168.4.1"
gateway: "192.168.1.1"
numeric: true
prefixlen: 24
flags: "net"
- name: Add a route with numeric mode
ibm.power_aix.route:
action: add
destination: "192.168.4.1"
gateway: "192.168.1.2"
numeric: true
prefixlen: 24
flags: "net"
- name: delete a route with numeric mode
ibm.power_aix.route:
action: delete
destination: "192.168.4.1"
gateway: "192.168.1.2"
numeric: true
prefixlen: 24
flags: "net"
- name: Add a route with ioctl preference
ibm.power_aix.route:
action: add
Expand Down Expand Up @@ -64,6 +96,20 @@
gateway: "192.168.3.1"
verbose: true
flags: "host"
- name: Add a route with verbose mode
ibm.power_aix.route:
action: add
destination: "192.168.4.0"
gateway: "192.168.3.2"
verbose: true
flags: "host"
- name: delete a route with verbose mode
ibm.power_aix.route:
action: delete
destination: "192.168.4.0"
gateway: "192.168.3.2"
verbose: true
flags: "host"
- name: Add a route with verbose,ioctl, numeric mode
ibm.power_aix.route:
action: add
Expand Down Expand Up @@ -120,7 +166,7 @@
- name: Add a route with weight and policy
ibm.power_aix.route:
action: add
destination: "192.158.3.2"
destination: "192.158.4.2"
gateway: "192.158.3.5"
arguments:
weight: "5"
Expand All @@ -129,27 +175,67 @@
- name: Change route parameters
ibm.power_aix.route:
action: change
destination: "192.158.3.2"
destination: "192.158.4.2"
gateway: "10.32.144.2"
flags: "host"
arguments:
weight: "6"
policy: "4"
weight: "4"
policy: "3"
- name: Set route attributes
ibm.power_aix.route:
action: set
destination: "192.158.3.2"
destination: "192.158.4.2"
gateway: "10.32.144.2"
arguments:
weight: "6"
policy: "4"
flags: "host"
- name: delete route attributes
ibm.power_aix.route:
action: delete
destination: "192.158.4.2"
gateway: "10.32.144.2"
arguments:
weight: "4"
policy: "2"
flags: "host"
- name: Add a route with weight and policy
ibm.power_aix.route:
action: add
destination: "192.158.4.2"
gateway: "192.158.3.5"
prefixlen: 24
arguments:
weight: "5"
policy: "4"
flags: "net"
- name: Change route parameters
ibm.power_aix.route:
action: change
destination: "192.158.4.2"
gateway: "10.32.144.2"
prefixlen: 24
flags: "net"
arguments:
weight: "6"
policy: "4"
- name: Set route attributes
ibm.power_aix.route:
action: set
destination: "192.158.4.2"
gateway: "10.32.144.2"
prefixlen: 24
arguments:
weight: "4"
policy: "4"
flags: "net"
- name: delete route attributes
ibm.power_aix.route:
action: delete
destination: "192.158.3.2"
destination: "192.158.4.2"
gateway: "10.32.144.2"
prefixlen: 24
arguments:
weight: "4"
policy: "2"
flags: "host"
flags: "net"
46 changes: 35 additions & 11 deletions plugins/modules/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def parse_routing_table(module):
module.fail_json(msg="Failed to fetch routing table.", rc=rc, stdout=stdout, stderr=stderr)

routing_table = []
gateways = []
lines = stdout.splitlines()

for line in lines:
Expand All @@ -222,19 +223,22 @@ def parse_routing_table(module):
continue
# Inspect raw entry
route_entry = columns[0]
gateway_entry = columns[1]
try:
if "/" in route_entry: # CIDR format
normalized_entry = normalize_route_entry(module, route_entry)
network = ipaddress.ip_network(normalized_entry, strict=False)
routing_table.append(network)
gateways.append(gateway_entry)
elif "." in route_entry: # IP format, assume /32
network = ipaddress.ip_network(f"{route_entry}/32", strict=False)
routing_table.append(network)
gateways.append(gateway_entry)
except Exception as e:
module.warn(f"Invalid route entry skipped: {route_entry}, Error: {str(e)}")
continue

return routing_table
return routing_table, gateways


def check_destination_in_routing_table(module, destination):
Expand All @@ -248,17 +252,37 @@ def check_destination_in_routing_table(module, destination):
Returns:
- bool: True if the destination matches an entry in the routing table, False otherwise.
"""
netmask = module.params['netmask']
prefixlen = module.params['prefixlen']
flags = module.params['flags']
gateway = module.params['gateway']
action = module.params['action']

if flags == 'net': # check if network flag is set
if netmask:
input_mask = netmask
elif prefixlen:
input_mask = prefixlen
else:
input_mask = 32
destination = calculate_network_address(module, destination, input_mask) # Call the function for network address
try:
destination_ip = ipaddress.ip_address(destination)
except Exception as e:
module.fail_json(msg=f"Invalid destination IP address: {destination}, Error: {str(e)}")
routing_table = parse_routing_table(module)
# Check if the destination IP matches any network in the table
for network in routing_table:
if destination_ip == network.network_address:
return True
elif destination_ip in network:
return True
routing_table, gateways = parse_routing_table(module)
# Check if the destination IP and gateway matches any network in the table
for network, gateway_get in zip(routing_table, gateways):
if action in ['set', 'change']: # Skip gateway validation for 'set' or 'change'
if destination_ip == network.network_address:
return True
elif destination_ip in network:
return True
else: # Perform full validation for other actions
if destination_ip == network.network_address and gateway == gateway_get:
return True
elif destination_ip in network and gateway == gateway_get:
return True
return False


Expand Down Expand Up @@ -331,7 +355,7 @@ def build_route_command(module):
if destination:
if flags == 'net': # check if network flag is set
cmd.append('-net')
if action in ['delete']:
if action in ['delete', 'set', 'change']:
if netmask:
input_mask = netmask
elif prefixlen:
Expand Down Expand Up @@ -381,12 +405,12 @@ def run_route_command(module):
result = check_destination_in_routing_table(module, destination)
if result and module.params['action'] in ['add']:
module.exit_json(
msg=f"The destination '{destination}' is found in the routing table.",
msg=f"The destination '{destination}' is found in the routing table during action {action}.",
changed=False
)
elif not result and module.params['action'] in ['delete', 'change', 'flush', 'get', 'set']:
module.exit_json(
msg=f"The destination '{destination}' is not found in the routing table.",
msg=f"The destination '{destination}' is not found in the routing table during action {action}.",
changed=False
)
cmd = build_route_command(module)
Expand Down

0 comments on commit 338b2b2

Please sign in to comment.