Update the /api/v0/ route handling to use the flask_blueprints Blueprint class

Instead of setting up the routes inside a function we can now use a
BlueprintSkip class, which allows us to register them at different
routes (eg. /api/v0/ and /api/v1/) and override any routes that will be
replaced by the new API version.
This commit is contained in:
Brian C. Lane 2019-04-22 14:59:02 -07:00
parent 7071e62985
commit 6d50a5874e
2 changed files with 886 additions and 882 deletions

View File

@ -1,5 +1,5 @@
#
# Copyright (C) 2017 Red Hat, Inc.
# Copyright (C) 2017-2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -83,4 +83,5 @@ def v0_status():
def bad_request(error):
return jsonify(status=False, errors=[{ "id": HTTP_ERROR, "code": error.code, "msg": error.name }]), error.code
v0_api(server)
# Register the v0 API on /api/v0/
server.register_blueprint(v0_api, url_prefix="/api/v0/")

View File

@ -1,5 +1,5 @@
#
# Copyright (C) 2017-2018 Red Hat, Inc.
# Copyright (C) 2017-2019 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -993,12 +993,14 @@ log = logging.getLogger("lorax-composer")
import os
from flask import jsonify, request, Response, send_file
from flask import current_app as api
import pytoml as toml
from pylorax.sysutils import joinpaths
from pylorax.api.checkparams import checkparams
from pylorax.api.compose import start_build, compose_types
from pylorax.api.errors import * # pylint: disable=wildcard-import
from pylorax.api.flask_blueprint import BlueprintSkip
from pylorax.api.projects import projects_list, projects_info, projects_depsolve
from pylorax.api.projects import modules_list, modules_info, ProjectsError, repo_to_source
from pylorax.api.projects import get_repo_sources, delete_repo_source, source_to_repo, dnf_repo_to_file_repo
@ -1026,7 +1028,7 @@ def take_limits(iterable, offset, limit):
"""
return iterable[offset:][:limit]
def blueprint_exists(api, branch, blueprint_name):
def blueprint_exists(branch, blueprint_name):
"""Return True if the blueprint exists
:param api: flask object
@ -1044,9 +1046,10 @@ def blueprint_exists(api, branch, blueprint_name):
except (RecipeError, RecipeFileError):
return False
def v0_api(api):
# Note that Sphinx will not generate documentations for any of these.
@api.route("/api/v0/blueprints/list")
# Create the v0 routes Blueprint with skip_routes support
v0_api = BlueprintSkip("v0_routes", __name__)
@v0_api.route("/blueprints/list")
def v0_blueprints_list():
"""List the available blueprints on a branch."""
branch = request.args.get("branch", "master")
@ -1064,8 +1067,8 @@ def v0_api(api):
limited_blueprints = take_limits(blueprints, offset, limit)
return jsonify(blueprints=limited_blueprints, limit=limit, offset=offset, total=len(blueprints))
@api.route("/api/v0/blueprints/info", defaults={'blueprint_names': ""})
@api.route("/api/v0/blueprints/info/<blueprint_names>")
@v0_api.route("/blueprints/info", defaults={'blueprint_names': ""})
@v0_api.route("/blueprints/info/<blueprint_names>")
@checkparams([("blueprint_names", "", "no blueprint names given")])
def v0_blueprints_info(blueprint_names):
"""Return the contents of the blueprint, or a list of blueprints"""
@ -1137,8 +1140,8 @@ def v0_api(api):
else:
return jsonify(changes=changes, blueprints=blueprints, errors=errors)
@api.route("/api/v0/blueprints/changes", defaults={'blueprint_names': ""})
@api.route("/api/v0/blueprints/changes/<blueprint_names>")
@v0_api.route("/blueprints/changes", defaults={'blueprint_names': ""})
@v0_api.route("/blueprints/changes/<blueprint_names>")
@checkparams([("blueprint_names", "", "no blueprint names given")])
def v0_blueprints_changes(blueprint_names):
"""Return the changes to a blueprint or list of blueprints"""
@ -1177,7 +1180,7 @@ def v0_api(api):
return jsonify(blueprints=blueprints, errors=errors, offset=offset, limit=limit)
@api.route("/api/v0/blueprints/new", methods=["POST"])
@v0_api.route("/blueprints/new", methods=["POST"])
def v0_blueprints_new():
"""Commit a new blueprint"""
branch = request.args.get("branch", "master")
@ -1205,8 +1208,8 @@ def v0_api(api):
else:
return jsonify(status=True)
@api.route("/api/v0/blueprints/delete", defaults={'blueprint_name': ""}, methods=["DELETE"])
@api.route("/api/v0/blueprints/delete/<blueprint_name>", methods=["DELETE"])
@v0_api.route("/blueprints/delete", defaults={'blueprint_name': ""}, methods=["DELETE"])
@v0_api.route("/blueprints/delete/<blueprint_name>", methods=["DELETE"])
@checkparams([("blueprint_name", "", "no blueprint name given")])
def v0_blueprints_delete(blueprint_name):
"""Delete a blueprint from git"""
@ -1227,7 +1230,7 @@ def v0_api(api):
else:
return jsonify(status=True)
@api.route("/api/v0/blueprints/workspace", methods=["POST"])
@v0_api.route("/blueprints/workspace", methods=["POST"])
def v0_blueprints_workspace():
"""Write a blueprint to the workspace"""
branch = request.args.get("branch", "master")
@ -1251,8 +1254,8 @@ def v0_api(api):
else:
return jsonify(status=True)
@api.route("/api/v0/blueprints/workspace", defaults={'blueprint_name': ""}, methods=["DELETE"])
@api.route("/api/v0/blueprints/workspace/<blueprint_name>", methods=["DELETE"])
@v0_api.route("/blueprints/workspace", defaults={'blueprint_name': ""}, methods=["DELETE"])
@v0_api.route("/blueprints/workspace/<blueprint_name>", methods=["DELETE"])
@checkparams([("blueprint_name", "", "no blueprint name given")])
def v0_blueprints_delete_workspace(blueprint_name):
"""Delete a blueprint from the workspace"""
@ -1272,9 +1275,9 @@ def v0_api(api):
else:
return jsonify(status=True)
@api.route("/api/v0/blueprints/undo", defaults={'blueprint_name': "", 'commit': ""}, methods=["POST"])
@api.route("/api/v0/blueprints/undo/<blueprint_name>", defaults={'commit': ""}, methods=["POST"])
@api.route("/api/v0/blueprints/undo/<blueprint_name>/<commit>", methods=["POST"])
@v0_api.route("/blueprints/undo", defaults={'blueprint_name': "", 'commit': ""}, methods=["POST"])
@v0_api.route("/blueprints/undo/<blueprint_name>", defaults={'commit': ""}, methods=["POST"])
@v0_api.route("/blueprints/undo/<blueprint_name>/<commit>", methods=["POST"])
@checkparams([("blueprint_name", "", "no blueprint name given"),
("commit", "", "no commit ID given")])
def v0_blueprints_undo(blueprint_name, commit):
@ -1299,8 +1302,8 @@ def v0_api(api):
else:
return jsonify(status=True)
@api.route("/api/v0/blueprints/tag", defaults={'blueprint_name': ""}, methods=["POST"])
@api.route("/api/v0/blueprints/tag/<blueprint_name>", methods=["POST"])
@v0_api.route("/blueprints/tag", defaults={'blueprint_name': ""}, methods=["POST"])
@v0_api.route("/blueprints/tag/<blueprint_name>", methods=["POST"])
@checkparams([("blueprint_name", "", "no blueprint name given")])
def v0_blueprints_tag(blueprint_name):
"""Tag a blueprint's latest blueprint commit as a 'revision'"""
@ -1323,10 +1326,10 @@ def v0_api(api):
else:
return jsonify(status=True)
@api.route("/api/v0/blueprints/diff", defaults={'blueprint_name': "", 'from_commit': "", 'to_commit': ""})
@api.route("/api/v0/blueprints/diff/<blueprint_name>", defaults={'from_commit': "", 'to_commit': ""})
@api.route("/api/v0/blueprints/diff/<blueprint_name>/<from_commit>", defaults={'to_commit': ""})
@api.route("/api/v0/blueprints/diff/<blueprint_name>/<from_commit>/<to_commit>")
@v0_api.route("/blueprints/diff", defaults={'blueprint_name': "", 'from_commit': "", 'to_commit': ""})
@v0_api.route("/blueprints/diff/<blueprint_name>", defaults={'from_commit': "", 'to_commit': ""})
@v0_api.route("/blueprints/diff/<blueprint_name>/<from_commit>", defaults={'to_commit': ""})
@v0_api.route("/blueprints/diff/<blueprint_name>/<from_commit>/<to_commit>")
@checkparams([("blueprint_name", "", "no blueprint name given"),
("from_commit", "", "no from commit ID given"),
("to_commit", "", "no to commit ID given")])
@ -1340,7 +1343,7 @@ def v0_api(api):
if VALID_API_STRING.match(branch) is None:
return jsonify(status=False, errors=[{"id": INVALID_CHARS, "msg": "Invalid characters in branch argument"}]), 400
if not blueprint_exists(api, branch, blueprint_name):
if not blueprint_exists(branch, blueprint_name):
return jsonify(status=False, errors=[{"id": UNKNOWN_BLUEPRINT, "msg": "Unknown blueprint name: %s" % blueprint_name}])
try:
@ -1375,8 +1378,8 @@ def v0_api(api):
diff = recipe_diff(old_blueprint, new_blueprint)
return jsonify(diff=diff)
@api.route("/api/v0/blueprints/freeze", defaults={'blueprint_names': ""})
@api.route("/api/v0/blueprints/freeze/<blueprint_names>")
@v0_api.route("/blueprints/freeze", defaults={'blueprint_names': ""})
@v0_api.route("/blueprints/freeze/<blueprint_names>")
@checkparams([("blueprint_names", "", "no blueprint names given")])
def v0_blueprints_freeze(blueprint_names):
"""Return the blueprint with the exact modules and packages selected by depsolve"""
@ -1441,8 +1444,8 @@ def v0_api(api):
else:
return jsonify(blueprints=blueprints, errors=errors)
@api.route("/api/v0/blueprints/depsolve", defaults={'blueprint_names': ""})
@api.route("/api/v0/blueprints/depsolve/<blueprint_names>")
@v0_api.route("/blueprints/depsolve", defaults={'blueprint_names': ""})
@v0_api.route("/blueprints/depsolve/<blueprint_names>")
@checkparams([("blueprint_names", "", "no blueprint names given")])
def v0_blueprints_depsolve(blueprint_names):
"""Return the dependencies for a blueprint"""
@ -1506,7 +1509,7 @@ def v0_api(api):
return jsonify(blueprints=blueprints, errors=errors)
@api.route("/api/v0/projects/list")
@v0_api.route("/projects/list")
def v0_projects_list():
"""List all of the available projects/packages"""
try:
@ -1525,8 +1528,8 @@ def v0_api(api):
projects = take_limits(available, offset, limit)
return jsonify(projects=projects, offset=offset, limit=limit, total=len(available))
@api.route("/api/v0/projects/info", defaults={'project_names': ""})
@api.route("/api/v0/projects/info/<project_names>")
@v0_api.route("/projects/info", defaults={'project_names': ""})
@v0_api.route("/projects/info/<project_names>")
@checkparams([("project_names", "", "no project names given")])
def v0_projects_info(project_names):
"""Return detailed information about the listed projects"""
@ -1547,8 +1550,8 @@ def v0_api(api):
return jsonify(projects=projects)
@api.route("/api/v0/projects/depsolve", defaults={'project_names': ""})
@api.route("/api/v0/projects/depsolve/<project_names>")
@v0_api.route("/projects/depsolve", defaults={'project_names': ""})
@v0_api.route("/projects/depsolve/<project_names>")
@checkparams([("project_names", "", "no project names given")])
def v0_projects_depsolve(project_names):
"""Return detailed information about the listed projects"""
@ -1569,7 +1572,7 @@ def v0_api(api):
return jsonify(projects=deps)
@api.route("/api/v0/projects/source/list")
@v0_api.route("/projects/source/list")
def v0_projects_source_list():
"""Return the list of source names"""
with api.config["DNFLOCK"].lock:
@ -1577,8 +1580,8 @@ def v0_api(api):
sources = sorted([r.id for r in repos])
return jsonify(sources=sources)
@api.route("/api/v0/projects/source/info", defaults={'source_names': ""})
@api.route("/api/v0/projects/source/info/<source_names>")
@v0_api.route("/projects/source/info", defaults={'source_names': ""})
@v0_api.route("/projects/source/info/<source_names>")
@checkparams([("source_names", "", "no source names given")])
def v0_projects_source_info(source_names):
"""Return detailed info about the list of sources"""
@ -1614,7 +1617,7 @@ def v0_api(api):
else:
return jsonify(sources=sources, errors=errors)
@api.route("/api/v0/projects/source/new", methods=["POST"])
@v0_api.route("/projects/source/new", methods=["POST"])
def v0_projects_source_new():
"""Add a new package source. Or change an existing one"""
if request.headers['Content-Type'] == "text/x-toml":
@ -1673,8 +1676,8 @@ def v0_api(api):
return jsonify(status=True)
@api.route("/api/v0/projects/source/delete", defaults={'source_name': ""}, methods=["DELETE"])
@api.route("/api/v0/projects/source/delete/<source_name>", methods=["DELETE"])
@v0_api.route("/projects/source/delete", defaults={'source_name': ""}, methods=["DELETE"])
@v0_api.route("/projects/source/delete/<source_name>", methods=["DELETE"])
@checkparams([("source_name", "", "no source name given")])
def v0_projects_source_delete(source_name):
"""Delete the named source and return a status response"""
@ -1703,8 +1706,8 @@ def v0_api(api):
return jsonify(status=True)
@api.route("/api/v0/modules/list")
@api.route("/api/v0/modules/list/<module_names>")
@v0_api.route("/modules/list")
@v0_api.route("/modules/list/<module_names>")
def v0_modules_list(module_names=None):
"""List available modules, filtering by module_names"""
if module_names and VALID_API_STRING.match(module_names) is None:
@ -1734,8 +1737,8 @@ def v0_api(api):
modules = take_limits(available, offset, limit)
return jsonify(modules=modules, offset=offset, limit=limit, total=len(available))
@api.route("/api/v0/modules/info", defaults={'module_names': ""})
@api.route("/api/v0/modules/info/<module_names>")
@v0_api.route("/modules/info", defaults={'module_names': ""})
@v0_api.route("/modules/info/<module_names>")
@checkparams([("module_names", "", "no module names given")])
def v0_modules_info(module_names):
"""Return detailed information about the listed modules"""
@ -1755,7 +1758,7 @@ def v0_api(api):
return jsonify(modules=modules)
@api.route("/api/v0/compose", methods=["POST"])
@v0_api.route("/compose", methods=["POST"])
def v0_compose_start():
"""Start a compose
@ -1795,7 +1798,7 @@ def v0_api(api):
if VALID_API_STRING.match(blueprint_name) is None:
errors.append({"id": INVALID_CHARS, "msg": "Invalid characters in API path"})
if not blueprint_exists(api, branch, blueprint_name):
if not blueprint_exists(branch, blueprint_name):
errors.append({"id": UNKNOWN_BLUEPRINT, "msg": "Unknown blueprint name: %s" % blueprint_name})
if errors:
@ -1812,7 +1815,7 @@ def v0_api(api):
return jsonify(status=True, build_id=build_id)
@api.route("/api/v0/compose/types")
@v0_api.route("/compose/types")
def v0_compose_types():
"""Return the list of enabled output types
@ -1821,23 +1824,23 @@ def v0_api(api):
share_dir = api.config["COMPOSER_CFG"].get("composer", "share_dir")
return jsonify(types=[{"name": k, "enabled": True} for k in compose_types(share_dir)])
@api.route("/api/v0/compose/queue")
@v0_api.route("/compose/queue")
def v0_compose_queue():
"""Return the status of the new and running queues"""
return jsonify(queue_status(api.config["COMPOSER_CFG"]))
@api.route("/api/v0/compose/finished")
@v0_api.route("/compose/finished")
def v0_compose_finished():
"""Return the list of finished composes"""
return jsonify(finished=build_status(api.config["COMPOSER_CFG"], "FINISHED"))
@api.route("/api/v0/compose/failed")
@v0_api.route("/compose/failed")
def v0_compose_failed():
"""Return the list of failed composes"""
return jsonify(failed=build_status(api.config["COMPOSER_CFG"], "FAILED"))
@api.route("/api/v0/compose/status", defaults={'uuids': ""})
@api.route("/api/v0/compose/status/<uuids>")
@v0_api.route("/compose/status", defaults={'uuids': ""})
@v0_api.route("/compose/status/<uuids>")
@checkparams([("uuids", "", "no UUIDs given")])
def v0_compose_status(uuids):
"""Return the status of the listed uuids"""
@ -1879,8 +1882,8 @@ def v0_api(api):
return jsonify(uuids=results, errors=errors)
@api.route("/api/v0/compose/cancel", defaults={'uuid': ""}, methods=["DELETE"])
@api.route("/api/v0/compose/cancel/<uuid>", methods=["DELETE"])
@v0_api.route("/compose/cancel", defaults={'uuid': ""}, methods=["DELETE"])
@v0_api.route("/compose/cancel/<uuid>", methods=["DELETE"])
@checkparams([("uuid", "", "no UUID given")])
def v0_compose_cancel(uuid):
"""Cancel a running compose and delete its results directory"""
@ -1901,8 +1904,8 @@ def v0_api(api):
else:
return jsonify(status=True, uuid=uuid)
@api.route("/api/v0/compose/delete", defaults={'uuids': ""}, methods=["DELETE"])
@api.route("/api/v0/compose/delete/<uuids>", methods=["DELETE"])
@v0_api.route("/compose/delete", defaults={'uuids': ""}, methods=["DELETE"])
@v0_api.route("/compose/delete/<uuids>", methods=["DELETE"])
@checkparams([("uuids", "", "no UUIDs given")])
def v0_compose_delete(uuids):
"""Delete the compose results for the listed uuids"""
@ -1926,8 +1929,8 @@ def v0_api(api):
results.append({"uuid":uuid, "status":True})
return jsonify(uuids=results, errors=errors)
@api.route("/api/v0/compose/info", defaults={'uuid': ""})
@api.route("/api/v0/compose/info/<uuid>")
@v0_api.route("/compose/info", defaults={'uuid': ""})
@v0_api.route("/compose/info/<uuid>")
@checkparams([("uuid", "", "no UUID given")])
def v0_compose_info(uuid):
"""Return detailed info about a compose"""
@ -1944,8 +1947,8 @@ def v0_api(api):
else:
return jsonify(**info)
@api.route("/api/v0/compose/metadata", defaults={'uuid': ""})
@api.route("/api/v0/compose/metadata/<uuid>")
@v0_api.route("/compose/metadata", defaults={'uuid': ""})
@v0_api.route("/compose/metadata/<uuid>")
@checkparams([("uuid","", "no UUID given")])
def v0_compose_metadata(uuid):
"""Return a tar of the metadata for the build"""
@ -1963,8 +1966,8 @@ def v0_api(api):
headers=[("Content-Disposition", "attachment; filename=%s-metadata.tar;" % uuid)],
direct_passthrough=True)
@api.route("/api/v0/compose/results", defaults={'uuid': ""})
@api.route("/api/v0/compose/results/<uuid>")
@v0_api.route("/compose/results", defaults={'uuid': ""})
@v0_api.route("/compose/results/<uuid>")
@checkparams([("uuid","", "no UUID given")])
def v0_compose_results(uuid):
"""Return a tar of the metadata and the results for the build"""
@ -1982,8 +1985,8 @@ def v0_api(api):
headers=[("Content-Disposition", "attachment; filename=%s.tar;" % uuid)],
direct_passthrough=True)
@api.route("/api/v0/compose/logs", defaults={'uuid': ""})
@api.route("/api/v0/compose/logs/<uuid>")
@v0_api.route("/compose/logs", defaults={'uuid': ""})
@v0_api.route("/compose/logs/<uuid>")
@checkparams([("uuid","", "no UUID given")])
def v0_compose_logs(uuid):
"""Return a tar of the metadata for the build"""
@ -2001,8 +2004,8 @@ def v0_api(api):
headers=[("Content-Disposition", "attachment; filename=%s-logs.tar;" % uuid)],
direct_passthrough=True)
@api.route("/api/v0/compose/image", defaults={'uuid': ""})
@api.route("/api/v0/compose/image/<uuid>")
@v0_api.route("/compose/image", defaults={'uuid': ""})
@v0_api.route("/compose/image/<uuid>")
@checkparams([("uuid","", "no UUID given")])
def v0_compose_image(uuid):
"""Return the output image for the build"""
@ -2026,8 +2029,8 @@ def v0_api(api):
# XXX - Will mime type guessing work for all our output?
return send_file(image_path, as_attachment=True, attachment_filename=image_name, add_etags=False)
@api.route("/api/v0/compose/log", defaults={'uuid': ""})
@api.route("/api/v0/compose/log/<uuid>")
@v0_api.route("/compose/log", defaults={'uuid': ""})
@v0_api.route("/compose/log/<uuid>")
@checkparams([("uuid","", "no UUID given")])
def v0_compose_log_tail(uuid):
"""Return the end of the main anaconda.log, defaults to 1Mbytes"""