Skip to content

Commit

Permalink
working on resource file support
Browse files Browse the repository at this point in the history
  • Loading branch information
tyayers committed Jan 29, 2024
1 parent 13c6cbd commit ccb34e1
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
5 changes: 3 additions & 2 deletions module/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ export class PlugInFilePolicyConfig {

export class FlowRunPoint {
name = '';
flowCondition: string = "";
stepCondition: string = "";
flowCondition?: string = "";
stepCondition?: string = "";
runPoints: RunPoint[] = [];
}

export enum RunPoint {
none = 'none',
preRequest = 'preRequest',
postRequest = 'postRequest',
preTarget = 'preTarget',
Expand Down
67 changes: 67 additions & 0 deletions module/lib/plugins/any.plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ApigeeTemplatePlugin, proxyEndpoint, PlugInResult, FlowRunPoint, RunPoint } from '../interfaces.js'

export class AnyConfig {
name: string = "";
flowRunPoints: FlowRunPoint[] = [];
properties: any = {};
}

export class AnyPlugin implements ApigeeTemplatePlugin {

applyTemplate (inputConfig: proxyEndpoint, additionalData?: any): Promise<PlugInResult> {
return new Promise((resolve) => {

const fileResult: PlugInResult = new PlugInResult(this.constructor.name);

let config: AnyConfig = additionalData;

fileResult.files.push({
policyConfig: {
name: `${config.name}`,
flowRunPoints: config.flowRunPoints
},
path: '/policies/' + config.name + '.xml',
contents: OBJtoXML(config.properties)
});

resolve(fileResult)
});
}
}

function OBJtoXML(obj: any): string {
var xml = '';
for (var prop in obj) {
xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += "<" + prop + ">";
xml += OBJtoXML(new Object(obj[prop][array]));
xml += "</" + prop + ">";
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml
}
51 changes: 51 additions & 0 deletions module/lib/plugins/resources.file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ApigeeTemplatePlugin, proxyEndpoint, PlugInResult, FlowRunPoint, RunPoint } from '../interfaces.js'

export class ResourceFileConfig {
name: string = "";
flowRunPoints: FlowRunPoint[] = [];
files: { [key: string]: string } = {};
}

export class ResourceFilePlugin implements ApigeeTemplatePlugin {

applyTemplate (inputConfig: proxyEndpoint, additionalData?: any): Promise<PlugInResult> {
return new Promise((resolve) => {
const fileResult: PlugInResult = new PlugInResult(this.constructor.name);

let config: ResourceFileConfig = additionalData;
//let fileContents: string = "";

for (const [key, value] of Object.entries(config.files)) {
fileResult.files.push({
policyConfig: {
name: `ML-${config.name}`,
flowRunPoints: [{
name: "file",
runPoints: [RunPoint.none]
}]
},
path: '/resources/' + key,
contents: value
});
}

resolve(fileResult)
});
}
}

0 comments on commit ccb34e1

Please sign in to comment.