Skip to content

Commit 0015291

Browse files
author
Anthony House
committed
Correct flake8/pylint errors
Signed-off-by: Anthony House <[email protected]>
1 parent 980e2bf commit 0015291

File tree

2 files changed

+71
-42
lines changed

2 files changed

+71
-42
lines changed

suzieq/poller/controller/source/nautobot.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
from pynautobot import api
99

1010
from suzieq.poller.controller.source.base_source import Source, SourceModel
11-
from suzieq.poller.controller.inventory_async_plugin import InventoryAsyncPlugin
11+
from suzieq.poller.controller.inventory_async_plugin import (
12+
InventoryAsyncPlugin,
13+
)
1214
from suzieq.shared.utils import get_sensitive_data
1315
from suzieq.shared.exceptions import InventorySourceError, SensitiveLoadError
1416

@@ -43,7 +45,9 @@ class NautobotSourceModel(SourceModel):
4345
ssl_verify: Optional[bool] = Field(alias="ssl-verify")
4446
server: Union[str, NautobotServerModel] = Field(alias="url")
4547
run_once: Optional[bool] = Field(default=False, alias="run_once")
46-
device_filters: Optional[Dict] = Field(default=None, alias="device_filters")
48+
device_filters: Optional[Dict] = Field(
49+
default=None, alias="device_filters"
50+
)
4751

4852
@validator("server", pre=True)
4953
def validate_and_set(cls, url, values):
@@ -59,7 +63,9 @@ def validate_and_set(cls, url, values):
5963
port = url_data.port or _DEFAULT_PORTS.get(protocol)
6064
if not port:
6165
raise ValueError(f"Unable to parse port {url}")
62-
server = NautobotServerModel(host=host, port=port, protocol=protocol)
66+
server = NautobotServerModel(
67+
host=host, port=port, protocol=protocol
68+
)
6369
ssl_verify = values["ssl_verify"]
6470
if ssl_verify is None:
6571
if server.protocol == "http":
@@ -68,7 +74,9 @@ def validate_and_set(cls, url, values):
6874
ssl_verify = True
6975
else:
7076
if server.protocol == "http" and ssl_verify:
71-
raise ValueError("Cannot use ssl_verify=True with http host")
77+
raise ValueError(
78+
"Cannot use ssl_verify=True with http host"
79+
)
7280
values["ssl_verify"] = ssl_verify
7381
return server
7482
elif isinstance(url, NautobotServerModel):
@@ -88,7 +96,7 @@ def validate_token(cls, token):
8896

8997

9098
class Nautobot(Source, InventoryAsyncPlugin):
91-
"""This class is used to dynamically retrieve the inventory from Nautobot"""
99+
"""Retrieve the inventory from Nautobot"""
92100

