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
1diff --git a/src/maasserver/api/doc.py b/src/maasserver/api/doc.py
2index 6bc7ae2..4e5c3d4 100644
3--- a/src/maasserver/api/doc.py
4+++ b/src/maasserver/api/doc.py
5@@ -36,6 +36,35 @@ def get_api_description():
6 return description
7
8
9+def get_api_landing_page():
10+ """Return the API landing page"""
11+ description = {
12+ "title": "MAAS API",
13+ "description": "API landing page for MAAS",
14+ "resources": [
15+ {
16+ "path": "/MAAS/api",
17+ "rel": "self",
18+ "type": "application/json",
19+ "title": "this document",
20+ },
21+ {
22+ "path": "/MAAS/api/2.0/openapi.yaml",
23+ "rel": "service-desc",
24+ "type": "application/openapi+yaml",
25+ "title": "the API definition",
26+ },
27+ {
28+ "path": "/MAAS/docs/api.html",
29+ "rel": "service-doc",
30+ "type": "text/html",
31+ "title": "the API documentation",
32+ },
33+ ],
34+ }
35+ return description
36+
37+
38 def find_api_resources(urlconf=None):
39 """Find the API resources defined in `urlconf`.
40
41diff --git a/src/maasserver/api/doc_handler.py b/src/maasserver/api/doc_handler.py
42index 3dd41c6..f8e5ba8 100644
43--- a/src/maasserver/api/doc_handler.py
44+++ b/src/maasserver/api/doc_handler.py
45@@ -71,6 +71,7 @@ from maasserver.api.doc import (
46 generate_pod_types_doc,
47 generate_power_types_doc,
48 get_api_description,
49+ get_api_landing_page,
50 )
51 from maasserver.api.templates import APITemplateRenderer
52 from maasserver.utils import build_absolute_uri
53@@ -242,3 +243,19 @@ def describe(request):
54 json.dumps(description, sort_keys=True),
55 content_type="application/json",
56 )
57+
58+
59+def api_landing_page(request):
60+ """Render a landing page with pointers for the MAAS API.
61+
62+ :return: An `HttpResponse` containing a JSON page with pointers to both
63+ human-readable documentation and api definitions.
64+ """
65+ description = get_api_landing_page()
66+ for link in description["resources"]:
67+ link["href"] = build_absolute_uri(request, link["path"])
68+ # Return as a JSON document
69+ return HttpResponse(
70+ json.dumps(description),
71+ content_type="application/json",
72+ )
73diff --git a/src/maasserver/api/tests/test_doc.py b/src/maasserver/api/tests/test_doc.py
74index 5873438..cb9d061 100644
75--- a/src/maasserver/api/tests/test_doc.py
76+++ b/src/maasserver/api/tests/test_doc.py
77@@ -2,6 +2,7 @@
78 # GNU Affero General Public License version 3 (see the file LICENSE).
79
80 from inspect import getdoc
81+import json
82 import random
83 import types
84 from unittest.mock import sentinel
85@@ -40,7 +41,7 @@ from maasserver.api.doc import (
86 generate_power_types_doc,
87 get_api_description,
88 )
89-from maasserver.api.doc_handler import render_api_docs
90+from maasserver.api.doc_handler import api_landing_page, render_api_docs
91 from maasserver.api.support import (
92 operation,
93 OperationsHandler,
94@@ -79,6 +80,19 @@ class TestGetAPIDescription(MAASTestCase):
95 mock_get_api_description_hash.assert_called_once()
96
97
98+class TestLandingPage(MAASTestCase):
99+ def test_links(self):
100+ request = factory.make_fake_request()
101+ landing_page = api_landing_page(request)
102+ content = json.loads(landing_page.content)
103+ resources = content["resources"]
104+ host = request.get_host()
105+ for link in resources:
106+ href = f"http://{host}{link['path']}"
107+ self.assertEqual(link["href"], href)
108+ self.assertEqual(resources[0]["type"], landing_page["content-type"])
109+
110+
111 class TestFindingResources(MAASTestCase):
112 """Tests for API inspection support: finding resources."""
113
114diff --git a/src/maasserver/urls.py b/src/maasserver/urls.py
115index 72bc2be..404dc11 100644
116--- a/src/maasserver/urls.py
117+++ b/src/maasserver/urls.py
118@@ -9,6 +9,7 @@ from django.urls import include, re_path
119 from django.views.generic import TemplateView
120
121 from maasserver import urls_api
122+from maasserver.api.doc_handler import api_landing_page
123 from maasserver.bootresources import (
124 simplestreams_file_handler,
125 simplestreams_stream_handler,
126@@ -76,6 +77,7 @@ urlpatterns += [re_path(r"^accounts/logout/$", logout, name="logout")]
127
128 # API URLs. If old API requested, provide error message directing to new API.
129 urlpatterns += [
130+ re_path(r"^api/$", api_landing_page),
131 re_path(r"^api/2\.0/", include(urls_api)),
132 re_path(
133 r"^api/version/",

Subscribers

People subscribed via source and target branches