From ea10339ef4c9c3f5dd1cf1f0be35341b18bda604 Mon Sep 17 00:00:00 2001 From: Chris Lumens Date: Wed, 8 Aug 2018 10:31:14 -0400 Subject: [PATCH] Add error IDs for bad state conditions. (cherry picked from commit d76e24053aa7274c91a756de6c7ca78599070a5d) --- src/pylorax/api/errors.py | 5 +++++ src/pylorax/api/v0.py | 14 +++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/pylorax/api/errors.py b/src/pylorax/api/errors.py index 77d7f5e2..b46d3cfb 100644 --- a/src/pylorax/api/errors.py +++ b/src/pylorax/api/errors.py @@ -19,3 +19,8 @@ # Returned from the API when ?limit= or ?offset= is given something that does # not convert into an integer. BAD_LIMIT_OR_OFFSET = "BadLimitOrOffset" + +# Returned from the API when it expected a build to be in a state other than +# what it currently is. This most often happens when asking for results from +# a build that is not yet done. +BUILD_IN_WRONG_STATE = "BuildInWrongState" diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 4664f7ba..b420b53b 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -1812,7 +1812,7 @@ def v0_api(api): return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 if status["queue_status"] not in ["WAITING", "RUNNING"]: - return jsonify(status=False, errors=["Build %s is not in WAITING or RUNNING." % uuid]) + return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s is not in WAITING or RUNNING." % uuid}]) try: uuid_cancel(api.config["COMPOSER_CFG"], uuid) @@ -1837,7 +1837,7 @@ def v0_api(api): if status is None: errors.append("%s is not a valid build uuid" % uuid) elif status["queue_status"] not in ["FINISHED", "FAILED"]: - errors.append("Build %s is not in FINISHED or FAILED." % uuid) + errors.append({"id": BUILD_IN_WRONG_STATE, "msg": "Build %s is not in FINISHED or FAILED." % uuid}) else: try: uuid_delete(api.config["COMPOSER_CFG"], uuid) @@ -1876,7 +1876,7 @@ def v0_api(api): if status is None: return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 if status["queue_status"] not in ["FINISHED", "FAILED"]: - return jsonify(status=False, errors=["Build %s not in FINISHED or FAILED state." % uuid]), 400 + return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: return Response(uuid_tar(api.config["COMPOSER_CFG"], uuid, metadata=True, image=False, logs=False), mimetype="application/x-tar", @@ -1896,7 +1896,7 @@ def v0_api(api): if status is None: return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 elif status["queue_status"] not in ["FINISHED", "FAILED"]: - return jsonify(status=False, errors=["Build %s not in FINISHED or FAILED state." % uuid]), 400 + return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: return Response(uuid_tar(api.config["COMPOSER_CFG"], uuid, metadata=True, image=True, logs=True), mimetype="application/x-tar", @@ -1916,7 +1916,7 @@ def v0_api(api): if status is None: return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 elif status["queue_status"] not in ["FINISHED", "FAILED"]: - return jsonify(status=False, errors=["Build %s not in FINISHED or FAILED state." % uuid]), 400 + return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: return Response(uuid_tar(api.config["COMPOSER_CFG"], uuid, metadata=False, image=False, logs=True), mimetype="application/x-tar", @@ -1936,7 +1936,7 @@ def v0_api(api): if status is None: return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 elif status["queue_status"] not in ["FINISHED", "FAILED"]: - return jsonify(status=False, errors=["Build %s not in FINISHED or FAILED state." % uuid]), 400 + return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: image_name, image_path = uuid_image(api.config["COMPOSER_CFG"], uuid) @@ -1967,7 +1967,7 @@ def v0_api(api): if status is None: return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 elif status["queue_status"] == "WAITING": - return jsonify(status=False, errors=["Build %s has not started yet. No logs to view" % uuid]) + return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s has not started yet. No logs to view" % uuid}]) try: return Response(uuid_log(api.config["COMPOSER_CFG"], uuid, size), direct_passthrough=True) except RuntimeError as e: