@aws-amplify/cli-internal
Version:
Amplify CLI
205 lines • 11.2 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmplifyMigrationRefactorStep = void 0;
const step_1 = require("./_common/step");
const plan_1 = require("./_common/plan");
const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core");
const client_sts_1 = require("@aws-sdk/client-sts");
const stack_facade_1 = require("./refactor/stack-facade");
const auth_cognito_forward_1 = require("./refactor/auth/auth-cognito-forward");
const auth_cognito_rollback_1 = require("./refactor/auth/auth-cognito-rollback");
const storage_forward_1 = require("./refactor/storage/storage-forward");
const storage_rollback_1 = require("./refactor/storage/storage-rollback");
const storage_dynamo_forward_1 = require("./refactor/storage/storage-dynamo-forward");
const storage_dynamo_rollback_1 = require("./refactor/storage/storage-dynamo-rollback");
const analytics_forward_1 = require("./refactor/analytics/analytics-forward");
const analytics_rollback_1 = require("./refactor/analytics/analytics-rollback");
const auth_user_pool_groups_forward_1 = require("./refactor/auth/auth-user-pool-groups-forward");
const auth_user_pool_groups_rollback_1 = require("./refactor/auth/auth-user-pool-groups-rollback");
const cfn_1 = require("./_common/cfn");
const amplify_prompts_1 = require("@aws-amplify/amplify-prompts");
const chalk_1 = __importDefault(require("chalk"));
const assess_1 = require("./assess");
const custom_cdk_rollback_1 = require("./refactor/custom/custom-cdk-rollback");
const custom_cdk_forward_1 = require("./refactor/custom/custom-cdk-forward");
const GUIDE_LINK = 'https://docs.amplify.aws/react/start/migrate-to-gen2/migrate-existing-app/#step-8-post-refactor-critical';
class AmplifyMigrationRefactorStep extends step_1.AmplifyMigrationStep {
async forward() {
const toStack = this.extractParameters();
const { accountId, gen1Env, gen2Branch, cfn } = await this.createInfrastructure(toStack);
const refactorers = [];
const assessor = new assess_1.AmplifyMigrationAssessor(this.gen1App, this.logger);
const assessment = assessor.assess();
const discovered = this.gen1App.discover();
for (const resource of discovered) {
if (assessment.of(resource, 'refactor').level !== 'supported') {
continue;
}
switch (resource.key) {
case 'auth:Cognito': {
const isReferenceAuth = discovered
.filter((r) => r.category === 'auth')
.some((r) => {
var _a;
const meta = ((_a = this.gen1App.categoryMeta('auth')) !== null && _a !== void 0 ? _a : {})[r.resourceName];
return (meta === null || meta === void 0 ? void 0 : meta.serviceType) === 'imported';
});
if (!isReferenceAuth) {
refactorers.push(new auth_cognito_forward_1.AuthCognitoForwardRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
}
break;
}
case 'auth:Cognito-UserPool-Groups':
refactorers.push(new auth_user_pool_groups_forward_1.AuthUserPoolGroupsForwardRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'storage:S3':
refactorers.push(new storage_forward_1.StorageS3ForwardRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'storage:DynamoDB':
refactorers.push(new storage_dynamo_forward_1.StorageDynamoForwardRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'analytics:Kinesis':
refactorers.push(new analytics_forward_1.AnalyticsKinesisForwardRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'custom:customCDK':
refactorers.push(new custom_cdk_forward_1.CustomCDKForwardRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'function:Lambda':
case 'api:AppSync':
case 'api:API Gateway':
case 'geo:Map':
case 'geo:PlaceIndex':
break;
case 'geo:GeofenceCollection':
case 'UNKNOWN':
break;
}
}
const plan = await this.buildPlan(refactorers, assessment, [
'Stateful resources (Cognito, S3, DynamoDB, Kinesis, etc...) will be moved from' +
' Gen1 to Gen2 CloudFormation stacks — Gen2 will own them going forward',
], 'Execute');
plan.addOperation({
describe: async () => [],
validate: () => undefined,
execute: async () => {
amplify_prompts_1.printer.blankLine();
amplify_prompts_1.printer.info(chalk_1.default.bold(chalk_1.default.yellow('⚠️ Follow the post-refactor manual steps to avoid resource replacement ⚠️')));
amplify_prompts_1.printer.blankLine();
amplify_prompts_1.printer.info(chalk_1.default.yellow(GUIDE_LINK));
},
});
return plan;
}
async rollback() {
const toStack = this.extractParameters();
const { accountId, gen1Env, gen2Branch, cfn } = await this.createInfrastructure(toStack);
const refactorers = [];
const assessor = new assess_1.AmplifyMigrationAssessor(this.gen1App, this.logger);
const assessment = assessor.assess();
const discovered = this.gen1App.discover();
for (const resource of discovered) {
if (assessment.of(resource, 'refactor').level !== 'supported') {
continue;
}
switch (resource.key) {
case 'auth:Cognito': {
const isReferenceAuth = discovered
.filter((r) => r.category === 'auth')
.some((r) => {
var _a;
const meta = ((_a = this.gen1App.categoryMeta('auth')) !== null && _a !== void 0 ? _a : {})[r.resourceName];
return (meta === null || meta === void 0 ? void 0 : meta.serviceType) === 'imported';
});
if (!isReferenceAuth) {
refactorers.push(new auth_cognito_rollback_1.AuthCognitoRollbackRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
}
break;
}
case 'auth:Cognito-UserPool-Groups':
refactorers.push(new auth_user_pool_groups_rollback_1.AuthUserPoolGroupsRollbackRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'storage:S3':
refactorers.push(new storage_rollback_1.StorageS3RollbackRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'storage:DynamoDB':
refactorers.push(new storage_dynamo_rollback_1.StorageDynamoRollbackRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'analytics:Kinesis':
refactorers.push(new analytics_rollback_1.AnalyticsKinesisRollbackRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'custom:customCDK':
refactorers.push(new custom_cdk_rollback_1.CustomCDKRollbackRefactorer(gen1Env, gen2Branch, this.gen1App, accountId, this.logger, resource, cfn));
break;
case 'function:Lambda':
case 'api:AppSync':
case 'api:API Gateway':
case 'geo:Map':
case 'geo:PlaceIndex':
break;
case 'geo:GeofenceCollection':
case 'UNKNOWN':
break;
}
}
return this.buildPlan(refactorers, assessment, [
'Stateful resources (Cognito, S3, DynamoDB, Kinesis, etc...) will be moved back to Gen1 CloudFormation stacks — Gen1 will own them again',
], 'Rollback');
}
async createInfrastructure(toStack) {
const { Account: accountId } = await this.gen1App.clients.sts.send(new client_sts_1.GetCallerIdentityCommand({}));
if (!accountId) {
throw new amplify_cli_core_1.AmplifyError('ConfigurationError', { message: 'Unable to determine AWS account ID' });
}
const clients = this.gen1App.clients;
const gen1Env = new stack_facade_1.StackFacade(clients, this.gen1App.rootStackName);
const gen2Branch = new stack_facade_1.StackFacade(clients, toStack);
const cfn = new cfn_1.Cfn(this.gen1App, this.logger);
return { accountId, gen1Env, gen2Branch, cfn };
}
async buildPlan(refactorers, assessment, implications, title) {
const operations = [];
operations.push({
describe: async () => [],
validate: () => ({ description: 'Environment Locked', run: () => this.validateLockStatus() }),
execute: async () => { },
});
operations.push({
describe: async () => [],
validate: () => ({
description: 'Assessment',
run: async () => {
const valid = assessment.validFor('refactor');
return { valid, report: valid ? undefined : assessment.render() };
},
}),
execute: async () => { },
});
for (const refactorer of refactorers) {
operations.push(...(await refactorer.plan()));
}
return new plan_1.Plan({ operations, logger: this.logger, implications, title });
}
async validateLockStatus() {
try {
await this.validations.validateLockStatus();
return { valid: true };
}
catch (e) {
return { valid: false, report: e.message };
}
}
extractParameters() {
var _a, _b;
const toStack = (_b = (_a = this.context.parameters) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.to;
if (!toStack) {
throw new amplify_cli_core_1.AmplifyError('InputValidationError', { message: '--to is required' });
}
return toStack;
}
}
exports.AmplifyMigrationRefactorStep = AmplifyMigrationRefactorStep;
//# sourceMappingURL=refactor.js.map