-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate _opendistro endpoints through merger tool
Signed-off-by: Theo Truong <[email protected]>
- Loading branch information
Showing
10 changed files
with
698 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import fs from "fs"; | ||
import YAML from "yaml"; | ||
import { OperationPath, HttpVerb, ReplacedOperationMap } from "../types"; | ||
import { write2file } from "../helpers"; | ||
|
||
// One-time script to generate _replaced_operations.yaml file for OpenDistro | ||
// Keeping this for now in case we need to update the file in the near future. Can be removed after a few months. | ||
// TODO: Remove this file in 2025. | ||
export default class OpenDistro { | ||
input: Record<OperationPath, HttpVerb[]>; | ||
output: ReplacedOperationMap = {}; | ||
|
||
constructor(file_path: string) { | ||
this.input = YAML.parse(fs.readFileSync(file_path, 'utf8')); | ||
this.build_output(); | ||
write2file(file_path, this.output); | ||
} | ||
|
||
build_output() { | ||
for(const [path, operations] of Object.entries(this.input)) { | ||
const replaced_by = path.replace('_opendistro', '_plugins'); | ||
this.output[path] = { replaced_by, operations }; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {OperationSpec, ReplacedOperationMap} from "../types"; | ||
import YAML from "yaml"; | ||
import fs from "fs"; | ||
import _ from "lodash"; | ||
|
||
export default class ReplacedOpsGenerator { | ||
file_path: string; | ||
replaced_ops: ReplacedOperationMap; | ||
|
||
constructor(root_path: string) { | ||
this.file_path = root_path + '/_replaced_operations.yaml'; | ||
this.replaced_ops = YAML.parse(fs.readFileSync(this.file_path, 'utf8')); | ||
} | ||
|
||
generate(spec: Record<string, any>): void { | ||
for(const [path, { replaced_by, operations }] of _.entries(this.replaced_ops)) { | ||
const regex = this.path_to_regex(replaced_by); | ||
const operation_keys = operations.map(op => op.toLowerCase()); | ||
const replaced_path = this.copy_params(replaced_by, path); | ||
const path_entry = _.entries(spec.paths).find(([path, _]) => regex.test(path)); | ||
if(!path_entry) console.log(`Path not found: ${replaced_by}`); | ||
else spec.paths[replaced_path] = this.path_object(path_entry[1] as any, operation_keys); | ||
} | ||
} | ||
|
||
path_object(obj: Record<string, any>, keys: string[]): Record<string, any> { | ||
const cloned_obj = _.cloneDeep(_.pick(obj, keys)); | ||
for(const key in cloned_obj) { | ||
const operation = cloned_obj[key] as OperationSpec; | ||
operation.operationId = operation.operationId + '_replaced'; | ||
operation.deprecated = true; | ||
operation['x-ignorable'] = true; | ||
} | ||
return cloned_obj; | ||
} | ||
|
||
path_to_regex(path: string): RegExp { | ||
const source = '^' + path.replace(/\{.+?}/g, '\\{.+?\\}').replace(/\//g, '\\/') + '$'; | ||
return new RegExp(source, 'g'); | ||
} | ||
|
||
copy_params(source: string, target: string): string { | ||
const target_parts = target.split('/'); | ||
const target_params = target_parts.filter(part => part.startsWith('{')); | ||
const source_params = source.split('/').filter(part => part.startsWith('{')).reverse(); | ||
if(target_params.length !== source_params.length) | ||
throw new Error('Mismatched parameters in source and target paths: ' + source + ' -> ' + target); | ||
return target_parts.map((part) => part.startsWith('{') ? source_params.pop()! : part).join('/'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/replaced/adopting/{a}/something/{b}: | ||
replaced_by: /adopt/{animal}/dockets/{docket} | ||
operations: | ||
- GET | ||
- DELETE | ||
/something/else: | ||
replaced_by: /not/here | ||
operations: | ||
- POST | ||
- PUT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters