-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest2raml.js
346 lines (319 loc) · 9.99 KB
/
rest2raml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const fs = require("fs")
const YAML = require("js-yaml")
// TODO const wap = require("webapi-parser").WebApiParser
const rosSchemaFilename = "./ros-inspect"
const ramlSchemaFilename = "./ros-rest"
const { parseArgs } = require("util")
async function main() {
// STEP ZERO: calling the script "manually"...
// a. install bun (on MacOS, "brew install oven-sh/bun/bun")
// b. save script this to a new directory, like ~/rest2raml/...
// c. install YAML parse, "bun install js-yaml"
// d. Router's IP and authentication are provided by env variables that are provide in shell:
// > URLBASE=https://change.me/rest BASICAUTH=admin:changeme bun rest2raml.js
// e. Wait a while as for this code to run – may take an HOUR for entire schema to process
// f. Optionally, rest2raml.js takes args with path to start out, seperated by *spaces*:
// > bun rest2raml.js ip address
// So, assuming, done getting version for router should work...
const { opts, argPath } = parseArguments()
if (opts && opts.validate) {
// TODO
validateRaml(opts.validate)
return 1 // return error code since it's not implemented
}
const ver = await fetchVersion()
if (opts && opts.version) {
console.log(ver)
return 0
}
console.log(`Using version ${ver}...`)
// STEP ONE: use REST to traverse router's /console/inspect output (save to )
let rosSchema = {}
if (process.env.INSPECTFILE) {
rosSchema = JSON.parse(
fs.readFileSync(process.env.INSPECTFILE, { encoding: "utf-8" })
)
} else {
rosSchema = await parseChildren(argPath)
const rosSchemaPath = `${rosSchemaFilename}-${argPath.join("+") || "all"
}.json`
fs.writeFileSync(rosSchemaPath, JSON.stringify(rosSchema))
console.log(`Fetching /console/inspect data written to: ${rosSchemaPath}`)
}
// STEP TWO: process capture data into RAML for endpoints
const ramlSchema = parse(arrayToNestedObject(argPath, rosSchema))
// STEP THREE: add RAML boilerplate and output to
const ramlPath = `${ramlSchemaFilename}-${argPath.join("+") || "all"}.raml`
fs.writeFileSync(
ramlPath,
`#%RAML ${opts.ramlspec}\n` +
YAML.dump({
...generateRAMLPrefix(ver, argPath.join("+") || "all"),
...ramlSchema,
})
)
console.log(`Done, exported ${ramlPath}`)
}
function parseArguments() {
const { values, positionals } = parseArgs({
args: Bun.argv,
options: {
version: {
type: "boolean",
},
validate: {
type: "string",
},
ramlspec: {
type: "string",
default: "1.0"
}
},
strict: true,
allowPositionals: true,
})
const [, , ...argPath] = positionals
const opts = values
return { opts, argPath }
}
async function validateRaml(filename) {
// const raml = fs.readFileSync(filename, { encoding: "utf-8" })
// const model = await wap.raml10.parse(raml)
// const report = await wap.raml10.validate(model)
// console.log("Validation errors:", report.toString())
console.warn("Not implemented.")
}
function generateRAMLPrefix(ver = "7.0", tag = "dev") {
return {
title: `RouterOS REST Schema v${ver} (${tag})`,
version: `v${ver}`,
protocols: ["https", "http"],
mediaType: ["application/json"],
securitySchemes: {
basic: {
description:
"Mikrotik REST API only supports Basic Authentication, secured by HTTPS\n",
type: "Basic Authentication",
},
},
securedBy: ["basic"],
baseUri: "https://{host}:{port}/rest",
baseUriParameters: {
host: {
description: "RouterOS device IP or host name",
default: "192.168.88.1",
},
port: {
description: "RouterOS https port to use",
default: "443",
},
},
documentation: [
{
title: "RouterOS RAML Schema for REST API",
content:
"Schema is generated using `/console/inspect` from a RouterOS device, and\ninterpreted into a schema based on the rules in\n[Mikrotik REST documentation](https://help.mikrotik.com)\n",
},
],
}
}
function arrayToNestedObject(arr, lastValue = {}, initialValue = {}) {
let current = initialValue
for (let i = 0; i < arr.length; i++) {
const item = arr[i]
if (i === arr.length - 1) {
current[item] = lastValue
} else {
current[item] = {}
current = current[item]
}
}
if (arr.length == 0) {
return lastValue
}
return initialValue
}
async function fetchPost(url, body) {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(body),
credentials: "include",
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(process.env.BASICAUTH)}`,
},
})
return await response.json()
}
async function fetchVersion() {
const resturl = `${process.env.URLBASE}/system/resource/get`
const body = {
"value-name": "version",
}
let resp = await fetchPost(resturl, body)
return resp.ret.split(" ")[0]
}
async function fetchInspect(what, path, input = "") {
const resturl = `${process.env.URLBASE}/console/inspect`
const body = {
request: what,
path: path,
}
console.log(`Inspecting... ${path} (${what})`)
return await fetchPost(resturl, body)
}
async function fetchSyntax(path = []) {
return await fetchInspect("syntax", path.toString())
}
async function fetchChild(path = []) {
return await fetchInspect("child", path.toString())
}
async function parseChildren(rpath = [], memo = {}) {
let start = memo
let children = await fetchChild(rpath)
for (const child of children) {
if (child.type == "child") {
const newpath = [...rpath, child.name]
memo[child.name] = { _type: child["node-type"] }
// try {
if (child["node-type"] == "arg") {
if (
newpath.includes("where") ||
newpath.includes("do") ||
newpath.includes("else") ||
newpath.includes("rule") ||
newpath.includes("command") ||
newpath.includes("on-error")
) {
// these crash the REST server, skipping
} else {
const syntax = await fetchSyntax(newpath)
if (syntax.length == 1 && syntax[0].text.length > 0) {
memo[child.name].desc = syntax[0].text
}
}
}
//catch (e) {
//console.error("error", e)
//}
await parseChildren(Array.from(newpath), memo[child.name])
}
}
return start
}
function ramlRestResponses(successJsonType = "any") {
return {
200: {
description: "Success",
body: { "application/json": { type: successJsonType } },
},
400: {
description: "Bad command or error",
body: { "application/json": { type: "object" } },
},
401: {
description: "Unauthorized",
body: { "application/json": { type: "object" } },
},
}
}
function cmdToGetQueryParams(obj) {
var props = {}
Object.entries(obj)
.filter((i) => i[1]._type == "arg")
.map((j) => {
props[j[0]] = {
type: "any",
required: false,
description: j[1].description,
}
})
return props
}
function cmdToPostSchema(obj) {
let op = {}
op.description = obj.desc
op.body = {}
op.body["application/json"] = { type: "object" }
let props = (op.body["application/json"].properties = {})
Object.entries(obj)
.filter((i) => i[1]._type == "arg")
.map((j) => {
props[j[0]] = {
type: "any",
required: false,
description: j[1].description,
}
//delete obj[j[0]]
//delete obj.type
})
op.responses = ramlRestResponses()
return op
}
function parse(obj) {
function parser(currentObj) {
for (const key in currentObj) {
const prev = currentObj[key]
if (currentObj[key]._type == "cmd") {
if (typeof currentObj[`/${key}`] !== "object")
currentObj[`/${key}`] = {}
currentObj[`/${key}`].post = cmdToPostSchema(prev)
currentObj[`/${key}`].post.body["application/json"].properties[
".proplist"
] = {
type: "any",
required: false,
}
currentObj[`/${key}`].post.body["application/json"].properties[
".query"
] = {
type: "array",
required: false,
}
if (key == "get") {
const getqueryparams = cmdToGetQueryParams(currentObj["get"])
currentObj["get"] = {
queryParameters: getqueryparams,
responses: ramlRestResponses("array"),
}
if (typeof currentObj["/{id}"] !== "object") currentObj["/{id}"] = {}
currentObj["/{id}"].get = { responses: ramlRestResponses() }
currentObj["/{id}"].uriParameters = { id: { type: "string" } }
}
if (key == "set") {
if (typeof currentObj["/{id}"] !== "object") currentObj["/{id}"] = {}
currentObj["/{id}"].patch = { ...cmdToPostSchema(currentObj[key]) }
currentObj["/{id}"].uriParameters = { id: { type: "string" } }
}
if (key == "add") {
currentObj.put = cmdToPostSchema(currentObj[key])
}
if (key == "remove") {
if (typeof currentObj["/{id}"] !== "object") currentObj["/{id}"] = {}
currentObj["/{id}"].delete = { ...cmdToPostSchema(currentObj[key]) }
currentObj["/{id}"].uriParameters = { id: { type: "string" } }
}
if (key != "get") delete currentObj[key]
} else if (typeof currentObj[key] === "object") {
const src = currentObj[key]
currentObj[`/${key}`] = currentObj[key]
if (
currentObj[`/${key}`]._type == "path" ||
currentObj[`/${key}`]._type == "dir"
) {
delete currentObj[`/${key}`]._type
}
parser(currentObj[`/${key}`])
delete currentObj[key]
}
}
return currentObj
}
return parser(obj)
}
await main()
// To build with bun...
// bun build ros2raml.js --compile --outfile ros2raml
// To general HTML from RAML...
// bun install raml2html raml2html-slate-theme
// raml2html --theme raml2html-slate-theme ros-rest-all.raml > ros-rest.all.html