Skip to content

Commit 84b2488

Browse files
Add all virtualization modules (#36)
1 parent b04768a commit 84b2488

12 files changed

+1471
-28
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ matrix:
2626
- docker-compose up -d
2727
- cd ..
2828
- pip install -U pip
29-
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.3b0
29+
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0
3030
- pip install pynetbox==4.0.6 cryptography codecov jmespath ansible==$ANSIBLE_VER
3131

3232
- name: "Python 3.6 - Netbox Latest - Ansible Devel"
@@ -41,7 +41,7 @@ matrix:
4141
- docker-compose up -d
4242
- cd ..
4343
- pip install -U pip
44-
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.3b0
44+
- pip install pytest==4.6.5 pytest-mock pytest-xdist jinja2 PyYAML black==19.10b0
4545
- pip install pynetbox==4.0.6 cryptography jmespath
4646
# This is due to ansible-test only being available within devel branch
4747
- git clone https://github.com/ansible/ansible.git

README.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
[![Build Status](https://travis-ci.org/FragmentedPacket/netbox_modules.svg?branch=master)](https://travis-ci.org/FragmentedPacket/netbox_modules)
22
# Netbox modules for Ansible using Ansible Collections
33

4-
**THIS IS A WIP DUE TO COLLECTIONS BEING IN TECH REVIEW CURRENTLY (Ansible 2.8) BUT WILL BE UPDATED AS NECESSARY AS COLLECTIONS MATURES**
5-
6-
## Todo
7-
8-
- ~~initial build and changes to properly import module_utils~~
9-
- Add testing to Travis CI
10-
- ~~Initial unit tests~~
11-
- ~~Test different Python versions (3.6/3.7)~~
12-
- ~~Integration testing against at least two versions of Netbox~~
13-
- Local testing using Tox
14-
- ~~Test currently supported versions of Ansible~~
15-
- ~~Add documentation on how to use to README~~
16-
- ~~Add documentation on how to contribute to collection~~
17-
184
## Requirements
195

206
- Netbox 2.5+
@@ -26,6 +12,9 @@
2612
## Existing Modules
2713

2814
- netbox_aggregate
15+
- netbox_cluster
16+
- netbox_cluster_group
17+
- netbox_cluster_type
2918
- netbox_device_bay
3019
- netbox_device_interface
3120
- netbox_device_role
@@ -45,6 +34,8 @@
4534
- netbox_site
4635
- netbox_tenant_group
4736
- netbox_tenant
37+
- netbox_virtual_machine
38+
- netbox_vm_interface
4839
- netbox_vlan_group
4940
- netbox_vlan
5041
- netbox_vrf

plugins/module_utils/netbox_utils.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@
5656
],
5757
secrets=[],
5858
tenancy=["tenants", "tenant_groups"],
59-
virtualization=["clusters"],
59+
virtualization=["cluster_groups", "cluster_types", "clusters", "virtual_machines"],
6060
)
6161

6262
# Used to normalize data for the respective query types used to find endpoints
6363
QUERY_TYPES = dict(
6464
cluster="name",
65+
cluster_group="slug",
66+
cluster_type="slug",
6567
device="name",
6668
device_role="slug",
6769
device_type="slug",
@@ -96,6 +98,8 @@
9698
# Specifies keys within data that need to be converted to ID and the endpoint to be used when queried
9799
CONVERT_TO_ID = dict(
98100
cluster="clusters",
101+
cluster_group="cluster_groups",
102+
cluster_type="cluster_types",
99103
device="devices",
100104
device_role="device_roles",
101105
device_type="device_types",
@@ -125,6 +129,7 @@
125129
tenant_group="tenant_groups",
126130
untagged_vlan="vlans",
127131
virtual_machine="virtual_machines",
132+
virtual_machine_role="device_roles",
128133
vlan="vlans",
129134
vlan_group="vlan_groups",
130135
vlan_role="roles",
@@ -133,6 +138,9 @@
133138

134139
ENDPOINT_NAME_MAPPING = {
135140
"aggregates": "aggregate",
141+
"clusters": "cluster",
142+
"cluster_groups": "cluster_group",
143+
"cluster_types": "cluster_type",
136144
"device_bays": "device_bay",
137145
"devices": "device",
138146
"device_roles": "device_role",
@@ -153,6 +161,7 @@
153161
"sites": "site",
154162
"tenants": "tenant",
155163
"tenant_groups": "tenant_group",
164+
"virtual_machines": "virtual_machine",
156165
"vlans": "vlan",
157166
"vlan_groups": "vlan_group",
158167
"vrfs": "vrf",
@@ -256,8 +265,14 @@
256265

257266
INTF_MODE = {"access": 100, "tagged": 200, "tagged all": 300}
258267

268+
VIRTUAL_MACHINE_STATUS = dict(offline=0, active=1, staged=3)
269+
270+
# This is used when attempting to search for existing endpoints
259271
ALLOWED_QUERY_PARAMS = {
260272
"aggregate": set(["prefix", "rir"]),
273+
"cluster": set(["name", "type"]),
274+
"cluster_group": set(["slug"]),
275+
"cluster_type": set(["slug"]),
261276
"device_bay": set(["name", "device"]),
262277
"device": set(["name"]),
263278
"device_role": set(["slug"]),
@@ -287,13 +302,14 @@
287302
"tenant": set(["name"]),
288303
"tenant_group": set(["name"]),
289304
"untagged_vlan": set(["name", "site", "vlan_group", "tenant"]),
305+
"virtual_machine": set(["name", "cluster"]),
290306
"vlan": set(["name", "site", "tenant"]),
291307
"vlan_group": set(["slug", "site"]),
292308
"vrf": set(["name", "tenant"]),
293309
}
294310

295311
QUERY_PARAMS_IDS = set(
296-
["device", "group", "rir", "vrf", "site", "vlan_group", "tenant"]
312+
["cluster", "device", "group", "rir", "vrf", "site", "vlan_group", "tenant", "type"]
297313
)
298314

299315
# This is used when converting static choices to an ID value acceptable to Netbox API
@@ -306,22 +322,28 @@
306322
"racks": [{"status": RACK_STATUS, "outer_unit": RACK_UNIT, "type": RACK_TYPE}],
307323
"services": [{"protocol": SERVICE_PROTOCOL}],
308324
"sites": [{"status": SITE_STATUS}],
325+
"virtual_machines": [{"status": VIRTUAL_MACHINE_STATUS, "face": FACE_ID}],
309326
"vlans": [{"status": VLAN_STATUS}],
310327
}
311328

312329
# 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
313330
CONVERT_KEYS = {
331+
"cluster_type": "type",
332+
"cluster_group": "group",
314333
"parent_region": "parent",
315334
"prefix_role": "role",
316335
"rack_group": "group",
317336
"rack_role": "role",
318337
"tenant_group": "group",
338+
"virtual_machine_role": "role",
319339
"vlan_role": "role",
320340
"vlan_group": "group",
321341
}
322342

323343
# This is used to dynamically conver name to slug on endpoints requiring a slug
324344
SLUG_REQUIRED = {
345+
"cluster_groups",
346+
"cluster_types",
325347
"device_roles",
326348
"ipam_roles",
327349
"rack_groups",

plugins/module_utils/netbox_virtualization.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ansible_collections.fragmentedpacket.netbox_modules.plugins.module_utils.netbox_utils import (
1111
NetboxModule,
1212
ENDPOINT_NAME_MAPPING,
13+
SLUG_REQUIRED,
1314
)
1415
except ImportError:
1516
import sys
@@ -18,6 +19,13 @@
1819
from netbox_utils import NetboxModule, ENDPOINT_NAME_MAPPING
1920

2021

22+
NB_VIRTUAL_MACHINES = "virtual_machines"
23+
NB_CLUSTERS = "clusters"
24+
NB_CLUSTER_GROUP = "cluster_groups"
25+
NB_CLUSTER_TYPE = "cluster_types"
26+
NB_VM_INTERFACES = "interfaces"
27+
28+
2129
class NetboxVirtualizationModule(NetboxModule):
2230
def __init__(self, module, endpoint):
2331
super().__init__(module, endpoint)
@@ -27,22 +35,36 @@ def run(self):
2735
This function should have all necessary code for endpoints within the application
2836
to create/update/delete the endpoint objects
2937
Supported endpoints:
38+
- clusters
39+
- cluster_groups
40+
- cluster_types
41+
- interfaces
42+
- virtual_machines
43+
- netbox_cluster
3044
"""
3145
# Used to dynamically set key when returning results
3246
endpoint_name = ENDPOINT_NAME_MAPPING[self.endpoint]
3347

3448
self.result = {"changed": False}
3549

36-
application = self._find_app(self.endpoint)
50+
if self.endpoint == "interfaces":
51+
application = "virtualization"
52+
else:
53+
application = self._find_app(self.endpoint)
3754
nb_app = getattr(self.nb, application)
3855
nb_endpoint = getattr(nb_app, self.endpoint)
3956

4057
data = self.data
4158

4259
# Used for msg output
43-
name = data.get("name")
44-
45-
data["slug"] = self._to_slug(name)
60+
if data.get("name"):
61+
name = data["name"]
62+
elif data.get("slug"):
63+
name = data["slug"]
64+
65+
if self.endpoint in SLUG_REQUIRED:
66+
if not data.get("slug"):
67+
data["slug"] = self._to_slug(name)
4668

4769
object_query_params = self._build_query_params(endpoint_name, data)
4870
try:

0 commit comments

Comments
 (0)