diff --git a/src/pylorax/api/errors.py b/src/pylorax/api/errors.py index b46d3cfb..823d8532 100644 --- a/src/pylorax/api/errors.py +++ b/src/pylorax/api/errors.py @@ -24,3 +24,6 @@ BAD_LIMIT_OR_OFFSET = "BadLimitOrOffset" # 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" + +# Returned from the API when a UUID that was requested does not exist. +UNKNOWN_UUID = "UnknownUUID" diff --git a/src/pylorax/api/queue.py b/src/pylorax/api/queue.py index 66d5408b..d3fbbc03 100644 --- a/src/pylorax/api/queue.py +++ b/src/pylorax/api/queue.py @@ -452,7 +452,7 @@ def uuid_info(cfg, uuid): :type cfg: ComposerConfig :param uuid: The UUID of the build :type uuid: str - :returns: dictionary of information about the composition + :returns: dictionary of information about the composition or None :rtype: dict :raises: RuntimeError if there was a problem @@ -468,7 +468,7 @@ def uuid_info(cfg, uuid): """ uuid_dir = joinpaths(cfg.get("composer", "lib_dir"), "results", uuid) if not os.path.exists(uuid_dir): - raise RuntimeError("%s is not a valid build_id" % uuid) + return None # Load the compose configuration cfg_path = joinpaths(uuid_dir, "config.toml") diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 7d9122a2..50f34f79 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -1798,12 +1798,15 @@ def v0_api(api): return jsonify(status=False, errors=["Invalid characters in API path"]), 400 results = [] + errors = [] for uuid in [n.strip().lower() for n in uuids.split(",")]: details = uuid_status(api.config["COMPOSER_CFG"], uuid) if details is not None: results.append(details) + else: + errors.append({"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}) - return jsonify(uuids=results) + return jsonify(uuids=results, errors=errors) @api.route("/api/v0/compose/cancel", defaults={'uuid': ""}, methods=["DELETE"]) @api.route("/api/v0/compose/cancel/", methods=["DELETE"]) @@ -1816,7 +1819,7 @@ def v0_api(api): status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 if status["queue_status"] not in ["WAITING", "RUNNING"]: return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s is not in WAITING or RUNNING." % uuid}]) @@ -1842,7 +1845,7 @@ def v0_api(api): for uuid in [n.strip().lower() for n in uuids.split(",")]: status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - errors.append("%s is not a valid build uuid" % uuid) + errors.append({"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}) elif status["queue_status"] not in ["FINISHED", "FAILED"]: errors.append({"id": BUILD_IN_WRONG_STATE, "msg": "Build %s is not in FINISHED or FAILED." % uuid}) else: @@ -1868,7 +1871,10 @@ def v0_api(api): except Exception as e: return jsonify(status=False, errors=[str(e)]), 400 - return jsonify(**info) + if info is None: + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 + else: + return jsonify(**info) @api.route("/api/v0/compose/metadata", defaults={'uuid': ""}) @api.route("/api/v0/compose/metadata/") @@ -1881,7 +1887,7 @@ def v0_api(api): status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 if status["queue_status"] not in ["FINISHED", "FAILED"]: return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: @@ -1901,7 +1907,7 @@ def v0_api(api): status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 elif status["queue_status"] not in ["FINISHED", "FAILED"]: return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: @@ -1921,7 +1927,7 @@ def v0_api(api): status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 elif status["queue_status"] not in ["FINISHED", "FAILED"]: return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: @@ -1941,7 +1947,7 @@ def v0_api(api): status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 elif status["queue_status"] not in ["FINISHED", "FAILED"]: return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s not in FINISHED or FAILED state." % uuid}]), 400 else: @@ -1972,7 +1978,7 @@ def v0_api(api): status = uuid_status(api.config["COMPOSER_CFG"], uuid) if status is None: - return jsonify(status=False, errors=["%s is not a valid build uuid" % uuid]), 400 + return jsonify(status=False, errors=[{"id": UNKNOWN_UUID, "msg": "%s is not a valid build uuid" % uuid}]), 400 elif status["queue_status"] == "WAITING": return jsonify(status=False, errors=[{"id": BUILD_IN_WRONG_STATE, "msg": "Build %s has not started yet. No logs to view" % uuid}]) try: diff --git a/tests/pylorax/test_server.py b/tests/pylorax/test_server.py index 597ae731..950b39c4 100644 --- a/tests/pylorax/test_server.py +++ b/tests/pylorax/test_server.py @@ -27,6 +27,7 @@ import unittest from flask import json import pytoml as toml from pylorax.api.config import configure, make_yum_dirs, make_queue_dirs +from pylorax.api.errors import * # pylint: disable=wildcard-import from pylorax.api.queue import start_queue_monitor from pylorax.api.recipes import open_or_create_repo, commit_recipe_directory from pylorax.api.server import server, GitLock, YumLock @@ -785,14 +786,15 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to get an error for a bad uuid: %s" % data) - self.assertEqual(data["errors"], ["NO-UUID-TO-SEE-HERE is not a valid build uuid"], "Failed to get errors: %s" % data) + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "NO-UUID-TO-SEE-HERE is not a valid build uuid"}], + "Failed to get errors: %s" % data) def test_compose_05_delete_fail(self): """Test that requesting a delete for a bad uuid fails.""" resp = self.server.delete("/api/v0/compose/delete/NO-UUID-TO-SEE-HERE") data = json.loads(resp.data) self.assertNotEqual(data, None) - self.assertEqual(data["errors"], ["no-uuid-to-see-here is not a valid build uuid"], + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "no-uuid-to-see-here is not a valid build uuid"}], "Failed to get an error for a bad uuid: %s" % data) def test_compose_06_info_fail(self): @@ -801,7 +803,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to get an error for a bad uuid: %s" % data) - self.assertEqual(data["errors"], ["NO-UUID-TO-SEE-HERE is not a valid build_id"], + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "NO-UUID-TO-SEE-HERE is not a valid build uuid"}], "Failed to get errors: %s" % data) def test_compose_07_metadata_fail(self): @@ -810,7 +812,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to get an error for a bad uuid: %s" % data) - self.assertEqual(data["errors"], ["NO-UUID-TO-SEE-HERE is not a valid build uuid"], + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "NO-UUID-TO-SEE-HERE is not a valid build uuid"}], "Failed to get errors: %s" % data) def test_compose_08_results_fail(self): @@ -819,7 +821,8 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to get an error for a bad uuid: %s" % data) - self.assertEqual(data["errors"], ["NO-UUID-TO-SEE-HERE is not a valid build uuid"], "Failed to get errors: %s" % data) + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "NO-UUID-TO-SEE-HERE is not a valid build uuid"}], + "Failed to get errors: %s" % data) def test_compose_09_logs_fail(self): """Test that requesting logs for a bad uuid fails.""" @@ -827,7 +830,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to get an error for a bad uuid: %s" % data) - self.assertEqual(data["errors"], ["NO-UUID-TO-SEE-HERE is not a valid build uuid"], + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "NO-UUID-TO-SEE-HERE is not a valid build uuid"}], "Failed to get errors: %s" % data) def test_compose_10_log_fail(self): @@ -836,7 +839,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to get an error for a bad uuid: %s" % data) - self.assertEqual(data["errors"], ["NO-UUID-TO-SEE-HERE is not a valid build uuid"], + self.assertEqual(data["errors"], [{"id": UNKNOWN_UUID, "msg": "NO-UUID-TO-SEE-HERE is not a valid build uuid"}], "Failed to get errors: %s" % data) def test_compose_11_create_failed(self):