Unverified Commit eff5a574 authored by Russell Cohen's avatar Russell Cohen Committed by John DiSanti
Browse files

Source sdk-partitions from the models instead of hardcoding

1. Update `sync-models` to sync from aws-sdk-rust. This location includes `sdk-partitions.json`
2. Update aws:sdk gradle file to properly set this variable in codegen./
3. Add an additional safeguard to ensure that the default hardcoded version is only used in tests and adhoc SDK generation.
parent 8eb7654a
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -49,14 +49,6 @@ fun baseTest(service: String, module: String, imports: List<String> = listOf()):
            "includeFluentClient": false,
            "nullabilityCheckMode": "${getNullabilityCheckMode()}"
        """,
        extraConfig = """
            , "customizationConfig": {
                "awsSdk": {
                    "generateReadme": false,
                    "requireEndpointResolver": false
                }
            }
        """,
    )
}

+50 −11
Original line number Diff line number Diff line
@@ -10,14 +10,56 @@ import software.amazon.smithy.rust.codegen.core.smithy.CoreRustSettings
import software.amazon.smithy.rust.codegen.core.util.orNull
import java.nio.file.Path
import java.nio.file.Paths
import java.util.logging.Logger

/**
 * SDK-specific settings within the Rust codegen `customizationConfig.awsSdk` object.
 */
class SdkSettings private constructor(private val awsSdk: ObjectNode?) {
    private fun warnOnUnusedProperties() {
        if (awsSdk == null) {
            return
        }
        val logger = Logger.getLogger("SdkSettings")
        if (awsSdk.getMember("generateReadme").isPresent) {
            logger.warning(
                "`generateReadme` parameter is now ignored. Readmes are now only generated when " +
                    "`awsSdkBuild` is set to `true`. You can use `suppressReadme` to explicitly suppress the readme in that case.",
            )
        }

        if (awsSdk.getMember("requireEndpointResolver").isPresent) {
            logger.warning(
                "`requireEndpointResolver` is no a no-op and you may remove it from your configuration. " +
                    "An endpoint resolver is only required when `awsSdkBuild` is set to true.",
            )
        }
    }

    companion object {
        fun from(coreRustSettings: CoreRustSettings): SdkSettings =
            SdkSettings(coreRustSettings.customizationConfig?.getObjectMember("awsSdk")?.orNull())
        fun from(coreRustSettings: CoreRustSettings): SdkSettings {
            val settings = SdkSettings(coreRustSettings.customizationConfig?.getObjectMember("awsSdk")?.orNull())
            if (shouldPrintWarning()) {
                settings.warnOnUnusedProperties()
                warningPrinted()
            }
            return settings
        }

        @Volatile
        var warningPrinted = false

        private fun warningPrinted() {
            synchronized(this) {
                this.warningPrinted = true
            }
        }

        private fun shouldPrintWarning(): Boolean {
            synchronized(this) {
                return !this.warningPrinted
            }
        }
    }

    /** Path to the `sdk-default-configuration.json` config file */
@@ -25,33 +67,30 @@ class SdkSettings private constructor(private val awsSdk: ObjectNode?) {
        get() =
            awsSdk?.getStringMember("defaultConfigPath")?.orNull()?.value.let { Paths.get(it) }

    /** Path to the `sdk-endpoints.json` configuration */
    val endpointsConfigPath: Path?
        get() =
            awsSdk?.getStringMember("endpointsConfigPath")?.orNull()?.value?.let { Paths.get(it) }

    /** Path to the `default-partitions.json` configuration */
    val partitionsConfigPath: Path?
        get() =
            awsSdk?.getStringMember("partitionsConfigPath")?.orNull()?.value?.let { Paths.get(it) }

    val awsSdkBuild: Boolean
        get() = awsSdk?.getBooleanMember("awsSdkBuild")?.orNull()?.value ?: false

    /** Path to AWS SDK integration tests */
    val integrationTestPath: String
        get() =
            awsSdk?.getStringMember("integrationTestPath")?.orNull()?.value ?: "aws/sdk/integration-tests"

    /** Version number of the `aws-config` crate */
    /** Version number of the `aws-config` crate. This is used to set the dev-dependency when generating readme's  */
    val awsConfigVersion: String?
        get() =
            awsSdk?.getStringMember("awsConfigVersion")?.orNull()?.value

    /** Whether to generate a README */
    val generateReadme: Boolean
        get() =
            awsSdk?.getBooleanMember("generateReadme")?.orNull()?.value ?: false
        get() = awsSdkBuild && !(awsSdk?.getBooleanMember("suppressReadme")?.orNull()?.value ?: false)

    val requireEndpointResolver: Boolean
        get() = awsSdk?.getBooleanMember("requireEndpointResolver")?.orNull()?.value ?: true
        get() = awsSdkBuild
}

fun ClientCodegenContext.sdkSettings() = SdkSettings.from(this.settings)
+14 −7
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegen
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.awsStandardLib
import software.amazon.smithy.rust.codegen.core.util.PANIC
import software.amazon.smithy.rustsdk.SdkSettings
import kotlin.io.path.readText

@@ -29,11 +30,17 @@ class AwsEndpointsStdLib() : ClientCodegenDecorator {

    private fun partitionMetadata(sdkSettings: SdkSettings): ObjectNode {
        if (partitionsCache == null) {
            val partitionsJson = when (val path = sdkSettings.partitionsConfigPath) {
                null -> (
            val partitionsJson =
                when (val path = sdkSettings.partitionsConfigPath) {
                    null -> {
                        if (sdkSettings.awsSdkBuild) {
                            PANIC("cannot use hardcoded partitions in AWS SDK build")
                        }
                        (
                            javaClass.getResource("/default-partitions.json")
                                ?: throw IllegalStateException("Failed to find default-partitions.json in the JAR")
                        ).readText()
                    }

                    else -> path.readText()
                }
+1 −105
Original line number Diff line number Diff line
{
  "version": "1.1",
  "partitions": [
    {
      "id": "aws",
      "regionRegex": "^(us|eu|ap|sa|ca|me|af)-\\w+-\\d+$",
      "regions": {
        "af-south-1": {},
        "ap-east-1": {},
        "ap-northeast-1": {},
        "ap-northeast-2": {},
        "ap-northeast-3": {},
        "ap-south-1": {},
        "ap-southeast-1": {},
        "ap-southeast-2": {},
        "ap-southeast-3": {},
        "ca-central-1": {},
        "eu-central-1": {},
        "eu-north-1": {},
        "eu-south-1": {},
        "eu-west-1": {},
        "eu-west-2": {},
        "eu-west-3": {},
        "me-central-1": {},
        "me-south-1": {},
        "sa-east-1": {},
        "us-east-1": {},
        "us-east-2": {},
        "us-west-1": {},
        "us-west-2": {},
        "aws-global": {}
      },
      "outputs": {
        "name": "aws",
        "dnsSuffix": "amazonaws.com",
        "dualStackDnsSuffix": "api.aws",
        "supportsFIPS": true,
        "supportsDualStack": true
      }
    },
    {
      "id": "aws-us-gov",
      "regionRegex": "^us\\-gov\\-\\w+\\-\\d+$",
      "regions": {
        "us-gov-west-1": {},
        "us-gov-east-1": {},
        "aws-us-gov-global": {}
      },
      "outputs": {
        "name": "aws-us-gov",
        "dnsSuffix": "amazonaws.com",
        "dualStackDnsSuffix": "api.aws",
        "supportsFIPS": true,
        "supportsDualStack": true
      }
    },
    {
      "id": "aws-cn",
      "regionRegex": "^cn\\-\\w+\\-\\d+$",
      "regions": {
        "cn-north-1": {},
        "cn-northwest-1": {},
        "aws-cn-global": {}
      },
      "outputs": {
        "name": "aws-cn",
        "dnsSuffix": "amazonaws.com.cn",
        "dualStackDnsSuffix": "api.amazonwebservices.com.cn",
        "supportsFIPS": true,
        "supportsDualStack": true
      }
    },
    {
      "id": "aws-iso",
      "regionRegex": "^us\\-iso\\-\\w+\\-\\d+$",
      "outputs": {
        "name": "aws-iso",
        "dnsSuffix": "c2s.ic.gov",
        "supportsFIPS": true,
        "supportsDualStack": false,
        "dualStackDnsSuffix": "c2s.ic.gov"
      },
      "regions": {
        "us-iso-east-1":  {},
        "us-iso-west-1": {},
        "aws-iso-global": {}
      }
    },
    {
      "id": "aws-iso-b",
      "regionRegex": "^us\\-isob\\-\\w+\\-\\d+$",
      "outputs": {
        "name": "aws-iso-b",
        "dnsSuffix": "sc2s.sgov.gov",
        "supportsFIPS": true,
        "supportsDualStack": false,
        "dualStackDnsSuffix": "sc2s.sgov.gov"
      },
      "regions": {
        "us-isob-east-1": {},
        "aws-iso-b-global": {}
      }
    }
  ]
}
+1 −105
Original line number Diff line number Diff line
../../../../sdk/aws-models/sdk-partitions.json
 No newline at end of file
Loading