Client library for OpenStack containing Infra business logic
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import warnings
  16. import keystoneauth1.exceptions
  17. import os_client_config
  18. import pbr.version
  19. import requestsexceptions
  20. # The star import is for backwards compat reasons
  21. from shade.exc import * # noqa
  22. from shade import exc
  23. from shade.openstackcloud import OpenStackCloud
  24. from shade.operatorcloud import OperatorCloud
  25. from shade import _log
  26. __version__ = pbr.version.VersionInfo('shade').version_string()
  27. if requestsexceptions.SubjectAltNameWarning:
  28. warnings.filterwarnings(
  29. 'ignore', category=requestsexceptions.SubjectAltNameWarning)
  30. def _get_openstack_config(app_name=None, app_version=None):
  31. # Protect against older versions of os-client-config that don't expose this
  32. try:
  33. return os_client_config.OpenStackConfig(
  34. app_name=app_name, app_version=app_version)
  35. except Exception:
  36. return os_client_config.OpenStackConfig()
  37. def simple_logging(debug=False, http_debug=False):
  38. if http_debug:
  39. debug = True
  40. if debug:
  41. log_level = logging.DEBUG
  42. else:
  43. log_level = logging.INFO
  44. if http_debug:
  45. # Enable HTTP level tracing
  46. log = _log.setup_logging('keystoneauth')
  47. log.addHandler(logging.StreamHandler())
  48. log.setLevel(log_level)
  49. # We only want extra shade HTTP tracing in http debug mode
  50. log = _log.setup_logging('shade.http')
  51. log.setLevel(log_level)
  52. else:
  53. # We only want extra shade HTTP tracing in http debug mode
  54. log = _log.setup_logging('shade.http')
  55. log.setLevel(logging.WARNING)
  56. # Simple case - we only care about request id log during debug
  57. log = _log.setup_logging('shade.request_ids')
  58. log.setLevel(log_level)
  59. log = _log.setup_logging('shade')
  60. log.addHandler(logging.StreamHandler())
  61. log.setLevel(log_level)
  62. # Suppress warning about keystoneauth loggers
  63. log = _log.setup_logging('keystoneauth.identity.base')
  64. log = _log.setup_logging('keystoneauth.identity.generic.base')
  65. def openstack_clouds(
  66. config=None, debug=False, cloud=None, strict=False,
  67. app_name=None, app_version=None, use_direct_get=False):
  68. if not config:
  69. config = _get_openstack_config(app_name, app_version)
  70. try:
  71. if cloud is None:
  72. return [
  73. OpenStackCloud(
  74. cloud=f.name, debug=debug,
  75. cloud_config=f,
  76. strict=strict,
  77. use_direct_get=use_direct_get,
  78. **f.config)
  79. for f in config.get_all_clouds()
  80. ]
  81. else:
  82. return [
  83. OpenStackCloud(
  84. cloud=f.name, debug=debug,
  85. cloud_config=f,
  86. strict=strict,
  87. use_direct_get=use_direct_get,
  88. **f.config)
  89. for f in config.get_all_clouds()
  90. if f.name == cloud
  91. ]
  92. except keystoneauth1.exceptions.auth_plugins.NoMatchingPlugin as e:
  93. raise exc.OpenStackCloudException(
  94. "Invalid cloud configuration: {exc}".format(exc=str(e)))
  95. def openstack_cloud(
  96. config=None, strict=False, app_name=None, app_version=None,
  97. use_direct_get=False, **kwargs):
  98. if not config:
  99. config = _get_openstack_config(app_name, app_version)
  100. try:
  101. cloud_config = config.get_one_cloud(**kwargs)
  102. except keystoneauth1.exceptions.auth_plugins.NoMatchingPlugin as e:
  103. raise exc.OpenStackCloudException(
  104. "Invalid cloud configuration: {exc}".format(exc=str(e)))
  105. return OpenStackCloud(
  106. cloud_config=cloud_config, strict=strict,
  107. use_direct_get=use_direct_get)
  108. def operator_cloud(
  109. config=None, strict=False, app_name=None, app_version=None,
  110. use_direct_get=False, **kwargs):
  111. if not config:
  112. config = _get_openstack_config(app_name, app_version)
  113. try:
  114. cloud_config = config.get_one_cloud(**kwargs)
  115. except keystoneauth1.exceptions.auth_plugins.NoMatchingPlugin as e:
  116. raise exc.OpenStackCloudException(
  117. "Invalid cloud configuration: {exc}".format(exc=str(e)))
  118. return OperatorCloud(
  119. cloud_config=cloud_config, strict=strict,
  120. use_direct_get=use_direct_get)