Skip to content

Commit 7e88e1e

Browse files
author
Thomas
committed
Implemtend methods to update and delete by id
1 parent 1c76028 commit 7e88e1e

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed

netbox/extras.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ def delete_config_context(self, name):
3333
raise exceptions.NotFoundException('config-context: {}'.format(name)) from None
3434
return self.netbox_con.delete('/extras/config-contexts/', config_context_id)
3535

36+
def delete_config_context_by_id(self, config_context_id):
37+
"""Delete config-context
38+
39+
:param config_context_id: config-context to delete
40+
:return: bool True if succesful otherwase delete exception
41+
"""
42+
return self.netbox_con.delete('/extras/config-contexts/', config_context_id)
43+
3644
def update_config_context(self, name, **kwargs):
3745
"""Update config-context
3846
@@ -47,6 +55,15 @@ def update_config_context(self, name, **kwargs):
4755

4856
return self.netbox_con.patch('/extras/config-contexts/', config_context_id, **kwargs)
4957

58+
def update_config_context_by_id(self, config_context_id, **kwargs):
59+
"""Update config-context
60+
61+
:param config_context_id: config-context to update
62+
:param kwargs: requests body dict
63+
:return: bool True if successful otherwise raise UpdateException
64+
"""
65+
return self.netbox_con.patch('/extras/config-contexts/', config_context_id, **kwargs)
66+
5067
def get_tags(self, **kwargs):
5168
"""Returns all tags"""
5269
return self.netbox_con.get('/extras/tags/', **kwargs)
@@ -74,6 +91,14 @@ def delete_tag(self, name):
7491
raise exceptions.NotFoundException('tag: {}'.format(name)) from None
7592
return self.netbox_con.delete('/extras/tags/', tag_id)
7693

94+
def delete_tag_by_id(self, tag_id):
95+
"""Delete tag
96+
97+
:param tag_id: tag to delete
98+
:return: bool True if succesful otherwase delete exception
99+
"""
100+
return self.netbox_con.delete('/extras/tags/', tag_id)
101+
77102
def update_tag(self, name, **kwargs):
78103
"""Update tag
79104
@@ -88,6 +113,15 @@ def update_tag(self, name, **kwargs):
88113

89114
return self.netbox_con.patch('/extras/tags/', tag_id, **kwargs)
90115

116+
def update_tag_by_id(self, tag_id, **kwargs):
117+
"""Update tag
118+
119+
:param tag_id: tag id
120+
:param kwargs: requests body dict
121+
:return: bool True if successful otherwise raise UpdateException
122+
"""
123+
return self.netbox_con.patch('/extras/tags/', tag_id, **kwargs)
124+
91125
def get_object_changes(self, **kwargs):
92126
"""Returns all object changes"""
93127
return self.netbox_con.get('/extras/object-changes/', **kwargs)

netbox/ipam.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ def update_ip(self, ip_address, **kwargs):
7474
raise exceptions.NotFoundException('ip: {}'.format(ip_address)) from None
7575
return self.netbox_con.patch('/ipam/ip-addresses/', ip_id, **kwargs)
7676

77+
def update_ip_by_id(self, ip_id, **kwargs):
78+
"""Update ip address
79+
80+
:param ip_id: ip id to update
81+
:param kwargs: requests body dict
82+
:return: bool True if successful otherwise raise UpdateException
83+
"""
84+
return self.netbox_con.patch('/ipam/ip-addresses/', ip_id, **kwargs)
85+
7786
def delete_ip_address(self, ip_address):
7887
"""Delete IP address
7988
@@ -122,6 +131,14 @@ def delete_ip_prefix(self, **kwargs):
122131
raise exceptions.NotFoundException('ip-prefix') from None
123132
return self.netbox_con.delete('/ipam/prefixes/', ip_prefix_id)
124133

134+
def delete_ip_prefix_by_id(self, ip_prefix_id):
135+
"""Delete IP prefix
136+
137+
:param ip_prefix_id: Delete prefix based on id
138+
:return: bool True if successful otherwise raise DeleteException
139+
"""
140+
return self.netbox_con.delete('/ipam/prefixes/', ip_prefix_id)
141+
125142
def update_ip_prefix(self, ip_prefix, **kwargs):
126143
"""Update ip address
127144
@@ -135,6 +152,15 @@ def update_ip_prefix(self, ip_prefix, **kwargs):
135152
raise exceptions.NotFoundException('ip-prefix: {}'.format(ip_prefix)) from None
136153
return self.netbox_con.patch('/ipam/prefixes/', ip_prefix_id, **kwargs)
137154

