Skip to content

Commit

Permalink
Updated with extensions support
Browse files Browse the repository at this point in the history
  • Loading branch information
tyayers committed Nov 28, 2023
1 parent dcdff48 commit e03fdd6
Show file tree
Hide file tree
Showing 23 changed files with 556 additions and 338 deletions.
5 changes: 4 additions & 1 deletion cli/bin/apigee-templater.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
* limitations under the License.
*/

import { ApigeeTemplater } from 'apigee-templater-module'
import cli from "../dist/cli.js";

process.removeAllListeners('warning');

const myCli = new cli();
let apigeeTemplater = new ApigeeTemplater();

const myCli = new cli(apigeeTemplater);
myCli.process(process.argv);
12 changes: 6 additions & 6 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apigee-templater",
"version": "1.0.2",
"version": "2.0.0",
"description": "A CLI to easily template and deploy Apigee proxies.",
"homepage": "https://github.com/tyayers/apigee-templater",
"bugs": "https://github.com/tyayers/apigee-templater/issues",
Expand Down Expand Up @@ -28,7 +28,7 @@
"author": "tayers",
"license": "ISC",
"dependencies": {
"apigee-templater-module": "^1.0.1",
"apigee-templater-module": "^2.0.2",
"apigee-x-module": "^1.0.1",
"arg": "^5.0.1",
"axios": "^1.6.2",
Expand Down
63 changes: 45 additions & 18 deletions cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import fs from 'fs'
import { performance } from 'perf_hooks'
import inquirer from 'inquirer'
import chalk from 'chalk'
import { ApigeeTemplateInput, ApigeeTemplateService, ApigeeGenerator, GenerateResult } from 'apigee-templater-module'
import { ApigeeTemplateInput, ApigeeTemplateService, ApigeeTemplater, GenerateResult } from 'apigee-templater-module'
import { ApigeeService, ApiManagementInterface, EnvironmentGroup, EnvironmentGroupAttachment, ProxyDeployment, ProxyRevision } from 'apigee-x-module'

import axios from 'axios';
Expand Down Expand Up @@ -51,7 +51,18 @@ export class cli {
*
* @type {ApigeeTemplateService}
*/
apigeeGenerator: ApigeeTemplateService = new ApigeeGenerator();
apigeeTemplater: ApigeeTemplateService;

/**
* Constructor
* @param name
*/
constructor(templaterInstance?: ApigeeTemplateService) {
if (templaterInstance)
this.apigeeTemplater = templaterInstance;
else
this.apigeeTemplater = new ApigeeTemplater();
}

/**
* Parses the user inputs
Expand Down Expand Up @@ -216,12 +227,18 @@ export class cli {
* @param {cliArgs} args The user input args to the process
*/
async process(args: string[]) {
let options: cliArgs = this.parseArgumentsIntoOptions(args)
if (options.keyPath) process.env.GOOGLE_APPLICATION_CREDENTIALS = options.keyPath
if (options.verbose) this.logVerbose(JSON.stringify(options), 'options:')
let options: cliArgs = this.parseArgumentsIntoOptions(args);
if (options.verbose && fs.existsSync("apigee-templater-log.txt")) {
// Delete old log file, if it exists
fs.unlinkSync("apigee-templater-log.txt")
}

if (options.keyPath) process.env.GOOGLE_APPLICATION_CREDENTIALS = options.keyPath;

console.log(`${chalk.bold(chalk.magentaBright('> Welcome to Apigee Templater'))}, ${chalk.green('use -h for more command line options.')} `)

if (options.verbose) this.logVerbose(JSON.stringify(options, null, 2), 'start:');

if (options.help) {
this.printHelp()
return
Expand All @@ -248,7 +265,7 @@ export class cli {
try {
options = await this.promptForMissingOptions(options)
} catch (error) {
console.error(`${chalk.redBright('! Error:')} Error during prompt for inputs, that's all we know.`)
console.error(`${chalk.redBright('! Error:')} Error during prompt for inputs.`)
if (options.verbose) this.logVerbose(JSON.stringify(error), 'prompt error:')
}

Expand All @@ -265,7 +282,9 @@ export class cli {
target: {
name: 'default',
table: options.targetBigQueryTable
}
},
parameters: {},
extensionSteps: []
}
]
});
Expand All @@ -280,7 +299,9 @@ export class cli {
target: {
name: 'default',
url: options.targetUrl
}
},
parameters: {},
extensionSteps: []
}
]
});
Expand All @@ -301,7 +322,7 @@ export class cli {
process.env.PROJECT = (await this.apigeeService.getOrg()).toString();
}

if (options.verbose) this.logVerbose(`Set Project to ${process.env.PROJECT}`, 'env:')
if (options.verbose) this.logVerbose(`Project set to ${process.env.PROJECT}`, 'project:')

