@@ -308,6 +308,60 @@ export const mocks = async (specification: OpenAPIV2.Document & { [key: string]:
308308 }
309309}
310310
311+ /**
312+ * Generates a Terraform string based on the provided Swagger API specification
313+ * @param specification the Swagger API specification object
314+ */
315+ export const terraform = async ( spec : OpenAPIV2 . Document & { [ key : string ] : any } ) : Promise < string > => {
316+ try {
317+ // validate and dereference the specification, and generate mock integrations
318+ const specification = await mocks ( cloneDeep ( spec ) )
319+ // initialize the terraform string
320+ let terraformString = ''
321+ terraformString += `variable "title" {` + '\n'
322+ terraformString += ' type = string' + '\n'
323+ terraformString += ` description = "The title of the API"` + '\n'
324+ terraformString += ` default = "${ specification . info . title } "` + '\n'
325+ terraformString += '}' + '\n' + '\n'
326+ terraformString += `variable "version" {` + '\n'
327+ terraformString += ' type = string' + '\n'
328+ terraformString += ` description = "The version of the API"` + '\n'
329+ terraformString += ` default = "${ specification . info . version } "` + '\n'
330+ terraformString += '}' + '\n' + '\n'
331+ // add terraform interpolation support to specification fields
332+ specification . info . title = '${var.title}'
333+ specification . info . version = '${var.version}'
334+ // generate terraform variables for providing AWS API Gateway integrations for each operation
335+ for ( let pathKey of Object . keys ( specification . paths ) ) {
336+ for ( let operationKey of Object . keys ( specification . paths [ pathKey ] ) ) {
337+ const operationId = specification . paths [ pathKey ] [ operationKey ] . operationId
338+ const integration = specification . paths [ pathKey ] [ operationKey ] [ 'x-amazon-apigateway-integration' ]
339+ const description = `Provide the AWS API Gateway integration configuration for the ${ operationId } operation`
340+ terraformString += `variable "${ operationId } " {` + '\n'
341+ terraformString += ' type = string' + '\n'
342+ terraformString += ` description = "${ description } "` + '\n'
343+ terraformString += ' default = <<EOF' + '\n'
344+ terraformString += `${ JSON . stringify ( integration , null , 2 ) } ` + '\n'
345+ terraformString += 'EOF' + '\n'
346+ terraformString += '}' + '\n' + '\n'
347+ specification . paths [ pathKey ] [ operationKey ] [ 'x-amazon-apigateway-integration' ] = '${var.' + operationId + '}'
348+ }
349+ }
350+ terraformString += `output "swagger_specification" {` + '\n'
351+ terraformString += ` description = "The interpolated Swagger Specification string"` + '\n'
352+ terraformString += ` value = local.swagger_specification` + '\n'
353+ terraformString += '}' + '\n' + '\n'
354+ terraformString += 'locals {' + '\n'
355+ terraformString += ' swagger_specification = <<EOF' + '\n'
356+ terraformString += `${ JSON . stringify ( specification , null , 2 ) } ` + '\n'
357+ terraformString += 'EOF' + '\n'
358+ terraformString += '}' + '\n'
359+ return terraformString
360+ } catch ( error ) {
361+ throw new Error ( `Error while generating the terraform for the swagger specification: ${ error . message } ` )
362+ }
363+ }
364+
311365export interface Deploy {
312366 id : string
313367 url : string
0 commit comments