| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import netaddr |
| 17 |
import six |
| 18 |
import re |
| 19 |
|
| 20 |
try: |
| 21 |
from oslo_log import log as logging |
| 22 |
except ImportError: |
| 23 |
import logging |
| 24 |
|
| 25 |
try: |
| 26 |
from oslo_serialization import jsonutils |
| 27 |
except ImportError: |
| 28 |
import json as jsonutils |
| 29 |
|
| 30 |
LOG = logging.getLogger(__name__) |
| 31 |
|
| 32 |
|
| 33 |
def is_valid_ip(ip): |
| 34 |
"""Check if the given IP address is valid.""" |
| 35 |
if not ip: |
| 36 |
LOG.info("IP address is not provided") |
| 37 |
return False |
| 38 |
try: |
| 39 |
netaddr.IPAddress(ip) |
| 40 |
except netaddr.core.AddrFormatError: |
| 41 |
LOG.info("Invalid IP address provided: %s", ip) |
| 42 |
return False |
| 43 |
return True |
| 44 |
|
| 45 |
|
| 46 |
def generate_duid(mac): |
| 47 |
"""DUID is consisted of 10 hex numbers. |
| 48 |
|
| 49 |
0x00 + mac with last 3 hex + mac with 6 hex |
| 50 |
""" |
| 51 |
valid = mac and isinstance(mac, six.string_types) |
| 52 |
if not valid: |
| 53 |
raise ValueError("Invalid argument was passed") |
| 54 |
return "00:" + mac[9:] + ":" + mac |
| 55 |
|
| 56 |
|
| 57 |
def determine_ip_version(ip_in): |
| 58 |
ip_ver = 4 |
| 59 |
if isinstance(ip_in, (list, tuple)): |
| 60 |
ip_in = ip_in[0] |
| 61 |
if ip_in: |
| 62 |
if isinstance(ip_in, int): |
| 63 |
if ip_in == 6: |
| 64 |
ip_ver = 6 |
| 65 |
else: |
| 66 |
ip_ver = 4 |
| 67 |
elif hasattr(ip_in, 'ip_version'): |
| 68 |
return ip_in.ip_version |
| 69 |
else: |
| 70 |
if type(ip_in) is dict: |
| 71 |
addr = ip_in['ip_address'] |
| 72 |
else: |
| 73 |
addr = ip_in |
| 74 |
try: |
| 75 |
ip = netaddr.IPAddress(addr) |
| 76 |
except ValueError: |
| 77 |
ip = netaddr.IPNetwork(addr) |
| 78 |
|
| 79 |
ip_ver = ip.version |
| 80 |
return ip_ver |
| 81 |
|
| 82 |
|
| 83 |
def safe_json_load(data): |
| 84 |
try: |
| 85 |
return jsonutils.loads(data) |
| 86 |
except ValueError: |
| 87 |
LOG.warning("Could not decode reply into json: %s", data) |
| 88 |
|
| 89 |
|
| 90 |
def try_value_to_bool(value, strict_mode=True): |
| 91 |
"""Tries to convert value into boolean. |
| 92 |
|
| 93 |
Args: |
| 94 |
value (str): Value that should be converted into boolean. |
| 95 |
|
| 96 |
strict_mode (bool): |
| 97 |
- If strict_mode is True, Only string representation of str(True) |
| 98 |
and str(False) are converted into booleans; |
| 99 |
- If strict_mode is False, anything that looks like True or False |
| 100 |
is converted into booleans: |
| 101 |
|
| 102 |
- Values accepted as True are: 'true', 'on', 'yes' (case |
| 103 |
independent) |
| 104 |
- Values accepted as False are: 'false', 'off', 'no' (case |
| 105 |
independent) |
| 106 |
|
| 107 |
Returns: |
| 108 |
True, False, or original value in case of failed conversion. |
| 109 |
""" |
| 110 |
if strict_mode: |
| 111 |
true_list = ('True',) |
| 112 |
false_list = ('False',) |
| 113 |
val = value |
| 114 |
else: |
| 115 |
true_list = ('true', 'on', 'yes') |
| 116 |
false_list = ('false', 'off', 'no') |
| 117 |
val = str(value).lower() |
| 118 |
|
| 119 |
if val in true_list: |
| 120 |
return True |
| 121 |
elif val in false_list: |
| 122 |
return False |
| 123 |
return value |
| 124 |
|
| 125 |
|
| 126 |
def paging(response, max_results): |
| 127 |
"""Returns WAPI response page by page |
| 128 |
|
| 129 |
Args: |
| 130 |
response (list): WAPI response. |
| 131 |
max_results (int): Maximum number of objects to be returned |
| 132 |
in one page. |
| 133 |
Returns: |
| 134 |
Generator object with WAPI response split page by page. |
| 135 |
""" |
| 136 |
i = 0 |
| 137 |
while i < len(response): |
| 138 |
yield response[i:i + max_results] |
| 139 |
i = i + max_results |
| 140 |
|
| 141 |
|
| 142 |
def mask_sensitive_data(data, sensitive_keys=None): |
| 143 |
if sensitive_keys is None: |
| 144 |
sensitive_keys = ['username', 'password', 'cert', 'key'] |
| 145 |
|
| 146 |
if isinstance(data, dict): |
| 147 |
|
| 148 |
return { |
| 149 |
k: ('****' if k in sensitive_keys else v) for k, v in data.items() |
| 150 |
} |
| 151 |
elif isinstance(data, str): |
| 152 |
|
| 153 |
patterns = {key: rf'(?<={key}=)[^&]*' for key in sensitive_keys} |
| 154 |
for key, pattern in patterns.items(): |
| 155 |
data = re.sub(pattern, '****', data) |
| 156 |
return data |
| 157 |
return data |
| 158 |
|
| 159 |
|
| 160 |
def format_html(html_content): |
| 161 |
if not html_content: |
| 162 |
return '' |
| 163 |
|
| 164 |
|
| 165 |
if isinstance(html_content, bytes): |
| 166 |
html_content = html_content.decode('utf-8') |
| 167 |
formatted_content = html_content.replace('<', '\n<') |
| 168 |
formatted_content = formatted_content.replace('>', '>\n') |
| 169 |
formatted_content = '\n'.join( |
| 170 |
line.strip() for line in formatted_content.split('\n') if line.strip()) |
| 171 |
return formatted_content |