Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding method to check valid ip address #200

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if ip_address is Zero. If Zero then return with error message..

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
Loading