From 9dd9da7be6d5588c803c975889bcd72c1bedf02d Mon Sep 17 00:00:00 2001 From: Roshane Pascual Date: Fri, 20 Sep 2024 10:48:02 -0700 Subject: [PATCH 1/2] update fallback for backend id resolvers if stack, app id, or branch are in args --- .changeset/itchy-vans-behave.md | 5 ++++ ...d_identifier_with_sandbox_fallback.test.ts | 27 ++++++++++++++++++- ...ackend_identifier_with_sandbox_fallback.ts | 22 ++++++++------- .../generate/forms/generate_forms_command.ts | 6 ++++- .../outputs/generate_outputs_command.ts | 7 ++++- .../generate_schema_command.ts | 8 +++--- 6 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 .changeset/itchy-vans-behave.md diff --git a/.changeset/itchy-vans-behave.md b/.changeset/itchy-vans-behave.md new file mode 100644 index 00000000000..7d823ef8380 --- /dev/null +++ b/.changeset/itchy-vans-behave.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend-cli': patch +--- + +update fallback for backend id resolvers if stack, app id, or branch are in args diff --git a/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.test.ts b/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.test.ts index f4dfc4155f5..0509b4e80b9 100644 --- a/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.test.ts +++ b/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.test.ts @@ -26,7 +26,7 @@ void it('if backend identifier resolves without error, the resolved id is return }); }); -void it('uses the sandbox id if the default identifier resolver fails', async () => { +void it('uses the sandbox id if the default identifier resolver fails and there is no stack, appId or branch in args', async () => { const appName = 'testAppName'; const namespaceResolver = { resolve: () => Promise.resolve(appName), @@ -54,3 +54,28 @@ void it('uses the sandbox id if the default identifier resolver fails', async () name: 'test-user', }); }); + +void it('does not use sandbox id if the default identifier resolver fails and there is stack, appId or branch in args', async () => { + const appName = 'testAppName'; + const namespaceResolver = { + resolve: () => Promise.resolve(appName), + }; + + const defaultResolver = new AppBackendIdentifierResolver(namespaceResolver); + const username = 'test-user'; + const sandboxResolver = new SandboxBackendIdResolver( + namespaceResolver, + () => + ({ + username, + } as never) + ); + const backendIdResolver = new BackendIdentifierResolverWithFallback( + defaultResolver, + sandboxResolver + ); + const resolvedId = await backendIdResolver.resolveDeployedBackendIdentifier({ + appId: 'testAppName', + }); + assert.deepEqual(resolvedId, undefined); +}); diff --git a/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts b/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts index 02a25c3ad96..6256e96bd12 100644 --- a/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts +++ b/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts @@ -18,23 +18,25 @@ export class BackendIdentifierResolverWithFallback private fallbackResolver: SandboxBackendIdResolver ) {} /** - * resolves the backend id, falling back to the sandbox id if there is an error + * resolves to deployed backend id, falling back to the sandbox id if there is an error and stack, appId, and branch are not provided */ resolveDeployedBackendIdentifier = async ( args: BackendIdentifierParameters ) => { - return ( - (await this.defaultResolver.resolveDeployedBackendIdentifier(args)) ?? - (await this.fallbackResolver.resolve()) - ); + if (args.stack || args.appId || args.branch) { + return await this.defaultResolver.resolveDeployedBackendIdentifier(args); + } + + return this.fallbackResolver.resolve(); }; /** - * Resolves deployed backend id to backend id, falling back to the sandbox id if there is an error + * Resolves deployed backend id to backend id, falling back to the sandbox id if there is an error and stack, appId, and branch are not provided */ resolveBackendIdentifier = async (args: BackendIdentifierParameters) => { - return ( - (await this.defaultResolver.resolveBackendIdentifier(args)) ?? - (await this.fallbackResolver.resolve()) - ); + if (args.stack || args.appId || args.branch) { + return await this.defaultResolver.resolveBackendIdentifier(args); + } + + return this.fallbackResolver.resolve(); }; } diff --git a/packages/cli/src/commands/generate/forms/generate_forms_command.ts b/packages/cli/src/commands/generate/forms/generate_forms_command.ts index 3c407da4886..34667029dc3 100644 --- a/packages/cli/src/commands/generate/forms/generate_forms_command.ts +++ b/packages/cli/src/commands/generate/forms/generate_forms_command.ts @@ -69,7 +69,11 @@ export class GenerateFormsCommand ); if (!backendIdentifier) { - throw new Error('Could not resolve the backend identifier'); + throw new AmplifyUserError('BackendIdentifierResolverError', { + message: 'Could not resolve the backend identifier.', + resolution: + 'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.', + }); } const backendOutputClient = this.backendOutputClientBuilder(); diff --git a/packages/cli/src/commands/generate/outputs/generate_outputs_command.ts b/packages/cli/src/commands/generate/outputs/generate_outputs_command.ts index 0a6e2921064..0edb427a00e 100644 --- a/packages/cli/src/commands/generate/outputs/generate_outputs_command.ts +++ b/packages/cli/src/commands/generate/outputs/generate_outputs_command.ts @@ -8,6 +8,7 @@ import { import { BackendIdentifierResolver } from '../../../backend-identifier/backend_identifier_resolver.js'; import { ClientConfigGeneratorAdapter } from '../../../client-config/client_config_generator_adapter.js'; import { ArgumentsKebabCase } from '../../../kebab_case.js'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; export type GenerateOutputsCommandOptions = ArgumentsKebabCase; @@ -60,7 +61,11 @@ export class GenerateOutputsCommand ); if (!backendIdentifier) { - throw new Error('Could not resolve the backend identifier'); + throw new AmplifyUserError('BackendIdentifierResolverError', { + message: 'Could not resolve the backend identifier.', + resolution: + 'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.', + }); } await this.clientConfigGenerator.generateClientConfigToFile( diff --git a/packages/cli/src/commands/generate/schema-from-database/generate_schema_command.ts b/packages/cli/src/commands/generate/schema-from-database/generate_schema_command.ts index 9aaefd9bc92..a0896a7b30e 100644 --- a/packages/cli/src/commands/generate/schema-from-database/generate_schema_command.ts +++ b/packages/cli/src/commands/generate/schema-from-database/generate_schema_command.ts @@ -3,7 +3,7 @@ import { BackendIdentifierResolver } from '../../../backend-identifier/backend_i import { ArgumentsKebabCase } from '../../../kebab_case.js'; import { SecretClient } from '@aws-amplify/backend-secret'; import { SchemaGenerator } from '@aws-amplify/schema-generator'; -import { AmplifyFault } from '@aws-amplify/platform-core'; +import { AmplifyUserError } from '@aws-amplify/platform-core'; const DEFAULT_OUTPUT = 'amplify/data/schema.sql.ts'; @@ -57,8 +57,10 @@ export class GenerateSchemaCommand await this.backendIdentifierResolver.resolveBackendIdentifier(args); if (!backendIdentifier) { - throw new AmplifyFault('BackendIdentifierFault', { - message: 'Could not resolve the backend identifier', + throw new AmplifyUserError('BackendIdentifierResolverError', { + message: 'Could not resolve the backend identifier.', + resolution: + 'Ensure stack name or Amplify App ID and branch specified are correct and exists, then re-run this command.', }); } From 249206cc7eb07db147a3b6a6d3a7f5680553195b Mon Sep 17 00:00:00 2001 From: Roshane Pascual Date: Fri, 20 Sep 2024 11:24:18 -0700 Subject: [PATCH 2/2] pr feedback --- .../backend_identifier_with_sandbox_fallback.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts b/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts index 6256e96bd12..f1bcbd2808a 100644 --- a/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts +++ b/packages/cli/src/backend-identifier/backend_identifier_with_sandbox_fallback.ts @@ -18,23 +18,23 @@ export class BackendIdentifierResolverWithFallback private fallbackResolver: SandboxBackendIdResolver ) {} /** - * resolves to deployed backend id, falling back to the sandbox id if there is an error and stack, appId, and branch are not provided + * resolves to deployed backend id, falling back to the sandbox id if stack or appId and branch inputs are not provided */ resolveDeployedBackendIdentifier = async ( args: BackendIdentifierParameters ) => { if (args.stack || args.appId || args.branch) { - return await this.defaultResolver.resolveDeployedBackendIdentifier(args); + return this.defaultResolver.resolveDeployedBackendIdentifier(args); } return this.fallbackResolver.resolve(); }; /** - * Resolves deployed backend id to backend id, falling back to the sandbox id if there is an error and stack, appId, and branch are not provided + * Resolves deployed backend id to backend id, falling back to the sandbox id if stack or appId and branch inputs are not provided */ resolveBackendIdentifier = async (args: BackendIdentifierParameters) => { if (args.stack || args.appId || args.branch) { - return await this.defaultResolver.resolveBackendIdentifier(args); + return this.defaultResolver.resolveBackendIdentifier(args); } return this.fallbackResolver.resolve();