155+
def update_ip_prefix_by_id(self, ip_prefix_id, **kwargs):
156+
"""Update ip address
157+
158+
:param ip_prefix_id: ip prefix to update
159+
:param kwargs: requests body dict
160+
:return: bool True if successful otherwise raise UpdateException
161+
"""
162+
return self.netbox_con.patch('/ipam/prefixes/', ip_prefix_id, **kwargs)
163+
138164
def get_next_available_ip(self, **kwargs):
139165
"""Return next available ip in prefix
140166
@@ -176,6 +202,14 @@ def delete_vrf(self, vrf_name):
176202
raise exceptions.NotFoundException('vrf: {}'.format(vrf_name)) from None
177203
return self.netbox_con.delete('/ipam/vrfs/', vrf_id)
178204

205+
def delete_vrf_by_id(self, vrf_id):
206+
"""Delete vrf
207+
208+
:param vrf_id: vrf to delete
209+
:return: bool True if successful otherwise raise DeleteException
210+
"""
211+
return self.netbox_con.delete('/ipam/vrfs/', vrf_id)
212+
179213
def update_vrf(self, vrf_name, **kwargs):
180214
"""Update vrf
181215
@@ -189,6 +223,15 @@ def update_vrf(self, vrf_name, **kwargs):
189223
raise exceptions.NotFoundException('vrf: {}'.format(vrf_name)) from None
190224
return self.netbox_con.patch('/ipam/vrfs/', vrf_id, **kwargs)
191225

226+
def update_vrf_by_id(self, vrf_id, **kwargs):
227+
"""Update vrf
228+
229+
:param vrf_id: vrf to update
230+
:param kwargs: requests body dict
231+
:return: bool True if successful otherwise raise UpdateException
232+
"""
233+
return self.netbox_con.patch('/ipam/vrfs/', vrf_id, **kwargs)
234+
192235
def get_aggregates(self, **kwargs):
193236
"""Return all aggregates"""
194237
return self.netbox_con.get('/ipam/aggregates/', **kwargs)
@@ -220,6 +263,15 @@ def update_aggregate(self, prefix, **kwargs):
220263
raise exceptions.NotFoundException('aggregate: {}'.format(prefix)) from None
221264
return self.netbox_con.patch('/ipam/aggregates/', aggregate_id, **kwargs)
222265

266+
def update_aggregate_by_id(self, aggregate_id, **kwargs):
267+
"""Update aggregate
268+
269+
:param aggregate_id: aggregate to update
270+
:param kwargs: requests body dict
271+
:return: bool True if successful otherwise raise UpdateException
272+
"""
273+
return self.netbox_con.patch('/ipam/aggregates/', aggregate_id, **kwargs)
274+
223275
def get_rirs(self, **kwargs):
224276
"""Return all rirs"""
225277
return self.netbox_con.get('/ipam/rirs/', **kwargs)
@@ -246,6 +298,14 @@ def delete_rir(self, rir_name):
246298
raise exceptions.NotFoundException('rir: {}'.format(rir_name)) from None
247299
return self.netbox_con.delete('/ipam/rirs/', rir_id)
248300

301+
def delete_rir_by_id(self, rir_id):
302+
"""Delete rir
303+
304+
:param rir_id: rir to delete
305+
:return: bool True if successful otherwise raise DeleteException
306+
"""
307+
return self.netbox_con.delete('/ipam/rirs/', rir_id)
308+
249309
def update_rir(self, rir_name, **kwargs):
250310
"""Update rir
251311
@@ -259,6 +319,15 @@ def update_rir(self, rir_name, **kwargs):
259319
raise exceptions.NotFoundException('rir: {}'.format(rir_name)) from None
260320
return self.netbox_con.patch('/ipam/rirs/', rir_id, **kwargs)
261321

322+
def update_rir_by_id(self, rir_id, **kwargs):
323+
"""Update rir
324+
325+
:param rir_id: rir to update
326+
:param kwargs: requests body dict
327+
:return: bool True if successful otherwise raise UpdateException
328+
"""
329+
return self.netbox_con.patch('/ipam/rirs/', rir_id, **kwargs)
330+
262331
def get_roles(self, **kwargs):
263332
"""Return all roles"""
264333
return self.netbox_con.get('/ipam/roles/', **kwargs)
@@ -285,6 +354,14 @@ def delete_role(self, role_name):
285354
raise exceptions.NotFoundException('prefix/vlan role: {}'.format(role_name)) from None
286355
return self.netbox_con.delete('/ipam/roles/', role_id)
287356

357+
def delete_role_by_id(self, role_id):
358+
"""Delete prefix/vlan role
359+
360+
:param role_id: prefix/vlan role to delete
361+
:return: bool True if successful otherwise raise DeleteException
362+
"""
363+
return self.netbox_con.delete('/ipam/roles/', role_id)
364+
288365
def update_role(self, role_name, **kwargs):
289366
"""Update prefix role
290367
@@ -298,6 +375,15 @@ def update_role(self, role_name, **kwargs):
298375
raise exceptions.NotFoundException('prefix/vlan role: {}'.format(role_name)) from None
299376
return self.netbox_con.patch('/ipam/roles/', prefix_role_id, **kwargs)
300377

