Skip to content

Commit

Permalink
refactor stack output writing to shared package (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle authored Sep 21, 2023
1 parent 1a6dd46 commit f75fa53
Show file tree
Hide file tree
Showing 57 changed files with 372 additions and 198 deletions.
16 changes: 16 additions & 0 deletions .changeset/tough-bats-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@aws-amplify/backend-output-schemas': minor
'@aws-amplify/backend-output-storage': patch
'@aws-amplify/integration-tests': patch
'@aws-amplify/storage-construct-alpha': patch
'@aws-amplify/backend-function': patch
'@aws-amplify/backend-graphql': patch
'@aws-amplify/backend-storage': patch
'@aws-amplify/auth-construct-alpha': patch
'create-amplify': patch
'@aws-amplify/client-config': minor
'@aws-amplify/backend-auth': patch
'@aws-amplify/backend': patch
---

Refactor OutputStorageStrategy into stateless shared dependency
1 change: 1 addition & 0 deletions .eslint_dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default [
'stdout',
'subcommand',
'subcommands',
'submodule',
'timestamps',
'tmpdir',
'toggleable',
Expand Down
30 changes: 27 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions packages/auth-construct/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ import { AuthOutput } from '@aws-amplify/backend-output-schemas/auth';
import { AuthResources } from '@aws-amplify/plugin-types';
import { aws_cognito } from 'aws-cdk-lib';
import { BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
import { BackendOutputWriter } from '@aws-amplify/plugin-types';
import { Construct } from 'constructs';
import { CustomAttributeConfig } from 'aws-cdk-lib/aws-cognito';
import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { ResourceProvider } from '@aws-amplify/plugin-types';
import { StandardAttributes } from 'aws-cdk-lib/aws-cognito';

// @public
export class AmplifyAuth extends Construct implements BackendOutputWriter, ResourceProvider<AuthResources> {
export class AmplifyAuth extends Construct implements ResourceProvider<AuthResources> {
constructor(scope: Construct, id: string, props?: AuthProps);
addTrigger: (event: TriggerEvent, handler: IFunction | AmplifyFunction) => void;
static attribute: (name: keyof aws_cognito.StandardAttributes) => AuthStandardAttribute;
static customAttribute: AuthCustomAttributeFactory;
readonly resources: AuthResources;
storeOutput: (outputStorageStrategy: BackendOutputStorageStrategy<AuthOutput>) => void;
}

// @public
Expand Down Expand Up @@ -72,6 +70,7 @@ export type AuthProps = {
userAttributes?: AuthUserAttribute[];
multifactor?: MFA;
accountRecovery?: aws_cognito.AccountRecovery;
outputStorageStrategy?: BackendOutputStorageStrategy<AuthOutput>;
};

// @public
Expand Down
3 changes: 2 additions & 1 deletion packages/auth-construct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"update:api": "api-extractor run --local"
},
"dependencies": {
"@aws-amplify/backend-output-schemas": "^0.2.0-alpha.2"
"@aws-amplify/backend-output-schemas": "^0.2.0-alpha.2",
"@aws-amplify/backend-output-storage": "^0.1.0"
},
"peerDependencies": {
"@aws-amplify/plugin-types": "^0.1.1-alpha.4",
Expand Down
29 changes: 27 additions & 2 deletions packages/auth-construct/src/construct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ describe('Auth construct', () => {
loginWith: {
email: true,
},
outputStorageStrategy: stubBackendOutputStorageStrategy,
});

const expectedUserPoolId = (
Expand All @@ -487,8 +488,6 @@ describe('Auth construct', () => {
).userPoolClientId;
const expectedRegion = Stack.of(authConstruct).region;

authConstruct.storeOutput(stubBackendOutputStorageStrategy);

const storeOutputArgs = storeOutputMock.mock.calls[0].arguments;
assert.equal(storeOutputArgs.length, 2);

Expand All @@ -505,6 +504,32 @@ describe('Auth construct', () => {
},
]);
});

it('stores output when no storage strategy is injected', () => {
const app = new App();
const stack = new Stack(app);

new AmplifyAuth(stack, 'test', {
loginWith: {
email: true,
},
});

const template = Template.fromStack(stack);
template.templateMatches({
Metadata: {
[authOutputKey]: {
version: '1',
stackOutputs: [
'userPoolId',
'webClientId',
'identityPoolId',
'authRegion',
],
},
},
});
});
});

describe('defaults', () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/auth-construct/src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
AmplifyFunction,
AuthResources,
BackendOutputStorageStrategy,
BackendOutputWriter,
ResourceProvider,
} from '@aws-amplify/plugin-types';
import {
Expand Down Expand Up @@ -32,6 +31,7 @@ import {
AuthStandardAttribute,
} from './attributes.js';
import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { StackMetadataBackendOutputStorageStrategy } from '@aws-amplify/backend-output-storage';

type DefaultRoles = { auth: Role; unAuth: Role };
type IdentityProviderSetupResult = {
Expand All @@ -55,7 +55,7 @@ const authProvidersList = {
*/
export class AmplifyAuth
extends Construct
implements BackendOutputWriter, ResourceProvider<AuthResources>
implements ResourceProvider<AuthResources>
{
/**
* The resources generated by the construct.
Expand Down Expand Up @@ -123,6 +123,7 @@ export class AmplifyAuth
identityPoolRoleAttachment,
},
};
this.storeOutput(props.outputStorageStrategy);
}

/**
Expand Down Expand Up @@ -437,8 +438,10 @@ export class AmplifyAuth
/**
* Stores auth output using the provided strategy
*/
storeOutput = (
outputStorageStrategy: BackendOutputStorageStrategy<AuthOutput>
private storeOutput = (
outputStorageStrategy: BackendOutputStorageStrategy<AuthOutput> = new StackMetadataBackendOutputStorageStrategy(
Stack.of(this)
)
): void => {
const output: AuthOutput['payload'] = {
userPoolId: this.resources.userPool.userPoolId,
Expand Down
4 changes: 4 additions & 0 deletions packages/auth-construct/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { aws_cognito as cognito } from 'aws-cdk-lib';
import { AuthUserAttribute } from './attributes.js';
import { triggerEvents } from './trigger_events.js';
import { BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
import { AuthOutput } from '@aws-amplify/backend-output-schemas/auth';

/**
* Email login options.
Expand Down Expand Up @@ -95,4 +97,6 @@ export type AuthProps = {
* If only email or phone are enabled, they will be the default recovery methods.
*/
accountRecovery?: cognito.AccountRecovery;

outputStorageStrategy?: BackendOutputStorageStrategy<AuthOutput>;
};
1 change: 1 addition & 0 deletions packages/auth-construct/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"compilerOptions": { "rootDir": "src", "outDir": "lib" },
"references": [
{ "path": "../backend-output-schemas" },
{ "path": "../backend-output-storage" },
{ "path": "../plugin-types" }
]
}
2 changes: 1 addition & 1 deletion packages/backend-auth/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class AmplifyAuthFactory implements ConstructFactory<AmplifyAuth & Resour
}

// @public (undocumented)
export type AmplifyAuthFactoryProps = AuthProps & TriggerConfig;
export type AmplifyAuthFactoryProps = Omit<AuthProps, 'outputStorageStrategy'> & TriggerConfig;

// @public
export const Auth: typeof AmplifyAuthFactory;
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-auth/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { AmplifyAuthFactory } from './factory.js';
import {
NestedStackResolver,
SingletonConstructContainer,
StackMetadataBackendOutputStorageStrategy,
ToggleableImportPathVerifier,
} from '@aws-amplify/backend/test-utils';
import { App, Stack, aws_lambda } from 'aws-cdk-lib';
Expand All @@ -19,6 +18,7 @@ import {
ResourceProvider,
} from '@aws-amplify/plugin-types';
import { triggerEvents } from '@aws-amplify/auth-construct-alpha';
import { StackMetadataBackendOutputStorageStrategy } from '@aws-amplify/backend-output-storage';

describe('AmplifyAuthFactory', () => {
let authFactory: AmplifyAuthFactory;
Expand Down
10 changes: 7 additions & 3 deletions packages/backend-auth/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export type TriggerConfig = {
>;
};

export type AmplifyAuthFactoryProps = AuthProps & TriggerConfig;
export type AmplifyAuthFactoryProps = Omit<AuthProps, 'outputStorageStrategy'> &
TriggerConfig;

/**
* Singleton factory for AmplifyAuth that can be used in Amplify project files
Expand Down Expand Up @@ -68,8 +69,11 @@ class AmplifyAuthGenerator implements ConstructContainerEntryGenerator {
) {}

generateContainerEntry = (scope: Construct) => {
const authConstruct = new AmplifyAuth(scope, this.defaultName, this.props);
authConstruct.storeOutput(this.getInstanceProps.outputStorageStrategy);
const authProps: AuthProps = {
...this.props,
outputStorageStrategy: this.getInstanceProps.outputStorageStrategy,
};
const authConstruct = new AmplifyAuth(scope, this.defaultName, authProps);
Object.entries(this.props.triggers || {}).forEach(
([triggerEvent, handlerFactory]) => {
authConstruct.addTrigger(
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { App, Stack } from 'aws-cdk-lib';
import {
NestedStackResolver,
SingletonConstructContainer,
StackMetadataBackendOutputStorageStrategy,
} from '@aws-amplify/backend/test-utils';
import { ConstructFactoryGetInstanceProps } from '@aws-amplify/plugin-types';
import assert from 'node:assert';
import { fileURLToPath } from 'url';
import * as path from 'path';
import { StackMetadataBackendOutputStorageStrategy } from '@aws-amplify/backend-output-storage';

describe('AmplifyFunctionFactory', () => {
let getInstanceProps: ConstructFactoryGetInstanceProps;
Expand Down
2 changes: 1 addition & 1 deletion packages/backend-graphql/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { App, Stack } from 'aws-cdk-lib';
import {
NestedStackResolver,
SingletonConstructContainer,
StackMetadataBackendOutputStorageStrategy,
ToggleableImportPathVerifier,
} from '@aws-amplify/backend/test-utils';
import { Template } from 'aws-cdk-lib/assertions';
Expand All @@ -25,6 +24,7 @@ import {
UserPool,
UserPoolClient,
} from 'aws-cdk-lib/aws-cognito';
import { StackMetadataBackendOutputStorageStrategy } from '@aws-amplify/backend-output-storage';

const testSchema = `
input AMPLIFY {globalAuthRule: AuthRule = { allow: public }} # FOR TESTING ONLY!
Expand Down
Loading

0 comments on commit f75fa53

Please sign in to comment.