Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: api containers on repushing does not fail #8416

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type ContainersStackProps = Readonly<{
createCloudMapService?: boolean;
gitHubSourceActionInfo?: GitHubSourceActionInfo;
existingEcrRepositories: Set<string>;
currentStackName: string;
}>;
export abstract class ContainersStack extends cdk.Stack {
protected readonly vpcId: string;
Expand Down Expand Up @@ -240,6 +241,7 @@ export abstract class ContainersStack extends cdk.Stack {
taskPorts,
isInitialDeploy,
desiredCount,
currentStackName,
createCloudMapService,
} = this.props;

Expand Down Expand Up @@ -313,7 +315,7 @@ export abstract class ContainersStack extends cdk.Stack {
if (build) {
const logicalId = `${name}Repository`;

const repositoryName = `${this.envName}-${categoryName}-${apiName}-${name}`;
const repositoryName = `${currentStackName}-${categoryName}-${apiName}-${name}`;

if (this.props.existingEcrRepositories.has(repositoryName)) {
repository = ecr.Repository.fromRepositoryName(this, logicalId, repositoryName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export async function generateContainersArtifacts(
isInitialDeploy,
desiredCount,
restrictAccess,
currentStackName: envName,
apiType,
exposedContainer,
secretsArns,
Expand Down
15 changes: 14 additions & 1 deletion packages/amplify-e2e-core/src/categories/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getCLIPath, updateSchema, nspawn as spawn, KEY_DOWN_ARROW } from '..';
import * as fs from 'fs-extra';
import * as path from 'path';
import { selectRuntime, selectTemplate } from './lambda-function';
import { singleSelect, multiSelect } from '../utils/selectors';
import _ from 'lodash';
Expand Down Expand Up @@ -590,6 +591,18 @@ export function addRestContainerApiForCustomPolicies(projectDir: string, setting
.wait('Select which container is the entrypoint')
.sendCarriageReturn()
.wait('"amplify publish" will build all your local backend and frontend resources')
.run((err: Error) => err ? reject(err) : resolve());
.run((err: Error) => (err ? reject(err) : resolve()));
});
}

export function modifyRestAPI(projectDir: string, apiName: string) {
const indexFilePath = path.join(projectDir, 'amplify', 'backend', 'api', apiName, 'src', 'express', 'index.js');
const filesString = fs.readFileSync(indexFilePath, { encoding: 'utf8' });
const fileLines = filesString.split(EOL);
const index = fileLines.findIndex(r => r === '// Error middleware must be defined last');
fs.writeFileSync(indexFilePath, fileLines.slice(0, index - 1).join(EOL));
fs.appendFileSync(indexFilePath, EOL + "app.put('/post', async(req, res, next) => {\
return {};\
});" + EOL);
fs.appendFileSync(indexFilePath, fileLines.slice(index, fileLines.length).join(EOL));
}
ammarkarachi marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 14 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/containers-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
deleteProject,
deleteProjectDir,
initJSProjectWithProfile,
getProjectMeta,
modifyRestAPI,
} from 'amplify-e2e-core';
import fetch from 'node-fetch';
import { getAWSExports } from '../aws-exports/awsExports';
Expand Down Expand Up @@ -44,4 +46,16 @@ describe('amplify api add', () => {
const result = await (await fetch(`${endpoint}/images`)).text();
expect(result).toEqual('Processing images...');
});

it('init project, enable containers and add multicontainer api push, edit and push', async () => {
const envName = 'devtest';
await initJSProjectWithProfile(projRoot, { name: 'multicontainer', envName });
await setupAmplifyProject(projRoot);
await addRestContainerApi(projRoot);
await amplifyPushWithoutCodegen(projRoot);
const meta = await getProjectMeta(projRoot);
const apiName = Object.keys(meta['api'])[0];
await modifyRestAPI(projRoot, apiName);
await amplifyPushWithoutCodegen(projRoot);
});
});