This is sample python script that delete virtual machines, keys and security groups where security group has a same name as keypair
import os, sys, time
from pprint import pprint
from novaclient.v1_1 import client
nt = client.Client("username", "pwd", "test", "http://192.168.0.155:1111/v1.1/", service_type="compute")
a = nt.servers.list()
for s in a:
print s.name + " | " + s.key_name + " | " + str(s.addresses)
varDelete = raw_input("Delete machine, keys and groups (y/n): ")
if varDelete == "y":
print "deleting"
print
print "checking if there is an IP for release"
for nets in s.addresses:
pprint (s.addresses[nets])
for a in s.addresses[nets]:
address = str(a["addr"])
print "checking address " + address
existInFloating=[checked for checked in nt.floating_ips.list() if checked.ip == address]
if len(existInFloating) > 0:
print "exist"
s.remove_floating_ip(address)
existInFloating[0].delete()
print "ip address released"
else:
print "not exist"
nt.servers.delete(s)
keyname = s.key_name
if keyname != "":
print "deleting keyname and group: " + keyname
selectedKeyPairs = [k for k in nt.keypairs.list() if k.name == keyname]
if len(selectedKeyPairs) > 0:
selectedKeyPairs[0].delete()
selectedGroups = [k for k in nt.security_groups.list() if k.name == keyname]
if len(selectedGroups) > 0:
time.sleep(5)
selectedGroups[0].delete()
print "deleted"
else:
print "skipping"