From d0f58161350f30cd9842597286ad98dbce5af307 Mon Sep 17 00:00:00 2001 From: Nicola Iacovelli Date: Wed, 1 Nov 2023 19:48:06 +0100 Subject: [PATCH 1/3] Added -kvm flag to use a key value map entry as target endpoint url --- bin/openapi2apigee | 1 + lib/commands/generateApi/generatePolicies.js | 17 ++++++ .../generateApi/generateProxyEndPoint.js | 4 ++ .../generateApi/generateTargetEndPoint.js | 4 +- lib/policy_templates/kvm/getKvmEntry.js | 58 +++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 lib/policy_templates/kvm/getKvmEntry.js diff --git a/bin/openapi2apigee b/bin/openapi2apigee index 9b69ff4..dfbb6e5 100755 --- a/bin/openapi2apigee +++ b/bin/openapi2apigee @@ -44,6 +44,7 @@ program .option('-p, --password ', 'Apigee Edge Password to Deploy') .option('-t, --token ', 'Apigee Edge Auth Token to Deploy') .option('-U, --backendurl ','Specify the target backend url') + .option('-K, --kvm ', 'Specify the kvm name to use inside target endpoint') .option('-O, --oauth ','Apigee enable oauth') .description('Generates Apigee API Bundle') diff --git a/lib/commands/generateApi/generatePolicies.js b/lib/commands/generateApi/generatePolicies.js index 1a0ffb8..36860b0 100644 --- a/lib/commands/generateApi/generatePolicies.js +++ b/lib/commands/generateApi/generatePolicies.js @@ -30,6 +30,7 @@ var validations = require('../../policy_templates/validations/valid.js') var raiseFault = require('../../policy_templates/raise-fault/raise.js') var verifyApiKey = require('../../policy_templates/security/apikey.js') var oauth2 = require('../../policy_templates/security/verifyAccessToken.js') +var getKvmEntry = require('../../policy_templates/kvm/getKvmEntry') var removeHeaderAuthorization = require('../../policy_templates/security/removeHeaderAuthorization.js') var async = require('async') @@ -64,6 +65,22 @@ module.exports = function generatePolicies (apiProxy, options, api, cb) { }) }) } + if (options.kvm) { + var option = { + apiName: apiProxy, + kvm: options.kvm + } + var xmlString = getKvmEntry.getKvmGenEntry(option, "getKvmEntry") + fs.writeFile(rootDirectory + '/policies/getKvmEntry.xml', xmlString, function (err) { + if (err) { + throw new Error() + } + writeCnt++ + if (writeCnt === xmlStrings.length) { + // cb(null, {}) + } + }) + } async.each(services, function (service, callback) { // Perform operation on file here. diff --git a/lib/commands/generateApi/generateProxyEndPoint.js b/lib/commands/generateApi/generateProxyEndPoint.js index 11d5a45..8119138 100644 --- a/lib/commands/generateApi/generateProxyEndPoint.js +++ b/lib/commands/generateApi/generateProxyEndPoint.js @@ -42,6 +42,10 @@ module.exports = function generateProxyEndPoint (apiProxy, options, api, cb) { var step2 = requestPipe.ele('Step', {}) step2.ele('Name', {}, 'removeHeaderAuthorization') } + if (options.kvm) { + requestPipe.ele('Step', {}) + .ele('Name', {}, 'getKvmEntry') + } services.forEach(function (serviceItem) { if (serviceItem.provider === 'x-cors') { useCors = serviceItem.name diff --git a/lib/commands/generateApi/generateTargetEndPoint.js b/lib/commands/generateApi/generateTargetEndPoint.js index 984bb73..5ea76f6 100644 --- a/lib/commands/generateApi/generateTargetEndPoint.js +++ b/lib/commands/generateApi/generateTargetEndPoint.js @@ -55,7 +55,9 @@ module.exports = function generateTargetEndPoint (apiProxy, options, api, cb) { var httpTargetConn = root.ele('HTTPTargetConnection') - if (options.backendurl) { + if (options.kvm) { + httpTargetConn.ele('URL', {}, "{" + options.kvm + "}") + } else if (options.backendurl) { httpTargetConn.ele('URL', {}, options.backendurl) } else { if (api.openapi) { diff --git a/lib/policy_templates/kvm/getKvmEntry.js b/lib/policy_templates/kvm/getKvmEntry.js new file mode 100644 index 0000000..ac46865 --- /dev/null +++ b/lib/policy_templates/kvm/getKvmEntry.js @@ -0,0 +1,58 @@ +/** + 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 + + https://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. + */ + +var builder = require('xmlbuilder') +var random = require('../../util/random.js') + +module.exports = { + getKvmEntry: getKvmEntry, + getKvmGenEntry: getKvmGenEntry +} + +function getKvmEntry (options) { + var name = options.name || 'getKvmEntry-' + random.randomText() + var aysnc = options.async || 'false' + var continueOnError = options.continueOnError || 'false' + var enabled = options.enabled || 'true' + var kvmName = options.kvm + var apiName = options.apiName + + + + var keyValueMapOperations = builder.create('KeyValueMapOperations') + keyValueMapOperations.att('async', aysnc) + keyValueMapOperations.att('continueOnError', continueOnError) + keyValueMapOperations.att('enabled', enabled) + keyValueMapOperations.att('name', name) + keyValueMapOperations.att('mapIdentifier', kvmName) + keyValueMapOperations.ele('DisplayName', {}, 'Get Target Endpoint') + keyValueMapOperations.ele('ExclusiveCache', {}, false) + keyValueMapOperations.ele('ExpiryTimeInSecs', {}, 300) + keyValueMapOperations.ele('Get') + .att('assignTo', apiName) + .att('index', 1) + .ele("Key") + .ele("Parameter", {}, apiName) + keyValueMapOperations.ele('Scope', {}, "environment") + var xmlString = keyValueMapOperations.end({ pretty: true, indent: ' ', newline: '\n' }) + return xmlString +} + +function getKvmGenEntry (options, name) { + var templateOptions = options + templateOptions.name = name || 'getKvmEntry' + return getKvmEntry(templateOptions) +} From afb1b1f7c1ed7c331722bad39c5ed24bed63d800 Mon Sep 17 00:00:00 2001 From: Nicola Iacovelli Date: Thu, 2 Nov 2023 10:14:58 +0100 Subject: [PATCH 2/3] fix kvm entry name inside target endpoint url --- lib/commands/generateApi/generateTargetEndPoint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commands/generateApi/generateTargetEndPoint.js b/lib/commands/generateApi/generateTargetEndPoint.js index 5ea76f6..a01d6da 100644 --- a/lib/commands/generateApi/generateTargetEndPoint.js +++ b/lib/commands/generateApi/generateTargetEndPoint.js @@ -56,7 +56,7 @@ module.exports = function generateTargetEndPoint (apiProxy, options, api, cb) { var httpTargetConn = root.ele('HTTPTargetConnection') if (options.kvm) { - httpTargetConn.ele('URL', {}, "{" + options.kvm + "}") + httpTargetConn.ele('URL', {}, "{" + options.apiProxy + "}") } else if (options.backendurl) { httpTargetConn.ele('URL', {}, options.backendurl) } else { From 282bd70225bd3e8a87fc7e99af77c056bbba6846 Mon Sep 17 00:00:00 2001 From: Nicola Iacovelli Date: Thu, 2 Nov 2023 10:34:03 +0100 Subject: [PATCH 3/3] fix typo --- lib/commands/generateApi/generateTargetEndPoint.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commands/generateApi/generateTargetEndPoint.js b/lib/commands/generateApi/generateTargetEndPoint.js index a01d6da..1613876 100644 --- a/lib/commands/generateApi/generateTargetEndPoint.js +++ b/lib/commands/generateApi/generateTargetEndPoint.js @@ -56,7 +56,7 @@ module.exports = function generateTargetEndPoint (apiProxy, options, api, cb) { var httpTargetConn = root.ele('HTTPTargetConnection') if (options.kvm) { - httpTargetConn.ele('URL', {}, "{" + options.apiProxy + "}") + httpTargetConn.ele('URL', {}, "{" + apiProxy + "}") } else if (options.backendurl) { httpTargetConn.ele('URL', {}, options.backendurl) } else {