Merge ~lloydwaltersj/maas:api-landing-page into maas:master

Proposed by Jack Lloyd-Walters
Status: Merged
Approved by: Jack Lloyd-Walters
Approved revision: 6e079a9a9f3ca631f50a611a559e37f01eea800c
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~lloydwaltersj/maas:api-landing-page
Merge into: maas:master
Diff against target: 133 lines (+63/-1)
4 files modified
src/maasserver/api/doc.py (+29/-0)
src/maasserver/api/doc_handler.py (+17/-0)
src/maasserver/api/tests/test_doc.py (+15/-1)
src/maasserver/urls.py (+2/-0)
Reviewer Review Type Date Requested Status
MAAS Lander Approve
Alexsander de Souza Approve
Review via email: mp+427167@code.launchpad.net

Commit message

Add API landing page

To post a comment you must log in.
Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) :
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b api-landing-page lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/130/consoleText
COMMIT: 2bd256e598efca53938789baa5528c821129c2c7

review: Needs Fixing
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b api-landing-page lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 3799f2adaf1b47330ad8d1881680d35814b4485d

review: Approve
Revision history for this message
Alberto Donato (ack) :
~lloydwaltersj/maas:api-landing-page updated
1fa18bb... by Jack Lloyd-Walters

inline json

Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) :
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b api-landing-page lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/134/consoleText
COMMIT: 1fa18bbfd1a8fb90f2201c38524c560a36289da2

review: Needs Fixing
Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b api-landing-page lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 1fa18bbfd1a8fb90f2201c38524c560a36289da2

review: Approve
Revision history for this message
Alexsander de Souza (alexsander-souza) wrote :

+1

review: Approve
Revision history for this message
Alexsander de Souza (alexsander-souza) :
~lloydwaltersj/maas:api-landing-page updated
6e079a9... by Jack Lloyd-Walters

initialize at once

