Skip to content
Merged
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
3 changes: 3 additions & 0 deletions changelogs/fragments/1182-fix-contact-groups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- Change `netbox_contact.contact_group` to `contact_groups`
- Add integration tests for contact groups
20 changes: 19 additions & 1 deletion plugins/module_utils/netbox_tenancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,27 @@ def run(self):

data = self.data

# In Netbox 4.3, Contact.contact_group was changed to contact_groups.
# We need to check that the correct field is being used.
if self.endpoint == NB_CONTACTS:
if data.get("groups"):
if not self._version_check_greater(
self.version, "4.3", greater_or_equal=True
):
raise Exception(
f"contact_groups is not available in Netbox {self.version}. Use contact_group instead, or upgrade to Netbox 4.3 or greater."
)
if data.get("group"):
if self._version_check_greater(
self.version, "4.3", greater_or_equal=True
):
raise Exception(
f"contact_group is not available in Netbox {self.version}. Use contact_groups instead."
)

# For ease and consistency of use, the contact assignment module takes the name of the contact, role, and target object rather than an ID or slug.
# We must massage the data a bit by looking up the ID corresponding to the given name so that we can pass the ID to the API.
if self.endpoint == "contact_assignments":
if self.endpoint == NB_CONTACT_ASSIGNMENTS:
# Not an identifier, just to populate the message field
name = f"{data['contact']} -> {data['object_name']}"

Expand Down
8 changes: 5 additions & 3 deletions plugins/module_utils/netbox_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
cluster_type="slug",
config_context="name",
config_template="name",
contact_group="name",
contact_groups="name",
contact_role="name",
custom_field="name",
choice_set="name",
Expand Down Expand Up @@ -251,7 +251,8 @@
"component": "interfaces",
"config_context": "config_contexts",
"config_template": "config_templates",
"contact_groups": "contact_groups",
"contact_group": "contact_groups", # Netbox version <4.3
"contact_groups": "contact_groups", # Netbox version >=4.3
"choice_set": "custom_field_choice_sets",
"dcim.consoleport": "console_ports",
"dcim.consoleserverport": "console_server_ports",
Expand Down Expand Up @@ -652,7 +653,8 @@
"cluster_type": "type",
"cluster_group": "group",
"component": "component_id",
"contact_group": "group",
"contact_group": "group", # Netbox version <4.3
"contact_groups": "groups", # Netbox version >=4.3
"device_role": "role",
"fhrp_group": "group",
"inventory_item_role": "role",
Expand Down
12 changes: 11 additions & 1 deletion plugins/modules/netbox_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@
type: str
contact_group:
description:
- Group assignment for the contact
- Group that the contact belongs to. Only available in Netbox version <4.3
required: false
type: raw
contact_groups:
description:
- Groups that the contact belongs to. Only available in Netbox versions >=4.3
required: false
type: list
elements: raw
link:
description:
- URL associated with the contact
Expand Down Expand Up @@ -121,6 +127,9 @@
title: Mr Contact
phone: 123456789
email: [email protected]
contact_groups:
- Group 1
- Group 2
tags:
- tagA
- tagB
Expand Down Expand Up @@ -169,6 +178,7 @@ def main():
description=dict(required=False, type="str"),
comments=dict(required=False, type="str"),
contact_group=dict(required=False, type="raw"),
contact_groups=dict(required=False, type="list", elements="raw"),
link=dict(required=False, type="str"),
tags=dict(required=False, type="list", elements="raw"),
custom_fields=dict(required=False, type="dict"),
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/netbox_virtual_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"""

RETURN = r"""
virtual machine:
virtual_machine:
description: Serialized object as created or already existent within NetBox
returned: success (when I(state=present))
type: dict
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/targets/v4.0/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
- name: NETBOX_CONTACT TESTS
ansible.builtin.include_tasks: netbox_contact.yml

- name: NETBOX_CONTACT_GROUP TESTS
ansible.builtin.include_tasks:
file: netbox_contact_group.yml
apply:
tags:
- netbox_contact_group
tags:
- netbox_contact_group

- name: NETBOX_CONTACT_ROLE TESTS
ansible.builtin.include_tasks: netbox_contact_role.yml

Expand Down
43 changes: 43 additions & 0 deletions tests/integration/targets/v4.0/tasks/netbox_contact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,46 @@
- test_five['contact']['phone'] == "12345678"
- test_five['contact']['tags'] | length == 3
- test_five['msg'] == "contact Contact ABC created"

- name: 6 Setup - Create contact group
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group With Contacts
register: test_group

- name: 6 - Create contact with contact group
netbox.netbox.netbox_contact:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Grouped Contact 1
contact_group: Contact Group With Contacts
register: test_six

- name: 6 - ASSERT
ansible.builtin.assert:
that:
- test_six is changed
- test_six['diff']['before']['state'] == "absent"
- test_six['diff']['after']['state'] == "present"
- test_six['contact']['name'] == "Grouped Contact 1"
- test_six['contact']['group'] == test_group['contact_group']['id']
- test_six['msg'] == "contact Grouped Contact 1 created"

- name: 7 - Try to create contact using contact_groups
netbox.netbox.netbox_contact:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Grouped Contact 2
contact_groups:
- Contact Group With Contacts
register: test_seven
ignore_errors: true

