Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ body:
Describe in detail the exact steps that someone else can take to
reproduce this bug using the current stable release of the Ansible NetBox collection.
Include any sanatized playbooks, variables, & tasks specifically showing just the issue.
#placeholder: |
# placeholder: |

validations:
required: true
Expand Down
19 changes: 13 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ jobs:
- name: Run black
uses: psf/black@stable

- name: Run yamllint
uses: ibiqlik/action-yamllint@v3
with:
file_or_dir: .
config_file: .yamllint.yml
strict: true

unit_testing:
runs-on: ubuntu-24.04
needs: linting
Expand Down Expand Up @@ -66,7 +73,7 @@ jobs:
strategy:
fail-fast: false
matrix:
include:
include:
- VERSION: "v4.1"
NETBOX_DOCKER_VERSION: 3.0.1
- VERSION: "v4.2"
Expand Down Expand Up @@ -114,7 +121,7 @@ jobs:
docker container ls
docker logs netbox-docker-netbox-1
timeout 300 bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:32768/login/)" != "200" ]]; do echo "waiting for NetBox"; sleep 5; done' || false
working-directory: /home/runner/.ansible/collections/ansible_collections/netbox/netbox
working-directory: /home/runner/.ansible/collections/ansible_collections/netbox/netbox

- name: Pre-populate NetBox
run: ./tests/integration/netbox-deploy.py
Expand All @@ -126,13 +133,13 @@ jobs:
ansible-test integration -v --color --coverage --python ${{ env.python-version }} inventory-${{ matrix.VERSION }}
working-directory: /home/runner/.ansible/collections/ansible_collections/netbox/netbox

- name: Run regression tests
- name: Run regression tests
continue-on-error: true
run: |
ansible-test integration -v --color --coverage --python ${{ env.python-version }} regression-${{ matrix.VERSION }}
run: |
ansible-test integration -v --color --coverage --python ${{ env.python-version }} regression-${{ matrix.VERSION }}
working-directory: /home/runner/.ansible/collections/ansible_collections/netbox/netbox

- name: Run integration tests
- name: Run integration tests
run: |
ansible-test integration -v --color --coverage --python ${{ env.python-version }} ${{ matrix.VERSION }}
ansible-test coverage report --all --omit "tests/*,hacking/*,docs/*" --show-missing
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ jobs:
'${{ needs.unit-galaxy.result }}',
'${{ needs.unit-source.result }}',
'${{ needs.ansible-lint.result }}'
])"
])"
52 changes: 52 additions & 0 deletions .yamllint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
extends: default

# see rules at https://yamllint.readthedocs.io/en/latest/rules.html
ignore: |
- .cache/
- .venv/
- .github/

rules:
braces:
max-spaces-inside: 20
level: error
brackets:
max-spaces-inside: 20
level: error
colons:
max-spaces-before: 1
max-spaces-after: 20
level: error
commas:
max-spaces-before: 50
min-spaces-after: 1
max-spaces-after: 20
level: error
comments:
min-spaces-from-content: 1
comments-indentation: disable
document-start:
present: true
empty-lines:
max: 3
level: error
hyphens:
level: error
indentation:
spaces: 2
indent-sequences: whatever
check-multi-line-strings: false
key-duplicates: enable
line-length:
max: 230
level: warning
allow-non-breakable-words: true
allow-non-breakable-inline-mappings: true
new-line-at-end-of-file: enable
new-lines:
type: unix
trailing-spaces: enable
truthy: disable
octal-values:
forbid-explicit-octal: true
1 change: 1 addition & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
ancestor: null
releases:
0.1.0:
Expand Down
1 change: 1 addition & 0 deletions changelogs/fragments/1433-add-custom-headers.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- Add support for custom headers for all modules
1 change: 1 addition & 0 deletions changelogs/fragments/1435-fix-duplicate-task-name.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
bugfixes:
- Fix task duplicate task name in documentation that cause ansible-lint error
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
bugfixes:
- nb_inventory - Fix service collection for version greater than 4.3
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
bugfixes:
- Add netbox version check to support service creation for netbox version prior of 4.3
- Use dedicated function to check netbox version istead of self.full_version for rack.
Expand Down
1 change: 1 addition & 0 deletions changelogs/fragments/1459-add-netbox-data-sources.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- netbox_data_source - New module `#1459 <https://github.com/netbox-community/ansible_modules/pull/1459>`
1 change: 1 addition & 0 deletions changelogs/fragments/1462-fix-pylint-errors.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- Fix ansible-bad-import-from pylint errors
1 change: 1 addition & 0 deletions changelogs/fragments/1464-fix-mac-lookup-by-id.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
bugfixes:
- "nb_device_interface: Fix specifying primary_mac_address objects by id for disambiguation"
1 change: 1 addition & 0 deletions changelogs/fragments/1469-fix-old-apis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- Fix broken code path when using old api path on old netbox systems
9 changes: 9 additions & 0 deletions changelogs/fragments/1479-fix-services_parent_object.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
bugfixes:
- add parent_object_type and parent_object_id to services ALLOWED_QUERY_PARAMS
- add workaround to _build_query_params for services and Netbox 4.3.0 - 4.4.3 (wrong parent_object_type data type)
minor_changes:
- sanitized full_version in netbox_utils
- added _version_less_than to netbox_utils
- improved _version_check_greater in netbox_utils
- added yamllint and errors fixes
1 change: 1 addition & 0 deletions changelogs/fragments/contacts.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- netbox_contact_assignment - New module `#1480 <https://github.com/netbox-community/ansible_modules/pull/1480>`
1 change: 1 addition & 0 deletions changelogs/fragments/nb_inventory.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
bugfixes:
- nb_inventory - Fixed empty inventory results when netbox server URL is a non-root path
1 change: 1 addition & 0 deletions changelogs/fragments/netbox_circuit_termination.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- netbox_circuit_termination - Add parameters termination_id and termination_type for NetBox 4.2+
1 change: 1 addition & 0 deletions changelogs/fragments/netbox_custom_field.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- Support for related_object_filter when related_object_type is "object"
1 change: 1 addition & 0 deletions changelogs/fragments/netbox_service.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
bugfixes:
- netbox_service - Fix issue 1426 - broken netbox_service module
1 change: 1 addition & 0 deletions changelogs/fragments/netbox_tag.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
minor_changes:
- netbox_tag - Add support for object_types on tags
90 changes: 65 additions & 25 deletions plugins/module_utils/netbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,17 @@
"rir": set(["slug"]),
"role": set(["slug"]),
"route_target": set(["name"]),
"services": set(["device", "virtual_machine", "name", "port", "protocol"]),
"services": set(
[
"device",
"virtual_machine",
"name",
"port",
"protocol",
"parent_object_type",
"parent_object_id",
]
),
"service_template": set(["name"]),
"site": set(["slug", "name"]),
"site_group": set(["slug"]),
Expand Down Expand Up @@ -771,7 +781,9 @@ def __init__(self, module, endpoint, nb_client=None):
try:
self.version = self.nb.version
try:
self.full_version = self.nb.status().get("netbox-version")
self.full_version = re.match(
r"^([\d.]+)", self.nb.status().get("netbox-version")
).group(1)
except Exception:
# For NetBox versions without /api/status endpoint
self.full_version = f"{self.version}.0"
Expand All @@ -788,32 +800,49 @@ def __init__(self, module, endpoint, nb_client=None):
data = self._find_ids(choices_data, query_params)
self.data = self._convert_identical_keys(data)

