Unverified Commit 99fe4a76 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

[customization] Add support for the ApiVersion header (#787)

Fixes #138
parent 1aa59693
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ vNext (Month Day, Year)

- Moved the contents of `aws-auth` into the `aws-http` runtime crate (smithy-rs#783)
- Add tracing output for resolved AWS endpoint (smithy-rs#784)
- Add support for the following Glacier customizations:
  - Set the ApiVersion header (smithy-rs#138, #787)

v0.0.21-alpha (October 15th, 2021)
==================================
+3 −1
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import software.amazon.smithy.rust.codegen.smithy.customize.CombinedCodegenDecor
import software.amazon.smithy.rustsdk.customize.apigateway.ApiGatewayDecorator
import software.amazon.smithy.rustsdk.customize.auth.DisabledAuthDecorator
import software.amazon.smithy.rustsdk.customize.ec2.Ec2Decorator
import software.amazon.smithy.rustsdk.customize.glacier.GlacierDecorator
import software.amazon.smithy.rustsdk.customize.s3.S3Decorator

val DECORATORS = listOf(
@@ -34,7 +35,8 @@ val DECORATORS = listOf(
    DisabledAuthDecorator(),
    ApiGatewayDecorator(),
    S3Decorator(),
    Ec2Decorator()
    Ec2Decorator(),
    GlacierDecorator()
)

class AwsCodegenDecorator : CombinedCodegenDecorator(DECORATORS) {
+37 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package software.amazon.smithy.rustsdk.customize.glacier

import software.amazon.smithy.rust.codegen.rustlang.Writable
import software.amazon.smithy.rust.codegen.rustlang.rust
import software.amazon.smithy.rust.codegen.rustlang.writable
import software.amazon.smithy.rust.codegen.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization
import software.amazon.smithy.rust.codegen.smithy.customize.OperationSection
import software.amazon.smithy.rust.codegen.util.dq

class ApiVersionHeader(
    /**
     * ApiVersion
     * This usually comes from the `version` field of the service shape and is usually a date like "2012-06-01"
     * */
    private val apiVersion: String
) : OperationCustomization() {
    override fun section(section: OperationSection): Writable = when (section) {
        is OperationSection.FinalizeOperation -> emptySection
        is OperationSection.OperationImplBlock -> emptySection
        is OperationSection.MutateRequest -> writable {
            rust(
                """${section.request}
                .http_mut()
                .headers_mut()
                .insert("x-amz-glacier-version", #T::HeaderValue::from_static(${apiVersion.dq()}));""",
                RuntimeType.http
            )
        }
        else -> emptySection
    }
}
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package software.amazon.smithy.rustsdk.customize.glacier

import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.smithy.customize.OperationCustomization
import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator

class GlacierDecorator : RustCodegenDecorator {
    override val name: String = "Glacier"
    override val order: Byte = 0

    private fun applies(codegenContext: CodegenContext) = codegenContext.serviceShape.id == ShapeId.from("com.amazonaws.glacier#Glacier")
    override fun operationCustomizations(
        codegenContext: CodegenContext,
        operation: OperationShape,
        baseCustomizations: List<OperationCustomization>
    ): List<OperationCustomization> {
        val extras = if (applies(codegenContext)) {
            val apiVersion = codegenContext.serviceShape.version
            listOf(ApiVersionHeader(apiVersion))
        } else {
            emptyList()
        }
        return baseCustomizations + extras
    }
}
+88 −0
Original line number Diff line number Diff line
$version: "1.0"

namespace com.amazonaws.glacier

use smithy.test#httpRequestTests
use aws.protocols#restJson1

// The account_id test and the checksums tests will fail until https://github.com/awslabs/smithy-rs/issues/137 and
// https://github.com/awslabs/smithy-rs/issues/135 are implemented

apply UploadArchive @httpRequestTests([
    {
        id: "GlacierVersionHeader",
        documentation: "Glacier requires that a version header be set on all requests.",
        protocol: restJson1,
        method: "POST",
        uri: "/foo/vaults/bar/archives",
        headers: {
            "X-Amz-Glacier-Version": "2012-06-01",
        },
        body: "",
        params: {
            accountId: "foo",
            vaultName: "bar",
        },
    },
//     {
//         id: "GlacierChecksums",
//         documentation: "Glacier requires checksum headers that are cumbersome to provide.",
//         protocol: restJson1,
//         method: "POST",
//         uri: "/foo/vaults/bar/archives",
//         headers: {
//             "X-Amz-Glacier-Version": "2012-06-01",
//             "X-Amz-Content-Sha256": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
//             "X-Amz-Sha256-Tree-Hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
//         },
//         body: "hello world",
//         params: {
//             accountId: "foo",
//             vaultName: "bar",
//             body: "hello world"
//         },
//         appliesTo: "client",
//     },
//     {
//         id: "GlacierAccountId",
//         documentation: """
//             Glacier requires that the account id be set, but you can just use a
//             hyphen (-) to indicate the current account. This should be default
//             behavior if the customer provides a null or empty string.""",
//         protocol: restJson1,
//         method: "POST",
//         uri: "/-/vaults/bar/archives",
//         headers: {
//             "X-Amz-Glacier-Version": "2012-06-01",
//         },
//         body: "",
//         params: {
//             accountId: "",
//             vaultName: "bar",
//         },
//         appliesTo: "client",
//     }
])

// apply UploadMultipartPart @httpRequestTests([
//     {
//         id: "GlacierMultipartChecksums",
//         documentation: "Glacier requires checksum headers that are cumbersome to provide.",
//         protocol: restJson1,
//         method: "PUT",
//         uri: "/foo/vaults/bar/multipart-uploads/baz",
//         headers: {
//             "X-Amz-Glacier-Version": "2012-06-01",
//             "X-Amz-Content-Sha256": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
//             "X-Amz-Sha256-Tree-Hash": "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
//         },
//         body: "hello world",
//         params: {
//             accountId: "foo",
//             vaultName: "bar",
//             uploadId: "baz",
//             body: "hello world"
//         },
//         appliesTo: "client",
//     }
// ])
Loading