-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: import dynamic templates where user can customise resource name…
…s and values
- Loading branch information
ljacobsson
committed
Apr 3, 2021
1 parent
f5a5110
commit 50ed7ef
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const inputUtil = require("../../shared/inputUtil"); | ||
const jp = require("jsonpath"); | ||
|
||
async function transform(template) { | ||
const metadata = template.Metadata; | ||
if (!metadata || !metadata.PatternTransform) { | ||
return template; | ||
} | ||
console.log("Applying transforms..."); | ||
template = await propertyTransforms(template); | ||
template = await placeholderTransforms(template); | ||
return template; | ||
} | ||
|
||
async function placeholderTransforms(template) { | ||
const metadata = template.Metadata; | ||
|
||
let templateString = JSON.stringify(template); | ||
for (const property of metadata.PatternTransform.Placeholders || []) { | ||
const value = await inputUtil.text( | ||
`Placeholder value for ${property.Placeholder}:` | ||
); | ||
templateString = templateString.replaceAll(property.Placeholder, value); | ||
} | ||
return JSON.parse(templateString); | ||
} | ||
async function propertyTransforms(template) { | ||
const metadata = template.Metadata; | ||
for (const property of metadata.PatternTransform.Properties || []) { | ||
let defaultValue, value; | ||
switch (property.InputType) { | ||
case "number": | ||
defaultValue = jp.query(template, property.JSONPath); | ||
console.log("Set value for " + property.JSONPath); | ||
value = parseInt( | ||
await inputUtil.text( | ||
`${property.Message}:`, | ||
defaultValue.length ? defaultValue[0] : undefined | ||
) | ||
); | ||
|
||
break; | ||
case "text": | ||
defaultValue = jp.query(template, property.JSONPath); | ||
value = await inputUtil.text( | ||
property.Message, | ||
defaultValue.length ? defaultValue[0] : undefined | ||
); | ||
break; | ||
case "runtime-select": | ||
value = await inputUtil.list( | ||
"Select Lambda runtime", | ||
[ | ||
"nodejs10.x", | ||
"nodejs12.x", | ||
"nodejs14.x", | ||
"python3.8", | ||
"python3.7", | ||
"python3.6", | ||
"python2.7", | ||
"ruby2.7", | ||
"ruby2.5", | ||
"java11", | ||
"java8.al2", | ||
"java8", | ||
"go1.x", | ||
"dotnetcore3.1", | ||
"dotnetcore2.1", | ||
].sort() | ||
); | ||
break; | ||
} | ||
jp.value(template, property.JSONPath, value); | ||
} | ||
return template; | ||
} | ||
|
||
module.exports = { | ||
transform, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const transformer = require("./transformer"); | ||
const inquirer = require("inquirer"); | ||
|
||
test("Test transform", async () => { | ||
inquirer.prompt = (questions) => Promise.resolve({ text: "test" }); | ||
|
||
await transformer.transform(template); | ||
}); | ||
|
||
const template = { | ||
AWSTemplateFormatVersion: "2010-09-09T00:00:00.000Z", | ||
Transform: ["AWS::Serverless-2016-10-31"], | ||
Metadata: { | ||
PatternTransform: { | ||
Placeholders: [ | ||
{ | ||
Placeholder: "The_", | ||
InputType: "text", | ||
Message: "Message type", | ||
}, | ||
], | ||
Properties: [ | ||
{ | ||
JSONPath: | ||
"$.Resources.The_Consumer.Properties.Environment.Variables.Number", | ||
InputType: "number", | ||
Message: "Enter a number", | ||
}, | ||
{ | ||
JSONPath: "$.Resources.TheConsumer.Properties.Runtime", | ||
InputType: "runtime-select", | ||
}, | ||
], | ||
}, | ||
}, | ||
Resources: { | ||
The_Consumer: { | ||
Type: "AWS::Serverless::Function", | ||
Properties: { | ||
Runtime: "nodejs14.x", | ||
CodeUri: "src/", | ||
Handler: "The_Consumer.js", | ||
Timeout: 3, | ||
Environment: { | ||
Variables: { | ||
Number: 123, | ||
}, | ||
}, | ||
Events: { | ||
SQS: { | ||
Type: "SQS", | ||
Properties: { | ||
Queue: null, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
The_Queue: { | ||
Type: "AWS::SQS::Queue", | ||
}, | ||
}, | ||
}; |