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: block init if amplify app creation fails #10839

Merged
merged 6 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
@@ -0,0 +1,40 @@
import { Amplify } from 'aws-sdk';
import { stateManager } from 'amplify-cli-core';
import { getConfiguredAmplifyClient } from '../aws-utils/aws-amplify';
import { checkAmplifyServiceIAMPermission } from '../amplify-service-permission-check';
import { init } from '../amplify-service-manager';

jest.mock('../aws-utils/aws-amplify');
jest.mock('../amplify-service-permission-check');

const amplifyClientStub = ({
createApp: jest.fn().mockReturnValue({
promise: jest.fn().mockRejectedValue({
code: 'LimitExceededException',
}),
}),
} as unknown) as Amplify;
const getConfiguredAmplifyClientMock = getConfiguredAmplifyClient as jest.MockedFunction<typeof getConfiguredAmplifyClient>;
getConfiguredAmplifyClientMock.mockResolvedValue(amplifyClientStub);

const checkAmplifyServiceIAMPermissionMock = checkAmplifyServiceIAMPermission as jest.MockedFunction<
typeof checkAmplifyServiceIAMPermission
>;
checkAmplifyServiceIAMPermissionMock.mockResolvedValue(true);

jest.spyOn(stateManager, 'teamProviderInfoExists').mockReturnValue(false);

describe('init', () => {
it('throws ProjectInitError if Amplify app limit has been reached', async () => {
const amplifyServiceParamsStub = {
context: {},
awsConfigInfo: {},
projectName: 'test-project',
envName: 'test',
stackName: 'test-stack-name',
};
await expect(init(amplifyServiceParamsStub)).rejects.toMatchInlineSnapshot(
`[ProjectInitError: You have reached the Amplify App limit for this account and region]`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { checkAmplifyServiceIAMPermission } = require('./amplify-service-permissi
const { stateManager, AmplifyFault, AmplifyError } = require('amplify-cli-core');
const { fileLogger } = require('./utils/aws-logger');
const { loadConfigurationForEnv } = require('./configuration-manager');
const { printer } = require('amplify-prompts');
Fixed Show fixed Hide fixed
edwardfoyle marked this conversation as resolved.
Show resolved Hide resolved
const logger = fileLogger('amplify-service-manager');

async function init(amplifyServiceParams) {
Expand Down Expand Up @@ -56,10 +57,14 @@ async function init(amplifyServiceParams) {
context.print.info(`Amplify AppID found: ${inputAmplifyAppId}. Amplify App name is: ${getAppResult.app.name}`);
amplifyAppId = inputAmplifyAppId;
} catch (e) {
throw new AmplifyError('ProjectNotFoundError', {
message: `Amplify AppID ${inputAmplifyAppId} not found.`,
resolution: `Please ensure your local profile matches the AWS account or region in which the Amplify app exists.`,
}, e)
throw new AmplifyError(
'ProjectNotFoundError',
{
message: `Amplify AppID ${inputAmplifyAppId} not found.`,
resolution: `Please ensure your local profile matches the AWS account or region in which the Amplify app exists.`,
},
e,
);
}
}

Expand Down Expand Up @@ -128,17 +133,24 @@ async function init(amplifyServiceParams) {
}
} catch (e) {
if (e.code === 'LimitExceededException') {
// Do nothing
} else if (
e.code === 'BadRequestException' &&
e.message.includes('Rate exceeded while calling CreateApp, please slow down or try again later.')
) {
// Do nothing
} else {
throw new AmplifyFault('ProjectInitFault', {
message: e.message,
}, e);
throw new AmplifyError(
'ProjectInitError',
{
message: 'You have reached the Amplify App limit for this account and region',
resolution:
'Use a different account or region with fewer apps, or request a service limit increase: https://docs.aws.amazon.com/general/latest/gr/amplify.html#service-quotas-amplify',
},
e,
);
}
throw amplifyFaultWithTroubleshootingLink(
edwardfoyle marked this conversation as resolved.
Show resolved Hide resolved
'ProjectInitFault',
{
message: e.message,
stack: e.stack,
},
e,
);
}
}

Expand Down Expand Up @@ -229,9 +241,13 @@ async function deleteEnv(context, envName, awsConfigInfo) {
if (ex.code === 'NotFoundException') {
context.print.warning(ex.message);
} else {
throw new AmplifyFault('ProjectDeleteFault', {
message: ex.message,
}, ex);
throw new AmplifyFault(
'ProjectDeleteFault',
{
message: ex.message,
},
ex,
);
}
}
}
Expand Down Expand Up @@ -316,9 +332,13 @@ async function postPushCheck(context) {
) {
// Do nothing
} else {
throw new AmplifyFault('ProjectInitFault', {
message: e.message,
}, e);
throw new AmplifyFault(
'ProjectInitFault',
{
message: e.message,
},
e,
);
}
}
}
Expand All @@ -343,7 +363,6 @@ async function postPushCheck(context) {
const tpi = stateManager.getTeamProviderInfo();
tpi[envName][ProviderName][AmplifyAppIdLabel] = amplifyAppId;
stateManager.setTeamProviderInfo(undefined, tpi);

}

async function SelectFromExistingAppId(context, appIdsInTheSameLocalProjectAndRegion) {
Expand Down