Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cognito): addDomain() on an imported user pool #8123

Merged
merged 6 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 29 additions & 32 deletions packages/@aws-cdk/aws-cognito/lib/user-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,33 +526,52 @@ export interface IUserPool extends IResource {
readonly userPoolArn: string;

/**
* Create a user pool client.
* Add a new app client to this user pool.
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html
*/
addClient(id: string, options?: UserPoolClientOptions): IUserPoolClient;

/**
* Associate a domain to this user pool.
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-assign-domain.html
*/
addDomain(id: string, options: UserPoolDomainOptions): UserPoolDomain;
}

abstract class UserPoolBase extends Resource implements IUserPool {
public abstract readonly userPoolId: string;
public abstract readonly userPoolArn: string;

public addClient(id: string, options?: UserPoolClientOptions): IUserPoolClient {
return new UserPoolClient(this, id, {
userPool: this,
...options,
});
}

public addDomain(id: string, options: UserPoolDomainOptions): UserPoolDomain {
return new UserPoolDomain(this, id, {
userPool: this,
...options,
});
}
}

/**
* Define a Cognito User Pool
*/
export class UserPool extends Resource implements IUserPool {
export class UserPool extends UserPoolBase {
/**
* Import an existing user pool based on its id.
*/
public static fromUserPoolId(scope: Construct, id: string, userPoolId: string): IUserPool {
class Import extends Resource implements IUserPool {
class Import extends UserPoolBase {
public readonly userPoolId = userPoolId;
public readonly userPoolArn = Stack.of(this).formatArn({
service: 'cognito-idp',
resource: 'userpool',
resourceName: userPoolId,
});

public addClient(clientId: string, options?: UserPoolClientOptions): IUserPoolClient {
return new UserPoolClient(this, clientId, {
userPool: this,
...options,
});
}
}
return new Import(scope, id);
}
Expand Down Expand Up @@ -669,28 +688,6 @@ export class UserPool extends Resource implements IUserPool {
(this.triggers as any)[operation.operationName] = fn.functionArn;
}

/**
* Add a new app client to this user pool.
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-client-apps.html
*/
public addClient(id: string, options?: UserPoolClientOptions): IUserPoolClient {
return new UserPoolClient(this, id, {
userPool: this,
...options,
});
}

/**
* Associate a domain to this user pool.
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-assign-domain.html
*/
public addDomain(id: string, options: UserPoolDomainOptions): UserPoolDomain {
return new UserPoolDomain(this, id, {
userPool: this,
...options,
});
}

private addLambdaPermission(fn: lambda.IFunction, name: string): void {
const capitalize = name.charAt(0).toUpperCase() + name.slice(1);
fn.addPermission(`${capitalize}Cognito`, {
Expand Down
29 changes: 29 additions & 0 deletions packages/@aws-cdk/aws-cognito/test/user-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,35 @@ test('addClient', () => {
});
});

test('addDomain', () => {
// GIVEN
const stack = new Stack();

// WHEN
const userpool = new UserPool(stack, 'Pool');
userpool.addDomain('UserPoolDomain', {
cognitoDomain: {
domainPrefix: 'userpooldomain',
},
});
const imported = UserPool.fromUserPoolId(stack, 'imported', 'imported-userpool-id');
imported.addDomain('UserPoolImportedDomain', {
cognitoDomain: {
domainPrefix: 'userpoolimporteddomain',
},
});

// THEN
expect(stack).toHaveResourceLike('AWS::Cognito::UserPoolDomain', {
Domain: 'userpooldomain',
UserPoolId: stack.resolve(userpool.userPoolId),
});
expect(stack).toHaveResourceLike('AWS::Cognito::UserPoolDomain', {
Domain: 'userpoolimporteddomain',
UserPoolId: stack.resolve(imported.userPoolId),
});
});

function fooFunction(scope: Construct, name: string): lambda.IFunction {
return new lambda.Function(scope, name, {
functionName: name,
Expand Down