-
Notifications
You must be signed in to change notification settings - Fork 55
feat: implement automatic prefix trimming for route53 resource ids #655
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "id": "8fa79617-cfb4-48f2-a031-32ee2f0341b3", | ||
| "type": "feature", | ||
| "description": "Implement ID prefix trimming for route53 resources.", | ||
| "issues": [ | ||
| "awslabs/aws-sdk-kotlin#509" | ||
| ] | ||
| } |
65 changes: 65 additions & 0 deletions
65
...odegen/src/main/kotlin/aws/sdk/kotlin/codegen/customization/route53/TrimResourcePrefix.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package aws.sdk.kotlin.codegen.customization.route53 | ||
|
|
||
| import aws.sdk.kotlin.codegen.sdkId | ||
| import software.amazon.smithy.kotlin.codegen.KotlinSettings | ||
| import software.amazon.smithy.kotlin.codegen.core.* | ||
| import software.amazon.smithy.kotlin.codegen.integration.KotlinIntegration | ||
| import software.amazon.smithy.kotlin.codegen.model.expectShape | ||
| import software.amazon.smithy.kotlin.codegen.model.hasTrait | ||
| import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolGenerator | ||
| import software.amazon.smithy.kotlin.codegen.rendering.protocol.ProtocolMiddleware | ||
| import software.amazon.smithy.kotlin.codegen.utils.getOrNull | ||
| import software.amazon.smithy.model.Model | ||
| import software.amazon.smithy.model.shapes.MemberShape | ||
| import software.amazon.smithy.model.shapes.OperationShape | ||
| import software.amazon.smithy.model.shapes.ServiceShape | ||
| import software.amazon.smithy.model.shapes.StructureShape | ||
| import software.amazon.smithy.model.traits.HttpLabelTrait | ||
|
|
||
| class TrimResourcePrefix : KotlinIntegration { | ||
| override fun enabledForService(model: Model, settings: KotlinSettings) = | ||
| model.expectShape<ServiceShape>(settings.service).sdkId.equals("route 53", ignoreCase = true) | ||
|
|
||
| override fun customizeMiddleware( | ||
| ctx: ProtocolGenerator.GenerationContext, | ||
| resolved: List<ProtocolMiddleware> | ||
| ): List<ProtocolMiddleware> = | ||
| resolved + TrimResourcePrefixMiddleware() | ||
| } | ||
|
|
||
| private class TrimResourcePrefixMiddleware : ProtocolMiddleware { | ||
| override val name: String | ||
| get() = "TrimResourcePrefix" | ||
|
|
||
| override val order: Byte = -128 | ||
|
|
||
| override fun isEnabledFor(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): Boolean { | ||
| val input = op.input.getOrNull()?.let { ctx.model.expectShape<StructureShape>(it) } | ||
| return input?.members()?.any(MemberShape::shouldTrimResourcePrefix) ?: false | ||
| } | ||
|
|
||
| override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { | ||
| val pathMember = ctx.model.expectShape<StructureShape>(op.input.get()).members().first( | ||
| MemberShape::shouldTrimResourcePrefix | ||
| ).defaultName() | ||
|
|
||
| writer.withBlock("op.execution.initialize.intercept { req, next -> ", "}") { | ||
| write("""val prefix = "^/?.*?/".toRegex()""") | ||
| withBlock("if (req.subject.#L?.contains(prefix) == true) {", "}", pathMember) { | ||
| write( | ||
| """val updated = req.subject.copy { #L = req.subject.#L?.replace(prefix, "") }""", | ||
| pathMember, | ||
| pathMember, | ||
| ) | ||
| write("next.call(#T(req.context, updated))", RuntimeTypes.Http.Operation.OperationRequest) | ||
| } | ||
| withBlock("else {", "}") { | ||
| write("next.call(req)") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun MemberShape.shouldTrimResourcePrefix(): Boolean = | ||
| (target.name == "ResourceId" || target.name == "ChangeId") && | ||
| hasTrait<HttpLabelTrait>() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| $version: "1.0" | ||
|
|
||
| namespace com.amazonaws.route53 | ||
|
|
||
| use smithy.test#httpRequestTests | ||
|
|
||
|
|
||
| apply ListResourceRecordSets @httpRequestTests([ | ||
| { | ||
| id: "ListResourceRecordSetsNoTrim", | ||
| documentation: "Validates that HostedZoneId isn't trimmed when not prefixed.", | ||
| method: "GET", | ||
| protocol: "aws.protocols#restXml", | ||
| uri: "/2013-04-01/hostedzone/IDOFMYHOSTEDZONE/rrset", | ||
| bodyMediaType: "application/xml", | ||
| params: { | ||
| "HostedZoneId": "IDOFMYHOSTEDZONE" | ||
| } | ||
| }, | ||
| { | ||
| id: "ListResourceRecordSetsTrim", | ||
| documentation: "Validates that HostedZoneId is trimmed.", | ||
| method: "GET", | ||
| protocol: "aws.protocols#restXml", | ||
| uri: "/2013-04-01/hostedzone/IDOFMYHOSTEDZONE/rrset", | ||
| bodyMediaType: "application/xml", | ||
| params: { | ||
| "HostedZoneId": "hostedzone/IDOFMYHOSTEDZONE" | ||
| } | ||
| }, | ||
| { | ||
| id: "ListResourceRecordSetsTrimLeadingSlash", | ||
| documentation: "Validates that HostedZoneId is trimmed even with a leading slash.", | ||
| method: "GET", | ||
| protocol: "aws.protocols#restXml", | ||
| uri: "/2013-04-01/hostedzone/IDOFMYHOSTEDZONE/rrset", | ||
| bodyMediaType: "application/xml", | ||
| params: { | ||
| "HostedZoneId": "/hostedzone/IDOFMYHOSTEDZONE" | ||
| } | ||
| }, | ||
| { | ||
| id: "ListResourceRecordSetsTrimMultislash", | ||
| documentation: "Validates that HostedZoneId isn't over-trimmed.", | ||
| method: "GET", | ||
| protocol: "aws.protocols#restXml", | ||
| uri: "/2013-04-01/hostedzone/IDOFMY%2FHOSTEDZONE/rrset", | ||
| bodyMediaType: "application/xml", | ||
| params: { | ||
| "HostedZoneId": "/hostedzone/IDOFMY/HOSTEDZONE" | ||
| } | ||
| }, | ||
| ]) | ||
|
|
||
| apply GetChange @httpRequestTests([ | ||
| { | ||
| id: "GetChangeTrimChangeId", | ||
| documentation: "This test validates that change id is correctly trimmed", | ||
| method: "GET", | ||
| protocol: "aws.protocols#restXml", | ||
| uri: "/2013-04-01/change/SOMECHANGEID", | ||
| bodyMediaType: "application/xml", | ||
| params: { | ||
| "Id": "/change/SOMECHANGEID" | ||
| } | ||
| }, | ||
| ]) | ||
|
|
||
| apply GetReusableDelegationSet @httpRequestTests([ | ||
| { | ||
| id: "GetReusableDelegationSetTrimDelegationSetId", | ||
| documentation: "This test validates that delegation set id is correctly trimmed", | ||
| method: "GET", | ||
| protocol: "aws.protocols#restXml", | ||
| uri: "/2013-04-01/delegationset/DELEGATIONSETID", | ||
| bodyMediaType: "application/xml", | ||
| params: { | ||
| "Id": "/delegationset/DELEGATIONSETID" | ||
| } | ||
| }, | ||
| ]) | ||
|
Comment on lines
+69
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: How/why is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This particular test case is one pulled from rust's codebase. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Why does this need to execute first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense in my mind that doing any modification on the user input would occur before serialization (so basically, it should execute first).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important to note that the middleware itself also is ordered by phase. The
initializationphase precedesserializationso it wouldn't matter what order they are in for codegen.