From d76e24053aa7274c91a756de6c7ca78599070a5d 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. --- 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 5aecf17a..7d9122a2 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -1819,7 +1819,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) @@ -1844,7 +1844,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) @@ -1883,7 +1883,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", @@ -1903,7 +1903,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", @@ -1923,7 +1923,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", @@ -1943,7 +1943,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) @@ -1974,7 +1974,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: