diff --git a/packages/amplify-category-auth/src/index.js b/packages/amplify-category-auth/src/index.js index 8cdca1b201d..5fec6981024 100644 --- a/packages/amplify-category-auth/src/index.js +++ b/packages/amplify-category-auth/src/index.js @@ -33,8 +33,8 @@ const { } = require('./provider-utils/awscloudformation/utils/auth-sms-workflow-helper'); const { AuthInputState } = require('./provider-utils/awscloudformation/auth-inputs-manager/auth-input-state'); const { printer } = require('amplify-prompts'); -const { checkAuthResourceMigration } = require('./provider-utils/awscloudformation/utils/migrate-override-resource'); const { privateKeys } = require('./provider-utils/awscloudformation/constants'); +const { checkAuthResourceMigration } = require('./provider-utils/awscloudformation/utils/check-for-auth-migration'); // this function is being kept for temporary compatability. async function add(context, skipNextSteps = false) { @@ -82,6 +82,10 @@ function canResourceBeTransformed(resourceName) { return resourceInputState.cliInputFileExists(); } +async function migrateAuthResource( context, resourceName ){ + await checkAuthResourceMigration( context, resourceName ); +} + async function externalAuthEnable(context, externalCategory, resourceName, requirements) { const { amplify } = context; const serviceMetadata = getSupportedServices.supportedServices; @@ -103,7 +107,7 @@ async function externalAuthEnable(context, externalCategory, resourceName, requi } currentAuthName = await getAuthResourceName(context); // check for migration when auth has been enabled - checkAuthResourceMigration(context, currentAuthName); + await checkAuthResourceMigration(context, currentAuthName); const cliState = new AuthInputState(currentAuthName); currentAuthParams = await cliState.loadResourceParameters(context, cliState.getCLIInputPayload()); @@ -496,6 +500,7 @@ async function isSMSWorkflowEnabled(context, resourceName) { module.exports = { externalAuthEnable, + migrateAuthResource, checkRequirements, add, migrate, @@ -517,4 +522,5 @@ module.exports = { AmplifyUserPoolGroupTransform, transformCategoryStack, AmplifyAuthCognitoStackTemplate, + }; diff --git a/packages/amplify-category-storage/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/s3-walkthrough.test.ts b/packages/amplify-category-storage/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/s3-walkthrough.test.ts index fa1cf9031a9..4a8b178a03c 100644 --- a/packages/amplify-category-storage/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/s3-walkthrough.test.ts +++ b/packages/amplify-category-storage/src/__tests__/provider-utils/awscloudformation/service-walkthroughs/s3-walkthrough.test.ts @@ -10,12 +10,14 @@ import { S3TriggerFunctionType, S3UserInputs, } from '../../../../provider-utils/awscloudformation/service-walkthrough-types/s3-user-input-types'; +import * as s3AuthAPI from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-auth-api'; import { S3CLITriggerUpdateMenuOptions, UserPermissionTypeOptions } from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-questions'; jest.mock('amplify-cli-core'); jest.mock('amplify-prompts'); jest.mock('../../../../provider-utils/awscloudformation/service-walkthroughs/s3-user-input-state'); jest.mock('../../../../provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform'); +jest.mock('../../../../provider-utils/awscloudformation/service-walkthroughs/s3-auth-api'); jest.mock('uuid'); describe('add s3 walkthrough tests', () => { @@ -61,6 +63,9 @@ describe('add s3 walkthrough tests', () => { it('addWalkthrough() simple-auth test', async () => { jest.spyOn(S3InputState.prototype, 'saveCliInputPayload').mockImplementation(() => true); jest.spyOn(AmplifyS3ResourceStackTransform.prototype, 'transform').mockImplementation(() => Promise.resolve()); + jest.spyOn(s3AuthAPI, 'migrateAuthDependencyResource').mockReturnValue(new Promise((resolve, _reject)=>{ + process.nextTick(() => resolve(undefined)); + })); const mockDataBuilder = new S3MockDataBuilder(undefined); const expectedCLIInputsJSON: S3UserInputs = mockDataBuilder.getCLIInputs(); diff --git a/packages/amplify-category-storage/src/commands/storage/override.ts b/packages/amplify-category-storage/src/commands/storage/override.ts index 249f2185831..9f44725ef3b 100644 --- a/packages/amplify-category-storage/src/commands/storage/override.ts +++ b/packages/amplify-category-storage/src/commands/storage/override.ts @@ -62,10 +62,10 @@ export const run = async (context: $TSContext) => { } } else if (amplifyMeta[categoryName][selectedResourceName].service === AmplifySupportedService.S3 ) { // S3 migration logic goes in here - const resourceInputState = new S3InputState(selectedResourceName, undefined); - if (!resourceInputState.cliInputFileExists()) { + const s3ResourceInputState = new S3InputState(selectedResourceName, undefined); + if (!s3ResourceInputState.cliInputFileExists()) { if (await prompter.yesOrNo('File migration required to continue. Do you want to continue?', true)) { - resourceInputState.migrate(); + await s3ResourceInputState.migrate(context); //migrate auth and storage config resources const stackGenerator = new AmplifyS3ResourceStackTransform(selectedResourceName, context); stackGenerator.transform( CLISubCommandType.MIGRATE ); } else { diff --git a/packages/amplify-category-storage/src/provider-utils/awscloudformation/service-walkthroughs/s3-auth-api.ts b/packages/amplify-category-storage/src/provider-utils/awscloudformation/service-walkthroughs/s3-auth-api.ts index ca3af603323..cb86bec0ee7 100644 --- a/packages/amplify-category-storage/src/provider-utils/awscloudformation/service-walkthroughs/s3-auth-api.ts +++ b/packages/amplify-category-storage/src/provider-utils/awscloudformation/service-walkthroughs/s3-auth-api.ts @@ -1,10 +1,16 @@ -import { $TSAny, $TSContext } from "amplify-cli-core"; -import { S3UserInputs } from "../service-walkthrough-types/s3-user-input-types"; -import { S3InputState } from "./s3-user-input-state"; +import { $TSAny, $TSContext} from "amplify-cli-core"; +import { printer } from "amplify-prompts"; +import { AmplifyCategories } from "amplify-cli-core"; /* This file contains all functions interacting with AUTH category */ //UPSTREAM API: function to be called from Storage to fetch or update Auth resources + +/** + * Get the name of the Auth resource used by S3 + * @param context used to fetch all auth resources used by storage(S3) + * @returns Name of the auth resource used by S3 + */ export async function getAuthResourceARN( context : $TSContext ) : Promise { let authResources = (await context.amplify.getResourceStatus('auth')).allResources; authResources = authResources.filter((resource: $TSAny) => resource.service === 'Cognito'); @@ -13,3 +19,19 @@ export async function getAuthResourceARN( context : $TSContext ) : Promise { +export async function migrateStorageCategory(context: $TSContext, resourceName: string): Promise { let cliInputsState = new S3InputState(resourceName, undefined); //Check if migration is required if (!cliInputsState.cliInputFileExists()) { - cliInputsState.migrate(); + await cliInputsState.migrate( context ); const stackGenerator = new AmplifyS3ResourceStackTransform(resourceName, context); await stackGenerator.transform(CLISubCommandType.MIGRATE); return stackGenerator.getCFN();