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

Use AWS Swift SDK for PersistenceExampleService. #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
swift: ["5.6"]
swift: ["5.6.1"]
application: ["EmptyExampleService", "PersistenceExampleService"]
runs-on: ${{ matrix.os }}
steps:
- uses: fwal/setup-swift@v1.14.0
- uses: swift-actions/setup-swift@v1.15.0
with:
swift-version: ${{ matrix.swift }}
- uses: actions/checkout@v2
Expand Down
105 changes: 75 additions & 30 deletions PersistenceExampleService/Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,35 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "7a4dfe026f6ee0f8ad741b58df74c60af296365d",
"version" : "1.9.0"
"revision" : "0f21b44d1ad5227ccbaa073aa40cd37eb8bbc337",
"version" : "1.11.0"
}
},
{
"identity" : "aws-crt-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/aws-crt-swift.git",
"state" : {
"revision" : "d654b82a55aec736ad8b4354027acab17394a060",
"version" : "0.2.2"
}
},
{
"identity" : "aws-sdk-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/awslabs/aws-sdk-swift",
"state" : {
"revision" : "e907e654466a8bfafbcd79a78cb87964abecb1a0",
"version" : "0.2.4"
}
},
{
"identity" : "collectionconcurrencykit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/JohnSundell/CollectionConcurrencyKit",
"state" : {
"revision" : "b4f23e24b5a1bff301efc5e70871083ca029ff95",
"version" : "0.2.0"
}
},
{
Expand All @@ -23,44 +50,44 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/amzn/service-model-swift-code-generate.git",
"state" : {
"revision" : "286181816fccc2b6ec92049ba68426d08cbe0059",
"version" : "3.0.0-beta.1"
"revision" : "a3eda1054e1e5599a24ffa70c5f595046f774334",
"version" : "3.0.0-beta.2"
}
},
{
"identity" : "smoke-aws",
"identity" : "smithy-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/amzn/smoke-aws.git",
"location" : "https://github.com/awslabs/smithy-swift.git",
"state" : {
"revision" : "59838cf3d823f36681fdd281c799e6d54f968d5e",
"version" : "2.42.44"
"revision" : "5c76b8f05f70f2b24276a2ae192fa8bd9c04beb6",
"version" : "0.2.3"
}
},
{
"identity" : "smoke-aws-credentials",
"identity" : "smoke-aws",
"kind" : "remoteSourceControl",
"location" : "https://github.com/amzn/smoke-aws-credentials.git",
"location" : "https://github.com/amzn/smoke-aws.git",
"state" : {
"revision" : "9b262b5d1c647fd1ab0deef0494461a08003b817",
"version" : "2.3.0"
"revision" : "783cb502ba0d6a5f42bdfa7a51957798879afcba",
"version" : "2.44.24"
}
},
{
"identity" : "smoke-aws-generate",
"kind" : "remoteSourceControl",
"location" : "https://github.com/amzn/smoke-aws-generate.git",
"state" : {
"revision" : "2d2902403408d4c7a9f6c69c90975cc048759f95",
"version" : "3.0.0-beta.1"
"revision" : "9a392605df6d9da60a0e2ec0e8f1f4e617074f24",
"version" : "3.0.0-beta.2"
}
},
{
"identity" : "smoke-dynamodb",
"kind" : "remoteSourceControl",
"location" : "https://github.com/amzn/smoke-dynamodb.git",
"state" : {
"revision" : "3eea222ba8ff58e50e822ab86ed26b6889bcaf41",
"version" : "3.1.0"
"branch" : "aws_sdk_swift",
"revision" : "cbf243f9dca9b84c582eea01e13d8d2c1c28e713"
}
},
{
Expand All @@ -77,8 +104,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/amzn/smoke-framework-application-generate",
"state" : {
"revision" : "7be9e3be5880b8f5fe0b17ca2be9f92e66aa101c",
"version" : "3.0.0-beta.2"
"revision" : "96fa351db41e4196116c5caa8f144f05bc77e1c5",
"version" : "3.0.0-beta.3"
}
},
{
Expand Down Expand Up @@ -108,6 +135,15 @@
"version" : "0.5.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "48254824bb4248676bf7ce56014ff57b142b77eb",
"version" : "1.0.2"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
Expand All @@ -131,53 +167,62 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-metrics.git",
"state" : {
"revision" : "eadb828f878fed144387e3845866225bb7082c56",
"version" : "2.3.0"
"revision" : "1c1408bf8fc21be93713e897d2badf500ea38419",
"version" : "2.3.1"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "d6e3762e0a5f7ede652559f53623baf11006e17c",
"version" : "2.39.0"
"revision" : "124119f0bb12384cef35aa041d7c3a686108722d",
"version" : "2.40.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "f73ca5ee9c6806800243f1ac415fcf82de9a4c91",
"version" : "1.10.2"
"revision" : "a75e92bde3683241c15df3dd905b7a6dcac4d551",
"version" : "1.12.1"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "50c25c132b140e62b45e90b5a76f13ded02c8a46",
"version" : "1.20.1"
"revision" : "108ac15087ea9b79abb6f6742699cf31de0e8772",
"version" : "1.22.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "b5260a31c2a72a89fa684f5efb3054d8725a2316",
"version" : "2.18.0"
"revision" : "42436a25ff32c390465567f5c089a9a8ce8d7baf",
"version" : "2.20.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "8ab824b140d0ebcd87e9149266ddc353e3705a3e",
"version" : "1.11.4"
"revision" : "2cb54f91ddafc90832c5fa247faf5798d0a7c204",
"version" : "1.13.0"
}
},
{
"identity" : "xmlcoder",
"kind" : "remoteSourceControl",
"location" : "https://github.com/MaxDesiatov/XMLCoder.git",
"state" : {
"revision" : "f30119af03996939cc4f54e0bf0dda9f88a84da5",
"version" : "0.13.1"
}
},
{
Expand Down
9 changes: 6 additions & 3 deletions PersistenceExampleService/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/amzn/smoke-framework.git", from: "2.7.0"),
.package(url: "https://github.com/amzn/smoke-aws-credentials.git", from: "2.0.0"),
.package(url: "https://github.com/amzn/smoke-aws.git", from: "2.0.0"),
.package(url: "https://github.com/amzn/smoke-dynamodb.git", from: "3.1.0"),
.package(url: "https://github.com/amzn/smoke-dynamodb.git", branch: "aws_sdk_swift"),
.package(url: "https://github.com/awslabs/aws-sdk-swift", from: "0.2.4"),
.package(url: "https://github.com/awslabs/smithy-swift.git", from: "0.2.3"),
.package(url: "https://github.com/amzn/smoke-framework-application-generate", from: "3.0.0-beta.1")
],
targets: [
Expand Down Expand Up @@ -69,7 +70,9 @@ let package = Package(
.executableTarget(
name: "PersistenceExampleService", dependencies: [
.target(name: "PersistenceExampleOperationsHTTP1"),
.product(name: "SmokeAWSCredentials", package: "smoke-aws-credentials"),
.product(name: "AWSDynamoDB", package: "aws-sdk-swift"),
.product(name: "ClientRuntime", package: "smithy-swift"),
.product(name: "SmokeAWSCore", package: "smoke-aws")
]),
.testTarget(
name: "PersistenceExampleOperationsTests", dependencies: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import PersistenceExampleModel
import SmokeOperations
import SmokeDynamoDB
import Logging
import NIO

extension PersistenceExampleModel.AddCustomerEmailAddressRequest: PersistenceExampleModel.CustomerEmailAddressAttributesShape { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import PersistenceExampleOperationsHTTP1
import SmokeOperationsHTTP1Server
import SmokeAWSCore
import SmokeDynamoDB
import SmokeAWSCredentials
import SmokeAWSHttp
import AsyncHTTPClient
import NIO
import AWSDynamoDB
import AWSClientRuntime

/**
Initializer for the PersistenceExampleService.
*/
@main
struct PersistenceExamplePerInvocationContextInitializer: PersistenceExamplePerInvocationContextInitializerProtocol {
let dynamodbTableGenerator: AWSDynamoDBCompositePrimaryKeyTableGenerator
let credentialsProvider: StoppableCredentialsProvider
let awsClientInvocationTraceContext = AWSClientInvocationTraceContext()
let dynamoDbClient: AWSDynamoDB.DynamoDbClient
let dynamodbTableName: String

/**
On application startup.
Expand All @@ -30,24 +29,9 @@ struct PersistenceExamplePerInvocationContextInitializer: PersistenceExamplePerI
CloudwatchStandardErrorLogger.enableLogging()

let environment = EnvironmentVariables.getEnvironment()

let clientEventLoopProvider = HTTPClient.EventLoopGroupProvider.shared(eventLoopGroup)

guard let credentialsProvider = AwsContainerRotatingCredentialsProvider.get(
fromEnvironment: environment,
eventLoopProvider: clientEventLoopProvider) else {
throw PersistenceExampleServiceError.unableToObtainCredentialsFromContainerEnvironment
}

self.credentialsProvider = credentialsProvider

let region = try environment.getRegion()

self.dynamodbTableGenerator = try PersistenceExamplePerInvocationContextInitializer.initializeDynamoDBTableGeneratorFromEnvironment(
environment: environment,
credentialsProvider: credentialsProvider,
region: region,
clientEventLoopProvider: clientEventLoopProvider)
self.dynamoDbClient = try await AWSDynamoDB.DynamoDbClient()
self.dynamodbTableName = try environment.get(EnvironmentVariables.dynamoTableName)
}

func timestampGenerator() -> String {
Expand All @@ -58,28 +42,15 @@ struct PersistenceExamplePerInvocationContextInitializer: PersistenceExamplePerI
UUID().uuidString
}

private static func initializeDynamoDBTableGeneratorFromEnvironment(
environment: [String: String],
credentialsProvider: CredentialsProvider,
region: AWSRegion,
clientEventLoopProvider: HTTPClient.EventLoopGroupProvider) throws -> AWSDynamoDBCompositePrimaryKeyTableGenerator {
let dynamoEndpointHostName = try environment.get(EnvironmentVariables.dynamoEndpointHostName)
let dynamoTableName = try environment.get(EnvironmentVariables.dynamoTableName)

return AWSDynamoDBCompositePrimaryKeyTableGenerator(
credentialsProvider: credentialsProvider,
region: region, endpointHostName: dynamoEndpointHostName,
tableName: dynamoTableName,
eventLoopProvider: clientEventLoopProvider)
}

/**
On invocation.
*/
public func getInvocationContext(
invocationReporting: SmokeServerInvocationReporting<SmokeInvocationTraceContext>) -> PersistenceExampleOperationsContext {
let awsClientInvocationReporting = invocationReporting.withInvocationTraceContext(traceContext: awsClientInvocationTraceContext)
let dynamodbTable = self.dynamodbTableGenerator.with(reporting: awsClientInvocationReporting)
public func getInvocationContext(invocationReporting: SmokeServerInvocationReporting<SmokeInvocationTraceContext>)
-> PersistenceExampleOperationsContext {
let dynamodbTable = AWSDynamoDBCompositePrimaryKeyTable(
dynamoDbClient: self.dynamoDbClient,
tableName: self.dynamodbTableName,
logger: invocationReporting.logger)

return PersistenceExampleOperationsContext(
dynamodbTable: dynamodbTable,
Expand All @@ -92,8 +63,7 @@ struct PersistenceExamplePerInvocationContextInitializer: PersistenceExamplePerI
On application shutdown.
*/
func onShutdown() async throws {
try await self.dynamodbTableGenerator.shutdown()
try await self.credentialsProvider.shutdown()
// nothing to do
}
}

Expand All @@ -110,11 +80,7 @@ private struct EnvironmentVariables {

static func getEnvironment() -> [String: String] {
#if DEBUG
let environment = [EnvironmentVariables.dynamoEndpointHostName: "dynamodb.us-west-2.amazonaws.com",
EnvironmentVariables.dynamoTableName: "PersistenceExampleTable",
EnvironmentVariables.region: "us-west-2",
AwsContainerRotatingCredentialsProvider.devIamRoleArnEnvironmentVariable:
"arn:aws:iam::000000000000:role/EcsTaskExecutionRole"]
let environment = [EnvironmentVariables.dynamoTableName: "PersistenceExampleTable"]
#else
let environment = ProcessInfo.processInfo.environment
#endif
Expand All @@ -132,15 +98,4 @@ private extension Dictionary where Key == String, Value == String {

return value
}

func getRegion() throws -> AWSRegion {
let regionString = try get(EnvironmentVariables.region)

guard let region = AWSRegion(rawValue: regionString) else {
throw PersistenceExampleServiceError.invalidEnvironmentVariable(reason:
"Specified '\(EnvironmentVariables.region)' environment variable '\(regionString)' not a valid region.")
}

return region
}
}
Loading