Skip to content

Instantly share code, notes, and snippets.

@larsks
Created February 3, 2014 19:33
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/8790725 to your computer and use it in GitHub Desktop.
Save larsks/8790725 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]
# This is the structure passed to keystone to obtain
# an authentication token. See:
#
# http://docs.openstack.org/api/openstack-identity-service/2.0/content/POST_authenticate_v2.0_tokens_.html
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)
)
# The response is a JSON dictionary that includes both
# a token and a service catalog.
auth_resp = resp.json()
catalog = auth_resp['access']['serviceCatalog']
token = auth_resp['access']['token']['id']
# Here we create a Session that will always set the
# x-auth-token header on requests.
sess = requests.Session()
sess.headers.update({'x-auth-token': token})
# Find the Neutron API endpoint.
endpoints = find_endpoint_for(catalog, svc_type='network')
network_url = endpoints[0]
# Find the Keystone API endpoint.
endpoints = find_endpoint_for(catalog, svc_type='identity', public=False)
id_url = endpoints[0]
# Get a list of floating ips. I couldn't find this documented
# anywhere, so I ran 'tcpdump' while running "neutron floatingip-list" on
# the command line.
resp = sess.get('%s/v2.0/floatingips.json' % network_url)
resp = resp.json()
# Iterate through each of the floating ips.
for fip in resp['floatingips']:
# Look up the tenant name.
tenant = fip['tenant_id']
resp = sess.get('%s/tenants/%s' % (id_url, tenant))
tenant_resp = resp.json()
# If the floating ip is associated with a port, get the UUID
# of the device connected to the port (which will generally
# be an instance).
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 it all out.
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