Skip to content

Commit 5116178

Browse files
authored
New Module: netbox_site_group (#547)
1 parent 8546bbe commit 5116178

File tree

7 files changed

+355
-0
lines changed

7 files changed

+355
-0
lines changed

plugins/module_utils/netbox_dcim.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
NB_REAR_PORT_TEMPLATES = "rear_port_templates"
4444
NB_REGIONS = "regions"
4545
NB_SITES = "sites"
46+
NB_SITE_GROUPS = "site_groups"
4647
NB_VIRTUAL_CHASSIS = "virtual_chassis"
4748

4849

@@ -79,6 +80,7 @@ def run(self):
7980
- power_ports
8081
- power_port_templates
8182
- sites
83+
- site_groups
8284
- racks
8385
- rack_roles
8486
- rack_groups

plugins/module_utils/netbox_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
nat_inside="address",
110110
nat_outside="address",
111111
parent_region="slug",
112+
parent_site_group="slug",
112113
parent_tenant_group="slug",
113114
power_panel="name",
114115
power_port="name",
@@ -184,6 +185,7 @@
184185
"parent_interface": "interfaces",
185186
"parent_vm_interface": "interfaces",
186187
"parent_region": "regions",
188+
"parent_site_group": "site_groups",
187189
"parent_tenant_group": "tenant_groups",
188190
"platforms": "platforms",
189191
"power_panel": "power_panels",
@@ -345,6 +347,7 @@
345347
"parent_interface": set(["name"]),
346348
"parent_vm_interface": set(["name"]),
347349
"parent_region": set(["slug"]),
350+
"parent_site_group": set(["slug"]),
348351
"parent_tenant_group": set(["slug"]),
349352
"platform": set(["slug"]),
350353
"power_feed": set(["name", "power_panel"]),
@@ -440,6 +443,7 @@
440443
"parent_interface": "parent",
441444
"parent_vm_interface": "parent",
442445
"parent_region": "parent",
446+
"parent_site_group": "parent",
443447
"parent_tenant_group": "parent",
444448
"power_port_template": "power_port",
445449
"prefix_role": "role",

plugins/modules/netbox_site_group.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2021, Andrew Simmons (@andybro19) <andrewkylesimmons@gmail.com>
4+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
6+
from __future__ import absolute_import, division, print_function
7+
8+
__metaclass__ = type
9+
10+
ANSIBLE_METADATA = {
11+
"metadata_version": "1.1",
12+
"status": ["preview"],
13+
"supported_by": "community",
14+
}
15+
16+
DOCUMENTATION = r"""
17+
---
18+
module: netbox_site_group
19+
short_description: Create, update, or delete site groups within NetBox
20+
description:
21+
- Creates, updates, or deletes site groups within NetBox
22+
notes:
23+
- Tags should be defined as a YAML list
24+
- This should be ran with connection C(local) and hosts C(localhost)
25+
author:
26+
- Andrew Simmons (@andybro19)
27+
requirements:
28+
- pynetbox
29+
version_added: '4.0.0'
30+
options:
31+
netbox_url:
32+
description:
33+
- URL of the NetBox instance resolvable by Ansible control host
34+
required: true
35+
type: str
36+
netbox_token:
37+
description:
38+
- The token created within NetBox to authorize API access
39+
required: true
40+
type: str
41+
cert:
42+
description:
43+
- Certificate path
44+
required: false
45+
type: raw
46+
data:
47+
type: dict
48+
description:
49+
- Defines the site group configuration
50+
suboptions:
51+
name:
52+
description:
53+
- Name of the site group to be created
54+
required: true
55+
type: str
56+
slug:
57+
description:
58+
- The slugified version of the name or custom slug.
59+
- This is auto-generated following NetBox rules if not provided
60+
required: false
61+
type: str
62+
parent_site_group:
63+
description:
64+
- The parent site group the site group will be associated with
65+
required: false
66+
type: raw
67+
description:
68+
description:
69+
- The description of the site group
70+
required: false
71+
type: str
72+
required: true
73+
state:
74+
description:
75+
- Use C(present) or C(absent) for adding or removing.
76+
choices: [ absent, present ]
77+
default: present
78+
type: str
79+
query_params:
80+
description:
81+
- This can be used to override the specified values in ALLOWED_QUERY_PARAMS that is defined
82+
- in plugins/module_utils/netbox_utils.py and provides control to users on what may make
83+
- an object unique in their environment.
84+
required: false
85+
type: list
86+
elements: str
87+
validate_certs:
88+
description:
89+
- |
90+
If C(no), SSL certificates will not be validated.
91+
This should only be used on personally controlled sites using self-signed certificates.
92+
default: true
93+
type: raw
94+
"""
95+
96+
EXAMPLES = r"""
97+
- name: "Test NetBox site group module"
98+
connection: local
99+
hosts: localhost
100+
gather_facts: False
101+
tasks:
102+
- name: Create site group within NetBox with only required information
103+
netbox.netbox.netbox_site_group:
104+
netbox_url: http://netbox.local
105+
netbox_token: thisIsMyToken
106+
data:
107+
name: Site group
108+
state: present
109+
110+
- name: Create site group within NetBox with a parent site group
111+
netbox.netbox.netbox_site_group:
112+
netbox_url: http://netbox.local
113+
netbox_token: thisIsMyToken
114+
data:
115+
name: Child site group
116+
parent_site_group: Site group
117+
state: present
118+
119+
- name: Delete site group within NetBox
120+
netbox.netbox.netbox_site_group:
121+
netbox_url: http://netbox.local
122+
netbox_token: thisIsMyToken
123+
data:
124+
name: Site group
125+
state: absent
126+
"""
127+
128+
RETURN = r"""
129+
site_group:
130+
description: Serialized object as created or already existent within NetBox
131+
returned: success (when I(state=present))
132+
type: dict
133+
msg:
134+
description: Message indicating failure or info about what has been achieved
135+
returned: always
136+
type: str
137+
"""
138+
139+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
140+
NetboxAnsibleModule,
141+
NETBOX_ARG_SPEC,
142+
)
143+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_dcim import (
144+
NetboxDcimModule,
145+
NB_SITE_GROUPS,
146+
)
147+
from copy import deepcopy
148+
149+
150+
def main():
151+
"""
152+
Main entry point for module execution
153+
"""
154+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
155+
argument_spec.update(
156+
dict(
157+
data=dict(
158+
type="dict",
159+
required=True,
160+
options=dict(
161+
name=dict(required=True, type="str"),
162+
slug=dict(required=False, type="str"),
163+
parent_site_group=dict(required=False, type="raw"),
164+
description=dict(required=False, type="str"),
165+
),
166+
),
167+
)
168+
)
169+
170+
required_if = [("state", "present", ["name"]), ("state", "absent", ["name"])]
171+
172+
module = NetboxAnsibleModule(
173+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
174+
)
175+
176+
netbox_site_group = NetboxDcimModule(module, NB_SITE_GROUPS)
177+
netbox_site_group.run()
178+
179+
180+
if __name__ == "__main__": # pragma: no cover
181+
main()

