Getting Your Nova Credentials -- The Drop-In Script Snippet for Your Python Scripts

You will be coming back to the token and API URL extraction code over and over again, which is why I decided to place it in a separate section of this book. That way I can make the examples shorter and you can focus of the important stuff. Here's the code in all it's glory:

### --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--

### Get OpenStack Credentials

# arguments

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

usehttps = True

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

url1 = "localhost: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"

params1 = '{auth{"passwordCredentials":{"username":osuser, "password":ospassword}}}'
headers1 = {"Content-Type": "application/json"}

# HTTP connection #1

if (usehttps == True):
    # set key_file and cert_file to wherever the key and cert files
    # are located
    conn1 = httplib.HTTPSConnection(url1, key_file='../cert/priv.pem', cert_file='../cert/srv_test.crt')
else:
    conn1 = httplib.HTTPConnection(url1)

conn1.request("POST", "/v2.0/tokens", params1, headers1)

# HTTP response #1

response1 = conn1.getresponse()
data1 = response1.read()
dd1 = json.loads(data1)

conn1.close()

# extract token and url

apitoken = dd1['auth']['token']['id']
apiurl = dd1['auth']['serviceCatalog']['nova'][0]['publicURL']
apiurlt = urlparse(dd1['auth']['serviceCatalog']['nova'][0]['publicURL'])

### --8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--


loading table of contents...