From e19e724951861806a08eedf0a616793d312ffd15 Mon Sep 17 00:00:00 2001 From: John Hockett Date: Wed, 3 Nov 2021 22:41:57 -0700 Subject: [PATCH] fix: pushing multiple APIs at a time (#8663) --- .../src/commands/api/override.ts | 2 +- .../awscloudformation/apigw-input-state.ts | 13 ++----------- .../cdk-stack-builder/apigw-stack-transform.ts | 4 ++-- .../src/provider-utils/awscloudformation/index.ts | 10 +++++----- .../service-walkthroughs/apigw-walkthrough.ts | 7 ++++++- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/packages/amplify-category-api/src/commands/api/override.ts b/packages/amplify-category-api/src/commands/api/override.ts index a7d10476369..b5c57c0bef4 100644 --- a/packages/amplify-category-api/src/commands/api/override.ts +++ b/packages/amplify-category-api/src/commands/api/override.ts @@ -54,7 +54,7 @@ export const run = async (context: $TSContext) => { throw 'To be implemented'; } else if (service === AmplifySupportedService.APIGW) { // Migration logic goes in here - const apigwInputState = ApigwInputState.getInstance(context, selectedResourceName); + const apigwInputState = new ApigwInputState(context, selectedResourceName); if (!apigwInputState.cliInputsFileExists()) { if (await prompter.yesOrNo('File migration required to continue. Do you want to continue?', true)) { await apigwInputState.migrateApigwResource(selectedResourceName); diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts index 758850e5a87..0eb42cb4b3a 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/apigw-input-state.ts @@ -17,22 +17,13 @@ import { ApigwInputs, ApigwStackTransform, CrudOperation, Path, PermissionSettin import { ApigwWalkthroughReturnPromise } from './service-walkthrough-types/apigw-types'; export class ApigwInputState { - private static instance: ApigwInputState; projectRootPath: string; resourceName: string; paths: { [pathName: string]: Path }; - private constructor(private readonly context: $TSContext, resourceName?: string) { + constructor(private readonly context: $TSContext, resourceName?: string) { this.projectRootPath = pathManager.findProjectRoot(); this.resourceName = resourceName; - ApigwInputState.instance = this; - } - - public static getInstance(context: $TSContext, resourceName?: string) { - if (!ApigwInputState.instance) { - new ApigwInputState(context, resourceName); - } - return ApigwInputState.instance; } public addAdminQueriesResource = async (adminQueriesProps: AdminQueriesProps) => { @@ -214,7 +205,7 @@ export class ApigwInputState { stateManager.setResourceParametersJson(this.projectRootPath, AmplifyCategories.API, this.resourceName, {}); - const stack = new ApigwStackTransform(this.context, this.resourceName); + const stack = new ApigwStackTransform(this.context, this.resourceName, this); await stack.transform(); } diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts index 75f77944294..924ad98e4cb 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/cdk-stack-builder/apigw-stack-transform.ts @@ -28,12 +28,12 @@ export class ApigwStackTransform { cfnInputParams!: {}; resourceName: string; - constructor(context: $TSContext, resourceName: string) { + constructor(context: $TSContext, resourceName: string, cliInputState?: ApigwInputState) { this._app = new cdk.App(); this.resourceName = resourceName; // Validate the cli-inputs.json for the resource - this.cliInputsState = ApigwInputState.getInstance(context, this.resourceName); + this.cliInputsState = cliInputState ?? new ApigwInputState(context, resourceName); this.cliInputs = this.cliInputsState.getCliInputPayload(); this.cliInputsState.isCLIInputsValid(); } diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts index f4563957d0c..f8e36add0fb 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/index.ts @@ -21,7 +21,7 @@ export async function addAdminQueriesApi( context: $TSContext, apiProps: { apiName: string; functionName: string; authResourceName: string; dependsOn: $TSObject[] }, ) { - const apigwInputState = ApigwInputState.getInstance(context, apiProps.apiName); + const apigwInputState = new ApigwInputState(context, apiProps.apiName); return apigwInputState.addAdminQueriesResource(apiProps); } @@ -29,13 +29,13 @@ export async function updateAdminQueriesApi( context: $TSContext, apiProps: { apiName: string; functionName: string; authResourceName: string; dependsOn: $TSObject[] }, ) { - const apigwInputState = ApigwInputState.getInstance(context, apiProps.apiName); + const apigwInputState = new ApigwInputState(context, apiProps.apiName); // Check for migration if (!apigwInputState.cliInputsFileExists()) { await apigwInputState.migrateAdminQueries(apiProps); } else { - return apigwInputState.addAdminQueriesResource(apiProps); + return apigwInputState.updateAdminQueriesResource(apiProps); } } @@ -79,7 +79,7 @@ async function addNonContainerResource(context: $TSContext, service: string, opt } return apiName; case AmplifySupportedService.APIGW: - const apigwInputState = ApigwInputState.getInstance(context); + const apigwInputState = new ApigwInputState(context); return apigwInputState.addApigwResource(serviceWalkthroughPromise, options); default: return legacyAddResource(serviceWalkthroughPromise, context, category, service, options); @@ -263,7 +263,7 @@ async function updateNonContainerResource(context: $TSContext, service: string) case AmplifySupportedService.APPSYNC: return updateWalkthroughPromise.then(getCfnApiArtifactHandler(context).updateArtifacts); default: - const apigwInputState = ApigwInputState.getInstance(context); + const apigwInputState = new ApigwInputState(context); return apigwInputState.updateApigwResource(updateWalkthroughPromise); } } diff --git a/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts b/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts index 049a09ed4f8..860e748aa26 100644 --- a/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts +++ b/packages/amplify-category-api/src/provider-utils/awscloudformation/service-walkthroughs/apigw-walkthrough.ts @@ -151,6 +151,11 @@ async function askApiName(context: $TSContext, defaultResourceName: string) { required: true, })(input); + const adminQueriesName = 'AdminQueries'; + if (input === adminQueriesName) { + return `${adminQueriesName} is a reserved name for REST API resources for use by the auth category. Run "amplify update auth" to create an Admin Queries API.`; + } + let uniqueCheck = false; try { uniqueCheck = isResourceNameUnique(category, input); @@ -585,7 +590,7 @@ async function askLambdaArn(context: $TSContext, currentPath?: ApigwPath) { } export async function migrate(context: $TSContext, projectPath: string, resourceName: string) { - const apigwInputState = ApigwInputState.getInstance(context, resourceName); + const apigwInputState = new ApigwInputState(context, resourceName); return apigwInputState.migrateApigwResource(resourceName); }