def _version_check_greater(self, greater, lesser, greater_or_equal=False):
def _version_check_greater(
self, ver_net: str, ver_def: str, greater_or_equal=False
) -> bool:
"""Determine if first argument is greater than second argument.

Args:
greater (str): decimal string
lesser (str): decimal string
ver_net (str): decimal string
ver_def (str): decimal string
"""
t_net = tuple(
int(x) for x in re.match(r"^([\d.]+)", ver_net).group(1).split(".")
)
t_def = tuple(
int(x) for x in re.match(r"^([\d.]+)", ver_def).group(1).split(".")
)

# Pad shorter tuple with zeros
max_len = max(len(t_net), len(t_def))
t_net += (0,) * (max_len - len(t_net))
t_def += (0,) * (max_len - len(t_def))

return t_net > t_def if not greater_or_equal else t_net >= t_def

def _version_less_than(self, ver_net: str, ver_def: str) -> bool:
"""Determine if first argument is smaller than second argument.

Args:
ver_net (str): decimal string
ver_def (str): decimal string
"""
g_major, g_minor = greater.split(".")
l_major, l_minor = lesser.split(".")

# convert to ints
g_major = int(g_major)
g_minor = int(g_minor)
l_major = int(l_major)
l_minor = int(l_minor)

# If major version is higher then return true right off the bat
if g_major > l_major:
return True
elif greater_or_equal and g_major == l_major and g_minor >= l_minor:
return True
# If major versions are equal, and minor version is higher, return True
elif g_major == l_major and g_minor > l_minor:
return True

return False
t_net = tuple(
int(x) for x in re.match(r"^([\d.]+)", ver_net).group(1).split(".")
)
t_def = tuple(
int(x) for x in re.match(r"^([\d.]+)", ver_def).group(1).split(".")
)

# Pad shorter tuple with zeros
max_len = max(len(t_net), len(t_def))
t_net += (0,) * (max_len - len(t_net))
t_def += (0,) * (max_len - len(t_def))

return t_net < t_def

def _connect_netbox_api(self, url, token, ssl_verify, cert, headers=None):
try:
Expand All @@ -830,7 +859,9 @@ def _connect_netbox_api(self, url, token, ssl_verify, cert, headers=None):
try:
self.version = nb.version
try:
self.full_version = nb.status().get("netbox-version")
self.full_version = re.match(
r"^([\d.]+)", nb.status().get("netbox-version")
).group(1)
except Exception:
# For NetBox versions without /api/status endpoint
self.full_version = f"{self.version}.0"
Expand Down Expand Up @@ -1173,6 +1204,15 @@ def _build_query_params(
query_dict["device_type_id"] = query_dict.pop("device_type")
else:
query_dict["devicetype_id"] = query_dict.pop("device_type")
# TODO workaround for Netbox 4.3.0 - 4.4.3 - #20554
# Remove 'elif parent == "services":' block after support for
# Netbox 4.3.0 - 4.4.3 is removed
elif parent == "services":
if self._version_check_greater(
self.version, "4.3", greater_or_equal=True
) and self._version_less_than(self.full_version, "4.4.4"):
query_dict.pop("parent_object_id", None)
query_dict.pop("parent_object_type", None)

if not query_dict:
provided_kwargs = child.keys() if child else module_data.keys()
Expand Down
1 change: 1 addition & 0 deletions requirements.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
collections:
- name: .
Loading