Advertisement
Guest User

Untitled

a guest
Jul 8th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. # Copyright 2011 OpenStack Foundation
  2. # Copyright 2010 United States Government as represented by the
  3. # Administrator of the National Aeronautics and Space Administration.
  4. # All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  7. # not use this file except in compliance with the License. You may obtain
  8. # a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. # License for the specific language governing permissions and limitations
  16. # under the License.
  17.  
  18. """RequestContext: context for requests that persist through all of Watcher."""
  19.  
  20. import copy
  21.  
  22. from oslo_context import context
  23. from oslo_log import log as logging
  24. from oslo_utils import timeutils
  25. import six
  26.  
  27. from watcher._i18n import _LW
  28. from watcher.common import exception
  29. from watcher.common import utils
  30. # from watcher import policy
  31.  
  32. LOG = logging.getLogger(__name__)
  33.  
  34.  
  35. class RequestContext(context.RequestContext):
  36. """Security context and request information.
  37.  
  38. Represents the user taking a given action within the system.
  39. """
  40.  
  41. def __init__(self, user_id=None, project_id=None, is_admin=None,
  42. roles=None, timestamp=None, request_id=None, auth_token=None,
  43. auth_url=None, overwrite=True, user_name=None,
  44. project_name=None, domain_name=None, domain_id=None,
  45. auth_token_info=None, **kwargs):
  46. """Request Context
  47.  
  48. :param kwargs: Extra arguments that might be present, but we ignore
  49. because they possibly came in from older rpc messages.
  50. """
  51. user = kwargs.pop('user', None)
  52. tenant = kwargs.pop('tenant', None)
  53. super(RequestContext, self).__init__(
  54. auth_token=auth_token,
  55. user=user_id or user,
  56. tenant=project_id or tenant,
  57. domain=kwargs.pop('domain', None) or domain_name or domain_id,
  58. user_domain=kwargs.pop('user_domain', None),
  59. project_domain=kwargs.pop('project_domain', None),
  60. is_admin=is_admin,
  61. read_only=kwargs.pop('read_only', False),
  62. show_deleted=kwargs.pop('show_deleted', False),
  63. request_id=request_id,
  64. resource_uuid=kwargs.pop('resource_uuid', None),
  65. overwrite=overwrite,
  66. roles=roles)
  67. # oslo_context's RequestContext.to_dict() generates this field, we can
  68. # safely ignore this as we don't use it.
  69. kwargs.pop('user_identity', None)
  70. if kwargs:
  71. LOG.warning(_LW('Arguments dropped when creating context: %s'),
  72. str(kwargs))
  73.  
  74. # FIXME(dims): user_id and project_id duplicate information that is
  75. # already present in the oslo_context's RequestContext. We need to
  76. # get rid of them.
  77. self.auth_url = auth_url
  78. self.domain_name = domain_name
  79. self.domain_id = domain_id
  80. self.auth_token_info = auth_token_info
  81. self.user_id = user_id
  82. self.project_id = project_id
  83. if not timestamp:
  84. timestamp = timeutils.utcnow()
  85. if isinstance(timestamp, six.string_types):
  86. timestamp = timeutils.parse_strtime(timestamp)
  87. self.timestamp = timestamp
  88. self.user_name = user_name
  89. self.project_name = project_name
  90. self.is_admin = is_admin
  91. # if self.is_admin is None:
  92. # self.is_admin = policy.check_is_admin(self)
  93.  
  94. def to_dict(self):
  95. values = super(RequestContext, self).to_dict()
  96. # FIXME(dims): defensive hasattr() checks need to be
  97. # removed once we figure out why we are seeing stack
  98. # traces
  99. values.update({
  100. 'user_id': getattr(self, 'user_id', None),
  101. 'user_name': getattr(self, 'user_name', None),
  102. 'project_id': getattr(self, 'project_id', None),
  103. 'project_name': getattr(self, 'project_name', None),
  104. 'domain_id': getattr(self, 'domain_id', None),
  105. 'domain_name': getattr(self, 'domain_name', None),
  106. 'auth_token_info': getattr(self, 'auth_token_info', None),
  107. 'is_admin': getattr(self, 'is_admin', None),
  108. 'timestamp': utils.strtime(self.timestamp) if hasattr(
  109. self, 'timestamp') else None,
  110. 'request_id': getattr(self, 'request_id', None),
  111. 'instance_lock_checked': getattr(self, 'instance_lock_checked',
  112. False)
  113. })
  114. return values
  115.  
  116. @classmethod
  117. def from_dict(cls, values):
  118. return cls(**values)
  119.  
  120. def __str__(self):
  121. return "<Context %s>" % self.to_dict()
  122.  
  123.  
  124. def make_context(*args, **kwargs):
  125. return RequestContext(*args, **kwargs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement