Skip to content

Commit

Permalink
feat(cognito): addDomain() on an imported user pool (#8123)
Browse files Browse the repository at this point in the history
In addition, reduce code duplication by introducing an abstract
UserPoolBase class.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Niranjan Jayakar authored May 28, 2020
1 parent ac001b8 commit 49c9f99
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
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

0 comments on commit 49c9f99

Please sign in to comment.