Skip to content

Commit 360b3ac

Browse files
ShadowCat567Vieltojarvi
andauthored
Seed Command using Identifier (#2804)
* seed command accepts identifier * updated tests * changeset * fix tests --------- Co-authored-by: Vieltojarvi <[email protected]>
1 parent 3843c6f commit 360b3ac

File tree

5 files changed

+141
-8
lines changed

5 files changed

+141
-8
lines changed

.changeset/silver-lies-jam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
update seed commands to take identifier

packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_command.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ void describe('sandbox seed command', () => {
121121
);
122122
assert.strictEqual(mockHandleProfile.mock.callCount(), 1);
123123
});
124+
125+
void it('run seed with identifier if provided', async () => {
126+
const output = await commandRunner.runCommand(
127+
'sandbox seed --identifier app-name',
128+
);
129+
130+
const outputArr = output.trimEnd().split('\n');
131+
const seedMessage = outputArr[1];
132+
const successMessage = outputArr[3].trimStart();
133+
134+
assert.ok(output !== undefined);
135+
assert.deepStrictEqual(seedMessage, 'seed has been run');
136+
assert.deepStrictEqual(
137+
successMessage,
138+
'✔ seed has successfully completed',
139+
);
140+
});
124141
});
125142

126143
void describe('seed script does not exist', () => {

packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_command.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Argv, CommandModule } from 'yargs';
1+
import { ArgumentsCamelCase, Argv, CommandModule } from 'yargs';
22
import path from 'path';
33
import { existsSync } from 'fs';
44
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
@@ -36,9 +36,11 @@ export class SandboxSeedCommand implements CommandModule<object> {
3636
/**
3737
* @inheritDoc
3838
*/
39-
handler = async (): Promise<void> => {
39+
handler = async (
40+
args: ArgumentsCamelCase<SandboxCommandGlobalOptions>,
41+
): Promise<void> => {
4042
printer.print(`${format.color('seed is running...', 'Blue')}`);
41-
const backendID = await this.backendIDResolver.resolve();
43+
const backendID = await this.backendIDResolver.resolve(args.identifier);
4244
const seedPath = path.join('amplify', 'seed', 'seed.ts');
4345
process.env.AMPLIFY_BACKEND_IDENTIFIER = JSON.stringify(backendID);
4446
try {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { after, before, describe, it, mock } from 'node:test';
2+
import fsp from 'fs/promises';
3+
import * as path from 'path';
4+
import yargs, { CommandModule } from 'yargs';
5+
import { TestCommandRunner } from '../../../test-utils/command_runner.js';
6+
import { printer } from '@aws-amplify/cli-core';
7+
import { CommandMiddleware } from '../../../command_middleware.js';
8+
import { SandboxSeedGeneratePolicyCommand } from './sandbox_seed_policy_command.js';
9+
import assert from 'node:assert';
10+
import { BackendIdentifier } from '@aws-amplify/plugin-types';
11+
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
12+
import { Effect, PolicyDocument, PolicyStatement } from 'aws-cdk-lib/aws-iam';
13+
import { generateSeedPolicyTemplate } from '../../../seed-policy-generation/generate_seed_policy_template.js';
14+
15+
const seedFileContents = 'console.log(`seed has been run`);';
16+
17+
const testBackendNameSpace = 'testSandboxId';
18+
const testSandboxName = 'testSandboxName';
19+
20+
const testBackendId: BackendIdentifier = {
21+
namespace: testBackendNameSpace,
22+
name: testSandboxName,
23+
type: 'sandbox',
24+
};
25+
26+
void describe('sandbox policy seed command', () => {
27+
let commandRunner: TestCommandRunner;
28+
29+
const commandMiddleware = new CommandMiddleware(printer);
30+
const mockHandleProfile = mock.method(
31+
commandMiddleware,
32+
'ensureAwsCredentialAndRegion',
33+
() => null,
34+
);
35+
36+
let amplifySeedDir: string;
37+
let fullPath: string;
38+
39+
const sandboxIdResolver: SandboxBackendIdResolver = {
40+
resolve: () => Promise.resolve(testBackendId),
41+
} as SandboxBackendIdResolver;
42+
43+
const seedPolicyMock = mock.fn(
44+
generateSeedPolicyTemplate,
45+
(): Promise<PolicyDocument> =>
46+
Promise.resolve(
47+
new PolicyDocument({
48+
statements: [
49+
new PolicyStatement({
50+
effect: Effect.ALLOW,
51+
actions: [
52+
'cognito-idp:AdminCreateUser',
53+
'cognito-idp:AdminAddUserToGroup',
54+
],
55+
resources: ['test-arn'],
56+
}),
57+
],
58+
}),
59+
),
60+
);
61+
62+
before(async () => {
63+
const sandboxSeedGeneratePolicyCommand =
64+
new SandboxSeedGeneratePolicyCommand(sandboxIdResolver, seedPolicyMock);
65+
66+
const parser = yargs().command(
67+
sandboxSeedGeneratePolicyCommand as unknown as CommandModule,
68+
);
69+
commandRunner = new TestCommandRunner(parser);
70+
mockHandleProfile.mock.resetCalls();
71+
72+
await fsp.mkdir(path.join(process.cwd(), 'amplify', 'seed'), {
73+
recursive: true,
74+
});
75+
76+
amplifySeedDir = path.join(process.cwd(), 'amplify');
77+
fullPath = path.join(process.cwd(), 'amplify', 'seed', 'seed.ts');
78+
await fsp.writeFile(fullPath, seedFileContents, 'utf8');
79+
});
80+
81+
after(async () => {
82+
await fsp.rm(amplifySeedDir, { recursive: true, force: true });
83+
if (process.env.AMPLIFY_BACKEND_IDENTIFIER) {
84+
delete process.env.AMPLIFY_BACKEND_IDENTIFIER;
85+
}
86+
});
87+
88+
void it('runs seed policy command without identifier', async () => {
89+
const output = await commandRunner.runCommand(
90+
'sandbox seed generate-policy',
91+
);
92+
93+
assert.ok(output !== undefined);
94+
});
95+
96+
void it('runs seed policy command with identifier', async () => {
97+
const output = await commandRunner.runCommand(
98+
'sandbox seed generate-policy --identifier app-name',
99+
);
100+
101+
assert.ok(output !== undefined);
102+
});
103+
});

packages/cli/src/commands/sandbox/sandbox-seed/sandbox_seed_policy_command.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Argv, CommandModule } from 'yargs';
1+
import { ArgumentsCamelCase, Argv, CommandModule } from 'yargs';
22
import { printer } from '@aws-amplify/cli-core';
33
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
44
import { generateSeedPolicyTemplate } from '../../../seed-policy-generation/generate_seed_policy_template.js';
5+
import { SandboxCommandGlobalOptions } from '../option_types.js';
56

67
/**
78
* Command that generates policy template with permissions to be able to run seed in sandbox environment
@@ -20,17 +21,22 @@ export class SandboxSeedGeneratePolicyCommand implements CommandModule<object> {
2021
/**
2122
* Generates policy to run seed, is a subcommand of seed
2223
*/
23-
constructor(private readonly backendIdResolver: SandboxBackendIdResolver) {
24+
constructor(
25+
private readonly backendIdResolver: SandboxBackendIdResolver,
26+
private readonly seedPolicyTemplate = generateSeedPolicyTemplate,
27+
) {
2428
this.command = 'generate-policy';
2529
this.describe = 'Generates policy for seeding';
2630
}
2731

2832
/**
2933
* @inheritDoc
3034
*/
31-
handler = async (): Promise<void> => {
32-
const backendId = await this.backendIdResolver.resolve();
33-
const policyDocument = await generateSeedPolicyTemplate(backendId);
35+
handler = async (
36+
args: ArgumentsCamelCase<SandboxCommandGlobalOptions>,
37+
): Promise<void> => {
38+
const backendId = await this.backendIdResolver.resolve(args.identifier);
39+
const policyDocument = await this.seedPolicyTemplate(backendId);
3440
printer.print(JSON.stringify(policyDocument.toJSON(), null, 2));
3541
};
3642

0 commit comments

Comments
 (0)