Skip to content

Commit bef6d9c

Browse files
authored
fix: copy env specific data from ccb on env checkout (#7512)
1 parent 08f7a3c commit bef6d9c

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

packages/amplify-category-function/src/provider-utils/awscloudformation/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { $TSAny, $TSContext, JSONUtilities, open, PathConstants, pathManager, stateManager } from 'amplify-cli-core';
1+
import {
2+
$TSAny,
3+
$TSContext,
4+
JSONUtilities,
5+
open,
6+
PathConstants,
7+
pathManager,
8+
readCFNTemplate,
9+
stateManager,
10+
writeCFNTemplate,
11+
} from 'amplify-cli-core';
212
import { FunctionParameters, FunctionTemplate, FunctionTriggerParameters, LambdaLayer } from 'amplify-function-plugin-interface';
313
import * as fs from 'fs-extra';
414
import _ from 'lodash';
@@ -381,6 +391,22 @@ export async function updateConfigOnEnvInit(context: $TSContext, resourceName: s
381391
if (currentCloudVersionHash) {
382392
_.set(amplifyMeta, [categoryName, resourceName, versionHash], currentCloudVersionHash);
383393
}
394+
395+
// Since the CFN template and parameters.json are updated on each new layer version which are specific to each env, we need to update
396+
// the files accordingly to ensure the correct status is shown after env checkout. The restore flag already handles this scenario.
397+
if (context.input.command === 'env' && context.input?.subCommands.includes('checkout') && !context.exeInfo?.inputParams?.restore) {
398+
const currentParametersJson =
399+
stateManager.getCurrentResourceParametersJson(projectPath, categoryName, resourceName, { throwIfNotExist: false }) || undefined;
400+
if (currentParametersJson) {
401+
const backendParametersJson = stateManager.getResourceParametersJson(projectPath, categoryName, resourceName);
402+
backendParametersJson.description = currentParametersJson.description;
403+
stateManager.setResourceParametersJson(projectPath, categoryName, resourceName, backendParametersJson);
404+
}
405+
406+
const currentCfnTemplatePath = pathManager.getCurrentCfnTemplatePath(projectPath, categoryName, resourceName);
407+
const { cfnTemplate: currentCfnTemplate } = await readCFNTemplate(currentCfnTemplatePath);
408+
await writeCFNTemplate(currentCfnTemplate, pathManager.getResourceCfnTemplatePath(projectPath, categoryName, resourceName));
409+
}
384410
}
385411
}
386412

packages/amplify-cli-core/src/state-manager/pathManager.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export const PathConstants = {
3939
CLIJSONFileName: 'cli.json',
4040
CLIJSONFileNameGlob: 'cli*.json',
4141
CLIJsonWithEnvironmentFileName: (env: string) => `cli.${env}.json`,
42+
43+
CfnFileName: (resourceName: string) => `${resourceName}-awscloudformation-template.json`,
4244
};
4345

