Skip to content

Commit c74b7d3

Browse files
2.10 Fixes (#396)
1 parent bf69006 commit c74b7d3

File tree

85 files changed

+402
-456
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+402
-456
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
run: ansible-test sanity -v --requirements --python ${{ matrix.python-version }} --skip-test pep8 plugins/
3434
working-directory: /home/runner/.ansible/collections/ansible_collections/netbox/netbox
3535
- name: Run Ansible Unit tests
36-
run: ansible-test units -v --coverage --python ${{ matrix.python-version }}
36+
run: ansible-test units -vvv --coverage --python ${{ matrix.python-version }}
3737
working-directory: /home/runner/.ansible/collections/ansible_collections/netbox/netbox
3838
- name: Run Ansible Coverage
3939
run: ansible-test coverage report --all --omit "tests/*,hacking/*,docs/*" --show-missing
@@ -46,13 +46,10 @@ jobs:
4646
matrix:
4747
include:
4848
- python-version: 3.6
49-
VERSION: "v2.8"
50-
INTEGRATION_TESTS: "v2.8"
51-
- python-version: 3.6
52-
VERSION: "v2.9"
53-
INTEGRATION_TESTS: "latest"
54-
- python-version: 3.7
5549
VERSION: "v2.9"
50+
INTEGRATION_TESTS: "v2.9"
51+
- python-version: 3.6
52+
VERSION: "latest"
5653
INTEGRATION_TESTS: "latest"
5754
steps:
5855
- name: Checkout repo
@@ -62,23 +59,12 @@ jobs:
6259
with:
6360
python-version: ${{ matrix.python-version }}
6461
- name: Clone & Start netbox-docker containers
65-
if: matrix.VERSION != 'v2.8'
66-
run: |
67-
cd ..
68-
git clone https://github.com/netbox-community/netbox-docker.git
69-
cd netbox-docker
70-
docker-compose up -d --quiet-pull
71-
docker container ls
72-
cd ..
73-
- name: Clone & Start netbox-docker containers - NetBox v2.8
74-
if: matrix.VERSION == 'v2.8'
62+
env:
63+
VERSION: ${{ matrix.VERSION }}
7564
run: |
7665
cd ..
7766
git clone https://github.com/netbox-community/netbox-docker.git
7867
cd netbox-docker
79-
git checkout 0.24.1
80-
sed -i 's/SKIP_STARTUP_SCRIPTS=false/SKIP_STARTUP_SCRIPTS=true/' env/netbox.env
81-
export VERSION=${{ matrix.VERSION }}
8268
docker-compose up -d --quiet-pull
8369
docker container ls
8470
cd ..

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ venv/
66
.vscode/
77
changelogs/.plugin-cache.yaml
88
docs/_build/*
9+
.python-version
910

1011
# https://github.com/ansible/ansible/issues/68499
1112
# ansible_collections/

plugins/module_utils/netbox_ipam.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def _ensure_ip_in_prefix_present_on_netif(
5757
"parent": data["prefix"],
5858
}
5959

60-
if self.version < 2.9:
60+
if not self._version_check_greater(self.version, "2.9", greater_or_equal=True):
6161
if not data.get("interface") or not data.get("prefix"):
6262
self._handle_errors("A prefix and interface is required")
6363
data_intf_key = "interface"

plugins/module_utils/netbox_utils.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import traceback
1313
import re
1414
import json
15+
1516
from itertools import chain
1617

1718
from ansible_collections.ansible.netcommon.plugins.module_utils.compat import ipaddress
@@ -392,7 +393,7 @@
392393
"vlan_group": "group",
393394
}
394395

395-
# This is used to dynamically conver name to slug on endpoints requiring a slug
396+
# This is used to dynamically convert name to slug on endpoints requiring a slug
396397
SLUG_REQUIRED = {
397398
"circuit_types",
398399
"cluster_groups",
@@ -438,7 +439,6 @@ def __init__(self, module, endpoint, nb_client=None):
438439
self.state = self.module.params["state"]
439440
self.check_mode = self.module.check_mode
440441
self.endpoint = endpoint
441-
self.version = None
442442
query_params = self.module.params.get("query_params")
443443

444444
if not HAS_PYNETBOX:
@@ -455,6 +455,10 @@ def __init__(self, module, endpoint, nb_client=None):
455455
self.nb = self._connect_netbox_api(url, token, ssl_verify)
456456
else:
457457
self.nb = nb_client
458+
try:
459+
self.version = self.nb.version
460+
except AttributeError:
461+
self.module.fail_json(msg="Must have pynetbox >=4.1.0")
458462

459463
# if self.module.params.get("query_params"):
460464
# self._validate_query_params(self.module.params["query_params"])
@@ -466,14 +470,39 @@ def __init__(self, module, endpoint, nb_client=None):
466470
data = self._find_ids(choices_data, query_params)
467471
self.data = self._convert_identical_keys(data)
468472

473+
def _version_check_greater(self, greater, lesser, greater_or_equal=False):
474+
"""Determine if first argument is greater than second argument.
475+
476+
Args:
477+
greater (str): decimal string
478+
lesser (str): decimal string
479+
"""
480+
g_major, g_minor = greater.split(".")
481+
l_major, l_minor = lesser.split(".")
482+
483+
# convert to ints
484+
g_major = int(g_major)
485+
g_minor = int(g_minor)
486+
l_major = int(l_major)
487+
l_minor = int(l_minor)
488+
489+
# If major version is higher then return true right off the bat
490+
if g_major > l_major:
491+
return True
492+
elif greater_or_equal and g_major == l_major and g_minor >= l_minor:
493+
return True
494+
# If major versions are equal, and minor version is higher, return True
495+
elif g_major == l_major and g_minor > l_minor:
496+
return True
497+
469498
def _connect_netbox_api(self, url, token, ssl_verify):
470499
try:
471500
session = requests.Session()
472501
session.verify = ssl_verify
473502
nb = pynetbox.api(url, token=token)
474503
nb.http_session = session
475504
try:
476-
self.version = float(nb.version)
505+
self.version = nb.version
477506
except AttributeError:
478507
self.module.fail_json(msg="Must have pynetbox >=4.1.0")
479508
except Exception:
@@ -557,9 +586,9 @@ def _convert_identical_keys(self, data):
557586
:params data (dict): Data dictionary after _find_ids method ran
558587
"""
559588
temp_dict = dict()
560-
if self.version and self.version >= 2.7:
589+
if self._version_check_greater(self.version, "2.7", greater_or_equal=True):
561590
if data.get("form_factor"):
562-
temp_dict["type"] = data["form_factor"]
591+
temp_dict["type"] = data.pop("form_factor")
563592
for key in data:
564593
if self.endpoint == "power_panels" and key == "rack_group":
565594
temp_dict[key] = data[key]
@@ -773,7 +802,12 @@ def _find_ids(self, data, user_query_params):
773802
"""
774803
for k, v in data.items():
775804
if k in CONVERT_TO_ID:
776-
if self.version < 2.9 and k == "tags":
805+
if (
806+
not self._version_check_greater(
807+
self.version, "2.9", greater_or_equal=True
808+
)
809+
and k == "tags"
810+
):
777811
continue
778812
if k == "termination_a":
779813
endpoint = CONVERT_TO_ID[data.get("termination_a_type")]

plugins/modules/netbox_service.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@
6060
port:
6161
description:
6262
- Specifies which port used by service
63-
required: true
63+
required: false
6464
type: int
65+
ports:
66+
description:
67+
- Specifies which ports used by service (NetBox 2.10 and newer)
68+
type: list
69+
elements: int
6570
protocol:
6671
description:
6772
- Specifies which protocol used by service
@@ -174,7 +179,8 @@ def main():
174179
device=dict(required=False, type="raw"),
175180
virtual_machine=dict(required=False, type="raw"),
176181
name=dict(required=True, type="str"),
177-
port=dict(required=True, type="int"),
182+
port=dict(required=False, type="int"),
183+
ports=dict(required=False, type="list", elements="int"),
178184
protocol=dict(required=True, type="raw"),
179185
ipaddresses=dict(required=False, type="raw"),
180186
description=dict(required=False, type="str"),
@@ -185,17 +191,30 @@ def main():
185191
)
186192
)
187193

188-
required_if = [("state", "present", ["name"]), ("state", "absent", ["name"])]
189-
required_one_of = [["device", "virtual_machine"]]
194+
required_if = [
195+
("state", "present", ["name"]),
196+
("state", "absent", ["name"]),
197+
]
198+
mutually_exclusive = [("port", "ports")]
199+
required_one_of = [["device", "virtual_machine"], ["port", "ports"]]
190200

191201
module = NetboxAnsibleModule(
192202
argument_spec=argument_spec,
193203
supports_check_mode=True,
194204
required_if=required_if,
195205
required_one_of=required_one_of,
206+
mutually_exclusive=mutually_exclusive,
196207
)
197208

198209
netbox_service = NetboxIpamModule(module, NB_SERVICES)
210+
211+
# Change port to ports for 2.10+ and convert to a list with the single integer
212+
if netbox_service.data.get("port") and netbox_service._version_check_greater(
213+
netbox_service.version, "2.10", greater_or_equal=True
214+
):
215+
netbox_service.data["ports"] = [netbox_service.data.pop("port")]
216+
217+
# Run the normal run() method
199218
netbox_service.run()
200219

201220

tests/integration/targets/inventory-latest/files/test-inventory-legacy.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
"id": 3,
6666
"ipaddresses": [],
6767
"name": "telnet",
68-
"port": 23,
68+
"ports": [
69+
23
70+
],
6971
"protocol": {
7072
"label": "TCP",
7173
"value": "tcp"
@@ -100,7 +102,9 @@
100102
"id": 4,
101103
"ipaddresses": [],
102104
"name": "ssh",
103-
"port": 22,
105+
"ports": [
106+
22
107+
],
104108
"protocol": {
105109
"label": "TCP",
106110
"value": "tcp"
@@ -189,7 +193,9 @@
189193
"id": 1,
190194
"ipaddresses": [],
191195
"name": "ssh",
192-
"port": 22,
196+
"ports": [
197+
22
198+
],
193199
"protocol": {
194200
"label": "TCP",
195201
"value": "tcp"
@@ -219,7 +225,9 @@
219225
}
220226
],
221227
"name": "http",
222-
"port": 80,
228+
"ports": [
229+
80
230+
],
223231
"protocol": {
224232
"label": "TCP",
225233
"value": "tcp"

tests/integration/targets/inventory-latest/files/test-inventory-options-flatten.json

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@
5050
"interfaces": [
5151
{
5252
"cable": null,
53+
"cable_peer": null,
54+
"cable_peer_type": null,
5355
"connected_endpoint": null,
56+
"connected_endpoint_reachable": null,
5457
"connected_endpoint_type": null,
55-
"connection_status": null,
5658
"count_ipaddresses": 1,
5759
"description": "",
5860
"device": {
@@ -102,9 +104,11 @@
102104
},
103105
{
104106
"cable": null,
107+
"cable_peer": null,
108+
"cable_peer_type": null,
105109
"connected_endpoint": null,
110+
"connected_endpoint_reachable": null,
106111
"connected_endpoint_type": null,
107-
"connection_status": null,
108112
"count_ipaddresses": 1,
109113
"description": "",
110114
"device": {
@@ -173,7 +177,9 @@
173177
"id": 3,
174178
"ipaddresses": [],
175179
"name": "telnet",
176-
"port": 23,
180+
"ports": [
181+
23
182+
],
177183
"protocol": {
178184
"label": "TCP",
179185
"value": "tcp"
@@ -238,7 +244,9 @@
238244
"id": 4,
239245
"ipaddresses": [],
240246
"name": "ssh",
241-
"port": 22,
247+
"ports": [
248+
22
249+
],
242250
"protocol": {
243251
"label": "TCP",
244252
"value": "tcp"
@@ -284,9 +292,11 @@
284292
"interfaces": [
285293
{
286294
"cable": null,
295+
"cable_peer": null,
296+
"cable_peer_type": null,
287297
"connected_endpoint": null,
298+
"connected_endpoint_reachable": null,
288299
"connected_endpoint_type": null,
289-
"connection_status": null,
290300
"count_ipaddresses": 1,
291301
"description": "",
292302
"device": {
@@ -336,9 +346,11 @@
336346
},
337347
{
338348
"cable": null,
349+
"cable_peer": null,
350+
"cable_peer_type": null,
339351
"connected_endpoint": null,
352+
"connected_endpoint_reachable": null,
340353
"connected_endpoint_type": null,
341-
"connection_status": null,
342354
"count_ipaddresses": 1,
343355
"description": "",
344356
"device": {
@@ -409,7 +421,9 @@
409421
"id": 1,
410422
"ipaddresses": [],
411423
"name": "ssh",
412-
"port": 22,
424+
"ports": [
425+
22
426+
],
413427
"protocol": {
414428
"label": "TCP",
415429
"value": "tcp"
@@ -439,7 +453,9 @@
439453
}
440454
],
441455
"name": "http",
442-
"port": 80,
456+
"ports": [
457+
80
458+
],
443459
"protocol": {
444460
"label": "TCP",
445461
"value": "tcp"

0 commit comments

Comments
 (0)