93101
def __init__(self, config_data: dict, validate: bool = True) -> None:
94102
self._status = "init"
@@ -126,7 +134,8 @@ def _init_session(self):
126134
headers ([dict]): headers to initialize the session
127135
"""
128136
url_address = (
129-
f"{self._server.protocol}://{self._server.host}:" f"{self._server.port}"
137+
f"{self._server.protocol}://{self._server.host}:"
138+
f"{self._server.port}"
130139
)
131140

132141
if not self._session:
@@ -149,15 +158,19 @@ async def get_inventory_list(self) -> List:
149158
self._init_session()
150159
try:
151160
if self._data.device_filters:
152-
devices = self._session.dcim.devices.filter(**self._data.device_filters)
161+
devices = self._session.dcim.devices.filter(
162+
**self._data.device_filters
163+
)
153164
else:
154165
devices = self._session.dcim.devices.all()
155166
except Exception as e:
156167
raise InventorySourceError(
157168
f"{self.name}: error while " f"getting devices: {e}"
158169
)
159170

160-
logger.info(f"Nautobot: Retrieved inventory list of {len(devices)} devices")
171+
logger.info(
172+
f"Nautobot: Retrieved inventory list of {len(devices)} devices"
173+
)
161174
return devices
162175

163176
def parse_inventory(self, inventory_list: list) -> Dict:
@@ -191,7 +204,8 @@ def parse_inventory(self, inventory_list: list) -> Dict:
191204

192205
if not address:
193206
logger.warning(
194-
f"Skipping {namespace}.{hostname}: doesn't have a management IP"
207+
f"Skipping {namespace}.{hostname}: "
208+
"doesn't have a management IP"
195209
)
196210
ignored_device_count += 1
197211
continue

tests/unit/poller/controller/sources/nautobot/test_nautobot.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def default_config() -> Dict:
3434

3535

3636
_RESPONSE_DATA_DIR = "tests/unit/poller/controller/sources/nautobot/responses/"
37+
_BASE_URL = "https://127.0.0.1:8080/api/"
38+
_DEVICES_URL = "https://127.0.0.1:8080/api/dcim/devices/"
39+
_IP_ADDRESSES_URL = "https://127.0.0.1:8080/api/ipam/ip-addresses/"
40+
_LOCATIONS_URL = "https://127.0.0.1:8080/api/dcim/locations/"
3741

3842
_TEST_CONFIGS = [
3943
{
@@ -44,24 +48,26 @@ def default_config() -> Dict:
4448
},
4549
"test_params": {
4650
"test_urls": {
47-
"https://127.0.0.1:8080/api/": get_json(
48-
_RESPONSE_DATA_DIR + "base_response.json"
49-
),
50-
"https://127.0.0.1:8080/api/dcim/devices/": get_json(
51+
_BASE_URL: get_json(_RESPONSE_DATA_DIR + "base_response.json"),
52+
_DEVICES_URL: get_json(
5153
_RESPONSE_DATA_DIR + "all-devices.json"
5254
),
5355
# ang01-edge-01
54-
"https://127.0.0.1:8080/api/ipam/ip-addresses/fe06d6c1-b233-4499-b5e9-f36af5a72dc3/": get_json(
56+
_IP_ADDRESSES_URL
57+
+ "fe06d6c1-b233-4499-b5e9-f36af5a72dc3/": get_json(
5558
_RESPONSE_DATA_DIR + "ang01-edge-01_ip.json"
5659
),
57-
"https://127.0.0.1:8080/api/dcim/locations/279b30b2-7aee-45be-8086-9d151ce22799/": get_json(
60+
_LOCATIONS_URL
61+
+ "279b30b2-7aee-45be-8086-9d151ce22799/": get_json(
5862
_RESPONSE_DATA_DIR + "ang01-edge-01_location.json"
5963
),
6064
# azd01-leaf-07
61-
"https://127.0.0.1:8080/api/ipam/ip-addresses/94f6cbb7-2897-4b9e-91ef-573f9c0b44d8/": get_json(
65+
_IP_ADDRESSES_URL
66+
+ "94f6cbb7-2897-4b9e-91ef-573f9c0b44d8/": get_json(
6267
_RESPONSE_DATA_DIR + "azd01-leaf-07_ip.json"
6368
),
64-
"https://127.0.0.1:8080/api/dcim/locations/f6aa82a1-c61a-4b3e-8f4d-03e09e32feb6/": get_json(
69+
_LOCATIONS_URL
70+
+ "f6aa82a1-c61a-4b3e-8f4d-03e09e32feb6/": get_json(
6571
_RESPONSE_DATA_DIR + "azd01-leaf-07_location.json"
6672
),
6773
},
@@ -78,24 +84,26 @@ def default_config() -> Dict:
7884
},
7985
"test_params": {
8086
"test_urls": {
81-
"https://127.0.0.1:8080/api/": get_json(
82-
_RESPONSE_DATA_DIR + "base_response.json"
83-
),
84-
"https://127.0.0.1:8080/api/dcim/devices/": get_json(
87+
_BASE_URL: get_json(_RESPONSE_DATA_DIR + "base_response.json"),
88+
_DEVICES_URL: get_json(
8589
_RESPONSE_DATA_DIR + "all-devices.json"
8690
),
8791
# ang01-edge-01
88-
"https://127.0.0.1:8080/api/ipam/ip-addresses/fe06d6c1-b233-4499-b5e9-f36af5a72dc3/": get_json(
92+
_IP_ADDRESSES_URL
93+
+ "fe06d6c1-b233-4499-b5e9-f36af5a72dc3/": get_json(
8994
_RESPONSE_DATA_DIR + "ang01-edge-01_ip.json"
9095
),
91-
"https://127.0.0.1:8080/api/dcim/locations/279b30b2-7aee-45be-8086-9d151ce22799/": get_json(
96+
_LOCATIONS_URL
97+
+ "279b30b2-7aee-45be-8086-9d151ce22799/": get_json(
9298
_RESPONSE_DATA_DIR + "ang01-edge-01_location.json"
9399
),
94100
# azd01-leaf-07
95-
"https://127.0.0.1:8080/api/ipam/ip-addresses/94f6cbb7-2897-4b9e-91ef-573f9c0b44d8/": get_json(
101+
_IP_ADDRESSES_URL
102+
+ "94f6cbb7-2897-4b9e-91ef-573f9c0b44d8/": get_json(
96103
_RESPONSE_DATA_DIR + "azd01-leaf-07_ip.json"
97104
),
98-
"https://127.0.0.1:8080/api/dcim/locations/f6aa82a1-c61a-4b3e-8f4d-03e09e32feb6/": get_json(
105+
_LOCATIONS_URL
106+
+ "f6aa82a1-c61a-4b3e-8f4d-03e09e32feb6/": get_json(
99107
_RESPONSE_DATA_DIR + "azd01-leaf-07_location.json"
100108
),
101109
},
@@ -113,17 +121,18 @@ def default_config() -> Dict:
113121
},
114122
"test_params": {
115123
"test_urls": {
116-
"https://127.0.0.1:8080/api/": get_json(
117-
_RESPONSE_DATA_DIR + "base_response.json"
118-
),
119-
"https://127.0.0.1:8080/api/dcim/devices/?"
124+
_BASE_URL: get_json(_RESPONSE_DATA_DIR + "base_response.json"),
125+
_DEVICES_URL
126+
+ "?"
120127
+ urllib.parse.urlencode({"name": "ang01-edge-01"}): get_json(
121128
_RESPONSE_DATA_DIR + "ang01-edge-01_device.json"
122129
),
123-
"https://127.0.0.1:8080/api/ipam/ip-addresses/fe06d6c1-b233-4499-b5e9-f36af5a72dc3/": get_json(
130+
_IP_ADDRESSES_URL
131+
+ "fe06d6c1-b233-4499-b5e9-f36af5a72dc3/": get_json(
124132
_RESPONSE_DATA_DIR + "ang01-edge-01_ip.json"
125133
),
126-
"https://127.0.0.1:8080/api/dcim/locations/279b30b2-7aee-45be-8086-9d151ce22799/": get_json(
134+
_LOCATIONS_URL
135+
+ "279b30b2-7aee-45be-8086-9d151ce22799/": get_json(
127136
_RESPONSE_DATA_DIR + "ang01-edge-01_location.json"
128137
),
129138
},
@@ -141,17 +150,18 @@ def default_config() -> Dict:
141150
},
142151
"test_params": {
143152
"test_urls": {
144-
"https://127.0.0.1:8080/api/": get_json(
145-
_RESPONSE_DATA_DIR + "base_response.json"
146-
),
147-
"https://127.0.0.1:8080/api/dcim/devices/?"
153+
_BASE_URL: get_json(_RESPONSE_DATA_DIR + "base_response.json"),
154+
_DEVICES_URL
155+
+ "?"
148156
+ urllib.parse.urlencode({"location": "AZD01"}): get_json(
149157
_RESPONSE_DATA_DIR + "azd01-leaf-07_device.json"
150158
),
151-
"https://127.0.0.1:8080/api/ipam/ip-addresses/94f6cbb7-2897-4b9e-91ef-573f9c0b44d8/": get_json(
159+
_IP_ADDRESSES_URL
160+
+ "94f6cbb7-2897-4b9e-91ef-573f9c0b44d8/": get_json(
152161
_RESPONSE_DATA_DIR + "azd01-leaf-07_ip.json"
153162
),
154-
"https://127.0.0.1:8080/api/dcim/locations/f6aa82a1-c61a-4b3e-8f4d-03e09e32feb6/": get_json(
163+
_LOCATIONS_URL
164+
+ "f6aa82a1-c61a-4b3e-8f4d-03e09e32feb6/": get_json(
155165
_RESPONSE_DATA_DIR + "azd01-leaf-07_location.json"
156166
),
157167
},
@@ -193,7 +203,9 @@ def update_config(server_conf: Dict, config: Dict) -> Dict:
193203
@pytest.mark.controller_source_nautobot
194204
@pytest.mark.asyncio
195205
@pytest.mark.parametrize("test_conf", _TEST_CONFIGS)
196-
async def test_valid_config(test_conf, default_config):
206+
async def test_valid_config(
207+
test_conf, default_config
208+
): # pylint: disable=redefined-outer-name
197209
"""Tests if the pulled inventory is valid
198210
199211
Args:
@@ -219,7 +231,9 @@ async def test_valid_config(test_conf, default_config):
219231
@pytest.mark.controller_unit_tests
220232
@pytest.mark.controller_source_nautobot
221233
@pytest.mark.asyncio
222-
async def test_invalid_filters(default_config):
234+
async def test_invalid_filters(
235+
default_config,
236+
): # pylint: disable=redefined-outer-name
223237
"""Tests for invalid filter parameters.
224238
225239
Args:
@@ -245,7 +259,8 @@ async def test_invalid_filters(default_config):
245259
with pytest.raises(InventorySourceError) as exc_info:
246260
src = Nautobot(config.copy())
247261
await asyncio.wait_for(src.run(), 10)
248-
assert (
249-
exc_info.value.args[0]
250-
== "nautobot0: error while getting devices: The request failed with code 400 Bad Request: {'foo': ['Unknown filter field']}"
262+
assert exc_info.value.args[0] == (
263+
"nautobot0: error while getting devices: "
264+
"The request failed with code 400 Bad Request: "
265+
"{'foo': ['Unknown filter field']}"
251266
)

0 commit comments

Comments
 (0)