Skip to content

Commit 573facd

Browse files
committed
Self review & refactoring
1 parent 82c7a3f commit 573facd

File tree

8 files changed

+125
-95
lines changed

8 files changed

+125
-95
lines changed

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/LazilyInitializedCredentialsProvider.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ public class LazilyInitializedCredentialsProvider(
2222
) : CredentialsProvider {
2323
private val provider = lazy(initializer)
2424

25-
override suspend fun resolve(attributes: Attributes): Credentials {
26-
val credentials = provider.value.resolve(attributes)
27-
if (businessMetric == null) return credentials
28-
return credentials.emitBusinessMetric(businessMetric)
29-
}
25+
override suspend fun resolve(attributes: Attributes): Credentials =
26+
if (businessMetric == null) {
27+
provider.value.resolve(attributes)
28+
} else {
29+
provider.value.resolve(attributes).emitBusinessMetric(businessMetric)
30+
}
3031

3132
override fun toString(): String = providerName
3233
}

aws-runtime/aws-config/common/test/aws/sdk/kotlin/runtime/util/AwsBusinessMetricsTestUtils.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,8 @@ internal fun testAttributes(vararg metrics: BusinessMetric): Attributes {
3737
internal fun Credentials.withBusinessMetrics(vararg metrics: BusinessMetric): Credentials =
3838
emitBusinessMetrics(metrics.toSet())
3939

40+
/**
41+
* Converts a [String] into an [AwsBusinessMetric.Credentials] if the identifier matches
42+
*/
4043
internal fun String.toAwsCredentialsBusinessMetric(): BusinessMetric =
4144
AwsBusinessMetric.Credentials.entries.find { it.identifier == this } ?: throw Exception("String '$this' is not an AWS business metric")

aws-runtime/aws-config/jvm/test/aws/sdk/kotlin/runtime/auth/credentials/DefaultChainCredentialsProviderTest.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import aws.sdk.kotlin.runtime.util.toAwsCredentialsBusinessMetric
1111
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
1212
import aws.smithy.kotlin.runtime.auth.awscredentials.copy
1313
import aws.smithy.kotlin.runtime.httptest.TestConnection
14-
import aws.smithy.kotlin.runtime.operation.ExecutionContext
1514
import aws.smithy.kotlin.runtime.time.Instant
1615
import aws.smithy.kotlin.runtime.util.Filesystem
1716
import aws.smithy.kotlin.runtime.util.OperatingSystem
@@ -169,8 +168,7 @@ class DefaultChainCredentialsProviderTest {
169168
fun executeTest(name: String) = runTest {
170169
val test = makeTest(name)
171170
val provider = DefaultChainCredentialsProvider(platformProvider = test.testPlatform, httpClient = test.testEngine)
172-
val attributes = ExecutionContext()
173-
val actual = runCatching { provider.resolve(attributes) }
171+
val actual = runCatching { provider.resolve() }
174172
val expected = test.expected
175173
when {
176174
expected is TestResult.Ok && actual.isFailure -> error("expected success, got error: $actual")

codegen/aws-sdk-codegen/src/main/kotlin/aws/sdk/kotlin/codegen/BusinessMetricsIntegration.kt

Lines changed: 0 additions & 86 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.codegen.businessmetrics
6+
7+
import aws.sdk.kotlin.codegen.AwsRuntimeTypes
8+
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter
9+
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
10+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator
11+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware
12+
import software.amazon.smithy.model.shapes.OperationShape
13+
14+
/**
15+
* Renders the addition of the [aws.sdk.kotlin.runtime.http.interceptors.businessmetrics.BusinessMetricsInterceptor]
16+
*/
17+
class BusinessMetricsInterceptorIntegration : KotlinIntegration {
18+
override fun customizeMiddleware(
19+
ctx: ProtocolGenerator.GenerationContext,
20+
resolved: List<ProtocolMiddleware>,
21+
): List<ProtocolMiddleware> = resolved + userAgentBusinessMetricsMiddleware
22+
23+
private val userAgentBusinessMetricsMiddleware = object : ProtocolMiddleware {
24+
override val name: String = "UserAgentBusinessMetrics"
25+
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) {
26+
writer.write(
27+
"op.interceptors.add(#T())",
28+
AwsRuntimeTypes.Http.Interceptors.BusinessMetrics.BusinessMetricsInterceptor,
29+
)
30+
}
31+
}
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package aws.sdk.kotlin.codegen.businessmetrics
2+
3+
import aws.sdk.kotlin.codegen.AwsRuntimeTypes
4+
import software.amazon.smithy.kotlin.codegen.core.KotlinWriter
5+
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes
6+
import software.amazon.smithy.kotlin.codegen.core.withBlock
7+
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
8+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator
9+
import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware
10+
import software.amazon.smithy.model.shapes.OperationShape
11+
12+
/**
13+
* Renders the addition of some of the credentials related business metrics.
14+
*/
15+
class CredentialsBusinessMetricsIntegration : KotlinIntegration {
16+
override fun customizeMiddleware(
17+
ctx: ProtocolGenerator.GenerationContext,
18+
resolved: List<ProtocolMiddleware>,
19+
): List<ProtocolMiddleware> = resolved + credentialsBusinessMetricsMiddleware
20+
21+
private val credentialsBusinessMetricsMiddleware = object : ProtocolMiddleware {
22+
override val name: String = "credentialsOverrideBusinessMetricsMiddleware"
23+
override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) {
24+
writer.withBlock(
25+
"if (config.credentialsProvider is #T) {",
26+
"}",
27+
AwsRuntimeTypes.Config.Credentials.StaticCredentialsProvider,
28+
) {
29+
write(
30+
"op.context.#T(#T.Credentials.CREDENTIALS_CODE)",
31+
RuntimeTypes.Core.BusinessMetrics.emitBusinessMetric,
32+
AwsRuntimeTypes.Http.Interceptors.BusinessMetrics.AwsBusinessMetric,
33+
)
34+
}
35+
}
36+
}
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package aws.sdk.kotlin.codegen.businessmetrics
2+
3+
import aws.sdk.kotlin.codegen.AwsRuntimeTypes
4+
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes
5+
import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes.Auth.Signing.AwsSigningCommon.AwsSigningAttributes
6+
import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration
7+
import software.amazon.smithy.kotlin.codegen.integration.SectionWriter
8+
import software.amazon.smithy.kotlin.codegen.integration.SectionWriterBinding
9+
import software.amazon.smithy.kotlin.codegen.rendering.endpoints.EndpointBusinessMetrics
10+
11+
/**
12+
* Renders the addition of endpoint & endpoint adjacent business metrics.
13+
*/
14+
class EndpointBusinessMetricsIntegration : KotlinIntegration {
15+
override val sectionWriters: List<SectionWriterBinding>
16+
get() = listOf(
17+
SectionWriterBinding(EndpointBusinessMetrics, endpointBusinessMetricsSectionWriter),
18+
)
19+
20+
private val endpointBusinessMetricsSectionWriter = SectionWriter { writer, _ ->
21+
writer.write("")
22+
writer.write(
23+
"if (endpoint.attributes.contains(#T)) request.context.#T(#T.SERVICE_ENDPOINT_OVERRIDE)",
24+
RuntimeTypes.Core.BusinessMetrics.ServiceEndpointOverride,
25+
RuntimeTypes.Core.BusinessMetrics.emitBusinessMetric,
26+
RuntimeTypes.Core.BusinessMetrics.SmithyBusinessMetric,
27+
)
28+
writer.write(
29+
"if (endpoint.attributes.contains(#T)) request.context.#T(#T.ACCOUNT_ID_BASED_ENDPOINT)",
30+
RuntimeTypes.Core.BusinessMetrics.AccountIdBasedEndpointAccountId,
31+
RuntimeTypes.Core.BusinessMetrics.emitBusinessMetric,
32+
RuntimeTypes.Core.BusinessMetrics.SmithyBusinessMetric,
33+
)
34+
writer.write(
35+
"if (endpoint.attributes.contains(#T.SigningService) && endpoint.attributes[#T.SigningService] == \"s3express\") request.context.#T(#T.S3_EXPRESS_BUCKET)",
36+
AwsSigningAttributes,
37+
AwsSigningAttributes,
38+
RuntimeTypes.Core.BusinessMetrics.emitBusinessMetric,
39+
AwsRuntimeTypes.Http.Interceptors.BusinessMetrics.AwsBusinessMetric,
40+
)
41+
writer.write("")
42+
}
43+
}

codegen/aws-sdk-codegen/src/main/resources/META-INF/services/software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ aws.sdk.kotlin.codegen.customization.cloudfrontkeyvaluestore.BackfillSigV4ACusto
4242
aws.sdk.kotlin.codegen.customization.s3.express.SigV4S3ExpressAuthSchemeIntegration
4343
aws.sdk.kotlin.codegen.customization.s3.express.S3ExpressIntegration
4444
aws.sdk.kotlin.codegen.customization.s3.S3ExpiresIntegration
45-
aws.sdk.kotlin.codegen.BusinessMetricsIntegration
45+
aws.sdk.kotlin.codegen.businessmetrics.BusinessMetricsInterceptorIntegration
46+
aws.sdk.kotlin.codegen.businessmetrics.CredentialsBusinessMetricsIntegration
47+
aws.sdk.kotlin.codegen.businessmetrics.EndpointBusinessMetricsIntegration
4648
aws.sdk.kotlin.codegen.smoketests.SmokeTestsDenyListIntegration

0 commit comments

Comments
 (0)