Revision history for this message
Jack Lloyd-Walters (lloydwaltersj) :
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b api-landing-page lp:~lloydwaltersj/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 6e079a9a9f3ca631f50a611a559e37f01eea800c

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/maasserver/api/doc.py b/src/maasserver/api/doc.py
index 6bc7ae2..4e5c3d4 100644
--- a/src/maasserver/api/doc.py
+++ b/src/maasserver/api/doc.py
@@ -36,6 +36,35 @@ def get_api_description():
36 return description36 return description
3737
3838
39def get_api_landing_page():
40 """Return the API landing page"""
41 description = {
42 "title": "MAAS API",
43 "description": "API landing page for MAAS",
44 "resources": [
45 {
46 "path": "/MAAS/api",
47 "rel": "self",
48 "type": "application/json",
49 "title": "this document",
50 },
51 {
52 "path": "/MAAS/api/2.0/openapi.yaml",
53 "rel": "service-desc",
54 "type": "application/openapi+yaml",
55 "title": "the API definition",
56 },
57 {
58 "path": "/MAAS/docs/api.html",
59 "rel": "service-doc",
60 "type": "text/html",
61 "title": "the API documentation",
62 },
63 ],
64 }
65 return description
66
67
39def find_api_resources(urlconf=None):68def find_api_resources(urlconf=None):
40 """Find the API resources defined in `urlconf`.69 """Find the API resources defined in `urlconf`.
4170
diff --git a/src/maasserver/api/doc_handler.py b/src/maasserver/api/doc_handler.py
index 3dd41c6..f8e5ba8 100644
--- a/src/maasserver/api/doc_handler.py
+++ b/src/maasserver/api/doc_handler.py
@@ -71,6 +71,7 @@ from maasserver.api.doc import (
71 generate_pod_types_doc,71 generate_pod_types_doc,
72 generate_power_types_doc,72 generate_power_types_doc,
73 get_api_description,73 get_api_description,
74 get_api_landing_page,
74)75)
75from maasserver.api.templates import APITemplateRenderer76from maasserver.api.templates import APITemplateRenderer
76from maasserver.utils import build_absolute_uri77from maasserver.utils import build_absolute_uri
@@ -242,3 +243,19 @@ def describe(request):
242 json.dumps(description, sort_keys=True),243 json.dumps(description, sort_keys=True),
243 content_type="application/json",244 content_type="application/json",
244 )245 )
246
247
248def api_landing_page(request):
249 """Render a landing page with pointers for the MAAS API.
250
251 :return: An `HttpResponse` containing a JSON page with pointers to both
252 human-readable documentation and api definitions.
253 """
254 description = get_api_landing_page()
255 for link in description["resources"]:
256 link["href"] = build_absolute_uri(request, link["path"])
257 # Return as a JSON document
258 return HttpResponse(
259 json.dumps(description),
260 content_type="application/json",
261 )
diff --git a/src/maasserver/api/tests/test_doc.py b/src/maasserver/api/tests/test_doc.py
index 5873438..cb9d061 100644
--- a/src/maasserver/api/tests/test_doc.py
+++ b/src/maasserver/api/tests/test_doc.py
@@ -2,6 +2,7 @@
2# GNU Affero General Public License version 3 (see the file LICENSE).2# GNU Affero General Public License version 3 (see the file LICENSE).
33
4from inspect import getdoc4from inspect import getdoc
5import json
5import random6import random
6import types7import types
7from unittest.mock import sentinel8from unittest.mock import sentinel
@@ -40,7 +41,7 @@ from maasserver.api.doc import (
40 generate_power_types_doc,41 generate_power_types_doc,
41 get_api_description,42 get_api_description,
42)43)
43from maasserver.api.doc_handler import render_api_docs44from maasserver.api.doc_handler import api_landing_page, render_api_docs
44from maasserver.api.support import (45from maasserver.api.support import (
45 operation,46 operation,
46 OperationsHandler,47 OperationsHandler,
@@ -79,6 +80,19 @@ class TestGetAPIDescription(MAASTestCase):
79 mock_get_api_description_hash.assert_called_once()80 mock_get_api_description_hash.assert_called_once()
8081
8182
83class TestLandingPage(MAASTestCase):
84 def test_links(self):
85 request = factory.make_fake_request()
86 landing_page = api_landing_page(request)
87 content = json.loads(landing_page.content)
88 resources = content["resources"]
89 host = request.get_host()
90 for link in resources:
91 href = f"http://{host}{link['path']}"
92 self.assertEqual(link["href"], href)
93 self.assertEqual(resources[0]["type"], landing_page["content-type"])
94
95
82class TestFindingResources(MAASTestCase):96class TestFindingResources(MAASTestCase):
83 """Tests for API inspection support: finding resources."""97 """Tests for API inspection support: finding resources."""
8498
diff --git a/src/maasserver/urls.py b/src/maasserver/urls.py
index 72bc2be..404dc11 100644
--- a/src/maasserver/urls.py
+++ b/src/maasserver/urls.py
@@ -9,6 +9,7 @@ from django.urls import include, re_path
9from django.views.generic import TemplateView9from django.views.generic import TemplateView
1010
11from maasserver import urls_api11from maasserver import urls_api
12from maasserver.api.doc_handler import api_landing_page
12from maasserver.bootresources import (13from maasserver.bootresources import (
13 simplestreams_file_handler,14 simplestreams_file_handler,
14 simplestreams_stream_handler,15 simplestreams_stream_handler,
@@ -76,6 +77,7 @@ urlpatterns += [re_path(r"^accounts/logout/$", logout, name="logout")]
7677
77# API URLs. If old API requested, provide error message directing to new API.78# API URLs. If old API requested, provide error message directing to new API.
78urlpatterns += [79urlpatterns += [
80 re_path(r"^api/$", api_landing_page),
79 re_path(r"^api/2\.0/", include(urls_api)),81 re_path(r"^api/2\.0/", include(urls_api)),
80 re_path(82 re_path(
81 r"^api/version/",83 r"^api/version/",

Subscribers

People subscribed via source and target branches