Skip to content

Commit

Permalink
fix(dynamodb): allow using PhysicalName.GENERATE_IF_NEEDED as the Tab…
Browse files Browse the repository at this point in the history
…le 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*
  • Loading branch information
skinny85 authored Aug 2, 2020
1 parent cd25812 commit 8ab7b10
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,15 +913,15 @@ export class Table extends TableBase {

this.encryptionKey = encryptionKey;

if (props.tableName) { this.node.addMetadata('aws:cdk:hasPhysicalName', props.tableName); }

this.tableArn = this.getResourceArnAttribute(this.table.attrArn, {
service: 'dynamodb',
resource: 'table',
resourceName: this.physicalName,
});
this.tableName = this.getResourceNameAttribute(this.table.ref);

if (props.tableName) { this.node.addMetadata('aws:cdk:hasPhysicalName', this.tableName); }

this.tableStreamArn = streamSpecification ? this.table.attrStreamArn : undefined;

this.scalingRole = this.makeScalingRole();
Expand Down
37 changes: 20 additions & 17 deletions packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ResourcePart, SynthUtils } from '@aws-cdk/assert';
import { ABSENT, ResourcePart, SynthUtils } from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as appscaling from '@aws-cdk/aws-applicationautoscaling';
import * as iam from '@aws-cdk/aws-iam';
import * as kms from '@aws-cdk/aws-kms';
import { App, CfnDeletionPolicy, ConstructNode, Duration, RemovalPolicy, Stack, Tag } from '@aws-cdk/core';
import { App, CfnDeletionPolicy, ConstructNode, Duration, PhysicalName, RemovalPolicy, Stack, Tag } from '@aws-cdk/core';
import {
Attribute,
AttributeType,
Expand Down Expand Up @@ -67,8 +67,12 @@ function* LSI_GENERATOR(): Generator<LocalSecondaryIndexProps, never> {
}

describe('default properties', () => {
let stack: Stack;
beforeEach(() => {
stack = new Stack();
});

test('hash key only', () => {
const stack = new Stack();
new Table(stack, CONSTRUCT_NAME, { partitionKey: TABLE_PARTITION_KEY });

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

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

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

});

test('hash + range key', () => {
const stack = new Stack();
new Table(stack, CONSTRUCT_NAME, {
partitionKey: TABLE_PARTITION_KEY,
sortKey: TABLE_SORT_KEY,
Expand All @@ -110,8 +112,6 @@ describe('default properties', () => {
});

test('hash + range key can also be specified in props', () => {
const stack = new Stack();

new Table(stack, CONSTRUCT_NAME, {
partitionKey: TABLE_PARTITION_KEY,
sortKey: TABLE_SORT_KEY,
Expand All @@ -132,7 +132,6 @@ describe('default properties', () => {
});

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

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

test('stream is not enabled', () => {
const stack = new Stack();
new Table(stack, CONSTRUCT_NAME, {
partitionKey: TABLE_PARTITION_KEY,
sortKey: TABLE_SORT_KEY,
Expand All @@ -198,7 +195,6 @@ describe('default properties', () => {
});

test('ttl is not enabled', () => {
const stack = new Stack();
new Table(stack, CONSTRUCT_NAME, {
partitionKey: TABLE_PARTITION_KEY,
sortKey: TABLE_SORT_KEY,
Expand All @@ -220,8 +216,6 @@ describe('default properties', () => {
});

test('can specify new and old images', () => {
const stack = new Stack();

new Table(stack, CONSTRUCT_NAME, {
tableName: TABLE_NAME,
readCapacity: 42,
Expand Down Expand Up @@ -249,8 +243,6 @@ describe('default properties', () => {
});

test('can specify new images only', () => {
const stack = new Stack();

new Table(stack, CONSTRUCT_NAME, {
tableName: TABLE_NAME,
readCapacity: 42,
Expand Down Expand Up @@ -278,8 +270,6 @@ describe('default properties', () => {
});

test('can specify old images only', () => {
const stack = new Stack();

new Table(stack, CONSTRUCT_NAME, {
tableName: TABLE_NAME,
readCapacity: 42,
Expand All @@ -305,6 +295,19 @@ describe('default properties', () => {
},
);
});

test('can use PhysicalName.GENERATE_IF_NEEDED as the Table name', () => {
new Table(stack, CONSTRUCT_NAME, {
tableName: PhysicalName.GENERATE_IF_NEEDED,
partitionKey: TABLE_PARTITION_KEY,
});

// since the resource has not been used in a cross-environment manner,
// so the name should not be filled
expect(stack).toHaveResourceLike('AWS::DynamoDB::Table', {
TableName: ABSENT,
});
});
});

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

0 comments on commit 8ab7b10

Please sign in to comment.