4446
export class PathManager {
@@ -78,6 +80,12 @@ export class PathManager {
7880
getCurrentCloudBackendDirPath = (projectPath?: string): string =>
7981
this.constructPath(projectPath, [PathConstants.AmplifyDirName, PathConstants.CurrentCloudBackendDirName]);
8082

83+
getCurrentResourceParametersJsonPath = (projectPath: string | undefined, categoryName: string, resourceName: string): string =>
84+
path.join(this.getCurrentCloudBackendDirPath(projectPath), categoryName, resourceName, PathConstants.ParametersJsonFileName);
85+
86+
getCurrentCfnTemplatePath = (projectPath: string | undefined, categoryName: string, resourceName: string): string =>
87+
path.join(this.getCurrentCloudBackendDirPath(projectPath), categoryName, resourceName, PathConstants.CfnFileName(resourceName));
88+
8189
getAmplifyRcFilePath = (projectPath?: string): string => this.constructPath(projectPath, [PathConstants.AmplifyRcFileName]);
8290

8391
getGitIgnoreFilePath = (projectPath?: string): string => this.constructPath(projectPath, [PathConstants.GitIgnoreFileName]);
@@ -112,6 +120,9 @@ export class PathManager {
112120
getResourceParametersFilePath = (projectPath: string | undefined, category: string, resourceName: string): string =>
113121
path.join(this.getResourceDirectoryPath(projectPath, category, resourceName), PathConstants.ParametersJsonFileName);
114122

123+
getResourceCfnTemplatePath = (projectPath: string | undefined, category: string, resourceName: string): string =>
124+
path.join(this.getResourceDirectoryPath(projectPath, category, resourceName), PathConstants.CfnFileName(resourceName));
125+
115126
getReadMeFilePath = (projectPath?: string): string =>
116127
this.constructPath(projectPath, [PathConstants.AmplifyDirName, PathConstants.ReadMeFileName]);
117128

packages/amplify-cli-core/src/state-manager/stateManager.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ export class StateManager {
133133
return this.getData<$TSAny>(filePath, mergedOptions);
134134
};
135135

136+
getCurrentResourceParametersJson = (
137+
projectPath: string | undefined,
138+
category: string,
139+
resourceName: string,
140+
options?: GetOptions<$TSAny>,
141+
): $TSAny => {
142+
const filePath = pathManager.getCurrentResourceParametersJsonPath(projectPath, category, resourceName);
143+
const mergedOptions = {
144+
throwIfNotExist: true,
145+
...options,
146+
};
147+
148+
return this.getData<$TSAny>(filePath, mergedOptions);
149+
};
150+
136151
getAmplifyAdminConfigEntry = (appId: string, options?: GetOptions<$TSAny>) => {
137152
const mergedOptions = {
138153
throwIfNotExist: false,

packages/amplify-e2e-tests/src/__tests__/layer.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import {
2-
addLayer,
32
addFunction,
3+
addLayer,
44
addOptData,
55
amplifyPull,
66
amplifyPushAuth,
77
amplifyPushLayer,
8+
amplifyStatus,
89
createNewProjectDir,
910
deleteProject,
1011
deleteProjectDir,
11-
initJSProjectWithProfile,
1212
getAppId,
13+
getCurrentLayerArnFromMeta,
14+
getProjectConfig,
1315
getProjectMeta,
16+
initJSProjectWithProfile,
1417
LayerPermission,
1518
LayerPermissionChoice,
1619
LayerPermissionName,
1720
LayerRuntime,
1821
removeLayer,
22+
removeLayerVersion,
1923
updateLayer,
2024
updateOptData,
2125
validateLayerDir,
2226
validateLayerMetadata,
2327
validatePushedVersion,
24-
getCurrentLayerArnFromMeta,
25-
getProjectConfig,
26-
removeLayerVersion,
2728
} from 'amplify-e2e-core';
2829
import { v4 as uuid } from 'uuid';
2930
import { addEnvironment, checkoutEnvironment, listEnvironment } from '../environment/env';
@@ -71,7 +72,7 @@ describe('amplify add lambda layer', () => {
7172
expect(validateLayerDir(projRoot, settings, settings.runtimes)).toBe(false);
7273
});
7374

74-
it('init a project add 4 layers and delete first 3 of them and push and verify', async () => {
75+
it('init a project, add layer, push 4 layer versions and delete first 3 of them, then push and verify', async () => {
7576
const [shortId] = uuid().split('-');
7677
const layerName = `simplelayer${shortId}`;
7778
const runtime: LayerRuntime = 'nodejs';
@@ -236,11 +237,13 @@ describe('amplify add lambda layer', () => {
236237
await amplifyPushLayer(projRoot, {
237238
acceptSuggestedLayerVersionConfigurations: true,
238239
});
240+
await amplifyStatus(projRoot, 'No Change');
239241
layerTestArns.push(getCurrentLayerArnFromMeta(projRoot, settings));
240242
validatePushedVersion(projRoot, settings, expectedPerms);
241243
await validateLayerMetadata(projRoot, settings, getProjectMeta(projRoot), newEnvName, layerTestArns);
242244

243245
await checkoutEnvironment(projRoot, { envName });
246+
await amplifyStatus(projRoot, 'No Change');
244247
validatePushedVersion(projRoot, settings, expectedPerms);
245248
await validateLayerMetadata(projRoot, settings, getProjectMeta(projRoot), envName, integtestArns);
246249
});
@@ -309,6 +312,7 @@ describe('amplify add lambda layer - with amplify console app', () => {
309312
}
310313

311314
await amplifyPull(projRoot, {});
315+
await amplifyStatus(projRoot, 'No Change');
312316

313317
validatePushedVersion(projRoot, settings, expectedPerms);
314318
});

0 commit comments

Comments
 (0)