Skip to content

Commit e9f0fb3

Browse files
author
Thomas
committed
added update_virtual_machine and bug fixes
1 parent c0b30a6 commit e9f0fb3

File tree

2 files changed

+86
-7
lines changed

2 files changed

+86
-7
lines changed

docs/netbox.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
netbox package
2+
==============
3+
4+
Submodules
5+
----------
6+
7+
netbox\.connection module
8+
-------------------------
9+
10+
.. automodule:: netbox.connection
11+
:members:
12+
:undoc-members:
13+
:show-inheritance:
14+
15+
netbox\.dcim module
16+
-------------------
17+
18+
.. automodule:: netbox.dcim
19+
:members:
20+
:undoc-members:
21+
:show-inheritance:
22+
23+
netbox\.exceptions module
24+
-------------------------
25+
26+
.. automodule:: netbox.exceptions
27+
:members:
28+
:undoc-members:
29+
:show-inheritance:
30+
31+
netbox\.ipam module
32+
-------------------
33+
34+
.. automodule:: netbox.ipam
35+
:members:
36+
:undoc-members:
37+
:show-inheritance:
38+
39+
netbox\.netbox module
40+
---------------------
41+
42+
.. automodule:: netbox.netbox
43+
:members:
44+
:undoc-members:
45+
:show-inheritance:
46+
47+
netbox\.virtualization module
48+
---------------------
49+
50+
.. automodule:: netbox.virtualization
51+
:members:
52+
:undoc-members:
53+
:show-inheritance:
54+
55+
Module contents
56+
---------------
57+
58+
.. automodule:: netbox
59+
:members:
60+
:undoc-members:
61+
:show-inheritance:

netbox/virtualization.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ def get_clusters(self, **kwargs):
88
"""Return all clusters"""
99
return self.netbox_con.get('/virtualization/clusters/', **kwargs)
1010

11-
# https://github.com/digitalocean/netbox/issues/1910
12-
def create_cluster(self, name, type_id, **kwargs):
11+
def create_cluster(self, name, type, **kwargs):
1312
"""Create a new cluster
1413
1514
:param name: name of the cluster
1615
:param type: cluster type
1716
:param kwargs: optional arguments
1817
:return: bool True if successful otherwise exception raised
1918
"""
20-
required_fields = {"name": name, "type": type_id}
19+
try:
20+
cluster_type_id = self.get_cluster_types(name=type)[0]['id']
21+
except IndexError:
22+
raise exceptions.NotFoundException('cluster-type: {}'.format(name)) from None
23+
24+
required_fields = {"name": name, "type": cluster_type_id}
2125
return self.netbox_con.post('/virtualization/clusters/', required_fields, **kwargs)
2226

2327
def delete_cluster(self, name):
@@ -27,7 +31,7 @@ def delete_cluster(self, name):
2731
:return: bool True if succesful otherwase delete exception
2832
"""
2933
try:
30-
cluster_id = self.get_cluster(name=name)[0]['id']
34+
cluster_id = self.get_clusters(name=name)[0]['id']
3135
except IndexError:
3236
raise exceptions.NotFoundException('cluster {}'.format(name)) from None
3337
return self.netbox_con.delete('/virtualization/clusters/', cluster_id)
@@ -40,7 +44,7 @@ def update_cluster(self, name, **kwargs):
4044
:return: bool True if successful otherwise raise UpdateException
4145
"""
4246
try:
43-
cluster_id = self.get_cluster(name=name)[0]['id']
47+
cluster_id = self.get_clusters(name=name)[0]['id']
4448
except IndexError:
4549
raise exceptions.NotFoundException('cluster: {}'.format(name)) from None
4650
return self.netbox_con.patch('/virtualization/clusters/', cluster_id, **kwargs)
@@ -67,7 +71,7 @@ def update_cluster_type(self, name, **kwargs):
6771
:return: bool True if successful otherwise raise UpdateException
6872
"""
6973
try:
70-
type_id = self.get_cluster_type(name)[0]['id']
74+
type_id = self.get_cluster_types(name=name)[0]['id']
7175
except IndexError:
7276
raise exceptions.NotFoundException(name) from None
7377
return self.netbox_con.patch('/virtualization/cluster-types/', type_id, **kwargs)
@@ -79,7 +83,7 @@ def delete_cluster_type(self, name):
7983
:return: bool True if succesful otherwase delete exception
8084
"""
8185
try:
82-
cluster_type_id = self.get_cluster_type(name=name)[0]['id']
86+
cluster_type_id = self.get_cluster_types(name=name)[0]['id']
8387
except IndexError:
8488
raise exceptions.NotFoundException('cluster-type: {}'.format(name)) from None
8589
return self.netbox_con.delete('/virtualization/cluster-types/', cluster_type_id)
@@ -154,3 +158,17 @@ def delete_virtual_machine(self, virtual_machine_name):
154158
except IndexError:
155159
raise exceptions.NotFoundException('virtual-machine: {}'.format(virtual_machine_name)) from None
156160
return self.netbox_con.delete('/virtualization/virtual-machines/', virtual_machine_id)
161+
162+
def update_virtual_machine(self, virtual_machine_name, **kwargs):
163+
"""Update virtual-machine
164+
165+
:param name: name of the virtual-machine to update
166+
:param kwargs: update data
167+
:return: bool True if successful otherwise raise UpdateException
168+
"""
169+
try:
170+
virtual_machine_id = self.get_virtual_machines(name=virtual_machine_name)[0]['id']
171+
except IndexError:
172+
raise exceptions.NotFoundException('virtual-machine: {}'
173+
.format(virtual_machine_name)) from None
174+
return self.netbox_con.patch('/virtualization/virtual-machines/', virtual_machine_id, **kwargs)

0 commit comments

Comments
 (0)