Skip to content

Commit dcd8672

Browse files
authored
New module: module_type (#887)
1 parent 56e1dbe commit dcd8672

File tree

6 files changed

+272
-1
lines changed

6 files changed

+272
-1
lines changed

meta/runtime.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ action_groups:
4545
- netbox_l2vpn
4646
- netbox_location
4747
- netbox_manufacturer
48+
- netbox_module_type
4849
- netbox_platform
4950
- netbox_power_feed
5051
- netbox_power_outlet

plugins/module_utils/netbox_dcim.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
NB_INVENTORY_ITEM_ROLES = "inventory_item_roles"
3232
NB_LOCATIONS = "locations"
3333
NB_MANUFACTURERS = "manufacturers"
34+
NB_MODULE_TYPES = "module_types"
3435
NB_PLATFORMS = "platforms"
3536
NB_POWER_FEEDS = "power_feeds"
3637
NB_POWER_OUTLETS = "power_outlets"

plugins/module_utils/netbox_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"inventory_item_roles",
5959
"locations",
6060
"manufacturers",
61+
"module_types",
6162
"platforms",
6263
"power_feeds",
6364
"power_outlets",
@@ -133,6 +134,7 @@
133134
l2vpn="name",
134135
location="slug",
135136
manufacturer="slug",
137+
module_type="model",
136138
nat_inside="address",
137139
nat_outside="address",
138140
parent_contact_group="name",
@@ -315,6 +317,7 @@
315317
"l2vpns": "l2vpn",
316318
"locations": "location",
317319
"manufacturers": "manufacturer",
320+
"module_types": "module_type",
318321
"platforms": "platform",
319322
"power_feeds": "power_feed",
320323
"power_outlets": "power_outlet",
@@ -419,6 +422,7 @@
419422
"l2vpn": set(["name"]),
420423
"lag": set(["name"]),
421424
"location": set(["slug"]),
425+
"module_type": set(["model"]),
422426
"manufacturer": set(["slug"]),
423427
"master": set(["name"]),
424428
"nat_inside": set(["vrf", "address"]),

plugins/modules/netbox_module_type.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2022, Martin Rødvand (@rodvand) <martin@rodvand.net>
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+
DOCUMENTATION = r"""
11+
---
12+
module: netbox_module_type
13+
short_description: Create, update or delete module types within NetBox
14+
description:
15+
- Creates, updates or removes module types from NetBox
16+
notes:
17+
- Tags should be defined as a YAML list
18+
- This should be ran with connection C(local) and hosts C(localhost)
19+
author:
20+
- Martin Rødvand (@rodvand)
21+
requirements:
22+
- pynetbox
23+
version_added: '3.10.0'
24+
extends_documentation_fragment:
25+
- netbox.netbox.common
26+
options:
27+
data:
28+
description:
29+
- Defines the device type configuration
30+
suboptions:
31+
manufacturer:
32+
description:
33+
- The manufacturer of the module type
34+
required: false
35+
type: raw
36+
model:
37+
description:
38+
- The model of the module type
39+
required: true
40+
type: raw
41+
part_number:
42+
description:
43+
- The part number of the module type
44+
required: false
45+
type: str
46+
comments:
47+
description:
48+
- Comments that may include additional information in regards to the module type
49+
required: false
50+
type: str
51+
tags:
52+
description:
53+
- Any tags that the module type may need to be associated with
54+
required: false
55+
type: list
56+
elements: raw
57+
custom_fields:
58+
description:
59+
- must exist in NetBox
60+
required: false
61+
type: dict
62+
required: true
63+
type: dict
64+
"""
65+
66+
EXAMPLES = r"""
67+
- name: "Test NetBox modules"
68+
connection: local
69+
hosts: localhost
70+
gather_facts: False
71+
72+
tasks:
73+
- name: Create module type within NetBox with only required information
74+
netbox.netbox.netbox_module_type:
75+
netbox_url: http://netbox.local
76+
netbox_token: thisIsMyToken
77+
data:
78+
model: ws-test-3750
79+
manufacturer: Test Manufacturer
80+
state: present
81+
82+
- name: Create module type within NetBox
83+
netbox.netbox.netbox_module_type:
84+
netbox_url: http://netbox.local
85+
netbox_token: thisIsMyToken
86+
data:
87+
model: ws-test-3750
88+
manufacturer: Test Manufacturer
89+
part_number: ws-3750g-v2
90+
state: present
91+
92+
- name: Delete module type within netbox
93+
netbox.netbox.netbox_module_type:
94+
netbox_url: http://netbox.local
95+
netbox_token: thisIsMyToken
96+
data:
97+
model: ws-test-3750
98+
state: absent
99+
"""
100+
101+
RETURN = r"""
102+
module_type:
103+
description: Serialized object as created or already existent within NetBox
104+
returned: success (when I(state=present))
105+
type: dict
106+
msg:
107+
description: Message indicating failure or info about what has been achieved
108+
returned: always
109+
type: str
110+
"""
111+
112+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
113+
NetboxAnsibleModule,
114+
NETBOX_ARG_SPEC,
115+
)
116+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_dcim import (
117+
NetboxDcimModule,
118+
NB_MODULE_TYPES,
119+
)
120+
from copy import deepcopy
121+
122+
123+
def main():
124+
"""
125+
Main entry point for module execution
126+
"""
127+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
128+
argument_spec.update(
129+
dict(
130+
data=dict(
131+
type="dict",
132+
required=True,
133+
options=dict(
134+
manufacturer=dict(required=False, type="raw"),
135+
model=dict(required=True, type="raw"),
136+
part_number=dict(required=False, type="str"),
137+
comments=dict(required=False, type="str"),
138+
tags=dict(required=False, type="list", elements="raw"),
139+
custom_fields=dict(required=False, type="dict"),
140+
),
141+
),
142+
)
143+
)
144+
145+
required_if = [
146+
("state", "present", ["model", "manufacturer"]),
147+
("state", "absent", ["model"]),
148+
]
149+
150+
module = NetboxAnsibleModule(
151+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
152+
)
153+
154+
netbox_device_type = NetboxDcimModule(module, NB_MODULE_TYPES)
155+
netbox_device_type.run()
156+
157+
158+
if __name__ == "__main__": # pragma: no cover
159+
main()

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,13 @@
254254
tags:
255255
- netbox_inventory_item_role
256256
tags:
257-
- netbox_inventory_item_role
257+
- netbox_inventory_item_role
258+
259+
- name: "NETBOX_MODULE_TYPE TESTS"
260+
include_tasks:
261+
file: "netbox_module_type.yml"
262+
apply:
263+
tags:
264+
- netbox_module_type
265+
tags:
266+
- netbox_module_type
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
##
3+
##
4+
### NETBOX_MODULE_TYPE
5+
##
6+
##
7+
- name: "MODULE_TYPE 1: Necessary info creation"
8+
netbox.netbox.netbox_module_type:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
model: ws-test-3750
13+
manufacturer: Test Manufacturer
14+
state: present
15+
register: test_one
16+
17+
- name: "MODULE_TYPE 1: ASSERT - Necessary info creation"
18+
assert:
19+
that:
20+
- test_one is changed
21+
- test_one['diff']['before']['state'] == "absent"
22+
- test_one['diff']['after']['state'] == "present"
23+
- test_one['module_type']['model'] == "ws-test-3750"
24+
- test_one['module_type']['manufacturer'] == 3
25+
- test_one['msg'] == "module_type ws-test-3750 created"
26+
27+
- name: "MODULE_TYPE 2: Create duplicate"
28+
netbox.netbox.netbox_module_type:
29+
netbox_url: http://localhost:32768
30+
netbox_token: 0123456789abcdef0123456789abcdef01234567
31+
data:
32+
model: "ws-test-3750"
33+
manufacturer: Test Manufacturer
34+
state: present
35+
register: test_two
36+
37+
- name: "MODULE_TYPE 2: ASSERT - Create duplicate"
38+
assert:
39+
that:
40+
- not test_two['changed']
41+
- test_one['module_type']['model'] == "ws-test-3750"
42+
- test_one['module_type']['manufacturer'] == 3
43+
- test_two['msg'] == "module_type ws-test-3750 already exists"
44+
45+
- name: "MODULE_TYPE 3: ASSERT - Update"
46+
netbox.netbox.netbox_module_type:
47+
netbox_url: http://localhost:32768
48+
netbox_token: 0123456789abcdef0123456789abcdef01234567
49+
data:
50+
model: ws-test-3750
51+
manufacturer: Test Manufacturer
52+
part_number: ws-3750g-v2
53+
state: present
54+
register: test_three
55+
56+
- name: "MODULE_TYPE 3: ASSERT - Update"
57+
assert:
58+
that:
59+
- test_three is changed
60+
- test_three['diff']['after']['part_number'] == "ws-3750g-v2"
61+
- test_three['module_type']['model'] == "ws-test-3750"
62+
- test_three['module_type']['manufacturer'] == 3
63+
- test_three['module_type']['part_number'] == "ws-3750g-v2"
64+
- test_three['msg'] == "module_type ws-test-3750 updated"
65+
66+
- name: "MODULE_TYPE 4: ASSERT - Delete"
67+
netbox.netbox.netbox_module_type:
68+
netbox_url: http://localhost:32768
69+
netbox_token: 0123456789abcdef0123456789abcdef01234567
70+
data:
71+
model: ws-test-3750
72+
state: absent
73+
register: test_four
74+
75+
- name: "MODULE_TYPE 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'] == "module_type ws-test-3750 deleted"
82+
83+
- name: "MODULE_TYPE 5: ASSERT - Delete non existing"
84+
netbox.netbox.netbox_module_type:
85+
netbox_url: http://localhost:32768
86+
netbox_token: 0123456789abcdef0123456789abcdef01234567
87+
data:
88+
model: "Test Module Type"
89+
state: absent
90+
register: test_five
91+
92+
- name: "MODULE_TYPE 5: ASSERT - Delete non existing`"
93+
assert:
94+
that:
95+
- not test_five['changed']
96+
- test_five['module_type'] == None
97+
- test_five['msg'] == "module_type Test Module Type already absent"

0 commit comments

Comments
 (0)