Getting the API Endpoint URL

The API token is only one essential piece of information you need to know to use the OpenStack Compute API. Another is the URL of the Nova API end point. That's where you will send your calls to the OpenStack Compute API accompanied by the token you just obtained. That information may be given to you by your OpenStack cloud provider or you may be able to extract that information from the authentication server's response:

Note: If the service provider is using the file-based template endpoint catalog, you get an empty service catalog. For example, you see "serviceCatalog": {} in the response when using a Devstack installation.

#!/usr/bin/python

import httplib
import json

# arguments

## change to False when you are using the test environment

usehttps = True

## make sure that url is set to the actual hostname/IP address,
## port number

url = "192.168.10.1:5000"

## make sure that osuser is set to your actual username, "admin"
## works for test installs on virtual machines, but it's a hack

osuser = "joe"

## use something else than "shhh" for your password

ospassword = "shhh"

params = '{auth{"passwordCredentials":{"username":osuser, "password":ospassword}}}'

headers = {"Content-Type": "application/json"}

# HTTP connection

if (usehttps == True):
    conn = httplib.HTTPSConnection(url, key_file='../cert/priv.pem', cert_file='../cert/srv_test.crt')
else:
    conn = httplib.HTTPConnection(url)

conn.request("POST", "/v2.0/tokens", params, headers)

# HTTP response

response = conn.getresponse()
data = response.read()
dd = json.loads(data)

conn.close()

apitoken = dd['auth']['token']['id']
apiurl = dd['auth']['serviceCatalog']['nova'][0]['publicURL']

print "Your token is: %s" % apitoken
print "Your Nova URL is: %s" % apiurl

When you run that script, you should see the following output. The token will most likely be something different than 999888777666 and the Nova URL will not always contain 10.0.2.15:

$ ./gettokenurl.py 
Your token is: 999888777666
Your Nova URL is: http://10.0.2.15:8774/v1.1/openstack


loading table of contents...