openapi2apigeeck2
Version:
A tool that converts openapi yaml file to Apigee API Proxy Bundle
83 lines (72 loc) • 2.86 kB
JavaScript
/**
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 fs = require('fs')
var path = require('path')
module.exports = function generateTargetEndPoint (apiProxy, options, api, cb) {
var destination = options.destination || path.join(__dirname, '../../../api_bundles')
if (destination.substr(-1) === '/') {
destination = destination.substr(0, destination.length - 1)
}
var rootDirectory = destination + '/' + apiProxy + '/apiproxy'
var root = builder.create('TargetEndpoint')
root.att('name', 'default')
root.ele('Description', {}, api.info.title)
var preFlow = root.ele('PreFlow', {name: 'PreFlow'})
// Add steps to preflow.
var requestPipe = preFlow.ele('Request')
preFlow.ele('Response')
for (var service in api['x-a127-services']) {
var serviceItem = api['x-a127-services'][service]
if (serviceItem.provider === 'x-headers') {
console.log("inside if")
if (serviceItem['apply'] && serviceItem['apply'].endPoint.indexOf('target') > -1) {
if (
serviceItem['apply'] &&
serviceItem['apply'].pipe.indexOf('request') > -1 &&
serviceItem['options'].displayName) {
console.log("inside if")
var step = requestPipe.ele('Step', {})
step.ele('Name', {}, serviceItem['options'].displayName)
}
}
}
}
root.ele('Flows', {})
var postFlow = root.ele('PostFlow', {name: 'PostFlow'})
postFlow.ele('Request')
postFlow.ele('Response')
var httpTargetConn = root.ele('HTTPTargetConnection')
var loadBalancer = httpTargetConn.ele('LoadBalancer')
if (options.backendurl) {
httpTargetConn.ele('URL', {}, options.backendurl)
} else {
if (api.server && api.path){
console.log('Initiating dynamic target creation')
loadBalancer.ele('Server', { name: api.server })
httpTargetConn.ele('Path',api.path)
}
else if (api.openapi && !(api.server && api.path)) {
httpTargetConn.ele('URL', {}, api.servers[0].url)
} else {
httpTargetConn.ele('URL', {}, api.schemes[0] + '://' + api.host + api.basePath)
}
}
var xmlString = root.end({ pretty: true, indent: ' ', newline: '\n' })
fs.writeFile(rootDirectory + '/targets/default.xml', xmlString, function (err) {
if (err) {
return cb(err, {})
}
cb(null, {})
})
}