- name: 7 - ASSERT
ansible.builtin.assert:
that:
- "'contact_groups is not available in Netbox 4.0. Use contact_group instead, or upgrade to Netbox 4.3 or greater.' in test_seven['module_stderr']"
119 changes: 119 additions & 0 deletions tests/integration/targets/v4.0/tasks/netbox_contact_group.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
---
##
##
### NETBOX_CONTACT_GROUP
##
##
- name: 1 - Test contact group creation
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group 1
register: test_one

- name: 1 - ASSERT
ansible.builtin.assert:
that:
- test_one is changed
- test_one['diff']['before']['state'] == "absent"
- test_one['diff']['after']['state'] == "present"
- test_one['contact_group']['name'] == "Contact Group 1"
- test_one['msg'] == "contact_group Contact Group 1 created"

- name: Test duplicate contact group
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group 1
register: test_two

- name: 2 - ASSERT
ansible.builtin.assert:
that:
- not test_two['changed']
- test_two['contact_group']['name'] == "Contact Group 1"
- test_two['msg'] == "contact_group Contact Group 1 already exists"

- name: 3 - Test update
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group 1
description: The first contact group
register: test_three

- name: 3 - ASSERT
ansible.builtin.assert:
that:
- test_three is changed
- test_three['diff']['after']['description'] == "The first contact group"
- test_three['contact_group']['name'] == "Contact Group 1"
- test_three['contact_group']['description'] == "The first contact group"
- test_three['msg'] == "contact_group Contact Group 1 updated"

- name: 4 - Test delete
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group 1
state: absent
register: test_four

- name: 4 - ASSERT
ansible.builtin.assert:
that:
- test_four is changed
- test_four['diff']['before']['state'] == "present"
- test_four['diff']['after']['state'] == "absent"
- test_four['msg'] == "contact_group Contact Group 1 deleted"

- name: 5 - Create contact group with all parameters
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group Parent
slug: the-parent-contact-group
description: The parent contact group
tags:
- tagA
- tagB
- tagC
state: present
register: test_five

- name: 5 - ASSERT
ansible.builtin.assert:
that:
- test_five is changed
- test_five['diff']['before']['state'] == "absent"
- test_five['diff']['after']['state'] == "present"
- test_five['contact_group']['name'] == "Contact Group Parent"
- test_five['contact_group']['slug'] == "the-parent-contact-group"
- test_five['contact_group']['description'] == "The parent contact group"
- test_five['contact_group']['tags'] | length == 3
- test_five['msg'] == "contact_group Contact Group Parent created"

- name: 6 - Create contact group with parent contact group
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group Child
parent_contact_group: Contact Group Parent
state: present
register: test_six

- name: 6 - ASSERT
ansible.builtin.assert:
that:
- test_six is changed
- test_six['diff']['before']['state'] == "absent"
- test_six['diff']['after']['state'] == "present"
- test_six['contact_group']['name'] == "Contact Group Child"
- test_six['contact_group']['parent'] == test_five['contact_group']['id']
- test_six['msg'] == "contact_group Contact Group Child created"
9 changes: 9 additions & 0 deletions tests/integration/targets/v4.1/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
- name: NETBOX_CONTACT TESTS
ansible.builtin.include_tasks: netbox_contact.yml

- name: NETBOX_CONTACT_GROUP TESTS
ansible.builtin.include_tasks:
file: netbox_contact_group.yml
apply:
tags:
- netbox_contact_group
tags:
- netbox_contact_group

- name: NETBOX_CONTACT_ROLE TESTS
ansible.builtin.include_tasks: netbox_contact_role.yml

Expand Down
43 changes: 43 additions & 0 deletions tests/integration/targets/v4.1/tasks/netbox_contact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,46 @@
- test_five['contact']['phone'] == "12345678"
- test_five['contact']['tags'] | length == 3
- test_five['msg'] == "contact Contact ABC created"

- name: 6 Setup - Create contact group
netbox.netbox.netbox_contact_group:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Contact Group With Contacts
register: test_group

- name: 6 - Create contact with contact group
netbox.netbox.netbox_contact:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Grouped Contact 1
contact_group: Contact Group With Contacts
register: test_six

- name: 6 - ASSERT
ansible.builtin.assert:
that:
- test_six is changed
- test_six['diff']['before']['state'] == "absent"
- test_six['diff']['after']['state'] == "present"
- test_six['contact']['name'] == "Grouped Contact 1"
- test_six['contact']['group'] == test_group['contact_group']['id']
- test_six['msg'] == "contact Grouped Contact 1 created"

- name: 7 - Try to create contact using contact_groups
netbox.netbox.netbox_contact:
netbox_url: http://localhost:32768
netbox_token: "0123456789abcdef0123456789abcdef01234567"
data:
name: Grouped Contact 2
contact_groups:
- Contact Group With Contacts
register: test_seven
ignore_errors: true

- name: 7 - ASSERT
ansible.builtin.assert:
that:
- "'contact_groups is not available in Netbox 4.1. Use contact_group instead, or upgrade to Netbox 4.3 or greater.' in test_seven['module_stderr']"
Loading
Loading