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

Generate a region setter in config when the model uses SigV4 #2960

Merged
merged 1 commit into from
Aug 30, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package software.amazon.smithy.rustsdk

import software.amazon.smithy.aws.traits.auth.SigV4Trait
import software.amazon.smithy.model.knowledge.ServiceIndex
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rulesengine.language.syntax.parameters.Builtins
Expand Down Expand Up @@ -82,7 +84,11 @@ class RegionDecorator : ClientCodegenDecorator {
override val name: String = "Region"
override val order: Byte = 0

private fun usesRegion(codegenContext: ClientCodegenContext) = codegenContext.getBuiltIn(Builtins.REGION) != null
// Services that have an endpoint ruleset that references the SDK::Region built in, or
// that use SigV4, both need a configurable region.
private fun usesRegion(codegenContext: ClientCodegenContext) =
codegenContext.getBuiltIn(Builtins.REGION) != null || ServiceIndex.of(codegenContext.model)
.getEffectiveAuthSchemes(codegenContext.serviceShape).containsKey(SigV4Trait.ID)

override fun configCustomizations(
codegenContext: ClientCodegenContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rustsdk

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import kotlin.io.path.readText

class RegionDecoratorTest {
private val modelWithoutRegionParamOrSigV4AuthScheme = """
namespace test

use aws.api#service
use aws.protocols#awsJson1_0
use smithy.rules#endpointRuleSet

@awsJson1_0
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {}
})
@service(sdkId: "dontcare")
service TestService { version: "2023-01-01", operations: [SomeOperation] }
structure SomeOutput { something: String }
operation SomeOperation { output: SomeOutput }
""".asSmithyModel()

private val modelWithRegionParam = """
namespace test

use aws.api#service
use aws.protocols#awsJson1_0
use smithy.rules#endpointRuleSet

@awsJson1_0
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {
"Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
}
})
@service(sdkId: "dontcare")
service TestService { version: "2023-01-01", operations: [SomeOperation] }
structure SomeOutput { something: String }
operation SomeOperation { output: SomeOutput }
""".asSmithyModel()

private val modelWithSigV4AuthScheme = """
namespace test

use aws.auth#sigv4
use aws.api#service
use aws.protocols#awsJson1_0
use smithy.rules#endpointRuleSet

@auth([sigv4])
@sigv4(name: "dontcare")
@awsJson1_0
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {}
})
@service(sdkId: "dontcare")
service TestService { version: "2023-01-01", operations: [SomeOperation] }
structure SomeOutput { something: String }
operation SomeOperation { output: SomeOutput }
""".asSmithyModel()

@Test
fun `models without region built-in params or SigV4 should not have configurable regions`() {
val path = awsSdkIntegrationTest(
modelWithoutRegionParamOrSigV4AuthScheme,
generateOrchestrator = true,
) { _, _ -> /* it should generate and compile successfully */ }
val configContents = path.resolve("src/config.rs").readText()
assertFalse(configContents.contains("fn set_region("))
}

@Test
fun `models with region built-in params should have configurable regions`() {
val path = awsSdkIntegrationTest(
modelWithRegionParam,
generateOrchestrator = true,
) { _, _ -> /* it should generate and compile successfully */ }
val configContents = path.resolve("src/config.rs").readText()
assertTrue(configContents.contains("fn set_region("))
}

@Test
fun `models with SigV4 should have configurable regions`() {
val path = awsSdkIntegrationTest(
modelWithSigV4AuthScheme,
generateOrchestrator = true,
) { _, _ -> /* it should generate and compile successfully */ }
val configContents = path.resolve("src/config.rs").readText()
assertTrue(configContents.contains("fn set_region("))
}
}