Skip to content

Commit 445664b

Browse files
Adding netbox_region, netbox_device_bay, and netbox_inventory_item (#20)
1 parent b99915c commit 445664b

File tree

9 files changed

+921
-28
lines changed

9 files changed

+921
-28
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
## Existing Modules
2727

2828
- netbox_aggregate
29-
- netbox_device
29+
- netbox_device_bay
3030
- netbox_device_interface
3131
- netbox_device_role
3232
- netbox_device_type
33+
- netbox_device
34+
- netbox_inventory_item
3335
- netbox_ip_address
3436
- netbox_ipam_role
3537
- netbox_manufacturer
@@ -38,10 +40,11 @@
3840
- netbox_rack_group
3941
- netbox_rack_role
4042
- netbox_rack
43+
- netbox_region
4144
- netbox_rir
4245
- netbox_site
43-
- netbox_tenant
4446
- netbox_tenant_group
47+
- netbox_tenant
4548
- netbox_vlan_group
4649
- netbox_vlan
4750
- netbox_vrf

plugins/module_utils/netbox_dcim.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@
2222
from netbox_utils import NetboxModule, ENDPOINT_NAME_MAPPING, SLUG_REQUIRED
2323

2424

25+
NB_DEVICE_BAYS = "device_bays"
2526
NB_DEVICES = "devices"
2627
NB_DEVICE_ROLES = "device_roles"
2728
NB_DEVICE_TYPES = "device_types"
2829
NB_INTERFACES = "interfaces"
30+
NB_INVENTORY_ITEMS = "inventory_items"
2931
NB_MANUFACTURERS = "manufacturers"
3032
NB_PLATFORMS = "platforms"
3133
NB_RACKS = "racks"
3234
NB_RACK_ROLES = "rack_roles"
3335
NB_RACK_GROUPS = "rack_groups"
36+
NB_REGIONS = "regions"
3437
NB_SITES = "sites"
3538

3639

@@ -43,16 +46,19 @@ def run(self):
4346
This function should have all necessary code for endpoints within the application
4447
to create/update/delete the endpoint objects
4548
Supported endpoints:
49+
- device_bays
4650
- devices
4751
- device_roles
4852
- device_types
4953
- interfaces
54+
- inventory_items
5055
- manufacturers
5156
- platforms
5257
- sites
5358
- racks
5459
- rack_roles
5560
- rack_groups
61+
- regions
5662
"""
5763
# Used to dynamically set key when returning results
5864
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]

plugins/module_utils/netbox_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
API_APPS_ENDPOINTS = dict(
2525
circuits=[],
2626
dcim=[
27+
"device_bays",
2728
"devices",
2829
"device_roles",
2930
"device_types",
3031
"interfaces",
32+
"inventory_items",
3133
"manufacturers",
3234
"platforms",
3335
"racks",
@@ -60,9 +62,11 @@
6062
device_role="slug",
6163
device_type="slug",
6264
group="slug",
65+
installed_device="name",
6366
manufacturer="slug",
6467
nat_inside="address",
6568
nat_outside="address",
69+
parent_region="slug",
6670
platform="slug",
6771
prefix_role="slug",
6872
primary_ip="address",
@@ -92,13 +96,15 @@
9296
device_role="device_roles",
9397
device_type="device_types",
9498
group="tenant_groups",
99+
installed_device="devices",
95100
interface="interfaces",
96101
ip_addresses="ip_addresses",
97102
lag="interfaces",
98103
manufacturer="manufacturers",
99104
nat_inside="ip_addresses",
100105
nat_outside="ip_addresses",
101106
platform="platforms",
107+
parent_region="regions",
102108
prefix_role="roles",
103109
primary_ip="ip_addresses",
104110
primary_ip4="ip_addresses",
@@ -123,17 +129,20 @@
123129

124130
ENDPOINT_NAME_MAPPING = {
125131
"aggregates": "aggregate",
132+
"device_bays": "device_bay",
126133
"devices": "device",
127134
"device_roles": "device_role",
128135
"device_types": "device_type",
129136
"interfaces": "interface",
137+
"inventory_items": "inventory_item",
130138
"ip_addresses": "ip_address",
131139
"manufacturers": "manufacturer",
132140
"platforms": "platform",
133141
"prefixes": "prefix",
134142
"racks": "rack",
135143
"rack_groups": "rack_group",
136144
"rack_roles": "rack_role",
145+
"regions": "region",
137146
"rirs": "rir",
138147
"roles": "role",
139148
"services": "services",
@@ -245,20 +254,25 @@
245254

246255
ALLOWED_QUERY_PARAMS = {
247256
"aggregate": set(["prefix", "rir"]),
257+
"device_bay": set(["name", "device"]),
248258
"device": set(["name"]),
249259
"device_role": set(["slug"]),
250260
"device_type": set(["slug"]),
261+
"installed_device": set(["name"]),
251262
"interface": set(["name", "device"]),
263+
"inventory_item": set(["name", "device"]),
252264
"ip_address": set(["address", "vrf"]),
253265
"ip_addresses": set(["address", "vrf", "device"]),
254266
"lag": set(["name"]),
255267
"manufacturer": set(["name", "slug"]),
256268
"nat_inside": set(["vrf", "address"]),
269+
"parent_region": set(["slug"]),
257270
"platform": set(["slug"]),
258271
"prefix": set(["prefix", "vrf"]),
259272
"rack": set(["name", "site"]),
260273
"rack_group": set(["slug"]),
261274
"rack_role": set(["slug"]),
275+
"region": set(["slug"]),
262276
"rir": set(["slug"]),
263277
"role": set(["slug"]),
264278
"services": set(["device", "virtual_machine", "name"]),
@@ -291,6 +305,7 @@
291305

292306
# This is used to map non-clashing keys to Netbox API compliant keys to prevent bad logic in code for similar keys but different modules
293307
CONVERT_KEYS = {
308+
"parent_region": "parent",
294309
"prefix_role": "role",
295310
"rack_group": "group",
296311
"rack_role": "role",
@@ -305,6 +320,7 @@
305320
"ipam_roles",
306321
"rack_groups",
307322
"rack_roles",
323+
"regions",
308324
"rirs",
309325
"roles",
310326
"manufacturers",

plugins/modules/netbox_device_bay.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2019, Mikhail Yohman (@FragmentedPacket) <mikhail.yohman@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_device_bay
19+
short_description: Create, update or delete device bays within Netbox
20+
description:
21+
- Creates, updates or removes device bays from 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+
- Mikhail Yohman (@FragmentedPacket)
27+
requirements:
28+
- pynetbox
29+
version_added: '0.1.0'
30+
options:
31+
netbox_url:
32+
description:
33+
- URL of the Netbox instance resolvable by Ansible control host
34+
required: true
35+
netbox_token:
36+
description:
37+
- The token created within Netbox to authorize API access
38+
required: true
39+
data:
40+
description:
41+
- Defines the device bay configuration
42+
suboptions:
43+
device:
44+
description:
45+
- The device the device bay will be associated to. The device type must be "parent".
46+
required: true
47+
name:
48+
description:
49+
- The name of the device bay
50+
required: true
51+
description:
52+
description:
53+
- The description of the device bay. This is supported on v2.6+ of Netbox
54+
installed_device:
55+
description:
56+
- The ddevice that will be installed into the bay. The device type must be "child".
57+
tags:
58+
description:
59+
- Any tags that the device bay may need to be associated with
60+
required: true
61+
state:
62+
description:
63+
- Use C(present) or C(absent) for adding or removing.
64+
choices: [ absent, present ]
65+
default: present
66+
validate_certs:
67+
description:
68+
- If C(no), SSL certificates will not be validated. This should only be used on personally controlled sites using self-signed certificates.
69+
default: 'yes'
70+
type: bool
71+
"""
72+
73+
EXAMPLES = r"""
74+
- name: "Test Netbox modules"
75+
connection: local
76+
hosts: localhost
77+
gather_facts: False
78+
79+
tasks:
80+
- name: Create device bay within Netbox with only required information
81+
netbox_device_bay:
82+
netbox_url: http://netbox.local
83+
netbox_token: thisIsMyToken
84+
data:
85+
device: Test Nexus One
86+
name: "Device Bay One"
87+
state: present
88+
89+
- name: Add device into device bay
90+
netbox_device_bay:
91+
netbox_url: http://netbox.local
92+
netbox_token: thisIsMyToken
93+
data:
94+
device: Test Nexus One
95+
name: "Device Bay One"
96+
description: "First child"
97+
installed_device: Test Nexus Child One
98+
state: absent
99+
100+
- name: Delete device bay within netbox
101+
netbox_device_bay:
102+
netbox_url: http://netbox.local
103+
netbox_token: thisIsMyToken
104+
data:
105+
name: Device Bay One
106+
state: absent
107+
108+
"""
109+
110+
RETURN = r"""
111+
device_bay:
112+
description: Serialized object as created or already existent within Netbox
113+
returned: success (when I(state=present))
114+
type: dict
115+
msg:
116+
description: Message indicating failure or info about what has been achieved
117+
returned: always
118+
type: str
119+
"""
120+
121+
from ansible.module_utils.basic import AnsibleModule
122+
from ansible_collections.fragmentedpacket.netbox_modules.plugins.module_utils.netbox_dcim import (
123+
NetboxDcimModule,
124+
NB_DEVICE_BAYS,
125+
)
126+
127+
128+
def main():
129+
"""
130+
Main entry point for module execution
131+
"""
132+
argument_spec = dict(
133+
netbox_url=dict(type="str", required=True),
134+
netbox_token=dict(type="str", required=True, no_log=True),
135+
data=dict(type="dict", required=True),
136+
state=dict(required=False, default="present", choices=["present", "absent"]),
137+
validate_certs=dict(type="bool", default=True),
138+
)
139+
140+
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
141+
142+
# Fail if device_bay name is not given
143+
if not module.params["data"].get("name"):
144+
module.fail_json(msg="missing name")
145+
146+
netbox_device_bay = NetboxDcimModule(module, NB_DEVICE_BAYS)
147+
netbox_device_bay.run()
148+
149+
150+
if __name__ == "__main__":
151+
main()

0 commit comments

Comments
 (0)