Skip to content

Commit 001d241

Browse files
authored
Merge pull request #1481 from dchiquito/1182-fix-contact-groups
Fix contact groups
2 parents d368f87 + b62a510 commit 001d241

File tree

17 files changed

+723
-6
lines changed

17 files changed

+723
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
minor_changes:
2+
- Change `netbox_contact.contact_group` to `contact_groups`
3+
- Add integration tests for contact groups

plugins/module_utils/netbox_tenancy.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,27 @@ def run(self):
107107

108108
data = self.data
109109

110+
# In Netbox 4.3, Contact.contact_group was changed to contact_groups.
111+
# We need to check that the correct field is being used.
112+
if self.endpoint == NB_CONTACTS:
113+
if data.get("groups"):
114+
if not self._version_check_greater(
115+
self.version, "4.3", greater_or_equal=True
116+
):
117+
raise Exception(
118+
f"contact_groups is not available in Netbox {self.version}. Use contact_group instead, or upgrade to Netbox 4.3 or greater."
119+
)
120+
if data.get("group"):
121+
if self._version_check_greater(
122+
self.version, "4.3", greater_or_equal=True
123+
):
124+
raise Exception(
125+
f"contact_group is not available in Netbox {self.version}. Use contact_groups instead."
126+
)
127+
110128
# 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.
111129
# 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.
112-
if self.endpoint == "contact_assignments":
130+
if self.endpoint == NB_CONTACT_ASSIGNMENTS:
113131
# Not an identifier, just to populate the message field
114132
name = f"{data['contact']} -> {data['object_name']}"
115133

plugins/module_utils/netbox_utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
cluster_type="slug",
160160
config_context="name",
161161
config_template="name",
162-
contact_group="name",
162+
contact_groups="name",
163163
contact_role="name",
164164
custom_field="name",
165165
choice_set="name",
@@ -251,7 +251,8 @@
251251
"component": "interfaces",
252252
"config_context": "config_contexts",
253253
"config_template": "config_templates",
254-
"contact_groups": "contact_groups",
254+
"contact_group": "contact_groups", # Netbox version <4.3
255+
"contact_groups": "contact_groups", # Netbox version >=4.3
255256
"choice_set": "custom_field_choice_sets",
256257
"dcim.consoleport": "console_ports",
257258
"dcim.consoleserverport": "console_server_ports",
@@ -652,7 +653,8 @@
652653
"cluster_type": "type",
653654
"cluster_group": "group",
654655
"component": "component_id",
655-
"contact_group": "group",
656+
"contact_group": "group", # Netbox version <4.3
657+
"contact_groups": "groups", # Netbox version >=4.3
656658
"device_role": "role",
657659
"fhrp_group": "group",
658660
"inventory_item_role": "role",

plugins/modules/netbox_contact.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@
6767
type: str
6868
contact_group:
6969
description:
70-
- Group assignment for the contact
70+
- Group that the contact belongs to. Only available in Netbox version <4.3
7171
required: false
7272
type: raw
73+
contact_groups:
74+
description:
75+
- Groups that the contact belongs to. Only available in Netbox versions >=4.3
76+
required: false
77+
type: list
78+
elements: raw
7379
link:
7480
description:
7581
- URL associated with the contact
@@ -121,6 +127,9 @@
121127
title: Mr Contact
122128
phone: 123456789
123129
130+
contact_groups:
131+
- Group 1
132+
- Group 2
124133
tags:
125134
- tagA
126135
- tagB
@@ -169,6 +178,7 @@ def main():
169178
description=dict(required=False, type="str"),
170179
comments=dict(required=False, type="str"),
171180
contact_group=dict(required=False, type="raw"),
181+
contact_groups=dict(required=False, type="list", elements="raw"),
172182
link=dict(required=False, type="str"),
173183
tags=dict(required=False, type="list", elements="raw"),
174184
custom_fields=dict(required=False, type="dict"),

plugins/modules/netbox_virtual_machine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
"""
197197

198198
RETURN = r"""
199-
virtual machine:
199+
virtual_machine:
200200
description: Serialized object as created or already existent within NetBox
201201
returned: success (when I(state=present))
202202
type: dict

tests/integration/targets/v4.0/tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
- name: NETBOX_CONTACT TESTS
2424
ansible.builtin.include_tasks: netbox_contact.yml
2525

