fork(1) download
  1. from six.moves.urllib import parse
  2. import keystoneclient
  3. from keystoneclient import client
  4. from rally import osclients
  5.  
  6. def _get_session(auth_url=None, version=None):
  7. from keystoneauth1 import discover
  8. from keystoneauth1 import session
  9. from keystoneclient.auth import identity
  10.  
  11. password_args = {
  12. "auth_url": "http://keystone:35357/v3",
  13. "username": "admin",
  14. "password": "foobar",
  15. "tenant_name": "admin"
  16. }
  17.  
  18. credential = {
  19. "auth_url": "http://keystone:35357/v3",
  20. "username": "admin",
  21. "password": "foobar",
  22. "tenant_name": "admin",
  23. "permission": "user",
  24. "region_name": None, "endpoint_type": None,
  25. "domain_name": "admin", "endpoint": None,
  26. "user_domain_name": None, "admin_domain_name": "Default",
  27. "project_domain_name": None,
  28. "https_insecure": False, "https_cacert": None,
  29. }
  30.  
  31. version = osclients.OSClient.get("keystone")(
  32. credential, {}, {}).choose_version(version)
  33. if version is None:
  34. # NOTE(rvasilets): If version not specified than we discover
  35. # available version with the smallest number. To be able to
  36. # discover versions we need session
  37. temp_session = session.Session(
  38. verify=True, timeout=10.0)
  39. version = str(discover.Discover(
  40. temp_session,
  41. "http://keystone:35357/v3").version_data()[0]["version"][0])
  42.  
  43. if "v2.0" not in password_args["auth_url"] and (
  44. version != "2"):
  45. password_args.update({
  46. "user_domain_name": None,
  47. "domain_name": "admin",
  48. "project_domain_name": None,
  49. })
  50. identity_plugin = identity.Password(**password_args)
  51. sess = session.Session(
  52. auth=identity_plugin, verify=True, timeout=10.0)
  53.  
  54. return sess, identity_plugin
  55.  
  56. def _remove_url_version():
  57. """Remove any version from the auth_url.
  58. The keystone Client code requires that auth_url be the root url
  59. if a version override is used.
  60. """
  61. url = parse.urlparse("http://keystone:35357/v3")
  62. # NOTE(bigjools): This assumes that non-versioned URLs have no
  63. # path component at all.
  64. parts = (url.scheme, url.netloc, "/", url.params, url.query,
  65. url.fragment)
  66. return parse.urlunparse(parts)
  67.  
  68. version = 3
  69.  
  70. auth_url = "http://keystone:35357/v3"
  71. if version is not None:
  72. auth_url = _remove_url_version()
  73. sess, plugin = _get_session(auth_url=auth_url)
  74. # NOTE(bigjools): When using sessions, keystoneclient no longer
  75. # does any pre-auth and calling client.authenticate() with
  76. # sessions is deprecated (it's still possible to call it but if
  77. # endpoint is defined it'll crash). We're forcing that pre-auth
  78. # here because the use of the service_catalog depends on doing
  79. # this. Also note that while the API has got the
  80. # endpoints.list() equivalent, there is no service_type in that
  81. # list which is why we need to ensure service_catalog is still
  82. # present.
  83. auth_ref = plugin.get_access(sess)
  84. print auth_ref
Runtime error #stdin #stdout #stderr 0.01s 9128KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 2, in <module>
ImportError: No module named keystoneclient