Skip to content

Commit c2b6402

Browse files
HDDS-10684. Add Swagger API for Recon as a Docusaurus page. (#264)
1 parent c975a67 commit c2b6402

10 files changed

Lines changed: 4002 additions & 14 deletions

File tree

.yamllint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ ignore:
2222
- node_modules
2323
- "**/static/docs"
2424
- build
25+
- static/recon-api.yaml
26+
- "**/recon-api.yaml"
2527

2628
rules:
2729
anchors: enable

docs/05-administrator-guide/03-operations/09-observability/02-recon/02-recon-rest-api.md

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Recon REST API
2+
3+
import SwaggerUI from '@site/src/components/SwaggerUI';
4+
5+
Ozone Recon's public **REST API** follows the [OpenAPI specification](https://www.openapis.org/), and is documented below.
6+
7+
The API is defined in the [recon-api.yaml](/recon-api.yaml) OpenAPI specification file and is available under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0.html).
8+
9+
<SwaggerUI spec="/recon-api.yaml" defaultServer="http://localhost:9888" />

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"prism-react-renderer": "^2.4.1",
2828
"react": "^18.3.1",
2929
"react-bootstrap-icons": "^1.11.5",
30-
"react-dom": "^18.3.1"
30+
"react-dom": "^18.3.1",
31+
"swagger-ui-react": "^5.31.0"
3132
},
3233
"devDependencies": {
3334
"@cspell/dict-docker": "^1.1.12",

pnpm-lock.yaml

Lines changed: 1171 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/SwaggerUI/index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import React, { useState, useRef, useEffect } from 'react';
21+
import SwaggerUI from 'swagger-ui-react';
22+
import 'swagger-ui-react/swagger-ui.css';
23+
import useBaseUrl from '@docusaurus/useBaseUrl';
24+
import styles from './styles.module.css';
25+
26+
export default function SwaggerUIComponent({ spec, defaultServer }) {
27+
const specUrl = useBaseUrl(spec);
28+
const [serverUrl, setServerUrl] = useState(defaultServer || 'http://localhost:9888');
29+
const [apiVersion, setApiVersion] = useState('v1');
30+
const [appliedServerUrl, setAppliedServerUrl] = useState(defaultServer || 'http://localhost:9888');
31+
const [appliedApiVersion, setAppliedApiVersion] = useState('v1');
32+
const swaggerSystemRef = useRef(null);
33+
34+
// Update the server URL in Swagger only when applied values change
35+
useEffect(() => {
36+
if (swaggerSystemRef.current) {
37+
const spec = swaggerSystemRef.current.getState().getIn(['spec', 'json']);
38+
if (spec) {
39+
// Construct full URL: {serverUrl}/api/{version}/
40+
const baseUrl = appliedServerUrl.endsWith('/') ? appliedServerUrl.slice(0, -1) : appliedServerUrl;
41+
const fullServerUrl = `${baseUrl}/api/${appliedApiVersion}/`;
42+
swaggerSystemRef.current.specActions.updateJsonSpec({
43+
...spec.toJS(),
44+
servers: [{ url: fullServerUrl, description: 'Configured Recon Server' }]
45+
});
46+
}
47+
}
48+
}, [appliedServerUrl, appliedApiVersion]);
49+
50+
const handleApply = () => {
51+
setAppliedServerUrl(serverUrl);
52+
setAppliedApiVersion(apiVersion);
53+
};
54+
55+
return (
56+
<div className={styles.swaggerWrapper}>
57+
<div className={styles.serverConfig}>
58+
<label htmlFor="recon-server-url" className={styles.serverLabel}>
59+
Recon Server URL:
60+
</label>
61+
<input
62+
id="recon-server-url"
63+
type="text"
64+
value={serverUrl}
65+
onChange={(e) => setServerUrl(e.target.value)}
66+
placeholder="http://localhost:9888"
67+
className={styles.serverInput}
68+
/>
69+
<label htmlFor="recon-api-version" className={styles.serverLabel}>
70+
API Version:
71+
</label>
72+
<input
73+
id="recon-api-version"
74+
type="text"
75+
value={apiVersion}
76+
onChange={(e) => setApiVersion(e.target.value)}
77+
placeholder="v1"
78+
className={styles.versionInput}
79+
/>
80+
<button
81+
onClick={handleApply}
82+
className={styles.applyButton}
83+
type="button"
84+
>
85+
Apply
86+
</button>
87+
<span className={styles.serverHint}>
88+
(Default for Docker quick start. Change server/version and click Apply.)
89+
</span>
90+
</div>
91+
<SwaggerUI
92+
url={specUrl}
93+
docExpansion="list"
94+
defaultModelsExpandDepth={1}
95+
defaultModelExpandDepth={1}
96+
onComplete={(system) => {
97+
// Store the system reference for later updates
98+
swaggerSystemRef.current = system;
99+
// Set initial server URL with API version
100+
const spec = system.getState().getIn(['spec', 'json']);
101+
if (spec) {
102+
const baseUrl = appliedServerUrl.endsWith('/') ? appliedServerUrl.slice(0, -1) : appliedServerUrl;
103+
const fullServerUrl = `${baseUrl}/api/${appliedApiVersion}/`;
104+
system.specActions.updateJsonSpec({
105+
...spec.toJS(),
106+
servers: [{ url: fullServerUrl, description: 'Configured Recon Server' }]
107+
});
108+
}
109+
}}
110+
/>
111+
</div>
112+
);
113+
}

0 commit comments

Comments
 (0)