Wrap several show api calls with correct_return_codes

Loadbalancer show API call can also raise 'incorrect' exception, so it
would be nice to wrap it with a helper decorator.

Also, this patch extends correct_return_codes decorator to cover
wrong usage of NotFound exception at osc-lib
(see https://review.opendev.org/675328)

Change-Id: If2565df22c2677bbad09f3eafacc148c740c0124
This commit is contained in:
Andrey Kurilin 2019-08-08 16:01:45 +03:00 committed by Michael Johnson
parent e76f3de21b
commit 02256c83e9
1 changed files with 29 additions and 7 deletions

View File

@ -15,6 +15,7 @@
import functools import functools
from osc_lib.api import api from osc_lib.api import api
from osc_lib import exceptions as osc_exc
from octaviaclient.api import constants as const from octaviaclient.api import constants as const
@ -29,14 +30,25 @@ def correct_return_codes(func):
try: try:
response = func(*args, **kwargs) response = func(*args, **kwargs)
except Exception as e: except Exception as e:
if not hasattr(e, 'response'): if hasattr(e, 'response'):
raise code = e.response.status_code
raise OctaviaClientException( message = e.response.json().get(
code=e.response.status_code,
message=e.response.json().get(
'faultstring', 'faultstring',
_status_dict.get(e.response.status_code, 'Unknown Error')), _status_dict.get(e.response.status_code, 'Unknown Error'))
request_id=e.request_id) request_id = e.request_id
elif (isinstance(e, osc_exc.ClientException)
and e.code != e.http_status):
# cover https://review.opendev.org/675328 case
code = e.http_status
message = e.code
request_id = "n/a"
else:
raise
raise OctaviaClientException(
code=code,
message=message,
request_id=request_id)
return response return response
return wrapper return wrapper
@ -75,6 +87,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def load_balancer_show(self, lb_id): def load_balancer_show(self, lb_id):
"""Show a load balancer """Show a load balancer
@ -186,6 +199,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def listener_show(self, listener_id): def listener_show(self, listener_id):
"""Show a listener """Show a listener
@ -296,6 +310,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def pool_show(self, pool_id): def pool_show(self, pool_id):
"""Show a pool's settings """Show a pool's settings
@ -339,6 +354,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def member_show(self, pool_id, member_id): def member_show(self, pool_id, member_id):
"""Showing a member details of a pool """Showing a member details of a pool
@ -449,6 +465,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def l7policy_show(self, l7policy_id): def l7policy_show(self, l7policy_id):
"""Show a l7policy's settings """Show a l7policy's settings
@ -523,6 +540,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def l7rule_show(self, l7rule_id, l7policy_id): def l7rule_show(self, l7rule_id, l7policy_id):
"""Show a l7rule's settings """Show a l7rule's settings
@ -599,6 +617,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def health_monitor_show(self, health_monitor_id): def health_monitor_show(self, health_monitor_id):
"""Show a health monitor's settings """Show a health monitor's settings
@ -642,6 +661,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def quota_show(self, project_id): def quota_show(self, project_id):
"""Show a quota """Show a quota
@ -695,6 +715,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def amphora_show(self, amphora_id): def amphora_show(self, amphora_id):
"""Show an amphora """Show an amphora
@ -831,6 +852,7 @@ class OctaviaAPI(api.BaseAPI):
return response return response
@correct_return_codes
def flavor_show(self, flavor_id): def flavor_show(self, flavor_id):
"""Show a flavor """Show a flavor