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

fix: Route 53 TrimHostedZone customization is incomplete #1445

Merged
merged 8 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,39 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import ClientRuntime

public struct Route53TrimHostedZoneMiddleware<Input, Output>: ClientRuntime.Middleware {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as the previously code-generated middleware, except:

  • The input & output are generics that get substituted for each operation at compile time.
  • The hosted zone ID on the input model is accessed via a key path that is passed in at initialization.

public let id: Swift.String = "Route53TrimHostedZoneMiddleware"
private let prefixes = ["/hostedzone/", "hostedzone/", "/hostedzone", "hostedzone"]

private let hostedZoneIDKeyPath: WritableKeyPath<Input, String?>

public init(_ hostedZoneIDKeyPath: WritableKeyPath<Input, String?>) {
self.hostedZoneIDKeyPath = hostedZoneIDKeyPath
}

public func handle<H>(context: Context,
input: Input,
next: H) async throws -> ClientRuntime.OperationOutput<Output>
where H: Handler,
Self.MInput == H.Input,
Self.MOutput == H.Output,
Self.Context == H.Context {
guard let hostedZoneId = input[keyPath: hostedZoneIDKeyPath] else {
return try await next.handle(context: context, input: input)
}
var copiedInput = input
let stripped = hostedZoneId.stripFirstMatching(prefixes: prefixes)
copiedInput[keyPath: hostedZoneIDKeyPath] = stripped
return try await next.handle(context: context, input: copiedInput)
}

public typealias MInput = Input
public typealias MOutput = ClientRuntime.OperationOutput<Output>
public typealias Context = ClientRuntime.HttpContext
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Route53TrimHostedZone : SwiftIntegration {
override fun enabledForService(model: Model, settings: SwiftSettings): Boolean {
return model.expectShape<ServiceShape>(settings.service).isRoute53
}

override fun preprocessModel(model: Model, settings: SwiftSettings): Model {
return ModelTransformer.create().mapShapes(model) {
if (isHostId(it)) {
Expand All @@ -38,8 +39,7 @@ class Route53TrimHostedZone : SwiftIntegration {
val inputShape = MiddlewareShapeUtils.inputShape(ctx.model, operationShape)
val hostedZoneMember = inputShape.members().find { it.hasTrait<TrimHostedZone>() }
if (hostedZoneMember != null) {
StripHostedZoneURLPathMiddleware.renderMiddleware(ctx, operationShape)
operationMiddleware.prependMiddleware(operationShape, StripHostedZoneUrlPathMiddlewareRenderable(ctx.model, ctx.symbolProvider))
operationMiddleware.prependMiddleware(operationShape, StripHostedZoneURLPathMiddlewareRenderable(ctx.model, ctx.symbolProvider))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The middleware is no longer rendered for each operation that needs it since a reusable one is used instead.

}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.swift.codegen.SwiftWriter
import software.amazon.smithy.swift.codegen.integration.ProtocolGenerator
import software.amazon.smithy.swift.codegen.integration.middlewares.handlers.MiddlewareShapeUtils
import software.amazon.smithy.swift.codegen.middleware.MiddlewarePosition
import software.amazon.smithy.swift.codegen.middleware.MiddlewareRenderable
import software.amazon.smithy.swift.codegen.middleware.MiddlewareStep

class StripHostedZoneUrlPathMiddlewareRenderable(
class StripHostedZoneURLPathMiddlewareRenderable(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type renamed

val model: Model,
val symbolProvider: SymbolProvider
) : MiddlewareRenderable {
Expand All @@ -21,7 +20,6 @@ class StripHostedZoneUrlPathMiddlewareRenderable(
override val position = MiddlewarePosition.AFTER

override fun render(ctx: ProtocolGenerator.GenerationContext, writer: SwiftWriter, op: OperationShape, operationStackName: String) {
val inputShapeName = MiddlewareShapeUtils.inputSymbol(symbolProvider, model, op).name
writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: ${inputShapeName}StripHostedZoneMiddleware())")
writer.write("$operationStackName.${middlewareStep.stringValue()}.intercept(position: ${position.stringValue()}, middleware: Route53TrimHostedZoneMiddleware(\\.hostedZoneId))")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reusable middleware, initialized with a key path, is inserted into the operation stack instead of a code-generated middleware.

}
}
Loading