Skip to content

Instantly share code, notes, and snippets.

@larsks
Created February 3, 2014 19:29
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 larsks/8790654 to your computer and use it in GitHub Desktop.
Save larsks/8790654 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Does anyone know if there is a better way to get a list of floating IPs, the VM
# & project they are associated with? currently I am querrying the DB: SELECT
# floatingips.floating_ip_address AS IP, instances.display_name AS VM,
# project.name AS PROJECT FROM quantum.floatingips LEFT JOIN (keystone.project)
# ON (floatingips.tenant_id=project.id) LEFT JOIN (nova.instances, quantum.ports)
# ON (fixed_port_id=ports.id AND instances.uuid=ports.dev
import os
import sys
import requests
import json
def find_endpoint_for(catalog, svc_name=None, svc_type=None,
svc_region='RegionOne', public=True):
for entry in catalog:
if svc_name is not None and entry['name'] != svc_name:
continue
if svc_type is not None and entry['type'] != svc_type:
continue
if public:
selector = 'publicURL'
else:
selector = 'adminURL'
return [
x[selector] for x in entry['endpoints']
if x['region'] == svc_region]
auth = {
'auth': {
'passwordCredentials': {
'username': os.environ['OS_USERNAME'],
'password': os.environ['OS_PASSWORD'],
},
'tenantName': os.environ['OS_TENANT_NAME'],
}
}
resp = requests.post(
'%s/tokens' % (os.environ['OS_AUTH_URL'],),
headers = {'content-type': 'application/json'},
data=json.dumps(auth)
)
auth_resp = resp.json()
catalog = auth_resp['access']['serviceCatalog']
token = auth_resp['access']['token']['id']
sess = requests.Session()
sess.headers.update({'x-auth-token': token})
endpoints = find_endpoint_for(catalog, svc_type='network')
network_url = endpoints[0]
endpoints = find_endpoint_for(catalog, svc_type='identity', public=False)
id_url = endpoints[0]
resp = sess.get('%s/v2.0/floatingips.json' % network_url)
resp = resp.json()
for fip in resp['floatingips']:
tenant = fip['tenant_id']
url = '%s/tenants/%s' % (id_url, tenant)
resp = sess.get(url)
tenant_resp = resp.json()
if fip['port_id'] is not None:
resp = sess.get('%s/v2.0/ports/%s' % (network_url, fip['port_id']))
device = resp.json()['port']['device_id']
else:
device = '<unassigned>'
print fip['floating_ip_address'], tenant_resp['tenant']['name'], device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment