2

I am editing a script, which authenticates to openstack keystone to get a token. The API-Call works but I want to use variables instead of the direct values to make it more readable und reusable. But the problem is that it is necessary for the values to be in quotes (") and I can't figure out how to do that. I found some code examples [1], [2] to use variables in strings in general but I need to use some kind of escape sequence to place the values in quotes.

At the moment my string assignment looks like this:

params = '{"auth":{"passwordCredentials":{"username":"nodermatt", "password":"feelfree"}, "tenantId":"4"}}'

As you can see the values of username, password and tenantID are in quotes and I'd like it to replace them with variables:

osuser = "nodermatt"
ospassword = "feelfree"
ostenant = "4"

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

I'd be very glad if could solve this "issue".

PS: If I missed a thread or a search result on google that matches my question, I'd be very gratefull for the link.

Thanks in advance! Best regards, Nicolas

[1] Subprocess in Python Add Variables

[2] http://ubuntuforums.org/showthread.php?t=786879

4 Answers 4

8

The easiest solution for this particular case is to define a Python dictionary first, and then use json.dumps() to convert it to JSON:

osuser = "nodermatt"
ospassword = "feelfree"
ostenant = "4"

d = {"auth": 
        {"passwordCredentials": {"username": osuser, "password": ospassword},
         "tenantId": ostenant}}
params = json.dumps(d)
Sign up to request clarification or add additional context in comments.

1 Comment

+1: For conneting the dots that the OP is actually trying to build a valid json string.
2

Have you tried using a format string?

params = '{"auth":{"passwordCredentials":{"username":%s, "password":%s}, "tenantId":%s}}' % (osuser, ospassword, ostenant)

Comments

1

There's a python library here: https://github.com/openstack/python-keystoneclient

It uses JSON encoding as Sven suggested.

From the README:

# use v2.0 auth with http://example.com:5000/v2.0")
>>> from keystoneclient.v2_0 import client
>>> keystone = client.Client(username=USERNAME, password=PASSWORD, tenant_name=TENANT, auth_url=KEYSTONE_URL)
>>> keystone.tenants.list()
>>> tenant = keystone.tenants.create(name="test", descrption="My new tenant!", enabled=True)
>>> tenant.delete()

Comments

1

A format string is what you're looking for, like this:

'{"username": "%s", "password":"%s"}' % (osuser, ospassword)

If you have lots of variables to substitute, you may want to use the extended syntax:

values = {'osuser': "nodermatt",
    'ospassword': "feelfree",
    'ostenant': 4}
params =  '''{"auth":{"passwordCredentials":{"username":"%(osuser)s",
"password":"%(ospassword)s"}, "tenantId":"%(ostenant)d"}}''' % values

Note that you can simply put quotes in the template string where you want them to appear. I made ostenant into an integer as an example, but you could also treat it as a string (both in the dict and in the template string).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.