Add a dry-run flag. Fix inconsistency with 'list' and 'delete' orphans

Closes-Bug: #1501965

Change-Id: If9b6af6397512c6e55aaf1116b3788d23a4f3551
This commit is contained in:
Kurt Payne 2015-10-01 17:27:53 -07:00
parent b2d3a35790
commit feb571c985
2 changed files with 55 additions and 1 deletions

View File

@ -1,3 +1,5 @@
#!/usr/bin/env python
"""
This script deletes all the floatingips a user has that are not
associated with a port_id.
@ -10,6 +12,9 @@ from neutronclient.v2_0 import client
def main():
dry_run = (len(sys.argv) > 1 and sys.argv[1] == '--dry-run')
try:
username = os.environ['OS_USERNAME']
tenant_name = os.environ['OS_TENANT_NAME']
@ -29,6 +34,7 @@ def main():
if not floatingip['port_id']:
print(("Deleting floatingip %s - %s") %
(floatingip['id'], floatingip['floating_ip_address']))
neutron.delete_floatingip(floatingip['id'])
if not dry_run:
neutron.delete_floatingip(floatingip['id'])
main()

View File

@ -0,0 +1,48 @@
#!/usr/bin/env python
"""
This script deletes all the floatingips a user has that are not
associated with a tenant.
"""
import os
import sys
from neutronclient.v2_0 import client
import keystoneclient.v2_0.client as ksclient
def main():
dry_run = (len(sys.argv) > 1 and sys.argv[1] == '--dry-run')
try:
username = os.environ['OS_USERNAME']
tenant_name = os.environ['OS_TENANT_NAME']
password = os.environ['OS_PASSWORD']
auth_url = os.environ['OS_AUTH_URL']
except KeyError:
print("You need to source your openstack creds file first!")
sys.exit(1)
neutron = client.Client(username=username,
tenant_name=tenant_name,
password=password,
auth_url=auth_url)
keystone = ksclient.Client(username=username,
tenant_name=tenant_name,
password=password,
auth_url=auth_url)
floatingips = neutron.list_floatingips()
for floatingip in floatingips['floatingips']:
try:
keystone.tenants.get(floatingip['tenant_id'])
# If the tenant ID doesn't exist, then this object is orphaned
except ksclient.exceptions.NotFound:
print(("Deleting floatingip %s - %s") %
(floatingip['id'], floatingip['floating_ip_address']))
if not dry_run:
neutron.delete_floatingip(floatingip['id'])
main()