26+
- name: NETBOX_CONTACT_GROUP TESTS
27+
ansible.builtin.include_tasks:
28+
file: netbox_contact_group.yml
29+
apply:
30+
tags:
31+
- netbox_contact_group
32+
tags:
33+
- netbox_contact_group
34+
2635
- name: NETBOX_CONTACT_ROLE TESTS
2736
ansible.builtin.include_tasks: netbox_contact_role.yml
2837

tests/integration/targets/v4.0/tasks/netbox_contact.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,46 @@
9898
- test_five['contact']['phone'] == "12345678"
9999
- test_five['contact']['tags'] | length == 3
100100
- test_five['msg'] == "contact Contact ABC created"
101+
102+
- name: 6 Setup - Create contact group
103+
netbox.netbox.netbox_contact_group:
104+
netbox_url: http://localhost:32768
105+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
106+
data:
107+
name: Contact Group With Contacts
108+
register: test_group
109+
110+
- name: 6 - Create contact with contact group
111+
netbox.netbox.netbox_contact:
112+
netbox_url: http://localhost:32768
113+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
114+
data:
115+
name: Grouped Contact 1
116+
contact_group: Contact Group With Contacts
117+
register: test_six
118+
119+
- name: 6 - ASSERT
120+
ansible.builtin.assert:
121+
that:
122+
- test_six is changed
123+
- test_six['diff']['before']['state'] == "absent"
124+
- test_six['diff']['after']['state'] == "present"
125+
- test_six['contact']['name'] == "Grouped Contact 1"
126+
- test_six['contact']['group'] == test_group['contact_group']['id']
127+
- test_six['msg'] == "contact Grouped Contact 1 created"
128+
129+
- name: 7 - Try to create contact using contact_groups
130+
netbox.netbox.netbox_contact:
131+
netbox_url: http://localhost:32768
132+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
133+
data:
134+
name: Grouped Contact 2
135+
contact_groups:
136+
- Contact Group With Contacts
137+
register: test_seven
138+
ignore_errors: true
139+
140+
- name: 7 - ASSERT
141+
ansible.builtin.assert:
142+
that:
143+
- "'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']"
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
##
3+
##
4+
### NETBOX_CONTACT_GROUP
5+
##
6+
##
7+
- name: 1 - Test contact group creation
8+
netbox.netbox.netbox_contact_group:
9+
netbox_url: http://localhost:32768
10+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
11+
data:
12+
name: Contact Group 1
13+
register: test_one
14+
15+
- name: 1 - ASSERT
16+
ansible.builtin.assert:
17+
that:
18+
- test_one is changed
19+
- test_one['diff']['before']['state'] == "absent"
20+
- test_one['diff']['after']['state'] == "present"
21+
- test_one['contact_group']['name'] == "Contact Group 1"
22+
- test_one['msg'] == "contact_group Contact Group 1 created"
23+
24+
- name: Test duplicate contact group
25+
netbox.netbox.netbox_contact_group:
26+
netbox_url: http://localhost:32768
27+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
28+
data:
29+
name: Contact Group 1
30+
register: test_two
31+
32+
- name: 2 - ASSERT
33+
ansible.builtin.assert:
34+
that:
35+
- not test_two['changed']
36+
- test_two['contact_group']['name'] == "Contact Group 1"
37+
- test_two['msg'] == "contact_group Contact Group 1 already exists"
38+
39+
- name: 3 - Test update
40+
netbox.netbox.netbox_contact_group:
41+
netbox_url: http://localhost:32768
42+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
43+
data:
44+
name: Contact Group 1
45+
description: The first contact group
46+
register: test_three
47+
48+
- name: 3 - ASSERT
49+
ansible.builtin.assert:
50+
that:
51+
- test_three is changed
52+
- test_three['diff']['after']['description'] == "The first contact group"
53+
- test_three['contact_group']['name'] == "Contact Group 1"
54+
- test_three['contact_group']['description'] == "The first contact group"
55+
- test_three['msg'] == "contact_group Contact Group 1 updated"
56+
57+
- name: 4 - Test delete
58+
netbox.netbox.netbox_contact_group:
59+
netbox_url: http://localhost:32768
60+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
61+
data:
62+
name: Contact Group 1
63+
state: absent
64+
register: test_four
65+
66+
- name: 4 - ASSERT
67+
ansible.builtin.assert:
68+
that:
69+
- test_four is changed
70+
- test_four['diff']['before']['state'] == "present"
71+
- test_four['diff']['after']['state'] == "absent"
72+
- test_four['msg'] == "contact_group Contact Group 1 deleted"
73+
74+
- name: 5 - Create contact group with all parameters
75+
netbox.netbox.netbox_contact_group:
76+
netbox_url: http://localhost:32768
77+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
78+
data:
79+
name: Contact Group Parent
80+
slug: the-parent-contact-group
81+
description: The parent contact group
82+
tags:
83+
- tagA
84+
- tagB
85+
- tagC
86+
state: present
87+
register: test_five
88+
89+
- name: 5 - ASSERT
90+
ansible.builtin.assert:
91+
that:
92+
- test_five is changed
93+
- test_five['diff']['before']['state'] == "absent"
94+
- test_five['diff']['after']['state'] == "present"
95+
- test_five['contact_group']['name'] == "Contact Group Parent"
96+
- test_five['contact_group']['slug'] == "the-parent-contact-group"
97+
- test_five['contact_group']['description'] == "The parent contact group"
98+
- test_five['contact_group']['tags'] | length == 3
99+
- test_five['msg'] == "contact_group Contact Group Parent created"
100+
101+
- name: 6 - Create contact group with parent contact group
102+
netbox.netbox.netbox_contact_group:
103+
netbox_url: http://localhost:32768
104+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
105+
data:
106+
name: Contact Group Child
107+
parent_contact_group: Contact Group Parent
108+
state: present
109+
register: test_six
110+
111+
- name: 6 - ASSERT
112+
ansible.builtin.assert:
113+
that:
114+
- test_six is changed
115+
- test_six['diff']['before']['state'] == "absent"
116+
- test_six['diff']['after']['state'] == "present"
117+
- test_six['contact_group']['name'] == "Contact Group Child"
118+
- test_six['contact_group']['parent'] == test_five['contact_group']['id']
119+
- test_six['msg'] == "contact_group Contact Group Child created"

