Skip to content

Commit dcca0b3

Browse files
authored
Add module netbox_virtual_disk (#1153)
Add new module netbox_virtual_disk
1 parent 70ec734 commit dcca0b3

File tree

8 files changed

+244
-1
lines changed

8 files changed

+244
-1
lines changed

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ansible
22
antsibull==0.48.0
3-
sphinx==3.4.2
3+
sphinx
44
Jinja2<3.2
55
sphinx_rtd_theme

meta/runtime.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ action_groups:
8181
- netbox_vlan
8282
- netbox_vlan_group
8383
- netbox_vm_interface
84+
- netbox_virtual_disk
8485
- netbox_vrf
8586
- netbox_webhook
8687
- netbox_wireless_lan

plugins/module_utils/netbox_utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
"cluster_types",
113113
"clusters",
114114
"virtual_machines",
115+
"virtual_disks",
115116
],
116117
wireless=["wireless_lans", "wireless_lan_groups", "wireless_links"],
117118
)
@@ -374,6 +375,7 @@
374375
"tenant_groups": "tenant_group",
375376
"virtual_chassis": "virtual_chassis",
376377
"virtual_machines": "virtual_machine",
378+
"virtual_disks": "virtual_disk",
377379
"vlans": "vlan",
378380
"vlan_groups": "vlan_group",
379381
"vrfs": "vrf",
@@ -508,6 +510,7 @@
508510
"untagged_vlan": set(["group", "name", "site", "vid", "vlan_group", "tenant"]),
509511
"virtual_chassis": set(["name", "master"]),
510512
"virtual_machine": set(["name", "cluster"]),
513+
"virtual_disk": set(["name", "virtual_machine"]),
511514
"vm_bridge": set(["name"]),
512515
"vlan": set(["group", "name", "site", "tenant", "vid", "vlan_group"]),
513516
"vlan_group": set(["name", "slug", "site", "scope"]),

plugins/module_utils/netbox_virtualization.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
NB_CLUSTER_GROUP = "cluster_groups"
1818
NB_CLUSTER_TYPE = "cluster_types"
1919
NB_VM_INTERFACES = "interfaces"
20+
NB_VIRTUAL_DISKS = "virtual_disks"
2021

2122

2223
class NetboxVirtualizationModule(NetboxModule):

