Merge "Adding details to VIM API Conflict responses"
This commit is contained in:
commit
303c4cdcaf
@ -59,17 +59,33 @@ def request(token_id, method, api_cmd, api_cmd_headers=None,
|
|||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
headers = list()
|
headers = list()
|
||||||
response_raw = dict()
|
response_raw = dict()
|
||||||
|
json_response = False
|
||||||
|
|
||||||
if e.fp is not None:
|
if e.fp is not None:
|
||||||
headers = list() # list of tuples
|
headers = list() # list of tuples
|
||||||
for key, value in e.fp.info().items():
|
for key, value in e.fp.info().items():
|
||||||
if key not in headers_per_hop:
|
if key not in headers_per_hop:
|
||||||
cap_key = '-'.join((ck.capitalize()
|
header = '-'.join((ck.capitalize()
|
||||||
for ck in key.split('-')))
|
for ck in key.split('-')))
|
||||||
headers.append((cap_key, value))
|
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()
|
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:
|
if httplib.FOUND == e.code:
|
||||||
return response_raw
|
return response_raw
|
||||||
|
|
||||||
@ -77,27 +93,11 @@ def request(token_id, method, api_cmd, api_cmd_headers=None,
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
elif httplib.CONFLICT == e.code:
|
elif httplib.CONFLICT == e.code:
|
||||||
raise Exception("Operation failed: conflict detected")
|
raise Exception("Operation failed: conflict detected. %s" % reason)
|
||||||
|
|
||||||
elif httplib.FORBIDDEN == e.code:
|
elif httplib.FORBIDDEN == e.code:
|
||||||
raise Exception("Authorization failed")
|
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"
|
print("Rest-API status=%s, %s, %s, headers=%s, payload=%s, response=%s"
|
||||||
% (e.code, method, api_cmd, api_cmd_headers, api_cmd_payload,
|
% (e.code, method, api_cmd, api_cmd_headers, api_cmd_payload,
|
||||||
response_raw))
|
response_raw))
|
||||||
|
@ -553,6 +553,8 @@ class SwUpdateStrategyAPI(rest.RestController):
|
|||||||
|
|
||||||
elif rpc.RPC_MSG_RESULT.FAILED == response.result:
|
elif rpc.RPC_MSG_RESULT.FAILED == response.result:
|
||||||
DLOG.info("Strategy delete failed")
|
DLOG.info("Strategy delete failed")
|
||||||
|
# TODO(abailey): consider adding error_string to
|
||||||
|
# DELETE_SW_UPDATE_STRATEGY_RESPONSE
|
||||||
return pecan.abort(httplib.CONFLICT)
|
return pecan.abort(httplib.CONFLICT)
|
||||||
|
|
||||||
DLOG.error("Unexpected result received, result=%s." % response.result)
|
DLOG.error("Unexpected result received, result=%s." % response.result)
|
||||||
@ -607,7 +609,7 @@ class SwPatchStrategyAPI(SwUpdateStrategyAPI):
|
|||||||
query_data.convert_strategy(strategy)
|
query_data.convert_strategy(strategy)
|
||||||
return query_data
|
return query_data
|
||||||
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
|
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)
|
DLOG.error("Unexpected result received, result=%s." % response.result)
|
||||||
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
||||||
@ -675,7 +677,7 @@ class SwUpgradeStrategyAPI(SwUpdateStrategyAPI):
|
|||||||
query_data.convert_strategy(strategy)
|
query_data.convert_strategy(strategy)
|
||||||
return query_data
|
return query_data
|
||||||
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
|
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)
|
DLOG.error("Unexpected result received, result=%s." % response.result)
|
||||||
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
||||||
@ -739,7 +741,7 @@ class FwUpdateStrategyAPI(SwUpdateStrategyAPI):
|
|||||||
query_data.convert_strategy(strategy)
|
query_data.convert_strategy(strategy)
|
||||||
return query_data
|
return query_data
|
||||||
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
|
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)
|
DLOG.error("Unexpected result received, result=%s." % response.result)
|
||||||
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
||||||
@ -829,7 +831,7 @@ class KubeRootcaUpdateStrategyAPI(SwUpdateStrategyAPI):
|
|||||||
query_data.convert_strategy(strategy)
|
query_data.convert_strategy(strategy)
|
||||||
return query_data
|
return query_data
|
||||||
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
|
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)
|
DLOG.error("Unexpected result received, result=%s." % response.result)
|
||||||
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
||||||
@ -902,7 +904,7 @@ class KubeUpgradeStrategyAPI(SwUpdateStrategyAPI):
|
|||||||
query_data.convert_strategy(strategy)
|
query_data.convert_strategy(strategy)
|
||||||
return query_data
|
return query_data
|
||||||
elif rpc.RPC_MSG_RESULT.CONFLICT == response.result:
|
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)
|
DLOG.error("Unexpected result received, result=%s." % response.result)
|
||||||
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
return pecan.abort(httplib.INTERNAL_SERVER_ERROR)
|
||||||
|
@ -59,7 +59,7 @@ class SwMgmtDirector(object):
|
|||||||
# Do not schedule the callback - if creation failed because a
|
# Do not schedule the callback - if creation failed because a
|
||||||
# strategy already exists, the callback will attempt to operate
|
# strategy already exists, the callback will attempt to operate
|
||||||
# on the old strategy, which is not what we want.
|
# 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
|
return None, reason
|
||||||
|
|
||||||
self._sw_update = objects.SwPatch()
|
self._sw_update = objects.SwPatch()
|
||||||
@ -87,7 +87,7 @@ class SwMgmtDirector(object):
|
|||||||
# Do not schedule the callback - if creation failed because a
|
# Do not schedule the callback - if creation failed because a
|
||||||
# strategy already exists, the callback will attempt to operate
|
# strategy already exists, the callback will attempt to operate
|
||||||
# on the old strategy, which is not what we want.
|
# 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
|
return None, reason
|
||||||
|
|
||||||
self._sw_update = objects.SwUpgrade()
|
self._sw_update = objects.SwUpgrade()
|
||||||
@ -119,7 +119,7 @@ class SwMgmtDirector(object):
|
|||||||
# Do not schedule the callback - if creation failed because a
|
# Do not schedule the callback - if creation failed because a
|
||||||
# strategy already exists, the callback will attempt to operate
|
# strategy already exists, the callback will attempt to operate
|
||||||
# on the old strategy, which is not what we want.
|
# 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
|
return None, reason
|
||||||
|
|
||||||
self._sw_update = objects.FwUpdate()
|
self._sw_update = objects.FwUpdate()
|
||||||
@ -154,7 +154,7 @@ class SwMgmtDirector(object):
|
|||||||
# Do not schedule the callback - if creation failed because a
|
# Do not schedule the callback - if creation failed because a
|
||||||
# strategy already exists, the callback will attempt to operate
|
# strategy already exists, the callback will attempt to operate
|
||||||
# on the old strategy, which is not what we want.
|
# 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
|
return None, reason
|
||||||
|
|
||||||
self._sw_update = objects.KubeRootcaUpdate()
|
self._sw_update = objects.KubeRootcaUpdate()
|
||||||
@ -196,7 +196,7 @@ class SwMgmtDirector(object):
|
|||||||
# Do not schedule the callback - if creation failed because a
|
# Do not schedule the callback - if creation failed because a
|
||||||
# strategy already exists, the callback will attempt to operate
|
# strategy already exists, the callback will attempt to operate
|
||||||
# on the old strategy, which is not what we want.
|
# 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
|
return None, reason
|
||||||
|
|
||||||
self._sw_update = objects.KubeUpgrade()
|
self._sw_update = objects.KubeUpgrade()
|
||||||
|
@ -142,6 +142,7 @@ def vim_sw_update_api_create_strategy(connection, msg):
|
|||||||
else:
|
else:
|
||||||
DLOG.error("Invalid message name: %s" % msg.sw_update_type)
|
DLOG.error("Invalid message name: %s" % msg.sw_update_type)
|
||||||
response = rpc.APIResponseCreateSwUpdateStrategy()
|
response = rpc.APIResponseCreateSwUpdateStrategy()
|
||||||
|
# todo(abailey): consider adding error_string to other error types
|
||||||
response.result = rpc.RPC_MSG_RESULT.FAILED
|
response.result = rpc.RPC_MSG_RESULT.FAILED
|
||||||
connection.send(response.serialize())
|
connection.send(response.serialize())
|
||||||
DLOG.verbose("Sent response=%s." % response)
|
DLOG.verbose("Sent response=%s." % response)
|
||||||
@ -150,10 +151,13 @@ def vim_sw_update_api_create_strategy(connection, msg):
|
|||||||
|
|
||||||
if uuid is None:
|
if uuid is None:
|
||||||
response = rpc.APIResponseCreateSwUpdateStrategy()
|
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.result = rpc.RPC_MSG_RESULT.CONFLICT
|
||||||
|
response.error_string = reason
|
||||||
else:
|
else:
|
||||||
response.result = rpc.RPC_MSG_RESULT.FAILED
|
response.result = rpc.RPC_MSG_RESULT.FAILED
|
||||||
|
# todo(abailey): consider adding error_string to other error types
|
||||||
connection.send(response.serialize())
|
connection.send(response.serialize())
|
||||||
DLOG.verbose("Sent response=%s." % response)
|
DLOG.verbose("Sent response=%s." % response)
|
||||||
connection.close()
|
connection.close()
|
||||||
|
@ -43,7 +43,7 @@ class FwUpdate(SwUpdate):
|
|||||||
from nfv_vim import strategy
|
from nfv_vim import strategy
|
||||||
|
|
||||||
if self._strategy:
|
if self._strategy:
|
||||||
reason = "strategy already exists"
|
reason = "strategy already exists of type:%s" % self._sw_update_type
|
||||||
return False, reason
|
return False, reason
|
||||||
|
|
||||||
self._strategy = strategy.FwUpdateStrategy(
|
self._strategy = strategy.FwUpdateStrategy(
|
||||||
|
@ -49,7 +49,7 @@ class KubeRootcaUpdate(SwUpdate):
|
|||||||
from nfv_vim import strategy
|
from nfv_vim import strategy
|
||||||
|
|
||||||
if self._strategy:
|
if self._strategy:
|
||||||
reason = "strategy already exists"
|
reason = "strategy already exists of type:%s" % self._sw_update_type
|
||||||
return False, reason
|
return False, reason
|
||||||
|
|
||||||
self._strategy = \
|
self._strategy = \
|
||||||
|
@ -50,7 +50,7 @@ class KubeUpgrade(SwUpdate):
|
|||||||
from nfv_vim import strategy
|
from nfv_vim import strategy
|
||||||
|
|
||||||
if self._strategy:
|
if self._strategy:
|
||||||
reason = "strategy already exists"
|
reason = "strategy already exists of type:%s" % self._sw_update_type
|
||||||
return False, reason
|
return False, reason
|
||||||
|
|
||||||
self._strategy = \
|
self._strategy = \
|
||||||
|
@ -43,7 +43,7 @@ class SwPatch(SwUpdate):
|
|||||||
from nfv_vim import strategy
|
from nfv_vim import strategy
|
||||||
|
|
||||||
if self._strategy:
|
if self._strategy:
|
||||||
reason = "strategy already exists"
|
reason = "strategy already exists of type:%s" % self._sw_update_type
|
||||||
return False, reason
|
return False, reason
|
||||||
|
|
||||||
self._strategy = strategy.SwPatchStrategy(
|
self._strategy = strategy.SwPatchStrategy(
|
||||||
|
@ -39,7 +39,7 @@ class SwUpgrade(SwUpdate):
|
|||||||
from nfv_vim import strategy
|
from nfv_vim import strategy
|
||||||
|
|
||||||
if self._strategy:
|
if self._strategy:
|
||||||
reason = "strategy already exists"
|
reason = "strategy already exists of type:%s" % self._sw_update_type
|
||||||
return False, reason
|
return False, reason
|
||||||
|
|
||||||
self._strategy = strategy.SwUpgradeStrategy(
|
self._strategy = strategy.SwUpgradeStrategy(
|
||||||
|
@ -143,6 +143,7 @@ class APIResponseCreateSwUpdateStrategy(RPCMessage):
|
|||||||
RPC API Response Message - Create Software Update Strategy
|
RPC API Response Message - Create Software Update Strategy
|
||||||
"""
|
"""
|
||||||
strategy = None
|
strategy = None
|
||||||
|
error_string = None
|
||||||
|
|
||||||
def __init__(self, msg_version=RPC_MSG_VERSION.VERSION_1_0,
|
def __init__(self, msg_version=RPC_MSG_VERSION.VERSION_1_0,
|
||||||
msg_type=RPC_MSG_TYPE.CREATE_SW_UPDATE_STRATEGY_RESPONSE,
|
msg_type=RPC_MSG_TYPE.CREATE_SW_UPDATE_STRATEGY_RESPONSE,
|
||||||
@ -152,9 +153,11 @@ class APIResponseCreateSwUpdateStrategy(RPCMessage):
|
|||||||
|
|
||||||
def serialize_payload(self, msg):
|
def serialize_payload(self, msg):
|
||||||
msg['strategy'] = self.strategy
|
msg['strategy'] = self.strategy
|
||||||
|
msg['error_string'] = self.error_string
|
||||||
|
|
||||||
def deserialize_payload(self, msg):
|
def deserialize_payload(self, msg):
|
||||||
self.strategy = msg.get('strategy', None)
|
self.strategy = msg.get('strategy', None)
|
||||||
|
self.error_string = msg.get('error_string', None)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "create-sw-update-strategy response: %s" % self.strategy
|
return "create-sw-update-strategy response: %s" % self.strategy
|
||||||
|
Loading…
Reference in New Issue
Block a user