|
| 1 | +package aws.sdk.kotlin.codegen.smoketests |
| 2 | + |
| 3 | +import aws.sdk.kotlin.codegen.AwsRuntimeTypes |
| 4 | +import software.amazon.smithy.kotlin.codegen.KotlinSettings |
| 5 | +import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes |
| 6 | +import software.amazon.smithy.kotlin.codegen.core.getContextValue |
| 7 | +import software.amazon.smithy.kotlin.codegen.core.withBlock |
| 8 | +import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration |
| 9 | +import software.amazon.smithy.kotlin.codegen.integration.SectionWriterBinding |
| 10 | +import software.amazon.smithy.kotlin.codegen.model.hasTrait |
| 11 | +import software.amazon.smithy.kotlin.codegen.rendering.smoketests.* |
| 12 | +import software.amazon.smithy.kotlin.codegen.utils.topDownOperations |
| 13 | +import software.amazon.smithy.model.Model |
| 14 | +import software.amazon.smithy.smoketests.traits.SmokeTestsTrait |
| 15 | + |
| 16 | +/** |
| 17 | + * Adds support for AWS specific client config during smoke tests code generation. |
| 18 | + */ |
| 19 | +class SmokeTestAwsVendorParamsIntegration : KotlinIntegration { |
| 20 | + override fun enabledForService(model: Model, settings: KotlinSettings): Boolean = |
| 21 | + model.topDownOperations(settings.service).any { it.hasTrait<SmokeTestsTrait>() } |
| 22 | + |
| 23 | + override val sectionWriters: List<SectionWriterBinding> |
| 24 | + get() = |
| 25 | + dualStackSectionWriters + |
| 26 | + sigv4aRegionSetSectionWriters + |
| 27 | + uriSectionWriters + |
| 28 | + accountIdEndpointSectionWriters + |
| 29 | + regionSectionWriters + |
| 30 | + useAccelerateSectionWriters + |
| 31 | + useMultiRegionAccessPointsSectionWriters + |
| 32 | + useGlobalEndpointSectionWriters |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Uses the AWS Kotlin SDK specific name for the dual stack config option i.e. `useDualstack` -> `useDualStack` |
| 37 | + */ |
| 38 | +private val dualStackSectionWriters = listOf( |
| 39 | + SectionWriterBinding(SmokeTestUseDualStackKey) { writer, _ -> writer.writeInline("useDualStack") }, |
| 40 | +) |
| 41 | + |
| 42 | +/** |
| 43 | + * Uses the AWS Kotlin SDK specific name for the sigV4a signing region set option i.e. |
| 44 | + * `sigv4aRegionSet` -> `sigV4aSigningRegionSet`. |
| 45 | + * |
| 46 | + * Converts the modeled sigV4a signing region set from a list to a set. |
| 47 | + * |
| 48 | + * Adds additional config needed for the SDK to make sigV4a calls |
| 49 | + */ |
| 50 | +private val sigv4aRegionSetSectionWriters = listOf( |
| 51 | + SectionWriterBinding(SmokeTestSigv4aRegionSetKey) { writer, _ -> writer.writeInline("sigV4aSigningRegionSet") }, |
| 52 | + SectionWriterBinding(SmokeTestSigv4aRegionSetValue) { writer, value -> |
| 53 | + writer.write("#L.toSet()", value) |
| 54 | + // TODO: Remove once sigV4a is supported for default signer. |
| 55 | + writer.write( |
| 56 | + "authSchemes = listOf(#T(#T))", |
| 57 | + RuntimeTypes.Auth.HttpAuthAws.SigV4AsymmetricAuthScheme, |
| 58 | + RuntimeTypes.Auth.AwsSigningCrt.CrtAwsSigner, |
| 59 | + ) |
| 60 | + }, |
| 61 | +) |
| 62 | + |
| 63 | +/** |
| 64 | + * Ensures we use the provided URI |
| 65 | + */ |
| 66 | +private val uriSectionWriters = listOf( |
| 67 | + SectionWriterBinding(SmokeTestUriKey) { writer, _ -> writer.writeInline("endpointProvider") }, |
| 68 | + SectionWriterBinding(SmokeTestUriValue) { writer, value -> |
| 69 | + val endpointProvider = writer.getContextValue(SmokeTestUriValue.EndpointProvider) |
| 70 | + val endpointParameters = writer.getContextValue(SmokeTestUriValue.EndpointParameters) |
| 71 | + writer.withBlock("object : #T {", "}", endpointProvider) { |
| 72 | + write( |
| 73 | + "override suspend fun resolveEndpoint(params: #T): #T = #T(#L)", |
| 74 | + endpointParameters, |
| 75 | + RuntimeTypes.SmithyClient.Endpoints.Endpoint, |
| 76 | + RuntimeTypes.SmithyClient.Endpoints.Endpoint, |
| 77 | + value, |
| 78 | + ) |
| 79 | + } |
| 80 | + }, |
| 81 | +) |
| 82 | + |
| 83 | +/** |
| 84 | + * Uses the AWS Kotlin SDK specific way of configuring `accountIdEndpointMode` |
| 85 | + */ |
| 86 | +private val accountIdEndpointSectionWriters = listOf( |
| 87 | + SectionWriterBinding(SmokeTestAccountIdBasedRoutingKey) { writer, _ -> writer.writeInline("accountIdEndpointMode") }, |
| 88 | + SectionWriterBinding(SmokeTestAccountIdBasedRoutingValue) { writer, value -> |
| 89 | + when (value) { |
| 90 | + "true" -> writer.write("#T.REQUIRED", AwsRuntimeTypes.Config.Endpoints.AccountIdEndpointMode) |
| 91 | + "false" -> writer.write("#T.DISABLED", AwsRuntimeTypes.Config.Endpoints.AccountIdEndpointMode) |
| 92 | + } |
| 93 | + }, |
| 94 | +) |
| 95 | + |
| 96 | +/** |
| 97 | + * Gets region override environment variable. |
| 98 | + * |
| 99 | + * Sets region override as default. |
| 100 | + * |
| 101 | + * Sets region override as default client config if no other client config is modelled. |
| 102 | + */ |
| 103 | +private val regionSectionWriters = listOf( |
| 104 | + SectionWriterBinding(SmokeTestAdditionalEnvVars) { writer, _ -> |
| 105 | + writer.write( |
| 106 | + "private val regionOverride = #T.System.getenv(#S)", |
| 107 | + RuntimeTypes.Core.Utils.PlatformProvider, |
| 108 | + "AWS_SMOKE_TEST_REGION", |
| 109 | + ) |
| 110 | + }, |
| 111 | + SectionWriterBinding(SmokeTestRegionDefault) { writer, _ -> |
| 112 | + writer.writeInline("regionOverride ?: ") |
| 113 | + }, |
| 114 | + SectionWriterBinding(SmokeTestDefaultConfig) { writer, _ -> writer.write("region = regionOverride") }, |
| 115 | +) |
| 116 | + |
| 117 | +/** |
| 118 | + * Uses the AWS Kotlin SDK specific name for the S3 accelerate config option i.e. `useAccelerate` -> `enableAccelerate` |
| 119 | + */ |
| 120 | +private val useAccelerateSectionWriters = listOf( |
| 121 | + SectionWriterBinding(SmokeTestUseAccelerateKey) { writer, _ -> writer.writeInline("enableAccelerate") }, |
| 122 | +) |
| 123 | + |
| 124 | +/** |
| 125 | + * Uses the AWS Kotlin SDK specific name for the S3 multi region access points (MRAP) config option i.e. |
| 126 | + * `useMultiRegionAccessPoints` -> `disableMrap`. |
| 127 | + * |
| 128 | + * Our config option is opt out while the modeled config is opt in so we invert the boolean values. |
| 129 | + */ |
| 130 | +private val useMultiRegionAccessPointsSectionWriters = listOf( |
| 131 | + SectionWriterBinding(SmokeTestUseMultiRegionAccessPointsKey) { writer, _ -> writer.writeInline("disableMrap") }, |
| 132 | + SectionWriterBinding(SmokeTestUseMultiRegionAccessPointsValue) { writer, value -> |
| 133 | + when (value) { |
| 134 | + "true" -> writer.write("false") |
| 135 | + "false" -> writer.write("true") |
| 136 | + } |
| 137 | + }, |
| 138 | +) |
| 139 | + |
| 140 | +/** |
| 141 | + * Leaves a comment in the client config whenever the use of the `useGlobalEndpoint` S3 config option is modeled. |
| 142 | + * |
| 143 | + * The SDK does not support this config option. |
| 144 | + * See `BindAwsEndpointBuiltins` & `S3_USE_GLOBAL_ENDPOINT` |
| 145 | + */ |
| 146 | +private val useGlobalEndpointSectionWriters = listOf( |
| 147 | + SectionWriterBinding(SmokeTestUseGlobalEndpoint) { writer, _ -> |
| 148 | + writer.write("// Smoke tests modeled the use of `useGlobalEndpoint` config, but it's not supported by the SDK") |
| 149 | + }, |
| 150 | +) |
0 commit comments