plugins/modules/netbox_circuit_type.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
- This is auto-generated following NetBox rules if not provided
4848
required: false
4949
type: str
50+
color:
51+
description:
52+
- Color to associate the circuit type with
53+
required: false
54+
type: str
55+
version_added: "3.17.0"
5056
tags:
5157
description:
5258
- The tags to add/update
@@ -122,6 +128,7 @@ def main():
122128
name=dict(required=True, type="str"),
123129
description=dict(required=False, type="str"),
124130
slug=dict(required=False, type="str"),
131+
color=dict(required=False, type="str"),
125132
tags=dict(required=False, type="list", elements="raw"),
126133
custom_fields=dict(required=False, type="dict"),
127134
),
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Copyright: (c) 2024, Martin Rødvand (@rodvand)
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_virtual_disk
13+
short_description: Creates or removes disks from virtual machines in NetBox
14+
description:
15+
- Creates or removes disks from virtual machines in 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.17.0"
24+
extends_documentation_fragment:
25+
- netbox.netbox.common
26+
options:
27+
data:
28+
description:
29+
- Defines the vm disk configuration
30+
suboptions:
31+
virtual_machine:
32+
description:
33+
- Name of the virtual machine the disk will be associated with (case-sensitive)
34+
required: false
35+
type: raw
36+
name:
37+
description:
38+
- Name of the disk to be created
39+
required: true
40+
type: str
41+
description:
42+
description:
43+
- The description of the disk
44+
required: false
45+
type: str
46+
size:
47+
description:
48+
- The size (in GB) of the disk
49+
required: false
50+
type: int
51+
tags:
52+
description:
53+
- Any tags that the virtual disk 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 virtual disk module"
68+
connection: local
69+
hosts: localhost
70+
gather_facts: False
71+
tasks:
72+
- name: Create virtual disk
73+
netbox_virtual_disk:
74+
data:
75+
virtual_machine: test100
76+
name: disk0
77+
size: 50
78+
state: present
79+
"""
80+
81+
RETURN = r"""
82+
virtual_disk:
83+
description: Serialized object as created or already existent within NetBox
84+
returned: on creation
85+
type: dict
86+
msg:
87+
description: Message indicating failure or info about what has been achieved
88+
returned: always
89+
type: str
90+
"""
91+
92+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_utils import (
93+
NetboxAnsibleModule,
94+
NETBOX_ARG_SPEC,
95+
)
96+
from ansible_collections.netbox.netbox.plugins.module_utils.netbox_virtualization import (
97+
NetboxVirtualizationModule,
98+
NB_VIRTUAL_DISKS,
99+
)
100+
from copy import deepcopy
101+
102+
103+
def main():
104+
"""
105+
Main entry point for module execution
106+
"""
107+
argument_spec = deepcopy(NETBOX_ARG_SPEC)
108+
argument_spec.update(
109+
dict(
110+
data=dict(
111+
type="dict",
112+
required=True,
113+
options=dict(
114+
virtual_machine=dict(required=False, type="raw"),
115+
name=dict(required=True, type="str"),
116+
description=dict(required=False, type="str"),
117+
size=dict(required=False, type="int"),
118+
tags=dict(required=False, type="list", elements="raw"),
119+
custom_fields=dict(required=False, type="dict"),
120+
),
121+
),
122+
)
123+
)
124+
125+
required_if = [
126+
("state", "present", ["virtual_machine", "name"]),
127+
("state", "absent", ["virtual_machine", "name"]),
128+
]
129+
130+
module = NetboxAnsibleModule(
131+
argument_spec=argument_spec, supports_check_mode=True, required_if=required_if
132+
)
133+
134+
netbox_virtual_disk = NetboxVirtualizationModule(module, NB_VIRTUAL_DISKS)
135+
netbox_virtual_disk.run()
136+
137+
138+
if __name__ == "__main__": # pragma: no cover
139+
main()

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,8 @@
311311
include_tasks: "netbox_config_template.yml"
312312
tags:
313313
- netbox_config_template
314+
315+
- name: "NETBOX_VIRTUAL_DISK"
316+
include_tasks: "netbox_virtual_disk.yml"
317+
tags:
318+
- netbox_virtual_disk
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
##
3+
##
4+
### NETBOX_VIRTUAL_DISK
5+
##
6+
##
7+
- name: "NETBOX_VIRTUAL_DISK 1: Necessary info creation"
8+
netbox.netbox.netbox_virtual_disk:
9+
netbox_url: http://localhost:32768
10+
netbox_token: 0123456789abcdef0123456789abcdef01234567
11+
data:
12+
virtual_machine: "test100-vm"
13+
name: "disk0"
14+
size: 50
15+
state: present
16+
register: test_one
17+
18+
- name: "NETBOX_VIRTUAL_DISK 1: ASSERT - Necessary info creation"
19+
assert:
20+
that:
21+
- test_one is changed
22+
- test_one['diff']['before']['state'] == "absent"
23+
- test_one['diff']['after']['state'] == "present"
24+
- test_one['virtual_disk']['name'] == "disk0"
25+
- test_one['virtual_disk']['virtual_machine'] == 1
26+
- test_one['msg'] == "virtual_disk disk0 created"
27+
28+
- name: "NETBOX_VIRTUAL_DISK 2: Create duplicate"
29+
netbox.netbox.netbox_virtual_disk:
30+
netbox_url: http://localhost:32768
31+
netbox_token: 0123456789abcdef0123456789abcdef01234567
32+
data:
33+
virtual_machine: "test100-vm"
34+
name: "disk0"
35+
size: 50
36+
state: present
37+
register: test_two
38+
39+
- name: "NETBOX_VIRTUAL_DISK 2: ASSERT - Create duplicate"
40+
assert:
41+
that:
42+
- not test_two['changed']
43+
- test_two['virtual_disk']['name'] == "disk0"
44+
- test_two['virtual_disk']['virtual_machine'] == 1
45+
- test_two['msg'] == "virtual_disk disk0 already exists"
46+
47+
- name: "NETBOX_VIRTUAL_DISK 3: Update"
48+
netbox.netbox.netbox_virtual_disk:
49+
netbox_url: http://localhost:32768
50+
netbox_token: 0123456789abcdef0123456789abcdef01234567
51+
data:
52+
virtual_machine: "test100-vm"
53+
name: "disk0"
54+
size: 60
55+
tags:
56+
- "Schnozzberry"
57+
state: present
58+
register: test_three
59+
60+
- name: "NETBOX_VIRTUAL_DISK 4: ASSERT - Updated"
61+
assert:
62+
that:
63+
- test_three is changed
64+
- test_three['diff']['after']['size'] == 60
65+
- test_three['virtual_disk']['name'] == "disk0"
66+
- test_three['virtual_disk']['virtual_machine'] == 1
67+
- test_three['virtual_disk']['size'] == 60
68+
- test_three['virtual_disk']['tags'][0] == 4
69+
- test_three['msg'] == "virtual_disk disk0 updated"
70+
71+
- name: "NETBOX_VIRTUAL_DISK 4: ASSERT - Delete"
72+
netbox.netbox.netbox_virtual_disk:
73+
netbox_url: http://localhost:32768
74+
netbox_token: 0123456789abcdef0123456789abcdef01234567
75+
data:
76+
name: "disk0"
77+
virtual_machine: "test100-vm"
78+
state: absent
79+
register: test_four
80+
81+
- name: "NETBOX_VIRTUAL_DISK 4: ASSERT - Delete"
82+
assert:
83+
that:
84+
- test_four is changed
85+
- test_four['virtual_disk']['name'] == "disk0"
86+
- test_four['virtual_disk']['virtual_machine'] == 1
87+
- test_four['msg'] == "virtual_disk disk0 deleted"

0 commit comments

Comments
 (0)