| 1 |
# Copyright (c) Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Make coding more python3-ish |
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
from ansible_collections.community.routeros.plugins.module_utils._api_data import PATHS |
| 10 |
|
| 11 |
|
| 12 |
FAKE_ROS_VERSION = '7.5.0' |
| 13 |
|
| 14 |
|
| 15 |
class FakeLibRouterosError(Exception): |
| 16 |
def __init__(self, message): |
| 17 |
self.message = message |
| 18 |
super(FakeLibRouterosError, self).__init__(self.message) |
| 19 |
|
| 20 |
|
| 21 |
class TrapError(FakeLibRouterosError): |
| 22 |
def __init__(self, message='failure: already have interface with such name'): |
| 23 |
super(TrapError, self).__init__(message) |
| 24 |
|
| 25 |
|
| 26 |
# fixtures |
| 27 |
class fake_ros_api(object): |
| 28 |
def __init__(self, api, path): |
| 29 |
pass |
| 30 |
|
| 31 |
@classmethod |
| 32 |
def path(cls, api, path): |
| 33 |
fake_bridge = [{".id": "*DC", "name": "b2", "mtu": "auto", "actual-mtu": 1500, |
| 34 |
"l2mtu": 65535, "arp": "enabled", "arp-timeout": "auto", |
| 35 |
"mac-address": "3A:C1:90:D6:E8:44", "protocol-mode": "rstp", |
| 36 |
"fast-forward": "true", "igmp-snooping": "false", |
| 37 |
"auto-mac": "true", "ageing-time": "5m", "priority": |
| 38 |
"0x8000", "max-message-age": "20s", "forward-delay": "15s", |
| 39 |
"transmit-hold-count": 6, "vlan-filtering": "false", |
| 40 |
"dhcp-snooping": "false", "running": "true", "disabled": "false"}] |
| 41 |
return fake_bridge |
| 42 |
|
| 43 |
@classmethod |
| 44 |
def arbitrary(cls, api, path): |
| 45 |
def retr(self, *args, **kwargs): |
| 46 |
if 'name' not in kwargs.keys(): |
| 47 |
raise TrapError(message="no such command") |
| 48 |
dummy_test_string = '/interface/bridge add name=unit_test_brige_arbitrary' |
| 49 |
result = "/%s/%s add name=%s" % (path[0], path[1], kwargs['name']) |
| 50 |
return [result] |
| 51 |
return retr |
| 52 |
|
| 53 |
def add(self, name): |
| 54 |
if name == "unit_test_brige_exist": |
| 55 |
raise TrapError |
| 56 |
return '*A1' |
| 57 |
|
| 58 |
def remove(self, id): |
| 59 |
if id != "*A1": |
| 60 |
raise TrapError(message="no such item (4)") |
| 61 |
return '*A1' |
| 62 |
|
| 63 |
def update(self, **kwargs): |
| 64 |
if kwargs['.id'] != "*A1" or 'name' not in kwargs.keys(): |
| 65 |
raise TrapError(message="no such item (4)") |
| 66 |
return ["updated: {'.id': '%s' % kwargs['.id'], 'name': '%s' % kwargs['name']}"] |
| 67 |
|
| 68 |
def select(self, *args): |
| 69 |
dummy_bridge = [{".id": "*A1", "name": "dummy_bridge_A1"}, |
| 70 |
{".id": "*A2", "name": "dummy_bridge_A2"}, |
| 71 |
{".id": "*A3", "name": "dummy_bridge_A3"}] |
| 72 |
|
| 73 |
result = [] |
| 74 |
for dummy in dummy_bridge: |
| 75 |
found = {} |
| 76 |
for search in args: |
| 77 |
if search in dummy.keys(): |
| 78 |
found[search] = dummy[search] |
| 79 |
else: |
| 80 |
continue |
| 81 |
if len(found.keys()) == 2: |
| 82 |
result.append(found) |
| 83 |
|
| 84 |
if result: |
| 85 |
return result |
| 86 |
else: |
| 87 |
return [] |
| 88 |
|
| 89 |
@classmethod |
| 90 |
def select_where(cls, api, path): |
| 91 |
api_path = Where() |
| 92 |
return api_path |
| 93 |
|
| 94 |
|
| 95 |
class Where(object): |
| 96 |
def __init__(self): |
| 97 |
pass |
| 98 |
|
| 99 |
def select(self, *args): |
| 100 |
return self |
| 101 |
|
| 102 |
def where(self, *args): |
| 103 |
return [{".id": "*A1", "name": "dummy_bridge_A1"}] |
| 104 |
|
| 105 |
|
| 106 |
class Key(object): |
| 107 |
def __init__(self, name): |
| 108 |
self.name = name |
| 109 |
self.str_return() |
| 110 |
|
| 111 |
def str_return(self): |
| 112 |
return str(self.name) |
| 113 |
|
| 114 |
|
| 115 |
class Or(object): |
| 116 |
def __init__(self, *args): |
| 117 |
self.args = args |
| 118 |
self.str_return() |
| 119 |
|
| 120 |
def str_return(self): |
| 121 |
return repr(self.args) |
| 122 |
|
| 123 |
|
| 124 |
def _normalize_entry(entry, path_info, on_create=False): |
| 125 |
for key, data in path_info.fields.items(): |
| 126 |
if key not in entry and data.default is not None and (not data.can_disable or on_create): |
| 127 |
entry[key] = data.default |
| 128 |
if data.can_disable: |
| 129 |
if key in entry and entry[key] in (None, data.remove_value): |
| 130 |
del entry[key] |
| 131 |
if ('!%s' % key) in entry: |
| 132 |
entry.pop(key, None) |
| 133 |
del entry['!%s' % key] |
| 134 |
if data.absent_value is not None and key in entry and entry[key] == data.absent_value: |
| 135 |
del entry[key] |
| 136 |
|
| 137 |
|
| 138 |
def massage_expected_result_data(values, path, keep_all=False, remove_dynamic=False, remove_builtin=False): |
| 139 |
versioned_path_info = PATHS[path] |
| 140 |
versioned_path_info.provide_version(FAKE_ROS_VERSION) |
| 141 |
path_info = versioned_path_info.get_data() |
| 142 |
if remove_dynamic: |
| 143 |
values = [entry for entry in values if not entry.get('dynamic', False)] |
| 144 |
if remove_builtin: |
| 145 |
values = [entry for entry in values if not entry.get('builtin', False)] |
| 146 |
values = [entry.copy() for entry in values] |
| 147 |
for entry in values: |
| 148 |
_normalize_entry(entry, path_info) |
| 149 |
if not keep_all: |
| 150 |
for key in list(entry): |
| 151 |
if key == '.id' or key in path_info.fields: |
| 152 |
continue |
| 153 |
del entry[key] |
| 154 |
for key, data in path_info.fields.items(): |
| 155 |
if data.absent_value is not None and key not in entry: |
| 156 |
entry[key] = data.absent_value |
| 157 |
return values |
| 158 |
|
| 159 |
|
| 160 |
class Path(object): |
| 161 |
def __init__(self, path, initial_values, read_only=False): |
| 162 |
self._path = path |
| 163 |
versioned_path_info = PATHS[path] |
| 164 |
versioned_path_info.provide_version(FAKE_ROS_VERSION) |
| 165 |
self._path_info = versioned_path_info.get_data() |
| 166 |
self._values = [entry.copy() for entry in initial_values] |
| 167 |
for entry in self._values: |
| 168 |
_normalize_entry(entry, self._path_info) |
| 169 |
self._new_id_counter = 0 |
| 170 |
self._read_only = read_only |
| 171 |
|
| 172 |
def _sanitize(self, entry): |
| 173 |
entry = entry.copy() |
| 174 |
for field, field_info in self._path_info.fields.items(): |
| 175 |
if field in entry: |
| 176 |
if field_info.write_only: |
| 177 |
del entry[field] |
| 178 |
return entry |
| 179 |
|
| 180 |
def __iter__(self): |
| 181 |
return [self._sanitize(entry) for entry in self._values].__iter__() |
| 182 |
|
| 183 |
def _find_id(self, id, required=False): |
| 184 |
for index, entry in enumerate(self._values): |
| 185 |
if entry['.id'] == id: |
| 186 |
return index |
| 187 |
if required: |
| 188 |
raise FakeLibRouterosError('Cannot find key "%s"' % id) |
| 189 |
return None |
| 190 |
|
| 191 |
def add(self, **kwargs): |
| 192 |
if self._path_info.fixed_entries or self._path_info.single_value: |
| 193 |
raise Exception('Cannot add entries') |
| 194 |
if self._read_only: |
| 195 |
raise Exception('Modifying read-only path: add %s' % repr(kwargs)) |
| 196 |
if '.id' in kwargs: |
| 197 |
raise Exception('Trying to create new entry with ".id" field: %s' % repr(kwargs)) |
| 198 |
if 'dynamic' in kwargs or 'builtin' in kwargs: |
| 199 |
raise Exception('Trying to add a dynamic or builtin entry') |
| 200 |
self._new_id_counter += 1 |
| 201 |
id = '*NEW%d' % self._new_id_counter |
| 202 |
entry = { |
| 203 |
'.id': id, |
| 204 |
} |
| 205 |
for field, value in kwargs.items(): |
| 206 |
if field.startswith('!'): |
| 207 |
field = field[1:] |
| 208 |
if field not in self._path_info.fields: |
| 209 |
raise ValueError('Trying to set unknown field "{field}"'.format(field=field)) |
| 210 |
field_info = self._path_info.fields[field] |
| 211 |
if field_info.read_only: |
| 212 |
raise ValueError('Trying to set read-only field "{field}"'.format(field=field)) |
| 213 |
entry[field] = value |
| 214 |
_normalize_entry(entry, self._path_info, on_create=True) |
| 215 |
self._values.append(entry) |
| 216 |
return id |
| 217 |
|
| 218 |
def remove(self, *args): |
| 219 |
if self._path_info.fixed_entries or self._path_info.single_value: |
| 220 |
raise Exception('Cannot remove entries') |
| 221 |
if self._read_only: |
| 222 |
raise Exception('Modifying read-only path: remove %s' % repr(args)) |
| 223 |
for id in args: |
| 224 |
index = self._find_id(id, required=True) |
| 225 |
entry = self._values[index] |
| 226 |
if entry.get('dynamic', False) or entry.get('builtin', False): |
| 227 |
raise Exception('Trying to remove a dynamic or builtin entry') |
| 228 |
del self._values[index] |
| 229 |
|
| 230 |
def update(self, **kwargs): |
| 231 |
if self._read_only: |
| 232 |
raise Exception('Modifying read-only path: update %s' % repr(kwargs)) |
| 233 |
if 'dynamic' in kwargs or 'builtin' in kwargs: |
| 234 |
raise Exception('Trying to update dynamic builtin fields') |
| 235 |
if self._path_info.single_value: |
| 236 |
index = 0 |
| 237 |
else: |
| 238 |
index = self._find_id(kwargs['.id'], required=True) |
| 239 |
entry = self._values[index] |
| 240 |
if entry.get('dynamic', False) or entry.get('builtin', False): |
| 241 |
raise Exception('Trying to update a dynamic or builtin entry') |
| 242 |
for field in kwargs: |
| 243 |
if field == '.id': |
| 244 |
continue |
| 245 |
if field.startswith('!'): |
| 246 |
field = field[1:] |
| 247 |
if field not in self._path_info.fields: |
| 248 |
raise ValueError('Trying to update unknown field "{field}"'.format(field=field)) |
| 249 |
field_info = self._path_info.fields[field] |
| 250 |
if field_info.read_only: |
| 251 |
raise ValueError('Trying to update read-only field "{field}"'.format(field=field)) |
| 252 |
entry.update(kwargs) |
| 253 |
_normalize_entry(entry, self._path_info) |
| 254 |
|
| 255 |
def __call__(self, command, *args, **kwargs): |
| 256 |
if self._read_only: |
| 257 |
raise Exception('Modifying read-only path: "%s" %s %s' % (command, repr(args), repr(kwargs))) |
| 258 |
if command != 'move': |
| 259 |
raise FakeLibRouterosError('Unsupported command "%s"' % command) |
| 260 |
if self._path_info.fixed_entries or self._path_info.single_value: |
| 261 |
raise Exception('Cannot move entries') |
| 262 |
yield None # make sure that nothing happens if the result isn't consumed |
| 263 |
source_index = self._find_id(kwargs.pop('numbers'), required=True) |
| 264 |
entry = self._values.pop(source_index) |
| 265 |
dest_index = self._find_id(kwargs.pop('destination'), required=True) |
| 266 |
self._values.insert(dest_index, entry) |
| 267 |
|
| 268 |
|
| 269 |
def create_fake_path(path, initial_values, read_only=False): |
| 270 |
def create(api, called_path): |
| 271 |
called_path = tuple(called_path) |
| 272 |
if path != called_path: |
| 273 |
raise AssertionError('Expected {path}, got {called_path}'.format(path=path, called_path=called_path)) |
| 274 |
return Path(path, initial_values, read_only=read_only) |
| 275 |
|
| 276 |
return create |