From f13d3f15f2ddb5e648d178bbca63b5dca382903a Mon Sep 17 00:00:00 2001 From: aseemsavio Date: Sun, 10 Sep 2023 19:14:53 +0530 Subject: [PATCH 1/4] DSLs related to client building --- .../extensions/crud/sync/client/Create.kt | 11 +++++++++ .../crud/sync/client/Credentials.kt | 24 +++++++++++++++++++ .../extensions/crud/sync/client/Region.kt | 9 +++++++ .../extensions/crud/sync/tables/Create.kt | 1 + 4 files changed, 45 insertions(+) create mode 100644 src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Create.kt create mode 100644 src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Credentials.kt create mode 100644 src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Region.kt create mode 100644 src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Create.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Create.kt new file mode 100644 index 0000000..577ba2e --- /dev/null +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Create.kt @@ -0,0 +1,11 @@ +package com.aseemsavio.dynamokt.extensions.crud.sync.client + +import software.amazon.awssdk.services.dynamodb.DynamoDbClient +import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder + +/** + * DSL for building a synchronous DynamoDb Client. + * + * @since 0.0.1 + */ +fun dynamoDbClient(block: DynamoDbClientBuilder.() -> Unit): DynamoDbClient = DynamoDbClient.builder().apply(block).build() diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Credentials.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Credentials.kt new file mode 100644 index 0000000..59be264 --- /dev/null +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Credentials.kt @@ -0,0 +1,24 @@ +package com.aseemsavio.dynamokt.extensions.crud.sync.client + +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider +import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder + +/** + * DSL for applying a [StaticCredentialsProvider] to [DynamoDbClientBuilder]. + */ +fun DynamoDbClientBuilder.credentials(block: StaticCredentialsBuilder.() -> Unit): DynamoDbClientBuilder = apply { + credentialsProvider(StaticCredentialsBuilder().apply(block).build()) +} + +class StaticCredentialsBuilder { + + var accessKey: String? = null + var secretKey: String? = null + + fun build(): StaticCredentialsProvider { + checkNotNull(accessKey) { "accessKey should not be null" } + checkNotNull(secretKey) { "secretKey should not be null" } + return StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey)) + } +} diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Region.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Region.kt new file mode 100644 index 0000000..d7f38ac --- /dev/null +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/client/Region.kt @@ -0,0 +1,9 @@ +package com.aseemsavio.dynamokt.extensions.crud.sync.client + +import software.amazon.awssdk.regions.Region +import software.amazon.awssdk.services.dynamodb.DynamoDbClientBuilder + +/** + * DSL for applying a [Region] to [DynamoDbClientBuilder]. + */ +fun DynamoDbClientBuilder.region(block: () -> String): DynamoDbClientBuilder = apply { region(Region.of(block())) } diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt new file mode 100644 index 0000000..2f4a967 --- /dev/null +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt @@ -0,0 +1 @@ +package com.aseemsavio.dynamokt.extensions.crud.sync.tables From 49038aea31c3c35380a0c6160ebee97d7c7988dd Mon Sep 17 00:00:00 2001 From: aseemsavio Date: Sun, 10 Sep 2023 20:46:49 +0530 Subject: [PATCH 2/4] changes to main --- .../extensions/crud/sync/tables/Create.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt index 2f4a967..5696bd5 100644 --- a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt @@ -1 +1,30 @@ package com.aseemsavio.dynamokt.extensions.crud.sync.tables + +import software.amazon.awssdk.services.dynamodb.DynamoDbClient +import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition +import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest +import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement + +// https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-dynamodb.html + +fun createTable() { + val a = CreateTableRequest.builder() + +} + +fun DynamoDbClient.newTable(block: CreateTableRequest.Builder.() -> Unit) { + val request = CreateTableRequest.builder().apply(block).build() + createTable(request) +} + +/** + * DSL for creating an [AttributeDefinition]. + */ +fun attributeDefinition(block: AttributeDefinition.Builder.() -> Unit): AttributeDefinition = + AttributeDefinition.builder().apply(block).build() + +/** + * DSL for creating a [KeySchemaElement]. + */ +fun keySchemaElement(block: KeySchemaElement.Builder.() -> Unit): KeySchemaElement = + KeySchemaElement.builder().apply(block).build() From 5f89db61b7bc35d5ffac6ecf2af6b696dcd24639 Mon Sep 17 00:00:00 2001 From: aseemsavio Date: Mon, 11 Sep 2023 08:05:22 +0530 Subject: [PATCH 3/4] gsi, lsi, provisioned throughput, sse DSLs added --- .../extensions/crud/sync/tables/Create.kt | 57 +++++++++++++++---- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt index 5696bd5..a365d33 100644 --- a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt @@ -1,20 +1,23 @@ package com.aseemsavio.dynamokt.extensions.crud.sync.tables +import software.amazon.awssdk.core.waiters.WaiterResponse import software.amazon.awssdk.services.dynamodb.DynamoDbClient -import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition -import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest -import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement +import software.amazon.awssdk.services.dynamodb.model.* -// https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-dynamodb.html - -fun createTable() { - val a = CreateTableRequest.builder() -} +// https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-dynamodb.html -fun DynamoDbClient.newTable(block: CreateTableRequest.Builder.() -> Unit) { - val request = CreateTableRequest.builder().apply(block).build() - createTable(request) +fun DynamoDbClient.newTable(block: CreateTableRequest.Builder.() -> Unit): TableName { + val waiter = waiter() + val createTableRequest = CreateTableRequest.builder().apply(block).build() + val tableName = createTableRequest.tableName() + val createTableResponse = createTable(createTableRequest) + val describeTableRequest = DescribeTableRequest.builder() + .tableName(tableName) + .build() + val waiterResponse: WaiterResponse = waiter.waitUntilTableExists(describeTableRequest) + waiterResponse.matched().response().ifPresent { println(it) } + return TableName(createTableResponse.tableDescription().tableName()) } /** @@ -28,3 +31,35 @@ fun attributeDefinition(block: AttributeDefinition.Builder.() -> Unit): Attribut */ fun keySchemaElement(block: KeySchemaElement.Builder.() -> Unit): KeySchemaElement = KeySchemaElement.builder().apply(block).build() + +/** + * DSL for creating a [GlobalSecondaryIndex]. + */ +fun globalSecondaryIndex(block: GlobalSecondaryIndex.Builder.() -> Unit): GlobalSecondaryIndex = + GlobalSecondaryIndex.builder().apply(block).build() + +/** + * DSL for creating a [LocalSecondaryIndex]. + */ +fun localSecondaryIndex(block: LocalSecondaryIndex.Builder.() -> Unit): LocalSecondaryIndex = + LocalSecondaryIndex.builder().apply(block).build() + +/** + * DSL for creating [SSESpecification]. + */ +fun CreateTableRequest.Builder.sseSpecificationDynamoKt(block: SSESpecification.Builder.() -> Unit) = apply { + sseSpecification(SSESpecification.builder().apply(block).build()) +} + +/** + * DSL for creating [ProvisionedThroughput]. + */ +fun CreateTableRequest.Builder.provisionedThroughputDynamoKt(block: ProvisionedThroughput.Builder.() -> Unit) = apply { + provisionedThroughput(ProvisionedThroughput.builder().apply(block).build()) +} + +/** + * [TableName] is a [String]. + */ +@JvmInline +value class TableName(val value: String) \ No newline at end of file From 45de0d1dc2a9d2c9061cfb905498cd24acefa69f Mon Sep 17 00:00:00 2001 From: aseemsavio Date: Thu, 14 Sep 2023 09:14:47 +0530 Subject: [PATCH 4/4] streams and tags --- .../extensions/crud/sync/tables/Create.kt | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt index a365d33..c993bc5 100644 --- a/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt +++ b/src/main/kotlin/com/aseemsavio/dynamokt/extensions/crud/sync/tables/Create.kt @@ -7,14 +7,17 @@ import software.amazon.awssdk.services.dynamodb.model.* // https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/examples-dynamodb.html +/** + * DSL for creating a new Dynamo DB table. + */ fun DynamoDbClient.newTable(block: CreateTableRequest.Builder.() -> Unit): TableName { val waiter = waiter() val createTableRequest = CreateTableRequest.builder().apply(block).build() val tableName = createTableRequest.tableName() val createTableResponse = createTable(createTableRequest) val describeTableRequest = DescribeTableRequest.builder() - .tableName(tableName) - .build() + .tableName(tableName) + .build() val waiterResponse: WaiterResponse = waiter.waitUntilTableExists(describeTableRequest) waiterResponse.matched().response().ifPresent { println(it) } return TableName(createTableResponse.tableDescription().tableName()) @@ -24,25 +27,30 @@ fun DynamoDbClient.newTable(block: CreateTableRequest.Builder.() -> Unit): Table * DSL for creating an [AttributeDefinition]. */ fun attributeDefinition(block: AttributeDefinition.Builder.() -> Unit): AttributeDefinition = - AttributeDefinition.builder().apply(block).build() + AttributeDefinition.builder().apply(block).build() /** * DSL for creating a [KeySchemaElement]. */ fun keySchemaElement(block: KeySchemaElement.Builder.() -> Unit): KeySchemaElement = - KeySchemaElement.builder().apply(block).build() + KeySchemaElement.builder().apply(block).build() /** * DSL for creating a [GlobalSecondaryIndex]. */ fun globalSecondaryIndex(block: GlobalSecondaryIndex.Builder.() -> Unit): GlobalSecondaryIndex = - GlobalSecondaryIndex.builder().apply(block).build() + GlobalSecondaryIndex.builder().apply(block).build() /** * DSL for creating a [LocalSecondaryIndex]. */ fun localSecondaryIndex(block: LocalSecondaryIndex.Builder.() -> Unit): LocalSecondaryIndex = - LocalSecondaryIndex.builder().apply(block).build() + LocalSecondaryIndex.builder().apply(block).build() + +/** + * DSL for creating a [Tag]. + */ +fun tag(block: Tag.Builder.() -> Unit): Tag = Tag.builder().apply(block).build() /** * DSL for creating [SSESpecification]. @@ -58,6 +66,13 @@ fun CreateTableRequest.Builder.provisionedThroughputDynamoKt(block: ProvisionedT provisionedThroughput(ProvisionedThroughput.builder().apply(block).build()) } +/** + * DSL for creating [StreamSpecification]. + */ +fun CreateTableRequest.Builder.streamSpecificationDynamoKt(block: StreamSpecification.Builder.() -> Unit) = apply { + streamSpecification(StreamSpecification.builder().apply(block).build()) +} + /** * [TableName] is a [String]. */