Initial commit of listorphans.py script

listorphans.py lists certain 'orphaned' objects - routers, floating IPs,
subnets, and networks - present in Neutron.  Orphans in this context are
objects that exist but whose project ID is no longer valid, e.g tenants
that have been deleted.

Change-Id: I41ea6f115d0b7a1a84e7f23005d333d39b800beb
This commit is contained in:
Nick Jones 2015-09-30 10:15:38 +01:00
parent 281f4fd277
commit d221811d03
1 changed files with 50 additions and 0 deletions

50
neutron/listorphans.py Executable file
View File

@ -0,0 +1,50 @@
#!/usr/bin/env python
import os
import sys
import keystoneclient.v2_0.client as ksclient
import neutronclient.v2_0.client as nclient
def usage():
print "listorphans.py <object> where object is one or more of",
print "'networks', 'routers', 'subnets', 'floatingips' or 'all'"
def get_credentials():
d = {}
d['username'] = os.environ['OS_USERNAME']
d['password'] = os.environ['OS_PASSWORD']
d['auth_url'] = os.environ['OS_AUTH_URL']
d['tenant_name'] = os.environ['OS_TENANT_NAME']
return d
credentials = get_credentials()
neutron = nclient.Client(**credentials)
keystone = ksclient.Client(**credentials)
def get_orphaned_neutron_objects(object):
objects = getattr(neutron, 'list_' + object)()
orphans = []
for object in objects.get(object):
try:
keystone.tenants.get(object['tenant_id'])
# If the tenant ID doesn't exist, then this object is orphaned
except ksclient.exceptions.NotFound:
orphans.append(object['id'])
return orphans
def main():
if len(sys.argv) > 1:
if sys.argv[1] == 'all':
objects = [ 'networks', 'routers', 'subnets', 'floatingips' ]
else:
objects = sys.argv[1:]
for object in objects:
orphans = get_orphaned_neutron_objects(object)
print len(orphans), 'orphan(s) found of type', object, '[%s]' % ', '.join(map(str, orphans))
else:
usage()
sys.exit(1)
if __name__ == '__main__':
main()