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