Skip to content

Instantly share code, notes, and snippets.

@dominikholler
Created June 18, 2019 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominikholler/19bcdc5f14f42ab5f069086fd2ff5e37 to your computer and use it in GitHub Desktop.
Save dominikholler/19bcdc5f14f42ab5f069086fd2ff5e37 to your computer and use it in GitHub Desktop.
import logging
import openstack
cloud = openstack.connect(cloud='int')
# cloud = openstack.connect(cloud='packstack')
NS = 'secgroups'
def ns(name):
return NS + name
def is_member(item, ns):
return item.name.startswith(ns)
def delete_router(cloud, router):
for port in cloud.list_router_interfaces(router):
cloud.remove_router_interface(router, port_id=port.id)
cloud.delete_router(router.id)
def clean(ns=NS, complete=False):
def _log(msg):
logging.warning(msg)
for item in cloud.list_security_groups():
try:
if complete:
cloud.delete_security_group(item)
if is_member(item, ns):
cloud.delete_security_group(item)
except Exception as e:
_log(e)
for item in cloud.list_routers():
try:
if complete:
delete_router(cloud, item)
if is_member(item, ns):
delete_router(cloud, item)
except Exception as e:
_log(e)
for item in cloud.list_ports():
try:
if complete:
cloud.delete_port(item.id)
elif is_member(item, ns):
cloud.delete_port(item.id)
except Exception as e:
_log(e)
for item in cloud.list_subnets():
try:
if complete:
cloud.delete_subnet(item.id)
elif is_member(item, ns):
cloud.delete_subnet(item.id)
except Exception as e:
_log(e)
for item in cloud.list_networks():
try:
if complete:
cloud.delete_network(item.id)
elif is_member(item, ns):
cloud.delete_network(item.id)
except Exception as e:
_log(e)
clean()
def get_by_name(collection, name):
return next(entity for entity in collection if entity.name == name)
group0_name = ns('group0')
group1_name = ns('group1')
group0 = cloud.create_security_group(name=group0_name, description='description')
group1 = cloud.create_security_group(name=group1_name, description='description')
groups = cloud.list_security_groups()
group0 = get_by_name(groups, group0_name)
group1 = get_by_name(groups, group1_name)
print group0.id
for rule in group0.security_group_rules:
print '\t{} -> {}'.format(rule.id, rule.security_group_id)
cloud.delete_security_group(group0.name)
cloud.delete_security_group(group1.id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment