我想对 openstack keystone 用户执行 CRUD 操作。根据 keystone api docs,我们必须使用 GET 请求以及 x-auth-token 信息。我为此编写了一个代码,但我无法获得任何用户列表,而是得到“404 Not Found,The resource could not be found”。我的代码如下
#!/usr/bin/python
import httplib
import urllib
import os
import json
from urlparse import urlparse
#Define openstack url
url = "x.x.x.x:5000"
osuser = "osuser"
ospassword = "whatever"
params ='{"auth":{"tenantName":"openstackDemo", "passwordCredentials":{"username":"osuser", "password": "ospassword"}}}'
headers = {"Content-Type": "application/json"}
#make http request
conn = httplib.HTTPConnection(url)
conn.request("POST", "/v2.0/tokens", params,headers)
#get http response
response = conn.getresponse()
data = response.read()
verify_services = json.loads(data)
conn.close()
#print "The service verificetion is:%s\n\t" % verify_services
#Get keystone token
keystone_token = verify_services ['access']['token']['id']
print "Printing api token :\n"
print keystone_token
user_id = verify_services['access']['user']['id']
print "Printing user id : \n"
print user_id
# Get keystone URL
keystone_url = verify_services ['access']['serviceCatalog'][5]
print "Printing keystone end points url: \n"
print keystone_url
#Now take the keystone public url and uid
for publicurl in keystone_url['endpoints']:
key_admin_url = publicurl['adminURL']
key_pub_url = publicurl['publicURL']
keystone_user_id = publicurl['id']
print "printing keystone public url:\n"
print key_pub_url
print "Printing keystone user id:\n"
print keystone_user_id
##################
# Get the user list
#################
apiurlt = urlparse(key_pub_url)
print apiurlt
url2 = apiurlt[1]
print url2
#params1 = '{"username":"samit", "email":"sanjaya@kth.se","enabled":true,"password":"secret", "roles":"member"}'
p = urllib.urlencode({})
headers1 = {"X-Auth-Token":"keystone_token", "Content-type":"application/json"}
conn2 = httplib.HTTPConnection(url2)
conn2.request("GET", "%s/users" %apiurlt[2] , p,headers1)
response2 = conn2.getresponse()
data2 = response2.read()
user_list = json.dumps(data2)
conn.close()
print "getting users:\n"
print response.status
print response.reason
print user_list