John Vrbanac 2a4fb02bb3 Replacing functional test authentication hookup
This change, replaces the authentication wrapper for our functional test
calls with a simple wrapper using Keystone client. As a result, this
change removes our dependence on Tempest trunk to run our functional
tests. Unfortunately, this was done primarally due to the uncompability
between Tempest's oslo.log and the oslo_log that we use in Barbican that
was causing our gates to fail and blocking merges across the project.

Change-Id: I0eee6a34d1ab5ca654e737d95c1e124465dc9c14
2015-03-11 02:39:33 -05:00

94 lines
2.9 KiB
Python

"""
Copyright 2014-2015 Rackspace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import json
import logging
import six
LOG = logging.getLogger(__name__)
class BaseModel(object):
"""Base class for models.
To allow simple (de)serialization we will use __dict__ to create
"""
def override_values(self, **kwargs):
for attr_name, attr_value in kwargs.items():
if hasattr(self, attr_name):
setattr(self, attr_name, attr_value)
def obj_to_json(self):
"""Convert this object to a JSON string.
:return A string of JSON containing the fields in this object
"""
return json.dumps(self.obj_to_dict())
def obj_to_dict(self):
"""Create a dict of the values for this model object.
If there are fields that are not set in this object then those
will NOT have entries in the returned dict.
:return A dict representing this model
"""
the_dict = self.__dict__
retval = self._remove_empty_fields_from_dict(the_dict)
return retval
def _remove_empty_fields_from_dict(self, dictionary):
"""Remove k,v pairs with empty values from a dictionary.
:param dictionary: a dictionary of stuff
:return: the same dictionary where all k,v pairs with empty values
have been removed.
"""
# NOTE(jaosorior): deleting a key from the incoming dictionary actually
# affects the model object. So we do a copy to avoid this.
resulting_dict = dictionary.copy()
# Dumping the keys to a list as we'll be changing the dict size
empty_keys = [k for k, v in six.iteritems(dictionary) if v is None]
for k in empty_keys:
del resulting_dict[k]
return resulting_dict
@classmethod
def json_to_obj(cls, serialized_str):
"""Create a model from a JSON string.
:param serialized_str: the JSON string
:return a secret object
"""
try:
json_dict = json.loads(serialized_str)
return cls.dict_to_obj(json_dict)
except TypeError as e:
LOG.error('Couldn\'t deserialize input: %s\n Because: %s',
serialized_str, e)
@classmethod
def dict_to_obj(cls, input_dict):
"""Create an object from a dict.
:param input_dict: A dict of fields.
:return a model object build from the passed in dict.
"""
return cls(**input_dict)