Skip to content

Commit a91ba9a

Browse files
feat: migration of API GW and Interactions
1 parent 0894bc0 commit a91ba9a

File tree

7 files changed

+237
-34
lines changed

7 files changed

+237
-34
lines changed

packages/amplify-category-api/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ async function migrate(context) {
2626
context.print.error(`Provider not configured for ${category}: ${resourceName}`);
2727
}
2828
} catch (e) {
29-
context.print.warning(`Could not run migration for ${category} category`);
29+
context.print.warning(`Could not run migration for ${category}: ${resourceName}`);
30+
throw e;
3031
}
3132
});
3233
}

packages/amplify-category-api/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fs = require('fs-extra');
55
const category = 'api';
66
const serviceName = 'API Gateway';
77
const parametersFileName = 'api-params.json';
8+
const cfnParametersFilename = 'parameters.json';
89
const uuid = require('uuid');
910

1011
async function serviceWalkthrough(context, defaultValuesFilename) {
@@ -566,6 +567,46 @@ async function askLambdaArn(context, currentPath) {
566567
};
567568
}
568569

570+
async function migrate(context, projectPath, resourceName) {
571+
const { amplify } = context;
572+
573+
const targetDir = amplify.pathManager.getBackendDirPath();
574+
const resourceDirPath = pathLib.join(targetDir, category, resourceName);
575+
const parametersFilePath = pathLib.join(resourceDirPath, parametersFileName);
576+
let parameters;
577+
try {
578+
parameters = JSON.parse(fs.readFileSync(parametersFilePath));
579+
} catch (e) {
580+
context.print.error(`Error reading api-params.json file for ${resourceName} resource`);
581+
throw e;
582+
}
583+
const pluginDir = `${__dirname}/../`;
584+
const copyJobs = [
585+
{
586+
dir: pluginDir,
587+
template: 'cloudformation-templates/apigw-cloudformation-template-default.json.ejs',
588+
target: `${targetDir}/${category}/${resourceName}/${resourceName}-cloudformation-template.json`,
589+
},
590+
];
591+
592+
// copy over the files
593+
await context.amplify.copyBatch(context, copyJobs, parameters, true, false);
594+
595+
// Create parameters.json file
596+
const cfnParameters = {
597+
authRoleName: {
598+
Ref: 'AuthRoleName',
599+
},
600+
unauthRoleName: {
601+
Ref: 'UnauthRoleName',
602+
},
603+
};
604+
605+
const cfnParametersFilePath = pathLib.join(resourceDirPath, cfnParametersFilename);
606+
const jsonString = JSON.stringify(cfnParameters, null, 4);
607+
fs.writeFileSync(cfnParametersFilePath, jsonString, 'utf8');
608+
}
609+
569610
// function checkIfAuthExists(context) {
570611
// const { amplify } = context;
571612
// const { amplifyMeta } = amplify.getProjectDetails();
@@ -585,4 +626,4 @@ async function askLambdaArn(context, currentPath) {
585626
// }
586627

587628

588-
module.exports = { serviceWalkthrough, updateWalkthrough };
629+
module.exports = { serviceWalkthrough, updateWalkthrough, migrate };

packages/amplify-category-interactions/index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
1+
const category = 'interactions';
2+
13
async function migrate(context) {
2-
context.print.info('hosting migrate...');
4+
const { projectPath, amplifyMeta } = context.migrationInfo;
5+
const migrateResourcePromises = [];
6+
Object.keys(amplifyMeta).forEach((categoryName) => {
7+
if (categoryName === category) {
8+
Object.keys(amplifyMeta[category]).forEach((resourceName) => {
9+
try {
10+
const providerController = require(`./provider-utils/${amplifyMeta[category][resourceName].providerPlugin}/index`);
11+
if (providerController) {
12+
migrateResourcePromises.push(providerController.migrateResource(
13+
context,
14+
projectPath,
15+
amplifyMeta[category][resourceName].service,
16+
resourceName,
17+
));
18+
} else {
19+
context.print.error(`Provider not configured for ${category}: ${resourceName}`);
20+
}
21+
} catch (e) {
22+
context.print.warning(`Could not run migration for ${category}: ${resourceName}`);
23+
throw e;
24+
}
25+
});
26+
}
27+
});
28+
29+
await Promise.all(migrateResourcePromises);
330
}
431

532
module.exports = {

packages/amplify-category-interactions/provider-utils/awscloudformation/cloudformation-templates/lex-cloudformation-template.json.ejs

Lines changed: 93 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,38 @@
2929
]
3030
}
3131
},
32+
"Conditions": {
33+
"ShouldNotCreateEnvResources": {
34+
"Fn::Equals": [
35+
{
36+
"Ref": "env"
37+
},
38+
"NONE"
39+
]
40+
}
41+
},
3242
"Resources": {
3343
"LambdaFunction": {
3444
"Type": "AWS::Lambda::Function",
3545
"Properties": {
3646
"Handler": "index.handler",
37-
"FunctionName": {
38-
"Fn::Join": [
39-
"-",
40-
[
41-
"<%= props.functionName %>",
42-
{
43-
"Ref": "env"
44-
}
45-
]
47+
"FunctionName": {
48+
"Fn::If": [
49+
"ShouldNotCreateEnvResources",
50+
"<%= props.functionName %>",
51+
{
52+
53+
"Fn::Join": [
54+
"",
55+
[
56+
"<%= props.functionName %>",
57+
"-",
58+
{
59+
"Ref": "env"
60+
}
61+
]
62+
]
63+
}
4664
]
4765
},
4866
"Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
@@ -56,14 +74,22 @@
5674
"Type": "AWS::IAM::Role",
5775
"Properties": {
5876
"RoleName": {
59-
"Fn::Join": [
60-
"-",
61-
[
62-
"<%= props.roleName %>",
63-
{
64-
"Ref": "env"
65-
}
66-
]
77+
"Fn::If": [
78+
"ShouldNotCreateEnvResources",
79+
"<%= props.roleName %>",
80+
{
81+
82+
"Fn::Join": [
83+
"",
84+
[
85+
"<%= props.roleName %>",
86+
"-",
87+
{
88+
"Ref": "env"
89+
}
90+
]
91+
]
92+
}
6793
]
6894
},
6995
"AssumeRolePolicyDocument": {
@@ -172,9 +198,24 @@
172198
},
173199
{ "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
174200
":bot:",
175-
"<%= props.botName %>_",
176201
{
177-
"Ref": "env"
202+
"Fn::If": [
203+
"ShouldNotCreateEnvResources",
204+
"<%= props.botName %>",
205+
{
206+
207+
"Fn::Join": [
208+
"",
209+
[
210+
"<%= props.botName %>",
211+
"_",
212+
{
213+
"Ref": "env"
214+
}
215+
]
216+
]
217+
}
218+
]
178219
},
179220
":*"
180221
]
@@ -214,9 +255,24 @@
214255
},
215256
{ "Fn::Select" : [ "4", { "Fn::Split": [":", {"Ref": "authRoleArn"}]}]},
216257
":bot:",
217-
"<%= props.botName %>_",
218258
{
219-
"Ref": "env"
259+
"Fn::If": [
260+
"ShouldNotCreateEnvResources",
261+
"<%= props.botName %>",
262+
{
263+
264+
"Fn::Join": [
265+
"",
266+
[
267+
"<%= props.botName %>",
268+
"_",
269+
{
270+
"Ref": "env"
271+
}
272+
]
273+
]
274+
}
275+
]
220276
},
221277
":*"
222278
]
@@ -237,14 +293,22 @@
237293
},
238294
"BotName": {
239295
"Value": {
240-
"Fn::Join": [
241-
"",
242-
[
243-
"<%= props.botName %>_",
244-
{
245-
"Ref": "env"
246-
}
247-
]
296+
"Fn::If": [
297+
"ShouldNotCreateEnvResources",
298+
"<%= props.botName %>",
299+
{
300+
301+
"Fn::Join": [
302+
"",
303+
[
304+
"<%= props.botName %>",
305+
"_",
306+
{
307+
"Ref": "env"
308+
}
309+
]
310+
]
311+
}
248312
]
249313
}
250314
},

packages/amplify-category-interactions/provider-utils/awscloudformation/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,19 @@ function updateResource(context, category, service) {
119119
});
120120
}
121121

122+
async function migrateResource(context, projectPath, service, resourceName) {
123+
serviceMetadata = JSON.parse(fs.readFileSync(`${__dirname}/../supported-services.json`))[service];
124+
const { serviceWalkthroughFilename } = serviceMetadata;
125+
const serviceWalkthroughSrc = `${__dirname}/service-walkthroughs/${serviceWalkthroughFilename}`;
126+
const { migrate } = require(serviceWalkthroughSrc);
127+
128+
if (!migrate) {
129+
context.print.info(`No migration required for ${resourceName}`);
130+
return;
131+
}
132+
133+
return await migrate(context, projectPath, resourceName);
134+
}
135+
122136

123-
module.exports = { addResource, updateResource };
137+
module.exports = { addResource, updateResource, migrateResource };

packages/amplify-category-interactions/provider-utils/awscloudformation/service-walkthroughs/lex-walkthrough.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('fs-extra');
44

55
const category = 'interactions';
66
const parametersFileName = 'lex-params.json';
7+
const cfnParametersFilename = 'parameters.json';
78
const serviceName = 'Lex';
89
const fuzzy = require('fuzzy');
910

@@ -709,4 +710,55 @@ async function askLambda(context) {
709710
};
710711
}
711712

712-
module.exports = { addWalkthrough, updateWalkthrough };
713+
async function migrate(context, projectPath, resourceName) {
714+
const { amplify } = context;
715+
716+
const targetDir = amplify.pathManager.getBackendDirPath();
717+
const resourceDirPath = path.join(targetDir, category, resourceName);
718+
const parametersFilePath = path.join(resourceDirPath, parametersFileName);
719+
const defaultValuesSrc = `${__dirname}/../default-values/lex-defaults.js`;
720+
const { getAllDefaults } = require(defaultValuesSrc);
721+
const defaultValues = getAllDefaults(amplify.getProjectDetails());
722+
723+
let parameters;
724+
try {
725+
parameters = JSON.parse(fs.readFileSync(parametersFilePath));
726+
} catch (e) {
727+
context.print.error(`Error reading api-params.json file for ${resourceName} resource`);
728+
throw e;
729+
}
730+
Object.assign(defaultValues, parameters);
731+
const pluginDir = `${__dirname}/../`;
732+
const copyJobs = [
733+
{
734+
dir: pluginDir,
735+
template: 'cloudformation-templates/lex-cloudformation-template.json.ejs',
736+
target: `${targetDir}/${category}/${resourceName}/${resourceName}-cloudformation-template.json`,
737+
},
738+
];
739+
740+
// copy over the files
741+
await context.amplify.copyBatch(context, copyJobs, defaultValues, true, false);
742+
743+
// Create parameters.json file
744+
const cfnParameters = {
745+
authRoleArn: {
746+
'Fn::GetAtt': [
747+
'AuthRole',
748+
'Arn',
749+
],
750+
},
751+
authRoleName: {
752+
Ref: 'AuthRoleName',
753+
},
754+
unauthRoleName: {
755+
Ref: 'UnauthRoleName',
756+
},
757+
};
758+
759+
const cfnParametersFilePath = path.join(resourceDirPath, cfnParametersFilename);
760+
const jsonString = JSON.stringify(cfnParameters, null, 4);
761+
fs.writeFileSync(cfnParametersFilePath, jsonString, 'utf8');
762+
}
763+
764+
module.exports = { addWalkthrough, updateWalkthrough, migrate };

packages/amplify-cli/src/lib/migrate-project.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ function generateBackendConfig(amplifyMeta) {
251251
backendConfig[category][resourceName].service = amplifyMeta[category][resourceName].service;
252252
backendConfig[category][resourceName].providerPlugin =
253253
amplifyMeta[category][resourceName].providerPlugin;
254+
backendConfig[category][resourceName].dependsOn =
255+
amplifyMeta[category][resourceName].dependsOn;
256+
backendConfig[category][resourceName].build =
257+
amplifyMeta[category][resourceName].build;
254258
});
255259
}
256260
});

0 commit comments

Comments
 (0)