diff options
Diffstat (limited to 'ansible_collections/cisco/dnac/plugins/module_utils')
-rw-r--r-- | ansible_collections/cisco/dnac/plugins/module_utils/dnac.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/ansible_collections/cisco/dnac/plugins/module_utils/dnac.py b/ansible_collections/cisco/dnac/plugins/module_utils/dnac.py index a12e7eaf4..1d4b804cd 100644 --- a/ansible_collections/cisco/dnac/plugins/module_utils/dnac.py +++ b/ansible_collections/cisco/dnac/plugins/module_utils/dnac.py @@ -27,6 +27,7 @@ import json # import datetime import inspect import re +import socket class DnacBase(): @@ -485,6 +486,66 @@ class DnacBase(): 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_path_exists(self, file_path): + """ + Check if the file path 'file_path' exists or not. + + Parameters: + file_path (string) - Path of the provided file. + + Returns: + True/False (bool) - True if the file path exists, else False. + """ + + if not os.path.exists(file_path): + return False + + return True + + def is_json(self, file_path): + """ + Check if the file in the file path is JSON or not. + + Parameters: + file_path (string) - Path of the provided file. + + Returns: + True/False (bool) - True if the file is in JSON format, else False. + """ + + try: + with open(file_path, 'r') as file: + json.load(file) + return True + + except (ValueError, FileNotFoundError): + self.log("The provided file '{0}' is not in JSON format".format(file_path), "CRITICAL") + return False + def is_list_complex(x): return isinstance(x[0], dict) or isinstance(x[0], list) |