Skip to content

Commit

Permalink
Merge pull request #200 from abimishr/PNP_Code_Abinash
Browse files Browse the repository at this point in the history
Adding method to check valid ip address
  • Loading branch information
madhansansel authored Mar 27, 2024
2 parents e0f3fd2 + 3071e44 commit 9586045
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
25 changes: 25 additions & 0 deletions plugins/module_utils/dnac.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# import datetime
import inspect
import re
import socket


class DnacBase():
Expand Down Expand Up @@ -485,6 +486,30 @@ def update_site_type_key(self, config):

return new_config

def is_valid_ipv4(self, ip_address):
"""
Validates an IPv4 address.
Parameters:
ip_address - String denoting the IPv4 address passed.
Returns:
bool - Returns true if the passed IP address value is correct or it returns
false if it is incorrect
"""

try:
socket.inet_aton(ip_address)
octets = ip_address.split('.')
if len(octets) != 4:
return False
for octet in octets:
if not 0 <= int(octet) <= 255:
return False
return True
except socket.error:
return False


def is_list_complex(x):
return isinstance(x[0], dict) or isinstance(x[0], list)
Expand Down
38 changes: 38 additions & 0 deletions plugins/modules/discovery_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,43 @@ def validate_input(self, state=None):
self.status = "success"
return self

def validate_ip4_address_list(self):
"""
Validates each ip adress paased in the IP_address_list passed by the user before preprocessing it
"""

ip_address_list = self.validated_config[0].get('ip_address_list')
for ip in ip_address_list:
if '/' in ip:
ip = ip.split("/")[0]
if '-' in ip:
if len(ip.split('-')) == 2:
ip1, ip2 = ip.split('-')
if self.is_valid_ipv4(ip1) is False:
msg = "IP address {0} is not valid".format(ip1)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
if self.is_valid_ipv4(ip2) is False:
msg = "IP address {0} is not valid".format(ip2)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
ip1_parts = list(map(int, ip1.split('.')))
ip2_parts = list(map(int, ip2.split('.')))
for part in range(4):
if ip1_parts[part] > ip2_parts[part]:
msg = "Incorrect range passed: {0}. Please pass correct IP address range".format(ip)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
else:
msg = "Provided range '{0}' is incorrect. IP address range should have only upper and lower limit values".format(ip)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
if self.is_valid_ipv4(ip) is False and '-' not in ip:
msg = "IP address {0} is not valid".format(ip)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
self.log("All the IP addresses passed are correct", "INFO")

def get_creds_ids_list(self):
"""
Retrieve the list of credentials IDs associated with class instance.
Expand Down Expand Up @@ -1513,6 +1550,7 @@ def get_diff_merged(self):
- self: The instance of the class with updated attributes.
"""

self.validate_ip4_address_list()
devices_list_info = self.get_devices_list_info()
ip_address_list = self.preprocess_device_discovery(devices_list_info)
exist_discovery = self.get_exist_discovery()
Expand Down
38 changes: 38 additions & 0 deletions plugins/modules/discovery_workflow_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,43 @@ def validate_input(self, state=None):
self.status = "success"
return self

def validate_ip4_address_list(self):
"""
Validates each ip adress paased in the IP_address_list passed by the user before preprocessing it
"""

ip_address_list = self.validated_config[0].get('ip_address_list')
for ip in ip_address_list:
if '/' in ip:
ip = ip.split("/")[0]
if '-' in ip:
if len(ip.split('-')) == 2:
ip1, ip2 = ip.split('-')
if self.is_valid_ipv4(ip1) is False:
msg = "IP address {0} is not valid".format(ip1)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
if self.is_valid_ipv4(ip2) is False:
msg = "IP address {0} is not valid".format(ip2)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
ip1_parts = list(map(int, ip1.split('.')))
ip2_parts = list(map(int, ip2.split('.')))
for part in range(4):
if ip1_parts[part] > ip2_parts[part]:
msg = "Incorrect range passed: {0}. Please pass correct IP address range".format(ip)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
else:
msg = "Provided range '{0}' is incorrect. IP address range should have only upper and lower limit values".format(ip)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
if self.is_valid_ipv4(ip) is False and '-' not in ip:
msg = "IP address {0} is not valid".format(ip)
self.log(msg, "CRITICAL")
self.module.fail_json(msg=msg)
self.log("All the IP addresses passed are correct", "INFO")

def get_creds_ids_list(self):
"""
Retrieve the list of credentials IDs associated with class instance.
Expand Down Expand Up @@ -1513,6 +1550,7 @@ def get_diff_merged(self):
- self: The instance of the class with updated attributes.
"""

self.validate_ip4_address_list()
devices_list_info = self.get_devices_list_info()
ip_address_list = self.preprocess_device_discovery(devices_list_info)
exist_discovery = self.get_exist_discovery()
Expand Down

0 comments on commit 9586045

Please sign in to comment.