378+
def update_role_by_id(self, role_id, **kwargs):
379+
"""Update prefix role
380+
381+
:param role_id: role to update
382+
:param kwargs: requests body dict
383+
:return: bool True if successful otherwise raise UpdateException
384+
"""
385+
return self.netbox_con.patch('/ipam/roles/', role_id, **kwargs)
386+
301387
def get_vlans(self, **kwargs):
302388
"""Return all vlans"""
303389
return self.netbox_con.get('/ipam/vlans/', **kwargs)
@@ -325,6 +411,14 @@ def delete_vlan(self, vid):
325411
raise exceptions.NotFoundException('vlan: {}'.format(vid)) from None
326412
return self.netbox_con.delete('/ipam/vlans/', vid_id)
327413

414+
def delete_vlan_by_id(self, vlan_id):
415+
"""Delete VLAN based on VLAN ID
416+
417+
:param vlan_id: vlan id to delete
418+
:return: bool True if successful otherwise raise DeleteException
419+
"""
420+
return self.netbox_con.delete('/ipam/vlans/', vlan_id)
421+
328422
def update_vlan(self, vlan_name, **kwargs):
329423
"""Update vlan
330424
@@ -338,6 +432,15 @@ def update_vlan(self, vlan_name, **kwargs):
338432
raise exceptions.NotFoundException('vlan: {}'.format(vlan_name)) from None
339433
return self.netbox_con.patch('/ipam/vlans/', vlan_id, **kwargs)
340434

435+
def update_vlan_by_id(self, vlan_id, **kwargs):
436+
"""Update vlan
437+
438+
:param vlan_id: vlan to update
439+
:param kwargs: requests body dict
440+
:return: bool True if successful otherwise raise UpdateException
441+
"""
442+
return self.netbox_con.patch('/ipam/vlans/', vlan_id, **kwargs)
443+
341444
def get_vlan_groups(self, **kwargs):
342445
"""Return all vlan groups"""
343446
return self.netbox_con.get('/ipam/vlan-groups/', **kwargs)
@@ -365,6 +468,14 @@ def delete_vlan_group(self, name):
365468
raise exceptions.NotFoundException('vlan: {}'.format(name)) from None
366469
return self.netbox_con.delete('/ipam/vlan-groups/', vgrp_id)
367470

471+
def delete_vlan_group_by_id(self, vlan_group_id):
472+
"""Delete VLAN group
473+
474+
:param vlan_group_id: vlan-group to delete
475+
:return: bool True if successful otherwise raise DeleteException
476+
"""
477+
return self.netbox_con.delete('/ipam/vlan-groups/', vlan_group_id)
478+
368479
def update_vlan_group(self, name, **kwargs):
369480
"""Update vlan-group
370481
@@ -377,3 +488,12 @@ def update_vlan_group(self, name, **kwargs):
377488
except IndexError:
378489
raise exceptions.NotFoundException('name: {}'.format(name)) from None
379490
return self.netbox_con.patch('/ipam/vlan-groups/', vgrp_ip, **kwargs)
491+
492+
def update_vlan_group_by_id(self, vlan_group_id, **kwargs):
493+
"""Update vlan-group
494+
495+
:param vlan_group_id: vlan group to update
496+
:param kwargs: arguments
497+
:return: bool True if successful otherwise raise UpdateException
498+
"""
499+
return self.netbox_con.patch('/ipam/vlan-groups/', vlan_group_id, **kwargs)

netbox/virtualization.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def delete_cluster(self, name):
4545
raise exceptions.NotFoundException('cluster {}'.format(name)) from None
4646
return self.netbox_con.delete('/virtualization/clusters/', cluster_id)
4747

