Page MenuHomePhabricator
Paste P11631

af-netbox: delete ifaces and addresses of offline devices
ActivePublic

Authored by Volans on Mon, Jun 22, 9:57 PM.
>>> import uuid
>>> request_id = uuid.uuid4()
>>> user = User.objects.get(username='admin')
>>> devices = Device.objects.filter(status='offline', device_role__slug='server', interfaces__gt=0)
>>>
>>> def clean_interfaces(device, user, request_id):
... for interface in device.interfaces.all():
... for address in interface.ip_addresses.all():
... print(f'Deleting address {address} of device {device}')
... log = address.to_objectchange('delete')
... log.request_id = request_id
... log.user = user
... log.save()
... address.delete()
... print(f'Deleting iface {interface} of device {device}')
... log = interface.to_objectchange('delete')
... log.request_id = request_id
... log.user = user
... log.save()
... interface.delete()
... device.save()
...
>>> for device in devices:
... clean_interfaces(device, user, request_id)
...
Deleting iface mgmt of device alnilam
Deleting iface mgmt of device db1063
Deleting iface mgmt of device db1070
Deleting iface mgmt of device db1071
Deleting iface mgmt of device db1072
Deleting iface mgmt of device db1073
Deleting iface mgmt of device db2034
Deleting iface mgmt of device db2035
Deleting iface mgmt of device db2036
Deleting iface mgmt of device db2037
Deleting iface mgmt of device db2038
Deleting iface mgmt of device db2040
Deleting iface mgmt of device db2041
Deleting iface mgmt of device db2043
Deleting iface mgmt of device db2044
Deleting iface mgmt of device db2045
Deleting iface mgmt of device db2046
Deleting iface mgmt of device db2047
Deleting iface mgmt of device db2048
Deleting iface mgmt of device db2049
Deleting iface mgmt of device db2050
Deleting iface mgmt of device db2051
Deleting iface mgmt of device db2052
Deleting iface mgmt of device db2053
Deleting iface mgmt of device db2054
Deleting iface mgmt of device db2055
Deleting iface mgmt of device db2056
Deleting iface mgmt of device db2057
Deleting iface mgmt of device db2058
Deleting iface mgmt of device db2059
Deleting iface mgmt of device db2060
Deleting iface mgmt of device db2061
Deleting iface mgmt of device db2062
Deleting iface mgmt of device db2063
Deleting iface mgmt of device db2066
Deleting iface mgmt of device db2067
Deleting iface mgmt of device db2068
Deleting iface mgmt of device db2069
Deleting iface mgmt of device dbstore2001
Deleting iface mgmt of device dbstore2002
Deleting iface mgmt of device graphite2001
Deleting iface mgmt of device iron
Deleting iface mgmt of device lithium
Deleting iface mgmt of device ms-be1027
Deleting iface mgmt of device ms-be2013
Deleting iface mgmt of device ms-be2014
Deleting iface mgmt of device ms-be2015
Deleting iface mgmt of device sarin
Deleting iface mgmt of device wmf4727
>>>