serverless-import-apigateway
Version:
Dynamically import an existing AWS API Gateway into your Serverless stack.
90 lines (70 loc) • 2.88 kB
JavaScript
'use strict'
const chalk = require('chalk')
const get = (obj, path, defaultValue) => {
return path.split('.').filter(Boolean).every(step => !(step && !(obj = obj[step]))) ? obj : defaultValue
}
class ImportApiGatewayPlugin {
constructor(serverless, options) {
this.serverless = serverless
this.provider = this.serverless.providers.aws
this.serverless.service.provider.apiGateway = get(this.serverless.service, 'provider.apiGateway', {})
this.config = get(this.serverless.service, 'custom.importApiGateway', {})
this.hooks = {}
if (this.config.name) {
this.config.path = get(this.config, 'path', '/')
this.config.resources = get(this.config, 'resources', [])
this.hooks['before:package:setupProviderConfiguration'] = this.importApiGateway.bind(this)
}
}
async getRestApiPathIds(restApiId) {
let pathIds = {}
const response = await this.provider.request('APIGateway', 'getResources', { limit: 500, restApiId: restApiId })
if (response.items) {
for (let resource of response.items) {
pathIds[resource.path] = resource.id
}
}
return pathIds
}
async findRestApiIdByName(name) {
const response = await this.provider.request('APIGateway', 'getRestApis', { limit: 500 })
if (response.items) {
for (let restApi of response.items) {
if (name === restApi.name) {
return restApi.id
}
}
}
}
async importApiGateway() {
try {
const restApiId = await this.findRestApiIdByName(this.config.name)
if (!restApiId) {
this.serverless.cli.log(`Unable to find REST API named '${this.config.name}'`)
return
}
const pathIds = await this.getRestApiPathIds(restApiId)
const rootResourceId = pathIds[this.config.path]
if (!rootResourceId) {
this.serverless.cli.log(`Unable to find root resource path (${this.config.path}) for REST API (${restApiId})`)
return
}
const resourcesToImport = {}
for (let resourcePath of this.config.resources) {
const resourceId = pathIds[resourcePath]
if (!resourceId) {
this.serverless.cli.log(`Unable to find resource path (${resourcePath}) for REST API (${restApiId})`)
return
}
resourcesToImport[resourcePath] = resourceId
}
this.serverless.service.provider.apiGateway.restApiId = restApiId
this.serverless.service.provider.apiGateway.restApiRootResourceId = rootResourceId
this.serverless.service.provider.apiGateway.restApiResources = resourcesToImport
this.serverless.cli.log(`Imported API Gateway (${JSON.stringify(this.serverless.service.provider.apiGateway)})`)
} catch (e) {
console.error(chalk.red(`\n-------- Import API Gateway Error --------\n${e.message}`))
}
}
}
module.exports = ImportApiGatewayPlugin