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

fix(dynamodb): table name when the name contains invalid characters #289

Closed
wants to merge 5 commits into from
Closed
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
13 changes: 13 additions & 0 deletions dynamodb/dynamodb-base.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bring util;


pub class TableBase {
protected getImplicidTableName(path: str, addr: str): str {
let tableAddr = "{addr.substring(42-8)}";
let var tablePath = "{regex.compile("[^a-zA-Z0-9._-]+").replaceAll(path, "-")}";
if util.env("WING_TARGET") != "sim" {
tablePath = tablePath.substring(21, (255+21)-9);
}
return tablePath + "-" + tableAddr;
}
}
5 changes: 3 additions & 2 deletions dynamodb/dynamodb.sim.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bring cloud;
bring ui;
bring "./dynamodb-types.w" as dynamodb_types;
bring "./dynamodb-client.w" as dynamodb_client;
bring "./dynamodb-base.w" as dynamodb_base;

inflight interface Client {
inflight createTable(input: Json): Json;
Expand Down Expand Up @@ -101,7 +102,7 @@ class Host {
}
}

pub class Table_sim impl dynamodb_types.ITable {
pub class Table_sim extends dynamodb_base.TableBase impl dynamodb_types.ITable {
host: Host;
pub tableName: str;
pub connection: dynamodb_types.Connection;
Expand All @@ -111,7 +112,7 @@ pub class Table_sim impl dynamodb_types.ITable {
this.host = Host.of(this);

this.adminEndpoint = this.host.ui?.endpoint;
let tableName = props.name ?? nodeof(this).addr;
let tableName = props.name ?? this.getImplicidTableName(nodeof(this).path, nodeof(this).addr);
let state = new sim.State();
this.tableName = state.token("tableName");

Expand Down
10 changes: 3 additions & 7 deletions dynamodb/dynamodb.tf-aws.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bring "cdktf" as cdktf;
bring "@cdktf/provider-aws" as tfaws;
bring "./dynamodb-types.w" as dynamodb_types;
bring "./dynamodb-client.w" as dynamodb_client;
bring "./dynamodb-base.w" as dynamodb_base;

struct DynamoDBStreamEventDynamoDB {
ApproximateCreationDateTime: str;
Expand Down Expand Up @@ -39,20 +40,15 @@ class Util {
}
}

pub class Table_tfaws impl dynamodb_types.ITable {
pub class Table_tfaws extends dynamodb_base.TableBase impl dynamodb_types.ITable {
pub tableName: str;
table: tfaws.dynamodbTable.DynamodbTable;

pub connection: dynamodb_types.Connection;

new(props: dynamodb_types.TableProps) {
this.table = new tfaws.dynamodbTable.DynamodbTable({
// Generate a unique name for the table:
// - Replace slashes with hyphens
// - Get rid of the initial "root/Default/Default/" part (21 characters)
// - Make room for the last 8 digits of the address (9 characters including hyphen). 255 is the maximum length of an AWS resource name
// - Add the last 8 digits of the address
name: props.name ?? "{nodeof(this).path.replaceAll("/", "-").substring(21, (255+21)-9)}-{nodeof(this).addr.substring(42-8)}",
name: props.name ?? this.getImplicidTableName(nodeof(this).path, nodeof(this).addr),
attribute: props.attributes,
hashKey: props.hashKey,
rangeKey: props.rangeKey,
Expand Down
6 changes: 3 additions & 3 deletions dynamodb/tests/connection.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ bring expect;
bring "../dynamodb.w" as dynamodb;

let table = new dynamodb.Table(
name: "my-table-name",
name: "my-connection-table",
attributes: [ { name: "id", type: "S" } ],
hashKey: "id"
);

if util.env("WING_TARGET") == "sim" {

test "sim returns localhost" {
expect.equal(table.connection.tableName, "my-table-name");
expect.equal(table.connection.tableName, "my-connection-table");
expect.equal(table.connection.clientConfig?.credentials?.accessKeyId, "local");
expect.equal(table.connection.clientConfig?.credentials?.secretAccessKey, "local");
expect.equal(table.connection.clientConfig?.region, "local");
Expand All @@ -22,7 +22,7 @@ if util.env("WING_TARGET") == "sim" {
} else {

test "non-sim returns the table name and nil" {
expect.equal(table.connection.tableName, "my-table-name");
expect.equal(table.connection.tableName, "my-connection-table");
expect.equal(table.connection.clientConfig, nil);
}

Expand Down
21 changes: 11 additions & 10 deletions dynamodb/tests/name.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ bring expect;
bring "../dynamodb.w" as dynamodb;

let table = new dynamodb.Table(
name: "my-table-name",
name: "my-explicit-table-name",
attributes: [ { name: "id", type: "S" } ],
hashKey: "id"
) as "explicit_name";
) as "explicit name";

let table2 = new dynamodb.Table(
attributes: [ { name: "id", type: "S" } ],
hashKey: "id"
) as "implicit_name";
) as "implicit name";

test "explicit table name" {
expect.equal(table.tableName, "my-table-name");
}
// It is not possible to create 2 tests with the explicit
// table name when using AWS; running the tests in parallel
// will result in a conflict when creating more than one table
// with the same name.
test "explicit and implicit table name" {
expect.equal(table.tableName, "my-explicit-table-name");

test "implicit table name" {
log(table.tableName);
assert(table.tableName.length > 0);
}
assert(table2.tableName.contains("implicit-name"));
}
Loading