diff --git a/changelogs/fragments/1182-fix-contact-groups.yml b/changelogs/fragments/1182-fix-contact-groups.yml new file mode 100644 index 000000000..f82b69e31 --- /dev/null +++ b/changelogs/fragments/1182-fix-contact-groups.yml @@ -0,0 +1,3 @@ +minor_changes: + - Change `netbox_contact.contact_group` to `contact_groups` + - Add integration tests for contact groups diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index 2d5549849..f3fc473e9 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -155,7 +155,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", @@ -643,7 +643,7 @@ "cluster_type": "type", "cluster_group": "group", "component": "component_id", - "contact_group": "group", + "contact_groups": "groups", "device_role": "role", "fhrp_group": "group", "inventory_item_role": "role", diff --git a/plugins/modules/netbox_contact.py b/plugins/modules/netbox_contact.py index a89cd59c1..2846c2348 100644 --- a/plugins/modules/netbox_contact.py +++ b/plugins/modules/netbox_contact.py @@ -65,11 +65,12 @@ - Comments on the contact required: false type: str - contact_group: + contact_groups: description: - - Group assignment for the contact + - Groups that the contact belongs to required: false - type: raw + type: list + elements: raw link: description: - URL associated with the contact @@ -121,6 +122,9 @@ title: Mr Contact phone: 123456789 email: contac@contact.com + contact_groups: + - Group 1 + - Group 2 tags: - tagA - tagB @@ -168,7 +172,7 @@ def main(): address=dict(required=False, type="str"), 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"), diff --git a/tests/integration/targets/v4.3/tasks/main.yml b/tests/integration/targets/v4.3/tasks/main.yml index 33ce77bcf..c37909af6 100644 --- a/tests/integration/targets/v4.3/tasks/main.yml +++ b/tests/integration/targets/v4.3/tasks/main.yml @@ -71,6 +71,15 @@ tags: - netbox_contact +- 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: file: netbox_contact_role.yml diff --git a/tests/integration/targets/v4.3/tasks/netbox_contact.yml b/tests/integration/targets/v4.3/tasks/netbox_contact.yml index f5ede9c1a..a10ac0cfe 100644 --- a/tests/integration/targets/v4.3/tasks/netbox_contact.yml +++ b/tests/integration/targets/v4.3/tasks/netbox_contact.yml @@ -98,3 +98,31 @@ - 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_groups: + - 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']['groups'] == [test_group['contact_group']['id']] + - test_six['msg'] == "contact Grouped Contact 1 created" diff --git a/tests/integration/targets/v4.3/tasks/netbox_contact_group.yml b/tests/integration/targets/v4.3/tasks/netbox_contact_group.yml new file mode 100644 index 000000000..090d880a0 --- /dev/null +++ b/tests/integration/targets/v4.3/tasks/netbox_contact_group.yml @@ -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"