Skip to content

Commit 8ab7b10

Browse files
authored
fix(dynamodb): allow using PhysicalName.GENERATE_IF_NEEDED as the Table name (#9377)
Fixes #9374 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent cd25812 commit 8ab7b10

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

packages/@aws-cdk/aws-dynamodb/lib/table.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,15 +913,15 @@ export class Table extends TableBase {
913913

914914
this.encryptionKey = encryptionKey;
915915

916-
if (props.tableName) { this.node.addMetadata('aws:cdk:hasPhysicalName', props.tableName); }
917-
918916
this.tableArn = this.getResourceArnAttribute(this.table.attrArn, {
919917
service: 'dynamodb',
920918
resource: 'table',
921919
resourceName: this.physicalName,
922920
});
923921
this.tableName = this.getResourceNameAttribute(this.table.ref);
924922

923+
if (props.tableName) { this.node.addMetadata('aws:cdk:hasPhysicalName', this.tableName); }
924+
925925
this.tableStreamArn = streamSpecification ? this.table.attrStreamArn : undefined;
926926

927927
this.scalingRole = this.makeScalingRole();

packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ResourcePart, SynthUtils } from '@aws-cdk/assert';
1+
import { ABSENT, ResourcePart, SynthUtils } from '@aws-cdk/assert';
22
import '@aws-cdk/assert/jest';
33
import * as appscaling from '@aws-cdk/aws-applicationautoscaling';
44
import * as iam from '@aws-cdk/aws-iam';
55
import * as kms from '@aws-cdk/aws-kms';
6-
import { App, CfnDeletionPolicy, ConstructNode, Duration, RemovalPolicy, Stack, Tag } from '@aws-cdk/core';
6+
import { App, CfnDeletionPolicy, ConstructNode, Duration, PhysicalName, RemovalPolicy, Stack, Tag } from '@aws-cdk/core';
77
import {
88
Attribute,
99
AttributeType,
@@ -67,8 +67,12 @@ function* LSI_GENERATOR(): Generator<LocalSecondaryIndexProps, never> {
6767
}
6868

6969
describe('default properties', () => {
70+
let stack: Stack;
71+
beforeEach(() => {
72+
stack = new Stack();
73+
});
74+
7075
test('hash key only', () => {
71-
const stack = new Stack();
7276
new Table(stack, CONSTRUCT_NAME, { partitionKey: TABLE_PARTITION_KEY });
7377

7478
expect(stack).toHaveResource('AWS::DynamoDB::Table', {
@@ -82,15 +86,13 @@ describe('default properties', () => {
8286
});
8387

8488
test('removalPolicy is DESTROY', () => {
85-
const stack = new Stack();
8689
new Table(stack, CONSTRUCT_NAME, { partitionKey: TABLE_PARTITION_KEY, removalPolicy: RemovalPolicy.DESTROY });
8790

8891
expect(stack).toHaveResource('AWS::DynamoDB::Table', { DeletionPolicy: CfnDeletionPolicy.DELETE }, ResourcePart.CompleteDefinition);
8992

9093
});
9194

9295
test('hash + range key', () => {
93-
const stack = new Stack();
9496
new Table(stack, CONSTRUCT_NAME, {
9597
partitionKey: TABLE_PARTITION_KEY,
9698
sortKey: TABLE_SORT_KEY,
@@ -110,8 +112,6 @@ describe('default properties', () => {
110112
});
111113

112114
test('hash + range key can also be specified in props', () => {
113-
const stack = new Stack();
114-
115115
new Table(stack, CONSTRUCT_NAME, {
116116
partitionKey: TABLE_PARTITION_KEY,
117117
sortKey: TABLE_SORT_KEY,
@@ -132,7 +132,6 @@ describe('default properties', () => {
132132
});
133133

134134
test('point-in-time recovery is not enabled', () => {
135-
const stack = new Stack();
136135
new Table(stack, CONSTRUCT_NAME, {
137136
partitionKey: TABLE_PARTITION_KEY,
138137
sortKey: TABLE_SORT_KEY,
@@ -154,7 +153,6 @@ describe('default properties', () => {
154153
});
155154

156155
test('server-side encryption is not enabled', () => {
157-
const stack = new Stack();
158156
new Table(stack, CONSTRUCT_NAME, {
159157
partitionKey: TABLE_PARTITION_KEY,
160158
sortKey: TABLE_SORT_KEY,
@@ -176,7 +174,6 @@ describe('default properties', () => {
176174
});
177175

178176
test('stream is not enabled', () => {
179-
const stack = new Stack();
180177
new Table(stack, CONSTRUCT_NAME, {
181178
partitionKey: TABLE_PARTITION_KEY,
182179
sortKey: TABLE_SORT_KEY,
@@ -198,7 +195,6 @@ describe('default properties', () => {
198195
});
199196

200197
test('ttl is not enabled', () => {
201-
const stack = new Stack();
202198
new Table(stack, CONSTRUCT_NAME, {
203199
partitionKey: TABLE_PARTITION_KEY,
204200
sortKey: TABLE_SORT_KEY,
@@ -220,8 +216,6 @@ describe('default properties', () => {
220216
});
221217

222218
test('can specify new and old images', () => {
223-
const stack = new Stack();
224-
225219
new Table(stack, CONSTRUCT_NAME, {
226220
tableName: TABLE_NAME,
227221
readCapacity: 42,
@@ -249,8 +243,6 @@ describe('default properties', () => {
249243
});
250244

251245
test('can specify new images only', () => {
252-
const stack = new Stack();
253-
254246
new Table(stack, CONSTRUCT_NAME, {
255247
tableName: TABLE_NAME,
256248
readCapacity: 42,
@@ -278,8 +270,6 @@ describe('default properties', () => {
278270
});
279271

280272
test('can specify old images only', () => {
281-
const stack = new Stack();
282-
283273
new Table(stack, CONSTRUCT_NAME, {
284274
tableName: TABLE_NAME,
285275
readCapacity: 42,
@@ -305,6 +295,19 @@ describe('default properties', () => {
305295
},
306296
);
307297
});
298+
299+
test('can use PhysicalName.GENERATE_IF_NEEDED as the Table name', () => {
300+
new Table(stack, CONSTRUCT_NAME, {
301+
tableName: PhysicalName.GENERATE_IF_NEEDED,
302+
partitionKey: TABLE_PARTITION_KEY,
303+
});
304+
305+
// since the resource has not been used in a cross-environment manner,
306+
// so the name should not be filled
307+
expect(stack).toHaveResourceLike('AWS::DynamoDB::Table', {
308+
TableName: ABSENT,
309+
});
310+
});
308311
});
309312

310313
test('when specifying every property', () => {

0 commit comments

Comments
 (0)