Remove duplicated code in token_submit functions.

* Create a common helper function.
 * Both specialised password and generic submit now use this.
 * Provide more useful information for badly formatted json to token_submit.
 * Added exception support for stacktasks' errors list.

Change-Id: Id6fd38ae79226aa9da2af3301f320129122eb2b3
This commit is contained in:
Dale Smith 2016-02-24 12:29:54 +00:00 committed by adriant
parent 9b84cac78f
commit b0425f4c11
2 changed files with 22 additions and 14 deletions

View File

@ -48,8 +48,15 @@ class HTTPException(BaseException):
super(HTTPException, self).__init__(message)
try:
self.error = jsonutils.loads(message)
# Stacktask client: mangle the 'errors' return list into
# standard 'error' format
if 'errors' in self.error:
self.error['error'] = {
"message": ', '.join(self.error['errors']),
}
if 'error' not in self.error:
raise KeyError(_('Key "error" not exists'))
raise KeyError(_('Key "error" does not exist.'))
except KeyError:
# NOTE(jianingy): If key 'error' happens not exist,
# self.message becomes no sense. In this case, we

View File

@ -237,19 +237,10 @@ def do_token_show(sc, args):
help=_('Password of the new user.'))
def do_token_submit_password(sc, args):
"""
Submit this token to setup or update your password.
Submit this token to set or update your password.
"""
kwargs = {'password': args.password}
try:
sc.tokens.submit(args.token, kwargs)
except exc.HTTPNotFound as e:
print e.message
print "Requested token was not found."
except exc.BadRequest as e:
print e.message
print "Bad request. Did you omit a required parameter?"
else:
print "Token submitted."
json_data = {'password': args.password}
_token_submit(sc, args, json_data)
@utils.arg('token', metavar='<token>',
@ -261,7 +252,17 @@ def do_token_submit(sc, args):
Submit this token to finalise Task.
"""
try:
sc.tokens.submit(args.token, json.loads(args.data))
json_data = json.loads(args.data)
except ValueError as e:
print e.message
print "Json data invalid."
return
_token_submit(sc, args, json_data)
def _token_submit(sc, args, json_data):
try:
sc.tokens.submit(args.token, json_data)
except exc.HTTPNotFound as e:
print e.message
print "Requested token was not found."