tests/integration/targets/v2.11/tasks/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
- name: "NETBOX_SITE TESTS"
1818
include_tasks: "netbox_site.yml"
1919

20+
- name: "NETBOX_SITE_GROUP TESTS"
21+
include_tasks: "netbox_site_group.yml"
22+
2023
- name: "NETBOX_TENTANT TESTS"
2124
include_tasks: "netbox_tenant.yml"
2225

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
##
3+
##
4+
### NETBOX_SITE_GROUP
5+
##
6+
##
7+
- name: "SITE_GROUP 1: Necessary info creation"
8+
netbox.netbox.netbox_site_group:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
name: Site Group
13+
state: present
14+
register: test_one
15+
16+
- name: "SITE_GROUP 1: ASSERT - Necessary info creation"
17+
assert:
18+
that:
19+
- test_one is changed
20+
- test_one['diff']['before']['state'] == "absent"
21+
- test_one['diff']['after']['state'] == "present"
22+
- test_one['site_group']['name'] == "Site Group"
23+
- test_one['site_group']['slug'] == "site-group"
24+
- test_one['msg'] == "site_group Site Group created"
25+
26+
- name: "SITE_GROUP 2: Create duplicate"
27+
netbox.netbox.netbox_site_group:
28+
netbox_url: http://localhost:32768
29+
netbox_token: 0123456789abcdef0123456789abcdef01234567
30+
data:
31+
name: Site Group
32+
state: present
33+
register: test_two
34+
35+
- name: "SITE_GROUP 2: ASSERT - Create duplicate"
36+
assert:
37+
that:
38+
- not test_two['changed']
39+
- test_two['site_group']['name'] == "Site Group"
40+
- test_two['site_group']['slug'] == "site-group"
41+
- test_two['msg'] == "site_group Site Group already exists"
42+
43+
- name: "SITE_GROUP 3: Update"
44+
netbox.netbox.netbox_site_group:
45+
netbox_url: http://localhost:32768
46+
netbox_token: 0123456789abcdef0123456789abcdef01234567
47+
data:
48+
name: Site Group
49+
parent_site_group: Test Site Group
50+
description: This is a site group
51+
state: present
52+
register: test_three
53+
54+
- name: "SITE_GROUP 3: ASSERT - Update"
55+
assert:
56+
that:
57+
- test_three is changed
58+
- test_three['diff']['after']['parent'] == 1
59+
- test_three['diff']['after']['description'] == "This is a site group"
60+
- test_three['site_group']['name'] == "Site Group"
61+
- test_three['site_group']['slug'] == "site-group"
62+
- test_three['site_group']['parent'] == 1
63+
- test_three['site_group']['description'] == "This is a site group"
64+
- test_three['msg'] == "site_group Site Group updated"
65+
66+
- name: "SITE_GROUP 4: Delete"
67+
netbox.netbox.netbox_site_group:
68+
netbox_url: http://localhost:32768
69+
netbox_token: 0123456789abcdef0123456789abcdef01234567
70+
data:
71+
name: Site Group
72+
state: absent
73+
register: test_four
74+
75+
- name: "SITE_GROUP 4: ASSERT - Delete"
76+
assert:
77+
that:
78+
- test_four is changed
79+
- test_four['diff']['before']['state'] == "present"
80+
- test_four['diff']['after']['state'] == "absent"
81+
- test_four['msg'] == "site_group Site Group deleted"

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
- name: "NETBOX_SITE TESTS"
1818
include_tasks: "netbox_site.yml"
1919

20+
- name: "NETBOX_SITE_GROUP TESTS"
21+
include_tasks: "netbox_site_group.yml"
22+
2023
- name: "NETBOX_TENTANT TESTS"
2124
include_tasks: "netbox_tenant.yml"
2225

0 commit comments

Comments
 (0)