Skip to content

Instantly share code, notes, and snippets.

@dominikholler
Created February 19, 2018 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominikholler/f58658407ae7620280f4cb47c398d849 to your computer and use it in GitHub Desktop.
Save dominikholler/f58658407ae7620280f4cb47c398d849 to your computer and use it in GitHub Desktop.
#!/bin/env python
import logging
import shade
cloud = shade.openstack_cloud(cloud='ovirt')
NS = 'add_router_'
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, complete=False):
def _log(msg):
logging.warning(msg)
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(NS)
net = cloud.create_network(ns('net'))
subnet = cloud.create_subnet(net.id, cidr='10.0.0.0/24',
subnet_name=ns('subnet'), enable_dhcp=True,
gateway_ip='10.0.0.1')
router = cloud.create_router(
name=ns('router'),
ext_gateway_net_id=net.id,
ext_fixed_ips=[{"ip_address": "10.0.0.2", 'subnet_id': subnet.id}],
enable_snat=False
)
clean(NS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment