From 6e042d2e1f973665a8a3ef86cd40f76306ecf449 Mon Sep 17 00:00:00 2001 From: Kristi Nikolla Date: Thu, 25 May 2017 17:14:23 -0400 Subject: [PATCH] Move utility functions to utils.py Some general utility functions such as safe_get and safe_pop were moved to utils.py. Change-Id: I14f4fce68369746542abade39b7dc02d3ce272bf --- mixmatch/proxy.py | 46 ++++++--------------------------------------- mixmatch/utils.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 mixmatch/utils.py diff --git a/mixmatch/proxy.py b/mixmatch/proxy.py index 591d5c8..affc970 100644 --- a/mixmatch/proxy.py +++ b/mixmatch/proxy.py @@ -12,8 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import uuid - import requests import flask @@ -27,6 +25,7 @@ from mixmatch.session import request from mixmatch import auth from mixmatch import model from mixmatch import services +from mixmatch import utils METHODS_ACCEPTED = ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'PATCH'] RESOURCES_AGGREGATE = ['images', 'volumes', 'snapshots'] @@ -36,39 +35,6 @@ def stream_response(response): yield response.raw.read() -def safe_get(a, i, default=None): - """Return the i-th element if it exists, or default.""" - try: - return a[i] - except IndexError: - return default - - -def safe_pop(a, i=0, default=None): - """Pops the i-th element, if any, otherwise returns default""" - try: - return a.pop(i) - except (IndexError, KeyError): - return default - - -def is_uuid(value): - """Return true if value is a valid uuid.""" - try: - uuid.UUID(value, version=4) - return True - except (ValueError, TypeError): - return False - - -def pop_if_uuid(a): - """Pops the first element of the list only if it is a uuid.""" - if is_uuid(safe_get(a, 0)): - return safe_pop(a) - else: - return None - - def get_service(a): """Determine service type based on path.""" # NOTE(knikolla): Workaround to fix glance requests that do not @@ -92,11 +58,11 @@ def get_details(method, path, headers): # //// return {'method': method, 'service': get_service(path), - 'version': safe_pop(path), - 'project_id': pop_if_uuid(path), + 'version': utils.safe_pop(path), + 'project_id': utils.pop_if_uuid(path), 'action': path[:], # NOTE(knikolla): This includes - 'resource_type': safe_pop(path), # this - 'resource_id': pop_if_uuid(path), # and this + 'resource_type': utils.safe_pop(path), # this + 'resource_id': utils.pop_if_uuid(path), # and this 'token': headers.get('X-AUTH-TOKEN', None), 'headers': headers} @@ -351,7 +317,7 @@ class RequestHandler(object): # and set strip_details to true if (details['service'] == 'volume' and details['method'] == 'GET' and - safe_get(details['action'], -1) == 'volumes'): + utils.safe_get(details['action'], -1) == 'volumes'): self.strip_details = True details['action'].insert(len(details['action']), 'detail') else: diff --git a/mixmatch/utils.py b/mixmatch/utils.py new file mode 100644 index 0000000..214e531 --- /dev/null +++ b/mixmatch/utils.py @@ -0,0 +1,48 @@ +# Copyright 2017 Massachusetts Open Cloud +# +# 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 uuid + + +def safe_get(a, i, default=None): + """Return the i-th element if it exists, or default.""" + try: + return a[i] + except IndexError: + return default + + +def safe_pop(a, i=0, default=None): + """Pops the i-th element, if any, otherwise returns default""" + try: + return a.pop(i) + except (IndexError, KeyError): + return default + + +def is_uuid(value): + """Return true if value is a valid uuid.""" + try: + uuid.UUID(value, version=4) + return True + except (ValueError, TypeError): + return False + + +def pop_if_uuid(a): + """Pops the first element of the list only if it is a uuid.""" + if is_uuid(safe_get(a, 0)): + return safe_pop(a) + else: + return None