if (options.filter) {
// users can add their own preprocessing filter scripts here
Expand All @@ -311,12 +332,13 @@ export class cli {

if (options.verbose) this.logVerbose(options.input, 'template:')

this.apigeeGenerator.convertStringToProxyInput(options.input).then((inputTemplate: ApigeeTemplateInput) => {
this.apigeeTemplater.convertStringToProxyInput(options.input).then((inputTemplate: ApigeeTemplateInput) => {

if (options.basePath) inputTemplate.endpoints[0].basePath = options.basePath;
if (options.name) inputTemplate.name = options.name;

this.apigeeGenerator.generateProxy(inputTemplate, _proxyDir).then((generateResult: GenerateResult) => {
this.apigeeTemplater.generateProxy(inputTemplate, _proxyDir).then((generateResult: GenerateResult) => {
if (options.verbose) this.logVerbose(JSON.stringify(generateResult, null, 2), 'template result:');
if (inputTemplate.sharedFlow && generateResult && generateResult.template)
console.log(`${chalk.green('>')} Flow ${chalk.bold(chalk.blue(generateResult.template.name))} generated to ${chalk.magentaBright(chalk.bold(generateResult.localPath))} in ${chalk.bold(chalk.green(Math.round(generateResult.duration) + ' milliseconds'))}.`);
else if (generateResult && generateResult.template)
Expand All @@ -335,7 +357,7 @@ export class cli {
this.apigeeService.deployFlowRevision(options.environment, generateResult.template.name, updateResult.revision, options.deployServiceAccount).then((deployResult: ProxyDeployment) => {
const endTime = performance.now()
const duration = endTime - startTime
if (options.verbose) this.logVerbose(JSON.stringify(generateResult), 'deploy result:')
if (options.verbose) this.logVerbose(JSON.stringify(generateResult), 'deploy result:');
if (generateResult && generateResult.template) {
console.log(`${chalk.green('>')} Flow ${chalk.bold(chalk.blue(generateResult.template.name + ' version ' + updateResult.revision))} deployed to environment ${chalk.bold(chalk.magentaBright(options.environment))} in ${chalk.bold(chalk.green(Math.round(duration) + ' milliseconds'))}.`);
}
Expand All @@ -352,9 +374,9 @@ export class cli {
if (updateResult && updateResult.revision) {
if (generateResult && generateResult.template) {
this.apigeeService.deployProxyRevision(options.environment, generateResult.template.name, updateResult.revision, options.deployServiceAccount).then((deployResult: ProxyDeployment) => {
const endTime = performance.now()
const duration = endTime - startTime
if (options.verbose) this.logVerbose(JSON.stringify(generateResult), 'deploy result:')
const endTime = performance.now();
const duration = endTime - startTime;
if (options.verbose) this.logVerbose(JSON.stringify(generateResult), 'deploy result:');
if (generateResult && generateResult.template) {
console.log(`${chalk.green('>')} Proxy ${chalk.bold(chalk.blue(generateResult.template.name + ' version ' + updateResult.revision))} deployed to environment ${chalk.bold(chalk.magentaBright(options.environment))} in ${chalk.bold(chalk.green(Math.round(duration) + ' milliseconds'))}.`)

Expand Down Expand Up @@ -409,10 +431,15 @@ export class cli {
* @param {string} label An optional label as prefix label
*/
logVerbose(input: string, label?: string) {
let logString: string = '> ' + input + '\n';
if (label)
console.log(`${chalk.cyanBright('> ' + label + ' ' + input)}`);
else
console.log(`${chalk.cyanBright('> ' + input)} `)
logString = '> ' + label + ' ' + input + '\n';

console.log(`${chalk.cyanBright(logString)}`);
fs.appendFile('apigee-templater-log.txt', logString, function (err) {
if (err)
console.error(err);
});
}
}

Expand Down
6 changes: 2 additions & 4 deletions module/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export class proxyTarget {
table?= '';
authScopes?= [];
headers?: { [key: string]: string } = {};
preFlows?: string[] = [];
postFlows?: string[] = [];
}

/** A proxy endpoint describes a basepath, targets and other proxy features */
Expand Down Expand Up @@ -138,8 +136,8 @@ export class PlugInFilePolicyConfig {
/** Profile definition with plugins to be used for conversion */
export class ApigeeTemplateProfile {
plugins: ApigeeTemplatePlugin[] = [];
flowPlugins: {[key: string]: ApigeeTemplatePlugin} = {};
finalizePlugin?: ApigeeTemplatePlugin;
extensionPlugins: {[key: string]: ApigeeTemplatePlugin} = {};
finalizePlugins: ApigeeTemplatePlugin[] = [];
}

export interface ApigeeTemplatePlugin {
Expand Down
16 changes: 8 additions & 8 deletions module/lib/plugins/auth.apikey.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import { ApigeeTemplatePlugin, PlugInResult, proxyEndpoint, authTypes, policyIns
*/
export class AuthApiKeyPlugin implements ApigeeTemplatePlugin {
apikey_snippet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<VerifyAPIKey async="false" continueOnError="false" enabled="true" name="VerifyApiKey">
<DisplayName>Verify API Key</DisplayName>
<VerifyAPIKey async="false" continueOnError="false" enabled="true" name="VA-VerifyKey">
<DisplayName>VA-VerifyKey</DisplayName>
<APIKey ref="request.queryparam.apikey"/>
</VerifyAPIKey>`;

removekey_snippet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="RemoveApiKey">
<DisplayName>Remove Query Param apikey</DisplayName>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-RemoveApiKey">
<DisplayName>AM-RemoveKey</DisplayName>
<Remove>
<QueryParams>
<QueryParam name="apikey"/>
Expand Down Expand Up @@ -64,18 +64,18 @@ export class AuthApiKeyPlugin implements ApigeeTemplatePlugin {
fileResult.files = [
{
policyConfig: {
name: 'VerifyApiKey',
name: 'VA-VerifyKey',
triggers: [policyInsertPlaces.preRequest]
},
path: '/policies/VerifyApiKey.xml',
path: '/policies/VA-VerifyKey.xml',
contents: this.apikey_template({})
},
{
policyConfig: {
name: 'RemoveApiKey',
name: 'AM-RemoveApiKey',
triggers: [policyInsertPlaces.preRequest]
},
path: '/policies/RemoveApiKey.xml',
path: '/policies/AM-RemoveApiKey.xml',
contents: this.removekey_template({})
}
];
Expand Down
8 changes: 4 additions & 4 deletions module/lib/plugins/auth.sf.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { ApigeeTemplatePlugin, proxyEndpoint, authTypes, PlugInResult, policyIns
*/
export class AuthSfPlugin implements ApigeeTemplatePlugin {
snippet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FlowCallout continueOnError="false" enabled="true" name="VerifyJWT">
<DisplayName>VerifyJWT</DisplayName>
<FlowCallout continueOnError="false" enabled="true" name="FC-VerifyFlow">
<DisplayName>FC-VerifyFlow</DisplayName>
<FaultRules/>
<Properties/>
<Parameters>
Expand Down Expand Up @@ -69,10 +69,10 @@ export class AuthSfPlugin implements ApigeeTemplatePlugin {
fileResult.files = [
{
policyConfig: {
name: 'VerifyJWT',
name: 'FC-VerifyFlow',
triggers: [policyInsertPlaces.preRequest]
},
path: '/policies/VerifyJWT.xml',
path: '/policies/FC-VerifyFlow.xml',
contents: this.template({
audience: authConfig.parameters.audience,
roles: authConfig.parameters.roles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,47 @@ import { ApigeeTemplatePlugin, PlugInResult, policyInsertPlaces, proxyEndpoint }
*/
export class ProxyPlugin implements ApigeeTemplatePlugin {
snippet = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<PreFlow name="PreFlow">
<Request>
{{#each preRequestPolicies}}
<Step>
<Name>{{this}}</Name>
</Step>
{{/each}}
</Request>
<Response/>
</PreFlow>
<Flows/>
<PostFlow name="PostFlow">
<Request>
</Reqeust>
<Response>
</Response>
</PostFlow>
<HTTPProxyConnection>
<BasePath>{{basePath}}</BasePath>
</HTTPProxyConnection>
<RouteRule name="{{targetName}}">
<TargetEndpoint>{{targetName}}</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>`;
<ProxyEndpoint name="default">
<PreFlow name="PreFlow">
<Request>
{{#each preRequestPolicies}}
<Step>
<Name>{{this}}</Name>
</Step>
{{/each}}
</Request>
<Response>
{{#each postRequestPolicies}}
<Step>
<Name>{{this}}</Name>
</Step>
{{/each}}
</Response>
</PreFlow>
<Flows/>
<PostFlow name="PostFlow">
<Request>
{{#each preResponsePolicies}}
<Step>
<Name>{{this}}</Name>
</Step>
{{/each}}
</Request>
<Response>
{{#each postResponsePolicies}}
<Step>
<Name>{{this}}</Name>
</Step>
{{/each}}
</Response>
</PostFlow>
<HTTPProxyConnection>
<BasePath>{{basePath}}</BasePath>
</HTTPProxyConnection>
<RouteRule name="{{targetName}}">
<TargetEndpoint>{{targetName}}</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>`;

template = Handlebars.compile(this.snippet);

Expand Down
File renamed without changes.
Loading

0 comments on commit e03fdd6

Please sign in to comment.