Skip to content

Commit

Permalink
feat(codegen): move endpoint config to AddAwsRuntimeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase committed Feb 13, 2023
1 parent ae85506 commit 3e1cb31
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@

/**
* AWS clients need to know the service name for collecting metrics, the
* region name used to resolve endpoints. For non AWS clients that use
* AWS Auth, region name is used as signing region.
* region name used to resolve endpoints, and whether to use Dualstack and
* Fips endpoints. For non AWS clients that use AWS Auth, region name is
* used as signing region.
*
* <p>This plugin adds the following config interface fields:
*
* <ul>
* <li>serviceId: Unique name to identify the AWS service.</li>
* <li>region: The AWS region to which this client will send requests</li>
* <li>useDualstackEndpoint: Enables IPv6/IPv4 dualstack endpoint.</li>
* <li>useFipsEndpoint: Enables FIPS compatible endpoints.</li>
* </ul>
*
* <p>This plugin adds the following Node runtime specific values:
Expand All @@ -53,6 +56,8 @@
* <li>serviceId: Unique name to identify the AWS service.</li>
* <li>region: Uses the default region provider that checks things like
* environment variables and the AWS config file.</li>
* <li>useDualstackEndpoint: Uses default useDualstackEndpoint provider.</li>
* <li>useFipsEndpoint: Uses default useFipsEndpoint provider.</li>
* </ul>
*
* <p>This plugin adds the following Browser runtime specific values:
Expand All @@ -62,6 +67,8 @@
* <li>region: Throws an exception since a region must
* be explicitly provided in the browser (environment variables and
* the shared config can't be resolved from the browser).</li>
* <li>useDualstackEndpoint: Sets to false.</li>
* <li>useFipsEndpoint: Sets to false.</li>
* </ul>
*/
@SmithyInternalApi
Expand All @@ -79,6 +86,10 @@ public void addConfigInterfaceFields(
if (isAwsService(settings, model)) {
writer.writeDocs("Unique service identifier.\n@internal")
.write("serviceId?: string;\n");
writer.writeDocs("Enables IPv6/IPv4 dualstack endpoint.")
.write("useDualstackEndpoint?: boolean | __Provider<boolean>;\n");
writer.writeDocs("Enables FIPS compatible endpoints.")
.write("useFipsEndpoint?: boolean | __Provider<boolean>;\n");
}
if (isSigV4Service(settings, model)) {
writer.writeDocs(isAwsService(settings, model)
Expand Down Expand Up @@ -111,6 +122,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
}
}
runtimeConfigs.putAll(getDefaultConfig(target, settings, model));
runtimeConfigs.putAll(getEndpointConfigWriters(target, settings, model));
return runtimeConfigs;
}

Expand Down Expand Up @@ -147,4 +159,56 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
return Collections.emptyMap();
}
}

private Map<String, Consumer<TypeScriptWriter>> getEndpointConfigWriters(
LanguageTarget target,
TypeScriptSettings settings,
Model model
) {
if (!isAwsService(settings, model)) {
return Collections.emptyMap();
}
switch (target) {
case BROWSER:
return MapUtils.of(
"useDualstackEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("DEFAULT_USE_DUALSTACK_ENDPOINT", "DEFAULT_USE_DUALSTACK_ENDPOINT",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.write("(() => Promise.resolve(DEFAULT_USE_DUALSTACK_ENDPOINT))");
},
"useFipsEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("DEFAULT_USE_FIPS_ENDPOINT", "DEFAULT_USE_FIPS_ENDPOINT",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.write("(() => Promise.resolve(DEFAULT_USE_FIPS_ENDPOINT))");
}
);
case NODE:
return MapUtils.of(
"useDualstackEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS",
"NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.write("loadNodeConfig(NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS)");
},
"useFipsEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS",
"NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.write("loadNodeConfig(NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS)");
}
);
default:
return Collections.emptyMap();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
case NODE:
return MapUtils.of("useArnRegion", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER)
.addImport("loadConfig", "loadNodeConfig", TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName)
.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName)
.addDependency(AwsDependency.BUCKET_ENDPOINT_MIDDLEWARE)
.addImport("NODE_USE_ARN_REGION_CONFIG_OPTIONS", "NODE_USE_ARN_REGION_CONFIG_OPTIONS",
AwsDependency.BUCKET_ENDPOINT_MIDDLEWARE.packageName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
software.amazon.smithy.aws.typescript.codegen.AddEndpointsV2ParameterNameMap
software.amazon.smithy.aws.typescript.codegen.AddClientRuntimeConfig
software.amazon.smithy.aws.typescript.codegen.AddAwsRuntimeConfig
software.amazon.smithy.aws.typescript.codegen.AddBuiltinPlugins
software.amazon.smithy.aws.typescript.codegen.AddAwsAuthPlugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,22 @@ public void awsClient() {
// Check config interface fields
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/NotSameClient.ts").get(), containsString("serviceId?:"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/NotSameClient.ts").get(), containsString("region?:"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/NotSameClient.ts").get(), containsString("useDualstackEndpoint?:"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/NotSameClient.ts").get(), containsString("useFipsEndpoint?:"));


// Check config files
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.shared.ts").get(), containsString("serviceId: config?.serviceId ?? \"Not Same\""));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), containsString("region: config?.region ?? invalidProvider"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), containsString("region: config?.region ?? loadNodeConfig"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), containsString(
"useDualstackEndpoint: config?.useDualstackEndpoint ?? (() => Promise.resolve(DEFAULT_USE_DUALSTACK_ENDPOINT))"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), containsString(
"useDualstackEndpoint: config?.useDualstackEndpoint ?? loadNodeConfig(NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS)"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), containsString(
"useFipsEndpoint: config?.useFipsEndpoint ?? (() => Promise.resolve(DEFAULT_USE_FIPS_ENDPOINT))"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), containsString(
"useFipsEndpoint: config?.useFipsEndpoint ?? loadNodeConfig(NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS),"));
}

@Test
Expand Down Expand Up @@ -79,11 +90,17 @@ public void sigV4GenericClient() {
// Check config interface fields
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleSigV4Client.ts").get(), not(containsString("serviceId?:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleSigV4Client.ts").get(), containsString("region?:"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleSigV4Client.ts").get(), not(containsString("useDualstackEndpoint?:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleSigV4Client.ts").get(), not(containsString("useFipsEndpoint?:")));

// Check config files
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.shared.ts").get(), not(containsString("serviceId:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), containsString("region: config?.region ?? invalidProvider"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), containsString("region: config?.region ?? loadNodeConfig"));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), not(containsString("useDualstackEndpoint:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), not(containsString("useDualstackEndpoint:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), not(containsString("useFipsEndpoint:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), not(containsString("useFipsEndpoint:")));
}

@Test
Expand Down Expand Up @@ -115,10 +132,16 @@ public void notSigV4GenericClient() {
// Check config interface fields
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleClient.ts").get(), not(containsString("serviceId?:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleClient.ts").get(), not(containsString("region?:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleClient.ts").get(), not(containsString("useDualstackEndpoint?:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/SsdkExampleClient.ts").get(), not(containsString("useFipsEndpoint?:")));

// Check config files
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.shared.ts").get(), not(containsString("serviceId:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), not(containsString("region:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), not(containsString("region:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), not(containsString("useDualstackEndpoint:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), not(containsString("useDualstackEndpoint:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.browser.ts").get(), not(containsString("useFipsEndpoint:")));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/runtimeConfig.ts").get(), not(containsString("useFipsEndpoint:")));
}
}
Loading

0 comments on commit 3e1cb31

Please sign in to comment.