Merge "Adding details to VIM API Conflict responses"

This commit is contained in:
Zuul 2022-09-20 14:34:48 +00:00 committed by Gerrit Code Review
commit 303c4cdcaf
10 changed files with 45 additions and 36 deletions

View File

@ -59,17 +59,33 @@ def request(token_id, method, api_cmd, api_cmd_headers=None,
except urllib.error.HTTPError as e:
headers = list()
response_raw = dict()
json_response = False
if e.fp is not None:
headers = list() # list of tuples
for key, value in e.fp.info().items():
if key not in headers_per_hop:
cap_key = '-'.join((ck.capitalize()
for ck in key.split('-')))
headers.append((cap_key, value))
header = '-'.join((ck.capitalize()
for ck in key.split('-')))
headers.append((header, value))
# set a flag if the response is json
# to assist with extracting faultString
if 'Content-Type' == header:
if 'application/json' == value.split(';')[0]:
json_response = True
response_raw = e.fp.read()
reason = ''
if json_response:
try:
response = json.loads(response_raw)
message = response.get('faultstring', None)
if message is not None:
reason = str(message.rstrip('.'))
except ValueError:
pass
if httplib.FOUND == e.code:
return response_raw
@ -77,27 +93,11 @@ def request(token_id, method, api_cmd, api_cmd_headers=None,
return None
elif httplib.CONFLICT == e.code:
raise Exception("Operation failed: conflict detected")
raise Exception("Operation failed: conflict detected. %s" % reason)
elif httplib.FORBIDDEN == e.code:
raise Exception("Authorization failed")
# Attempt to get the reason for the http error from the response
reason = ''
for header, value in headers:
if 'Content-Type' == header:
if 'application/json' == value.split(';')[0]:
try:
response = json.loads(response_raw)
message = response.get('faultstring', None)
if message is not None:
reason = str(message.rstrip('.'))
print("Operation failed: %s" % reason)
break
except ValueError:
pass
print("Rest-API status=%s, %s, %s, headers=%s, payload=%s, response=%s"
% (e.code, method, api_cmd, api_cmd_headers, api_cmd_payload,
response_raw))

View File

@ -553,6 +553,8 @@ class SwUpdateStrategyAPI(rest.RestController):
elif rpc.RPC_MSG_RESULT.FAILED == response.result:
DLOG.info("Strategy delete failed")
# TODO(abailey): consider adding error_string to
# DELETE_SW_UPDATE_STRATEGY_RESPONSE
return pecan.abort(httplib.CONFLICT)
DLOG.error("Unexpected result received, result=%s." % response.result)
@ -607,7 +609,7 @@ class SwPatchStrategyAPI(SwUpdateStrategyAPI):
query_data.convert_strategy(strategy)
return query_data
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
return pecan.abort(httplib.CONFLICT)
return pecan.abort(httplib.CONFLICT, response.error_string)
DLOG.error("Unexpected result received, result=%s." % response.result)
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
@ -675,7 +677,7 @@ class SwUpgradeStrategyAPI(SwUpdateStrategyAPI):
query_data.convert_strategy(strategy)
return query_data
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
return pecan.abort(httplib.CONFLICT)
return pecan.abort(httplib.CONFLICT, response.error_string)
DLOG.error("Unexpected result received, result=%s." % response.result)
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
@ -739,7 +741,7 @@ class FwUpdateStrategyAPI(SwUpdateStrategyAPI):
query_data.convert_strategy(strategy)
return query_data
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
return pecan.abort(httplib.CONFLICT)
return pecan.abort(httplib.CONFLICT, response.error_string)
DLOG.error("Unexpected result received, result=%s." % response.result)
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
@ -829,7 +831,7 @@ class KubeRootcaUpdateStrategyAPI(SwUpdateStrategyAPI):
query_data.convert_strategy(strategy)
return query_data
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
return pecan.abort(httplib.CONFLICT)
return pecan.abort(httplib.CONFLICT, response.error_string)
DLOG.error("Unexpected result received, result=%s." % response.result)
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
@ -902,7 +904,7 @@ class KubeUpgradeStrategyAPI(SwUpdateStrategyAPI):
query_data.convert_strategy(strategy)
return query_data
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
return pecan.abort(httplib.CONFLICT)
return pecan.abort(httplib.CONFLICT, response.error_string)
DLOG.error("Unexpected result received, result=%s." % response.result)
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)

View File

@ -59,7 +59,7 @@ class SwMgmtDirector(object):
# Do not schedule the callback - if creation failed because a
# strategy already exists, the callback will attempt to operate
# on the old strategy, which is not what we want.
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update._sw_update_type
return None, reason
self._sw_update = objects.SwPatch()
@ -87,7 +87,7 @@ class SwMgmtDirector(object):
# Do not schedule the callback - if creation failed because a
# strategy already exists, the callback will attempt to operate
# on the old strategy, which is not what we want.
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update._sw_update_type
return None, reason
self._sw_update = objects.SwUpgrade()
@ -119,7 +119,7 @@ class SwMgmtDirector(object):
# Do not schedule the callback - if creation failed because a
# strategy already exists, the callback will attempt to operate
# on the old strategy, which is not what we want.
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update._sw_update_type
return None, reason
self._sw_update = objects.FwUpdate()
@ -154,7 +154,7 @@ class SwMgmtDirector(object):
# Do not schedule the callback - if creation failed because a
# strategy already exists, the callback will attempt to operate
# on the old strategy, which is not what we want.
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update._sw_update_type
return None, reason
self._sw_update = objects.KubeRootcaUpdate()
@ -196,7 +196,7 @@ class SwMgmtDirector(object):
# Do not schedule the callback - if creation failed because a
# strategy already exists, the callback will attempt to operate
# on the old strategy, which is not what we want.
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update._sw_update_type
return None, reason
self._sw_update = objects.KubeUpgrade()

View File

@ -142,6 +142,7 @@ def vim_sw_update_api_create_strategy(connection, msg):
else:
DLOG.error("Invalid message name: %s" % msg.sw_update_type)
response = rpc.APIResponseCreateSwUpdateStrategy()
# todo(abailey): consider adding error_string to other error types
response.result = rpc.RPC_MSG_RESULT.FAILED
connection.send(response.serialize())
DLOG.verbose("Sent response=%s." % response)
@ -150,10 +151,13 @@ def vim_sw_update_api_create_strategy(connection, msg):
if uuid is None:
response = rpc.APIResponseCreateSwUpdateStrategy()
if reason == "strategy already exists":
# change this to a prefix...
if reason is not None and reason.startswith("strategy already exists"):
response.result = rpc.RPC_MSG_RESULT.CONFLICT
response.error_string = reason
else:
response.result = rpc.RPC_MSG_RESULT.FAILED
# todo(abailey): consider adding error_string to other error types
connection.send(response.serialize())
DLOG.verbose("Sent response=%s." % response)
connection.close()

View File

@ -43,7 +43,7 @@ class FwUpdate(SwUpdate):
from nfv_vim import strategy
if self._strategy:
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update_type
return False, reason
self._strategy = strategy.FwUpdateStrategy(

View File

@ -49,7 +49,7 @@ class KubeRootcaUpdate(SwUpdate):
from nfv_vim import strategy
if self._strategy:
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update_type
return False, reason
self._strategy = \

View File

@ -50,7 +50,7 @@ class KubeUpgrade(SwUpdate):
from nfv_vim import strategy
if self._strategy:
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update_type
return False, reason
self._strategy = \

View File

@ -43,7 +43,7 @@ class SwPatch(SwUpdate):
from nfv_vim import strategy
if self._strategy:
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update_type
return False, reason
self._strategy = strategy.SwPatchStrategy(

View File

@ -39,7 +39,7 @@ class SwUpgrade(SwUpdate):
from nfv_vim import strategy
if self._strategy:
reason = "strategy already exists"
reason = "strategy already exists of type:%s" % self._sw_update_type
return False, reason
self._strategy = strategy.SwUpgradeStrategy(

View File

@ -143,6 +143,7 @@ class APIResponseCreateSwUpdateStrategy(RPCMessage):
RPC API Response Message - Create Software Update Strategy
"""
strategy = None
error_string = None
def __init__(self, msg_version=RPC_MSG_VERSION.VERSION_1_0,
msg_type=RPC_MSG_TYPE.CREATE_SW_UPDATE_STRATEGY_RESPONSE,
@ -152,9 +153,11 @@ class APIResponseCreateSwUpdateStrategy(RPCMessage):
def serialize_payload(self, msg):
msg['strategy'] = self.strategy
msg['error_string'] = self.error_string
def deserialize_payload(self, msg):
self.strategy = msg.get('strategy', None)
self.error_string = msg.get('error_string', None)
def __str__(self):
return "create-sw-update-strategy response: %s" % self.strategy