diff options
author | Trevor McKay <tmckay@redhat.com> | 2014-03-13 13:36:48 -0400 |
---|---|---|
committer | Trevor McKay <tmckay@redhat.com> | 2014-03-13 18:23:50 -0400 |
commit | 6b014ee3650e1d616195c5816f224e9d481c9746 (patch) | |
tree | f746305dbaaf55236dc6552d1e8f45f4453839bb /saharaclient/api/base.py | |
parent | 38882f71987cb6be80a660b51c2ff0a31c2eacb1 (diff) | |
download | python-saharaclient-6b014ee3650e1d616195c5816f224e9d481c9746.tar.gz |
Swap the saharaclient and savannaclient directories
Exchange the contents of the saharaclient and savannaclient directories
so that 'savannlient' is the effective alias and 'saharaclient' contains
all of the source code.
* Change all of the imports that reference savannaclient to saharaclient
* Change paths that reference savannaclient (under doc)
* Leave variable and class names unchaned at this point
* Leave references to python_savannaclient unchanged since the top
* level dir name has not yet changed
Partial-implements: blueprint savanna-renaming-client
Change-Id: I83cb21922ae5a8cec291990b3ab67bb9e3cb2d62
Diffstat (limited to 'saharaclient/api/base.py')
-rw-r--r-- | saharaclient/api/base.py | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/saharaclient/api/base.py b/saharaclient/api/base.py new file mode 100644 index 0000000..cf4c493 --- /dev/null +++ b/saharaclient/api/base.py @@ -0,0 +1,155 @@ +# Copyright (c) 2013 Mirantis Inc. +# +# 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 Resource(object): + resource_name = 'Something' + defaults = {} + + def __init__(self, manager, info): + self.manager = manager + info = info.copy() + self._info = info + self._set_defaults(info) + self._add_details(info) + + def _set_defaults(self, info): + for name, value in six.iteritems(self.defaults): + if name not in info: + info[name] = value + + def _add_details(self, info): + for (k, v) in six.iteritems(info): + try: + setattr(self, k, v) + self._info[k] = v + except AttributeError: + # In this case we already defined the attribute on the class + pass + + def __str__(self): + return '%s %s' % (self.resource_name, str(self._info)) + + +def _check_items(obj, searches): + try: + return all(getattr(obj, attr) == value for (attr, value) in searches) + except AttributeError: + return False + + +class ResourceManager(object): + resource_class = None + + def __init__(self, api): + self.api = api + + def find(self, **kwargs): + return [i for i in self.list() if _check_items(i, kwargs.items())] + + def _copy_if_defined(self, data, **kwargs): + for var_name, var_value in six.iteritems(kwargs): + if var_value is not None: + data[var_name] = var_value + + def _create(self, url, data, response_key=None, dump_json=True): + if dump_json: + data = json.dumps(data) + resp = self.api.client.post(url, data) + + if resp.status_code != 202: + self._raise_api_exception(resp) + + if response_key is not None: + data = get_json(resp)[response_key] + else: + data = get_json(resp) + return self.resource_class(self, data) + + def _update(self, url, data, response_key=None, dump_json=True): + if dump_json: + data = json.dumps(data) + resp = self.api.client.put(url, data) + + if resp.status_code != 202: + self._raise_api_exception(resp) + if response_key is not None: + data = get_json(resp)[response_key] + else: + data = get_json(resp) + return self.resource_class(self, data) + + def _list(self, url, response_key): + resp = self.api.client.get(url) + if resp.status_code == 200: + data = get_json(resp)[response_key] + + return [self.resource_class(self, res) + for res in data] + else: + self._raise_api_exception(resp) + + def _get(self, url, response_key=None): + resp = self.api.client.get(url) + + if resp.status_code == 200: + if response_key is not None: + data = get_json(resp)[response_key] + else: + data = get_json(resp) + return self.resource_class(self, data) + else: + self._raise_api_exception(resp) + + def _delete(self, url): + resp = self.api.client.delete(url) + + if resp.status_code != 204: + self._raise_api_exception(resp) + + def _plurify_resource_name(self): + return self.resource_class.resource_name + 's' + + def _raise_api_exception(self, resp): + error_data = get_json(resp) + raise APIException(error_code=error_data.get("error_code"), + error_name=error_data.get("error_name"), + error_message=error_data.get("error_message")) + + +def get_json(response): + """This method provided backward compatibility with old versions + of requests library + + """ + json_field_or_function = getattr(response, 'json', None) + if callable(json_field_or_function): + return response.json() + else: + return json.loads(response.content) + + +class APIException(Exception): + def __init__(self, error_code=None, error_name=None, error_message=None): + super(APIException, self).__init__(error_message) + self.error_code = error_code + self.error_name = error_name + self.error_message = error_message |