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_tenancy.py b/plugins/module_utils/netbox_tenancy.py index b593d98bd..6b5c97577 100644 --- a/plugins/module_utils/netbox_tenancy.py +++ b/plugins/module_utils/netbox_tenancy.py @@ -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']}" diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index 271cfefbe..b44bcefd3 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -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", @@ -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", @@ -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", diff --git a/plugins/modules/netbox_contact.py b/plugins/modules/netbox_contact.py index a89cd59c1..c76a10511 100644 --- a/plugins/modules/netbox_contact.py +++ b/plugins/modules/netbox_contact.py @@ -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 @@ -121,6 +127,9 @@ title: Mr Contact phone: 123456789 email: contac@contact.com + contact_groups: + - Group 1 + - Group 2 tags: - tagA - tagB @@ -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"), diff --git a/plugins/modules/netbox_virtual_machine.py b/plugins/modules/netbox_virtual_machine.py index b321cc069..e8a0999da 100644 --- a/plugins/modules/netbox_virtual_machine.py +++ b/plugins/modules/netbox_virtual_machine.py @@ -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 diff --git a/tests/integration/targets/v4.0/tasks/main.yml b/tests/integration/targets/v4.0/tasks/main.yml index f229a24a9..d4f5664ba 100644 --- a/tests/integration/targets/v4.0/tasks/main.yml +++ b/tests/integration/targets/v4.0/tasks/main.yml @@ -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 diff --git a/tests/integration/targets/v4.0/tasks/netbox_contact.yml b/tests/integration/targets/v4.0/tasks/netbox_contact.yml index f5ede9c1a..6403e808a 100644 --- a/tests/integration/targets/v4.0/tasks/netbox_contact.yml +++ b/tests/integration/targets/v4.0/tasks/netbox_contact.yml @@ -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']" diff --git a/tests/integration/targets/v4.0/tasks/netbox_contact_group.yml b/tests/integration/targets/v4.0/tasks/netbox_contact_group.yml new file mode 100644 index 000000000..090d880a0 --- /dev/null +++ b/tests/integration/targets/v4.0/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" diff --git a/tests/integration/targets/v4.1/tasks/main.yml b/tests/integration/targets/v4.1/tasks/main.yml index 1d56afb98..4914837a3 100644 --- a/tests/integration/targets/v4.1/tasks/main.yml +++ b/tests/integration/targets/v4.1/tasks/main.yml @@ -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 diff --git a/tests/integration/targets/v4.1/tasks/netbox_contact.yml b/tests/integration/targets/v4.1/tasks/netbox_contact.yml index f5ede9c1a..291bd6f04 100644 --- a/tests/integration/targets/v4.1/tasks/netbox_contact.yml +++ b/tests/integration/targets/v4.1/tasks/netbox_contact.yml @@ -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']" diff --git a/tests/integration/targets/v4.1/tasks/netbox_contact_group.yml b/tests/integration/targets/v4.1/tasks/netbox_contact_group.yml new file mode 100644 index 000000000..090d880a0 --- /dev/null +++ b/tests/integration/targets/v4.1/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" diff --git a/tests/integration/targets/v4.2/tasks/main.yml b/tests/integration/targets/v4.2/tasks/main.yml index 33ce77bcf..c37909af6 100644 --- a/tests/integration/targets/v4.2/tasks/main.yml +++ b/tests/integration/targets/v4.2/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.2/tasks/netbox_contact.yml b/tests/integration/targets/v4.2/tasks/netbox_contact.yml index f5ede9c1a..75f5f1719 100644 --- a/tests/integration/targets/v4.2/tasks/netbox_contact.yml +++ b/tests/integration/targets/v4.2/tasks/netbox_contact.yml @@ -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.2. Use contact_group instead, or upgrade to Netbox 4.3 or greater.' in test_seven['module_stderr']" diff --git a/tests/integration/targets/v4.2/tasks/netbox_contact_group.yml b/tests/integration/targets/v4.2/tasks/netbox_contact_group.yml new file mode 100644 index 000000000..090d880a0 --- /dev/null +++ b/tests/integration/targets/v4.2/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" diff --git a/tests/integration/targets/v4.3/tasks/main.yml b/tests/integration/targets/v4.3/tasks/main.yml index f6ad8a2ed..ed0745caa 100644 --- a/tests/integration/targets/v4.3/tasks/main.yml +++ b/tests/integration/targets/v4.3/tasks/main.yml @@ -80,6 +80,15 @@ tags: - netbox_contact_assignment +- 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..e62635607 100644 --- a/tests/integration/targets/v4.3/tasks/netbox_contact.yml +++ b/tests/integration/targets/v4.3/tasks/netbox_contact.yml @@ -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_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" + +- name: 7 - Try to create contact using contact_group + netbox.netbox.netbox_contact: + netbox_url: http://localhost:32768 + netbox_token: "0123456789abcdef0123456789abcdef01234567" + data: + name: Grouped Contact 2 + contact_group: Contact Group With Contacts + register: test_seven + ignore_errors: true + +- name: 7 - ASSERT + ansible.builtin.assert: + that: + - "'contact_group is not available in Netbox 4.3. Use contact_groups instead.' in test_seven['module_stderr']" 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"