48+
def delete_cluster_by_id(self, cluster_id):
49+
"""Delete a cluster
50+
51+
:param cluster_id: cluster to delete
52+
:return: netbox object if succesful otherwase delete exception
53+
"""
54+
return self.netbox_con.delete('/virtualization/clusters/', cluster_id)
55+
4856
def update_cluster(self, name, **kwargs):
4957
"""Update cluster
5058
@@ -58,6 +66,15 @@ def update_cluster(self, name, **kwargs):
5866
raise exceptions.NotFoundException('cluster: {}'.format(name)) from None
5967
return self.netbox_con.patch('/virtualization/clusters/', cluster_id, **kwargs)
6068

69+
def update_cluster_by_id(self, cluster_id, **kwargs):
70+
"""Update cluster
71+
72+
:param kwargs: filter arguments
73+
:param cluster_id: cluster to update
74+
:return: bool True if successful otherwise raise UpdateException
75+
"""
76+
return self.netbox_con.patch('/virtualization/clusters/', cluster_id, **kwargs)
77+
6178
def get_cluster_types(self, **kwargs):
6279
"""Return all cluster types"""
6380
return self.netbox_con.get('/virtualization/cluster-types/', **kwargs)
@@ -85,6 +102,15 @@ def update_cluster_type(self, name, **kwargs):
85102
raise exceptions.NotFoundException(name) from None
86103
return self.netbox_con.patch('/virtualization/cluster-types/', type_id, **kwargs)
87104

105+
def update_cluster_type_by_id(self, cluster_type_id, **kwargs):
106+
"""Update cluster type
107+
108+
:param cluster_type_id: ID of the cluster type
109+
:param kwargs: fields to update
110+
:return: bool True if successful otherwise raise UpdateException
111+
"""
112+
return self.netbox_con.patch('/virtualization/cluster-types/', cluster_type_id, **kwargs)
113+
88114
def delete_cluster_type(self, name):
89115
"""Delete a cluster type
90116
@@ -97,6 +123,14 @@ def delete_cluster_type(self, name):
97123
raise exceptions.NotFoundException('cluster-type: {}'.format(name)) from None
98124
return self.netbox_con.delete('/virtualization/cluster-types/', cluster_type_id)
99125

126+
def delete_cluster_type_by_id(self, cluster_type_id):
127+
"""Delete a cluster type
128+
129+
:param cluster_type_id: cluster type to delete
130+
:return: bool True if succesful otherwase delete exception
131+
"""
132+
return self.netbox_con.delete('/virtualization/cluster-types/', cluster_type_id)
133+
100134
def get_interfaces(self, **kwargs):
101135
"""Return all interfaces"""
102136
return self.netbox_con.get('/virtualization/interfaces/', **kwargs)
@@ -134,6 +168,15 @@ def update_interface(self, name, virtual_machine, **kwargs):
134168
.format(name, virtual_machine)) from None
135169
return self.netbox_con.patch('/virtualization/interfaces/', interface_id, **kwargs)
136170

171+
def update_interface_by_id(self, interface_id, **kwargs):
172+
"""Update virtual_machine interface
173+
174+
:param interface_id: id the interface to update
175+
:param kwargs: update data
176+
:return: bool True if successful otherwise raise UpdateException
177+
"""
178+
return self.netbox_con.patch('/virtualization/interfaces/', interface_id, **kwargs)
179+
137180
def delete_interface(self, name, virtual_machine):
138181
"""Delete interface from virtual_machine
139182
@@ -149,6 +192,14 @@ def delete_interface(self, name, virtual_machine):
149192

150193
return self.netbox_con.delete('/virtualization/interfaces/', interface_id)
151194

195+
def delete_interface_by_id(self, interface_id):
196+
"""Delete interface from virtual_machine
197+
198+
:param interface_id: interface to delete
199+
:return: bool True if successful otherwise raise DeleteException
200+
"""
201+
return self.netbox_con.delete('/virtualization/interfaces/', interface_id)
202+
152203
def get_virtual_machines(self, **kwargs):
153204
"""Return all virtual-machines"""
154205
return self.netbox_con.get('/virtualization/virtual-machines/', **kwargs)
@@ -184,6 +235,14 @@ def delete_virtual_machine(self, virtual_machine_name):
184235
raise exceptions.NotFoundException('virtual-machine: {}'.format(virtual_machine_name)) from None
185236
return self.netbox_con.delete('/virtualization/virtual-machines/', virtual_machine_id)
186237

238+
def delete_virtual_machine_by_id(self, virtual_machine_id):
239+
"""Delete virtual machine
240+
241+
:param virtual_machine_id: virtual machine to delete
242+
:return: bool True if successful otherwise raise exception
243+
"""
244+
return self.netbox_con.delete('/virtualization/virtual-machines/', virtual_machine_id)
245+
187246
def update_virtual_machine(self, virtual_machine_name, **kwargs):
188247
"""Update virtual-machine
189248
@@ -197,3 +256,12 @@ def update_virtual_machine(self, virtual_machine_name, **kwargs):
197256
raise exceptions.NotFoundException('virtual-machine: {}'
198257
.format(virtual_machine_name)) from None
199258
return self.netbox_con.patch('/virtualization/virtual-machines/', virtual_machine_id, **kwargs)
259+
260+
def update_virtual_machine_by_id(self, virtual_machine_id, **kwargs):
261+
"""Update virtual-machine
262+
263+
:param virtual_machine_id: virtual-machine to update
264+
:param kwargs: update data
265+
:return: bool True if successful otherwise raise UpdateException
266+
"""
267+
return self.netbox_con.patch('/virtualization/virtual-machines/', virtual_machine_id, **kwargs)

0 commit comments

Comments
 (0)