From d221811d03681085b8e381a35ba8f8e3e61c0f5c Mon Sep 17 00:00:00 2001 From: Nick Jones Date: Wed, 30 Sep 2015 10:15:38 +0100 Subject: [PATCH] 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 --- neutron/listorphans.py | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100755 neutron/listorphans.py diff --git a/neutron/listorphans.py b/neutron/listorphans.py new file mode 100755 index 0000000..fbbbf62 --- /dev/null +++ b/neutron/listorphans.py @@ -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 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()