-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d273a1
commit e64c632
Showing
15 changed files
with
327 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ dist | |
node_modules | ||
.npm | ||
doc | ||
test/data/root/middleware | ||
test/data/root/logs | ||
/middleware | ||
/.vscode | ||
coverage |
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
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
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,66 @@ | ||
'use strict'; | ||
|
||
import { ArgumentParser } from 'argparse'; | ||
|
||
const parser = new ArgumentParser({ | ||
addHelp: true, | ||
description: 'Tree-Gateway Admin', | ||
version: '0.0.1' | ||
}); | ||
|
||
parser.addArgument(['-s', '--swagger'], { | ||
help: 'Inform the URL pointing to the swagger file on the Tree Gateway target server', | ||
required: true | ||
}); | ||
|
||
parser.addArgument(['-u', '--username'], { | ||
help: 'Inform the user to sign in into the gateway API', | ||
required: true | ||
}); | ||
|
||
parser.addArgument(['-p', '--password'], { | ||
help: 'Inform the user password to sign in into the gateway API', | ||
required: true | ||
}); | ||
|
||
const commands = parser.addSubparsers({ | ||
dest: 'command', | ||
title: 'Commands (For help, <command> -h/--help)' | ||
}); | ||
|
||
const apisCommand = commands.addParser('apis', { | ||
addHelp: true, | ||
help: 'APIs configurations' | ||
}); | ||
|
||
apisCommand.addArgument( | ||
['-l', '--list'], { | ||
help: 'Inform the search params for API listing. Ex: name:test|version:0.0.1' | ||
} | ||
); | ||
|
||
apisCommand.addArgument( | ||
['-a', '--add'], { | ||
help: 'Inform the path to the api config file (JSON format) to be added', | ||
} | ||
); | ||
|
||
apisCommand.addArgument( | ||
['-u', '--update'], { | ||
help: 'Inform the path to the api config file (JSON format) to be updated', | ||
} | ||
); | ||
|
||
apisCommand.addArgument( | ||
['-r', '--remove'], { | ||
help: 'Inform the path to the api config file (JSON format) to be removed', | ||
} | ||
); | ||
|
||
apisCommand.addArgument( | ||
['-g', '--get'], { | ||
help: 'Inform the path to the api config file (JSON format) to be retrieved', | ||
} | ||
); | ||
|
||
export let configArgs = parser.parseArgs(); |
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,82 @@ | ||
'use strict'; | ||
|
||
import {SDK} from './sdk'; | ||
import { ApiConfig } from '../../config/api'; | ||
import * as fs from 'fs-extra-promise'; | ||
|
||
export class Cli { | ||
private args: any; | ||
|
||
constructor(configArgs: any) { | ||
this.args = configArgs; | ||
} | ||
|
||
processCommand() { | ||
this.doCommand() | ||
.then(() => { | ||
console.info(`Command ${this.args.command} completed.`); | ||
}).catch((err: any) => { | ||
console.error(`${err}`); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
private doCommand(): Promise<void> { | ||
switch (this.args.command) { | ||
case 'apis': | ||
return this.processApis(); | ||
default: | ||
return new Promise<void>((resolve, reject) => reject(`Command not found: ${this.args.command}`)); | ||
} | ||
} | ||
|
||
private processApis(): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
SDK.initialize(this.args.swagger, this.args.username, this.args.password) | ||
.then((sdk: SDK) => { | ||
if (this.args.list) { | ||
const args: any = {}; | ||
const params = this.args.list.split('\\|'); | ||
params.forEach((param: string) => { | ||
const parts = param.split(':'); | ||
if (parts.length !== 2) { | ||
throw new Error(`Invalid arguments for list command: ${this.args.list}`); | ||
} | ||
args[parts[0]] = parts[1]; | ||
}); | ||
sdk.apis.list(args) | ||
.then(apis => { | ||
console.info(JSON.stringify(apis)); | ||
}) | ||
.catch(reject); | ||
} else if (this.args.add) { | ||
fs.readJSONAsync(this.args.add) | ||
.then((api: ApiConfig) => sdk.apis.addApi(api)) | ||
.then(apiId => { | ||
console.info(`API created. ID: ${apiId}`); | ||
}) | ||
.catch(reject); | ||
} else if (this.args.update) { | ||
fs.readJSONAsync(this.args.update) | ||
.then((api: ApiConfig) => sdk.apis.updateApi(api.id, api)) | ||
.then(() => { | ||
console.info(`API updated`); | ||
}) | ||
.catch(reject); | ||
} else if (this.args.remove) { | ||
sdk.apis.deleteApi(this.args.remove) | ||
.then(() => { | ||
console.info(`API removed`); | ||
}) | ||
.catch(reject); | ||
} else if (this.args.get) { | ||
sdk.apis.getApi(this.args.get) | ||
.then(api => { | ||
console.info(JSON.stringify(api)); | ||
}) | ||
.catch(reject); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
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,11 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
import { configArgs } from './cli-args'; | ||
import { Cli } from './cli-tool'; | ||
|
||
try { | ||
new Cli(configArgs).processCommand(); | ||
} catch (e) { | ||
console.error(e); | ||
} |
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,128 @@ | ||
'use strict'; | ||
|
||
import { ApiConfig } from '../../config/api'; | ||
|
||
const swagger = require('swagger-client'); | ||
|
||
export interface Apis { | ||
list(filters: any): Promise<Array<ApiConfig>>; | ||
addApi(api: ApiConfig): Promise<string>; | ||
updateApi( id: string, api: ApiConfig): Promise<void>; | ||
deleteApi( id: string): Promise<void>; | ||
getApi( id: string): Promise<ApiConfig>; | ||
} | ||
|
||
export class SDK { | ||
private apisClient: ApisClient; | ||
|
||
private constructor(swaggerClient: any) { | ||
this.apisClient = new ApisClient(swaggerClient); | ||
} | ||
|
||
static initialize(swaggerUrl: string, login: string, password: string): Promise<SDK> { | ||
return new Promise<SDK>((resolve, reject) => { | ||
SDK.authenticate(swaggerUrl, login, password) | ||
.then(token => swagger(swaggerUrl, { | ||
authorizations: { | ||
Bearer: `JWT ${token}` | ||
} | ||
})) | ||
.then((swaggerClient: any) => { | ||
resolve(new SDK(swaggerClient)); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
static authenticate(swaggerUrl: string, login: string, password: string): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
swagger(swaggerUrl) | ||
.then((swaggerClient: any) => swaggerClient.apis.Users.UsersRestGetAuthToken({login, password})) | ||
.then((response: any) => { | ||
if (response.status === 200) { | ||
return resolve(response.text); | ||
} | ||
reject(response.text); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
get apis(): Apis { | ||
return this.apisClient; | ||
} | ||
} | ||
|
||
class ApisClient implements Apis { | ||
private swaggerClient: any; | ||
|
||
constructor(swaggerClient: any) { | ||
this.swaggerClient = swaggerClient; | ||
} | ||
|
||
list(filters: any): Promise<Array<ApiConfig>> { | ||
return new Promise<Array<ApiConfig>>((resolve, reject) => { | ||
this.swaggerClient.apis.APIs.APIRestList(filters) | ||
.then((response: any) => { | ||
if (response.status === 200) { | ||
return resolve(response.body); | ||
} | ||
reject(response.text); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
addApi(api: ApiConfig): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
this.swaggerClient.apis.APIs.APIRestAddApi({api}) | ||
.then((response: any) => { | ||
if (response.status === 201) { | ||
return resolve(response.headers['Location'].substring(5)); | ||
} | ||
reject(response.text); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
updateApi( id: string, api: ApiConfig): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
api.id = id; | ||
this.swaggerClient.apis.APIs.APIRestUpdateApi({id, api}) | ||
.then((response: any) => { | ||
if (response.status === 204) { | ||
return resolve(); | ||
} | ||
reject(response.text); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
deleteApi( id: string): Promise<void> { | ||
return new Promise<void>((resolve, reject) => { | ||
this.swaggerClient.apis.APIs.APIRestDeleteApi({id}) | ||
.then((response: any) => { | ||
if (response.status === 204) { | ||
return resolve(); | ||
} | ||
reject(response.text); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
|
||
getApi( id: string): Promise<ApiConfig> { | ||
return new Promise<ApiConfig>((resolve, reject) => { | ||
this.swaggerClient.apis.APIs.APIRestGetApi({id}) | ||
.then((response: any) => { | ||
if (response.status === 200) { | ||
return resolve(response.body); | ||
} | ||
reject(response.text); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.