tests/integration/targets/v4.1/tasks/main.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
- name: NETBOX_CONTACT TESTS
2424
ansible.builtin.include_tasks: netbox_contact.yml
2525

26+
- name: NETBOX_CONTACT_GROUP TESTS
27+
ansible.builtin.include_tasks:
28+
file: netbox_contact_group.yml
29+
apply:
30+
tags:
31+
- netbox_contact_group
32+
tags:
33+
- netbox_contact_group
34+
2635
- name: NETBOX_CONTACT_ROLE TESTS
2736
ansible.builtin.include_tasks: netbox_contact_role.yml
2837

tests/integration/targets/v4.1/tasks/netbox_contact.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,46 @@
9898
- test_five['contact']['phone'] == "12345678"
9999
- test_five['contact']['tags'] | length == 3
100100
- test_five['msg'] == "contact Contact ABC created"
101+
102+
- name: 6 Setup - Create contact group
103+
netbox.netbox.netbox_contact_group:
104+
netbox_url: http://localhost:32768
105+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
106+
data:
107+
name: Contact Group With Contacts
108+
register: test_group
109+
110+
- name: 6 - Create contact with contact group
111+
netbox.netbox.netbox_contact:
112+
netbox_url: http://localhost:32768
113+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
114+
data:
115+
name: Grouped Contact 1
116+
contact_group: Contact Group With Contacts
117+
register: test_six
118+
119+
- name: 6 - ASSERT
120+
ansible.builtin.assert:
121+
that:
122+
- test_six is changed
123+
- test_six['diff']['before']['state'] == "absent"
124+
- test_six['diff']['after']['state'] == "present"
125+
- test_six['contact']['name'] == "Grouped Contact 1"
126+
- test_six['contact']['group'] == test_group['contact_group']['id']
127+
- test_six['msg'] == "contact Grouped Contact 1 created"
128+
129+
- name: 7 - Try to create contact using contact_groups
130+
netbox.netbox.netbox_contact:
131+
netbox_url: http://localhost:32768
132+
netbox_token: "0123456789abcdef0123456789abcdef01234567"
133+
data:
134+
name: Grouped Contact 2
135+
contact_groups:
136+
- Contact Group With Contacts
137+
register: test_seven
138+
ignore_errors: true
139+
140+
- name: 7 - ASSERT
141+
ansible.builtin.assert:
142+
that:
143+
- "'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']"

0 commit comments

Comments
 (0)