Sync apiclient from Oslo

Sync'ing apiclient from Oslo to fix the "'unicode' object has no attribute
'get'" problem.

Closes-Bug: #1386470
Change-Id: I46233c04a95c6441ae86572915f4492c6c1a48c5
This commit is contained in:
Lucas Alvares Gomes 2014-10-30 14:29:58 +00:00
parent f7b7913c0f
commit 8d7a2e4f32
9 changed files with 257 additions and 104 deletions

View File

@ -1,16 +0,0 @@
# -*- coding: utf-8 -*-
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
# 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.

View File

@ -0,0 +1,40 @@
# 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.
"""oslo.i18n integration module.
See http://docs.openstack.org/developer/oslo.i18n/usage.html
"""
import oslo.i18n
# NOTE(dhellmann): This reference to o-s-l-o will be replaced by the
# application name when this module is synced into the separate
# repository. It is OK to have more than one translation function
# using the same domain, since there will still only be one message
# catalog.
_translators = oslo.i18n.TranslatorFactory(domain='ironicclient')
# The primary translation function using the well-known name "_"
_ = _translators.primary
# Translators for log levels.
#
# The abbreviated names are meant to reflect the usual use of a short
# name like '_'. The "L" is for "log" and the other letter comes from
# the level.
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error
_LC = _translators.log_critical

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2013 OpenStack Foundation
# Copyright 2013 Spanish National Research Council.
# All Rights Reserved.
@ -215,8 +213,8 @@ class BaseAuthPlugin(object):
:type service_type: string
:param endpoint_type: Type of endpoint.
Possible values: public or publicURL,
internal or internalURL,
admin or adminURL
internal or internalURL,
admin or adminURL
:type endpoint_type: string
:returns: tuple of token and endpoint strings
:raises: EndpointException

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2010 Jacob Kaplan-Moss
# Copyright 2011 OpenStack Foundation
# Copyright 2012 Grid Dynamics
@ -28,11 +26,12 @@ Base utilities to build API operation managers and objects on top of.
import abc
import copy
from oslo.utils import strutils
import six
from six.moves.urllib import parse
from ironicclient.openstack.common._i18n import _
from ironicclient.openstack.common.apiclient import exceptions
from ironicclient.openstack.common import strutils
def getid(obj):
@ -76,8 +75,8 @@ class HookableMixin(object):
:param cls: class that registers hooks
:param hook_type: hook type, e.g., '__pre_parse_args__'
:param **args: args to be passed to every hook function
:param **kwargs: kwargs to be passed to every hook function
:param args: args to be passed to every hook function
:param kwargs: kwargs to be passed to every hook function
"""
hook_funcs = cls._hooks_map.get(hook_type) or []
for hook_func in hook_funcs:
@ -100,12 +99,13 @@ class BaseManager(HookableMixin):
super(BaseManager, self).__init__()
self.client = client
def _list(self, url, response_key, obj_class=None, json=None):
def _list(self, url, response_key=None, obj_class=None, json=None):
"""List the collection.
:param url: a partial URL, e.g., '/servers'
:param response_key: the key to be looked up in response dictionary,
e.g., 'servers'
e.g., 'servers'. If response_key is None - all response body
will be used.
:param obj_class: class for constructing the returned objects
(self.resource_class will be used by default)
:param json: data that will be encoded as JSON and passed in POST
@ -119,7 +119,7 @@ class BaseManager(HookableMixin):
if obj_class is None:
obj_class = self.resource_class
data = body[response_key]
data = body[response_key] if response_key is not None else body
# NOTE(ja): keystone returns values as list as {'values': [ ... ]}
# unlike other services which just return the list...
try:
@ -129,15 +129,17 @@ class BaseManager(HookableMixin):
return [obj_class(self, res, loaded=True) for res in data if res]
def _get(self, url, response_key):
def _get(self, url, response_key=None):
"""Get an object from collection.
:param url: a partial URL, e.g., '/servers'
:param response_key: the key to be looked up in response dictionary,
e.g., 'server'
e.g., 'server'. If response_key is None - all response body
will be used.
"""
body = self.client.get(url).json()
return self.resource_class(self, body[response_key], loaded=True)
data = body[response_key] if response_key is not None else body
return self.resource_class(self, data, loaded=True)
def _head(self, url):
"""Retrieve request headers for an object.
@ -147,21 +149,23 @@ class BaseManager(HookableMixin):
resp = self.client.head(url)
return resp.status_code == 204
def _post(self, url, json, response_key, return_raw=False):
def _post(self, url, json, response_key=None, return_raw=False):
"""Create an object.
:param url: a partial URL, e.g., '/servers'
:param json: data that will be encoded as JSON and passed in POST
request (GET will be sent by default)
:param response_key: the key to be looked up in response dictionary,
e.g., 'servers'
e.g., 'server'. If response_key is None - all response body
will be used.
:param return_raw: flag to force returning raw JSON instead of
Python object of self.resource_class
"""
body = self.client.post(url, json=json).json()
data = body[response_key] if response_key is not None else body
if return_raw:
return body[response_key]
return self.resource_class(self, body[response_key])
return data
return self.resource_class(self, data)
def _put(self, url, json=None, response_key=None):
"""Update an object with PUT method.
@ -170,7 +174,8 @@ class BaseManager(HookableMixin):
:param json: data that will be encoded as JSON and passed in POST
request (GET will be sent by default)
:param response_key: the key to be looked up in response dictionary,
e.g., 'servers'
e.g., 'servers'. If response_key is None - all response body
will be used.
"""
resp = self.client.put(url, json=json)
# PUT requests may not return a body
@ -188,7 +193,8 @@ class BaseManager(HookableMixin):
:param json: data that will be encoded as JSON and passed in POST
request (GET will be sent by default)
:param response_key: the key to be looked up in response dictionary,
e.g., 'servers'
e.g., 'servers'. If response_key is None - all response body
will be used.
"""
body = self.client.patch(url, json=json).json()
if response_key is not None:
@ -221,7 +227,10 @@ class ManagerWithFind(BaseManager):
matches = self.findall(**kwargs)
num_matches = len(matches)
if num_matches == 0:
msg = "No %s matching %s." % (self.resource_class.__name__, kwargs)
msg = _("No %(name)s matching %(args)s.") % {
'name': self.resource_class.__name__,
'args': kwargs
}
raise exceptions.NotFound(msg)
elif num_matches > 1:
raise exceptions.NoUniqueMatch()
@ -375,7 +384,10 @@ class CrudManager(BaseManager):
num = len(rl)
if num == 0:
msg = "No %s matching %s." % (self.resource_class.__name__, kwargs)
msg = _("No %(name)s matching %(args)s.") % {
'name': self.resource_class.__name__,
'args': kwargs
}
raise exceptions.NotFound(404, msg)
elif num > 1:
raise exceptions.NoUniqueMatch
@ -443,8 +455,10 @@ class Resource(object):
def human_id(self):
"""Human-readable ID which can be used for bash completion.
"""
if self.NAME_ATTR in self.__dict__ and self.HUMAN_ID:
return strutils.to_slug(getattr(self, self.NAME_ATTR))
if self.HUMAN_ID:
name = getattr(self, self.NAME_ATTR, None)
if name is not None:
return strutils.to_slug(name)
return None
def _add_details(self, info):
@ -458,7 +472,7 @@ class Resource(object):
def __getattr__(self, k):
if k not in self.__dict__:
#NOTE(bcwaldon): disallow lazy-loading if already loaded once
# NOTE(bcwaldon): disallow lazy-loading if already loaded once
if not self.is_loaded():
self.get()
return self.__getattr__(k)
@ -481,6 +495,8 @@ class Resource(object):
new = self.manager.get(self.id)
if new:
self._add_details(new._info)
self._add_details(
{'x_request_id': self.manager.client.last_request_id})
def __eq__(self, other):
if not isinstance(other, Resource):

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2010 Jacob Kaplan-Moss
# Copyright 2011 OpenStack Foundation
# Copyright 2011 Piston Cloud Computing, Inc.
@ -27,6 +25,7 @@ OpenStack Client interface. Handles the REST calls and responses.
# E0202: An attribute inherited from %s hide this method
# pylint: disable=E0202
import hashlib
import logging
import time
@ -35,19 +34,22 @@ try:
except ImportError:
import json
from oslo.utils import encodeutils
from oslo.utils import importutils
import requests
from ironicclient.openstack.common._i18n import _
from ironicclient.openstack.common.apiclient import exceptions
from ironicclient.openstack.common import importutils
_logger = logging.getLogger(__name__)
SENSITIVE_HEADERS = ('X-Auth-Token', 'X-Subject-Token',)
class HTTPClient(object):
"""This client handles sending HTTP requests to OpenStack servers.
Features:
- share authentication information between several clients to different
services (e.g., for compute and image clients);
- reissue authentication request for expired tokens;
@ -98,6 +100,18 @@ class HTTPClient(object):
self.http = http or requests.Session()
self.cached_token = None
self.last_request_id = None
def _safe_header(self, name, value):
if name in SENSITIVE_HEADERS:
# because in python3 byte string handling is ... ug
v = value.encode('utf-8')
h = hashlib.sha1(v)
d = h.hexdigest()
return encodeutils.safe_decode(name), "{SHA1}%s" % d
else:
return (encodeutils.safe_decode(name),
encodeutils.safe_decode(value))
def _http_log_req(self, method, url, kwargs):
if not self.debug:
@ -110,7 +124,8 @@ class HTTPClient(object):
]
for element in kwargs['headers']:
header = "-H '%s: %s'" % (element, kwargs['headers'][element])
header = ("-H '%s: %s'" %
self._safe_header(element, kwargs['headers'][element]))
string_parts.append(header)
_logger.debug("REQ: %s" % " ".join(string_parts))
@ -153,10 +168,10 @@ class HTTPClient(object):
:param method: method of HTTP request
:param url: URL of HTTP request
:param kwargs: any other parameter that can be passed to
' requests.Session.request (such as `headers`) or `json`
requests.Session.request (such as `headers`) or `json`
that will be encoded as JSON and used as `data` argument
"""
kwargs.setdefault("headers", kwargs.get("headers", {}))
kwargs.setdefault("headers", {})
kwargs["headers"]["User-Agent"] = self.user_agent
if self.original_ip:
kwargs["headers"]["Forwarded"] = "for=%s;by=%s" % (
@ -177,6 +192,8 @@ class HTTPClient(object):
start_time, time.time()))
self._http_log_resp(resp)
self.last_request_id = resp.headers.get('x-openstack-request-id')
if resp.status_code >= 400:
_logger.debug(
"Request returned failure status: %s",
@ -208,7 +225,7 @@ class HTTPClient(object):
:param method: method of HTTP request
:param url: URL of HTTP request
:param kwargs: any other parameter that can be passed to
' `HTTPClient.request`
`HTTPClient.request`
"""
filter_args = {
@ -230,7 +247,7 @@ class HTTPClient(object):
**filter_args)
if not (token and endpoint):
raise exceptions.AuthorizationFailure(
"Cannot find endpoint or token for request")
_("Cannot find endpoint or token for request"))
old_token_endpoint = (token, endpoint)
kwargs.setdefault("headers", {})["X-Auth-Token"] = token
@ -247,6 +264,10 @@ class HTTPClient(object):
raise
self.cached_token = None
client.cached_endpoint = None
if self.auth_plugin.opts.get('token'):
self.auth_plugin.opts['token'] = None
if self.auth_plugin.opts.get('endpoint'):
self.auth_plugin.opts['endpoint'] = None
self.authenticate()
try:
token, endpoint = self.auth_plugin.token_and_endpoint(
@ -323,6 +344,10 @@ class BaseClient(object):
return self.http_client.client_request(
self, method, url, **kwargs)
@property
def last_request_id(self):
return self.http_client.last_request_id
def head(self, url, **kwargs):
return self.client_request("HEAD", url, **kwargs)
@ -353,8 +378,11 @@ class BaseClient(object):
try:
client_path = version_map[str(version)]
except (KeyError, ValueError):
msg = "Invalid %s client version '%s'. must be one of: %s" % (
(api_name, version, ', '.join(version_map.keys())))
msg = _("Invalid %(api_name)s client version '%(version)s'. "
"Must be one of: %(version_map)s") % {
'api_name': api_name,
'version': version,
'version_map': ', '.join(version_map.keys())}
raise exceptions.UnsupportedVersion(msg)
return importutils.import_class(client_path)

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2010 Jacob Kaplan-Moss
# Copyright 2011 Nebula, Inc.
# Copyright 2013 Alessio Ababilov
@ -27,6 +25,8 @@ import sys
import six
from ironicclient.openstack.common._i18n import _
class ClientException(Exception):
"""The base exception class for all exceptions this library raises.
@ -34,14 +34,6 @@ class ClientException(Exception):
pass
class MissingArgs(ClientException):
"""Supplied arguments are not sufficient for calling a function."""
def __init__(self, missing):
self.missing = missing
msg = "Missing argument(s): %s" % ", ".join(missing)
super(MissingArgs, self).__init__(msg)
class ValidationError(ClientException):
"""Error in validation on API client side."""
pass
@ -71,16 +63,16 @@ class AuthPluginOptionsMissing(AuthorizationFailure):
"""Auth plugin misses some options."""
def __init__(self, opt_names):
super(AuthPluginOptionsMissing, self).__init__(
"Authentication failed. Missing options: %s" %
_("Authentication failed. Missing options: %s") %
", ".join(opt_names))
self.opt_names = opt_names
class AuthSystemNotFound(AuthorizationFailure):
"""User has specified a AuthSystem that is not installed."""
"""User has specified an AuthSystem that is not installed."""
def __init__(self, auth_system):
super(AuthSystemNotFound, self).__init__(
"AuthSystemNotFound: %s" % repr(auth_system))
_("AuthSystemNotFound: %s") % repr(auth_system))
self.auth_system = auth_system
@ -103,7 +95,7 @@ class AmbiguousEndpoints(EndpointException):
"""Found more than one matching endpoint in Service Catalog."""
def __init__(self, endpoints=None):
super(AmbiguousEndpoints, self).__init__(
"AmbiguousEndpoints: %s" % repr(endpoints))
_("AmbiguousEndpoints: %s") % repr(endpoints))
self.endpoints = endpoints
@ -111,7 +103,7 @@ class HttpError(ClientException):
"""The base exception class for all HTTP exceptions.
"""
http_status = 0
message = "HTTP Error"
message = _("HTTP Error")
def __init__(self, message=None, details=None,
response=None, request_id=None,
@ -131,7 +123,7 @@ class HttpError(ClientException):
class HTTPRedirection(HttpError):
"""HTTP Redirection."""
message = "HTTP Redirection"
message = _("HTTP Redirection")
class HTTPClientError(HttpError):
@ -139,7 +131,7 @@ class HTTPClientError(HttpError):
Exception for cases in which the client seems to have erred.
"""
message = "HTTP Client Error"
message = _("HTTP Client Error")
class HttpServerError(HttpError):
@ -148,7 +140,7 @@ class HttpServerError(HttpError):
Exception for cases in which the server is aware that it has
erred or is incapable of performing the request.
"""
message = "HTTP Server Error"
message = _("HTTP Server Error")
class MultipleChoices(HTTPRedirection):
@ -158,7 +150,7 @@ class MultipleChoices(HTTPRedirection):
"""
http_status = 300
message = "Multiple Choices"
message = _("Multiple Choices")
class BadRequest(HTTPClientError):
@ -167,7 +159,7 @@ class BadRequest(HTTPClientError):
The request cannot be fulfilled due to bad syntax.
"""
http_status = 400
message = "Bad Request"
message = _("Bad Request")
class Unauthorized(HTTPClientError):
@ -177,7 +169,7 @@ class Unauthorized(HTTPClientError):
is required and has failed or has not yet been provided.
"""
http_status = 401
message = "Unauthorized"
message = _("Unauthorized")
class PaymentRequired(HTTPClientError):
@ -186,7 +178,7 @@ class PaymentRequired(HTTPClientError):
Reserved for future use.
"""
http_status = 402
message = "Payment Required"
message = _("Payment Required")
class Forbidden(HTTPClientError):
@ -196,7 +188,7 @@ class Forbidden(HTTPClientError):
to it.
"""
http_status = 403
message = "Forbidden"
message = _("Forbidden")
class NotFound(HTTPClientError):
@ -206,7 +198,7 @@ class NotFound(HTTPClientError):
in the future.
"""
http_status = 404
message = "Not Found"
message = _("Not Found")
class MethodNotAllowed(HTTPClientError):
@ -216,7 +208,7 @@ class MethodNotAllowed(HTTPClientError):
by that resource.
"""
http_status = 405
message = "Method Not Allowed"
message = _("Method Not Allowed")
class NotAcceptable(HTTPClientError):
@ -226,7 +218,7 @@ class NotAcceptable(HTTPClientError):
acceptable according to the Accept headers sent in the request.
"""
http_status = 406
message = "Not Acceptable"
message = _("Not Acceptable")
class ProxyAuthenticationRequired(HTTPClientError):
@ -235,7 +227,7 @@ class ProxyAuthenticationRequired(HTTPClientError):
The client must first authenticate itself with the proxy.
"""
http_status = 407
message = "Proxy Authentication Required"
message = _("Proxy Authentication Required")
class RequestTimeout(HTTPClientError):
@ -244,7 +236,7 @@ class RequestTimeout(HTTPClientError):
The server timed out waiting for the request.
"""
http_status = 408
message = "Request Timeout"
message = _("Request Timeout")
class Conflict(HTTPClientError):
@ -254,7 +246,7 @@ class Conflict(HTTPClientError):
in the request, such as an edit conflict.
"""
http_status = 409
message = "Conflict"
message = _("Conflict")
class Gone(HTTPClientError):
@ -264,7 +256,7 @@ class Gone(HTTPClientError):
not be available again.
"""
http_status = 410
message = "Gone"
message = _("Gone")
class LengthRequired(HTTPClientError):
@ -274,7 +266,7 @@ class LengthRequired(HTTPClientError):
required by the requested resource.
"""
http_status = 411
message = "Length Required"
message = _("Length Required")
class PreconditionFailed(HTTPClientError):
@ -284,7 +276,7 @@ class PreconditionFailed(HTTPClientError):
put on the request.
"""
http_status = 412
message = "Precondition Failed"
message = _("Precondition Failed")
class RequestEntityTooLarge(HTTPClientError):
@ -293,7 +285,7 @@ class RequestEntityTooLarge(HTTPClientError):
The request is larger than the server is willing or able to process.
"""
http_status = 413
message = "Request Entity Too Large"
message = _("Request Entity Too Large")
def __init__(self, *args, **kwargs):
try:
@ -310,7 +302,7 @@ class RequestUriTooLong(HTTPClientError):
The URI provided was too long for the server to process.
"""
http_status = 414
message = "Request-URI Too Long"
message = _("Request-URI Too Long")
class UnsupportedMediaType(HTTPClientError):
@ -320,7 +312,7 @@ class UnsupportedMediaType(HTTPClientError):
not support.
"""
http_status = 415
message = "Unsupported Media Type"
message = _("Unsupported Media Type")
class RequestedRangeNotSatisfiable(HTTPClientError):
@ -330,7 +322,7 @@ class RequestedRangeNotSatisfiable(HTTPClientError):
supply that portion.
"""
http_status = 416
message = "Requested Range Not Satisfiable"
message = _("Requested Range Not Satisfiable")
class ExpectationFailed(HTTPClientError):
@ -339,7 +331,7 @@ class ExpectationFailed(HTTPClientError):
The server cannot meet the requirements of the Expect request-header field.
"""
http_status = 417
message = "Expectation Failed"
message = _("Expectation Failed")
class UnprocessableEntity(HTTPClientError):
@ -349,7 +341,7 @@ class UnprocessableEntity(HTTPClientError):
errors.
"""
http_status = 422
message = "Unprocessable Entity"
message = _("Unprocessable Entity")
class InternalServerError(HttpServerError):
@ -358,7 +350,7 @@ class InternalServerError(HttpServerError):
A generic error message, given when no more specific message is suitable.
"""
http_status = 500
message = "Internal Server Error"
message = _("Internal Server Error")
# NotImplemented is a python keyword.
@ -369,7 +361,7 @@ class HttpNotImplemented(HttpServerError):
the ability to fulfill the request.
"""
http_status = 501
message = "Not Implemented"
message = _("Not Implemented")
class BadGateway(HttpServerError):
@ -379,7 +371,7 @@ class BadGateway(HttpServerError):
response from the upstream server.
"""
http_status = 502
message = "Bad Gateway"
message = _("Bad Gateway")
class ServiceUnavailable(HttpServerError):
@ -388,7 +380,7 @@ class ServiceUnavailable(HttpServerError):
The server is currently unavailable.
"""
http_status = 503
message = "Service Unavailable"
message = _("Service Unavailable")
class GatewayTimeout(HttpServerError):
@ -398,7 +390,7 @@ class GatewayTimeout(HttpServerError):
response from the upstream server.
"""
http_status = 504
message = "Gateway Timeout"
message = _("Gateway Timeout")
class HttpVersionNotSupported(HttpServerError):
@ -407,7 +399,7 @@ class HttpVersionNotSupported(HttpServerError):
The server does not support the HTTP protocol version used in the request.
"""
http_status = 505
message = "HTTP Version Not Supported"
message = _("HTTP Version Not Supported")
# _code_map contains all the classes that have http_status attribute.
@ -425,12 +417,17 @@ def from_response(response, method, url):
:param method: HTTP method used for request
:param url: URL used for request
"""
req_id = response.headers.get("x-openstack-request-id")
# NOTE(hdd) true for older versions of nova and cinder
if not req_id:
req_id = response.headers.get("x-compute-request-id")
kwargs = {
"http_status": response.status_code,
"response": response,
"method": method,
"url": url,
"request_id": response.headers.get("x-compute-request-id"),
"request_id": req_id,
}
if "retry-after" in response.headers:
kwargs["retry_after"] = response.headers["retry-after"]
@ -442,8 +439,8 @@ def from_response(response, method, url):
except ValueError:
pass
else:
if isinstance(body, dict):
error = list(body.values())[0]
if isinstance(body, dict) and isinstance(body.get("error"), dict):
error = body["error"]
kwargs["message"] = error.get("message")
kwargs["details"] = error.get("details")
elif content_type.startswith("text/"):

View File

@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
@ -35,7 +33,9 @@ from six.moves.urllib import parse
from ironicclient.openstack.common.apiclient import client
def assert_has_keys(dct, required=[], optional=[]):
def assert_has_keys(dct, required=None, optional=None):
required = required or []
optional = optional or []
for k in required:
try:
assert k in dct
@ -81,7 +81,7 @@ class FakeHTTPClient(client.HTTPClient):
def __init__(self, *args, **kwargs):
self.callstack = []
self.fixtures = kwargs.pop("fixtures", None) or {}
if not args and not "auth_plugin" in kwargs:
if not args and "auth_plugin" not in kwargs:
args = (None, )
super(FakeHTTPClient, self).__init__(*args, **kwargs)
@ -168,6 +168,8 @@ class FakeHTTPClient(client.HTTPClient):
else:
status, body = resp
headers = {}
self.last_request_id = headers.get('x-openstack-request-id',
'req-test')
return TestResponse({
"status_code": status,
"text": body,

View File

@ -0,0 +1,87 @@
#
# 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.
from oslo.utils import encodeutils
import six
from ironicclient.openstack.common._i18n import _
from ironicclient.openstack.common.apiclient import exceptions
from ironicclient.openstack.common import uuidutils
def find_resource(manager, name_or_id, **find_args):
"""Look for resource in a given manager.
Used as a helper for the _find_* methods.
Example:
.. code-block:: python
def _find_hypervisor(cs, hypervisor):
#Get a hypervisor by name or ID.
return cliutils.find_resource(cs.hypervisors, hypervisor)
"""
# first try to get entity as integer id
try:
return manager.get(int(name_or_id))
except (TypeError, ValueError, exceptions.NotFound):
pass
# now try to get entity as uuid
try:
if six.PY2:
tmp_id = encodeutils.safe_encode(name_or_id)
else:
tmp_id = encodeutils.safe_decode(name_or_id)
if uuidutils.is_uuid_like(tmp_id):
return manager.get(tmp_id)
except (TypeError, ValueError, exceptions.NotFound):
pass
# for str id which is not uuid
if getattr(manager, 'is_alphanum_id_allowed', False):
try:
return manager.get(name_or_id)
except exceptions.NotFound:
pass
try:
try:
return manager.find(human_id=name_or_id, **find_args)
except exceptions.NotFound:
pass
# finally try to find entity by name
try:
resource = getattr(manager, 'resource_class', None)
name_attr = resource.NAME_ATTR if resource else 'name'
kwargs = {name_attr: name_or_id}
kwargs.update(find_args)
return manager.find(**kwargs)
except exceptions.NotFound:
msg = _("No %(name)s with a name or "
"ID of '%(name_or_id)s' exists.") % \
{
"name": manager.resource_class.__name__.lower(),
"name_or_id": name_or_id
}
raise exceptions.CommandError(msg)
except exceptions.NoUniqueMatch:
msg = _("Multiple %(name)s matches found for "
"'%(name_or_id)s', use an ID to be more specific.") % \
{
"name": manager.resource_class.__name__.lower(),
"name_or_id": name_or_id
}
raise exceptions.CommandError(msg)

View File

@ -1,6 +1,7 @@
[DEFAULT]
# The list of modules to copy from openstack-common
module=_i18n
module=apiclient
module=cliutils
module=gettextutils