Skip to content

Commit

Permalink
fix(codegen): accIdEpMode config resolver generation plugin (#6544)
Browse files Browse the repository at this point in the history
* fix(codegen): accIdEpMode config resolver generation plugin

* chore(codegen): move to accIdEpMode TS integration)

* chore(codegen): ddb client with accIdepMode config resolver fixed imports

* chore(codegen): formatting fix

* chore(codegen): formatting fix

* chore(codegen): formatting fix

* chore(codegen): formatting fix
  • Loading branch information
siddsriv authored Oct 4, 2024
1 parent de4dc49 commit fc14586
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 112 deletions.
30 changes: 19 additions & 11 deletions clients/client-dynamodb/src/DynamoDBClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// smithy-typescript generated code
import { AccountIdEndpointMode } from "@aws-sdk/core/account-id-endpoint";
import {
AccountIdEndpointMode,
AccountIdEndpointModeInputConfig,
AccountIdEndpointModeResolvedConfig,
resolveAccountIdEndpointModeConfig,
} from "@aws-sdk/core/account-id-endpoint";
import {
EndpointDiscoveryInputConfig,
EndpointDiscoveryResolvedConfig,
Expand Down Expand Up @@ -461,6 +466,7 @@ export interface ClientDefaults extends Partial<__SmithyConfiguration<__HttpHand
*/
export type DynamoDBClientConfigType = Partial<__SmithyConfiguration<__HttpHandlerOptions>> &
ClientDefaults &
AccountIdEndpointModeInputConfig &
UserAgentInputConfig &
RetryInputConfig &
RegionInputConfig &
Expand All @@ -482,6 +488,7 @@ export interface DynamoDBClientConfig extends DynamoDBClientConfigType {}
export type DynamoDBClientResolvedConfigType = __SmithyResolvedConfiguration<__HttpHandlerOptions> &
Required<ClientDefaults> &
RuntimeExtensionsConfig &
AccountIdEndpointModeResolvedConfig &
UserAgentResolvedConfig &
RetryResolvedConfig &
RegionResolvedConfig &
Expand Down Expand Up @@ -531,18 +538,19 @@ export class DynamoDBClient extends __Client<
constructor(...[configuration]: __CheckOptionalClientConfig<DynamoDBClientConfig>) {
const _config_0 = __getRuntimeConfig(configuration || {});
const _config_1 = resolveClientEndpointParameters(_config_0);
const _config_2 = resolveUserAgentConfig(_config_1);
const _config_3 = resolveRetryConfig(_config_2);
const _config_4 = resolveRegionConfig(_config_3);
const _config_5 = resolveHostHeaderConfig(_config_4);
const _config_6 = resolveEndpointConfig(_config_5);
const _config_7 = resolveHttpAuthSchemeConfig(_config_6);
const _config_8 = resolveEndpointDiscoveryConfig(_config_7, {
const _config_2 = resolveAccountIdEndpointModeConfig(_config_1);
const _config_3 = resolveUserAgentConfig(_config_2);
const _config_4 = resolveRetryConfig(_config_3);
const _config_5 = resolveRegionConfig(_config_4);
const _config_6 = resolveHostHeaderConfig(_config_5);
const _config_7 = resolveEndpointConfig(_config_6);
const _config_8 = resolveHttpAuthSchemeConfig(_config_7);
const _config_9 = resolveEndpointDiscoveryConfig(_config_8, {
endpointDiscoveryCommandCtor: DescribeEndpointsCommand,
});
const _config_9 = resolveRuntimeExtensions(_config_8, configuration?.extensions || []);
super(_config_9);
this.config = _config_9;
const _config_10 = resolveRuntimeExtensions(_config_9, configuration?.extensions || []);
super(_config_10);
this.config = _config_10;
this.middlewareStack.use(getUserAgentPlugin(this.config));
this.middlewareStack.use(getRetryPlugin(this.config));
this.middlewareStack.use(getContentLengthPlugin(this.config));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,147 @@
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.typescript.codegen;
package software.amazon.smithy.aws.typescript.codegen;

import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Logger;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyInternalApi;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.logging.Logger;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait;
import software.amazon.smithy.typescript.codegen.LanguageTarget;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.endpointsV2.RuleSetParameterFinder;
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
* Generates accountIdEndpointMode configuration field for service clients
* that have the AccountIdEndpointMode built-in param in the ruleset.
*/
@SmithyInternalApi
public final class AddAccountIdEndpointModeRuntimeConfig implements TypeScriptIntegration {
/**
* Generates accountIdEndpointMode configuration field for service clients
* that have the AccountIdEndpointMode built-in param in the ruleset.
*/
@SmithyInternalApi
public final class AddAccountIdEndpointModeRuntimeConfig implements TypeScriptIntegration {

private static final Logger LOGGER = Logger.getLogger(AddAccountIdEndpointModeRuntimeConfig.class.getName());

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (isAwsService(settings, model)) {
ServiceShape service = settings.getService(model);
if (hasAccountIdEndpointParam(service)) {
writer.addImportSubmodule("AccountIdEndpointMode", null,
AwsDependency.AWS_SDK_CORE, "/account-id-endpoint");
writer.writeDocs("Defines if the AWS AccountId will be used for endpoint routing.");
writer.write("accountIdEndpointMode?: AccountIdEndpointMode | "
+ "__Provider<AccountIdEndpointMode>;\n");
writer.addImportSubmodule("resolveAccountIdEndpointModeConfig", null,
AwsDependency.AWS_SDK_CORE, "/account-id-endpoint");
}
}
}

private static final Logger LOGGER = Logger.getLogger(AddAccountIdEndpointModeRuntimeConfig.class.getName());
@Override
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
LanguageTarget target
) {
ServiceShape service = settings.getService(model);
Map<String, Consumer<TypeScriptWriter>> runtimeConfigs = new HashMap<>();
if (isAwsService(settings, model) || isSigV4Service(settings, model)) {
Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class);
if (endpointRuleSetTrait.isPresent()) {
RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service);
if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) {
switch (target) {
case BROWSER:
runtimeConfigs.put("accountIdEndpointMode", writer -> {
writer.addImportSubmodule("DEFAULT_ACCOUNT_ID_ENDPOINT_MODE", null,
AwsDependency.AWS_SDK_CORE,
"/account-id-endpoint");
writer.write("(() => Promise.resolve(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE))");
});
break;
case NODE:
runtimeConfigs.put("accountIdEndpointMode", writer -> {
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImportSubmodule("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS",
null, AwsDependency.AWS_SDK_CORE,
"/account-id-endpoint");
writer.write(
"loadNodeConfig(NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS)");
});
break;
default:
LOGGER.warning("AccountIdEndpointMode config not supported for target: " + target);
break;
}
}
}
}
return runtimeConfigs;
}

@Override
public void addConfigInterfaceFields(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
TypeScriptWriter writer
) {
if (isAwsService(settings, model)) {
ServiceShape service = settings.getService(model);
Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class);
if (endpointRuleSetTrait.isPresent()) {
RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service);
if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) {
writer.addDependency(AwsDependency.AWS_SDK_CORE);
// TODO: change to addImportSubmodule when available; smithy-ts, #pull-1280
writer.addImport("AccountIdEndpointMode", "AccountIdEndpointMode",
"@aws-sdk/core/account-id-endpoint");
writer.writeDocs("Defines if the AWS AccountId will be used for endpoint routing.");
writer.write("accountIdEndpointMode?: AccountIdEndpointMode | "
+ "__Provider<AccountIdEndpointMode>;\n");
}
}
}
}
// AccountIdEndpointMode resolver
@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return List.of(
RuntimeClientPlugin.builder()
.inputConfig(
Symbol.builder()
.namespace(
AwsDependency.AWS_SDK_CORE.getPackageName() + "/account-id-endpoint", "/"
)
.name("AccountIdEndpointModeInputConfig")
.build()
)
.resolvedConfig(
Symbol.builder()
.namespace(
AwsDependency.AWS_SDK_CORE.getPackageName() + "/account-id-endpoint", "/"
)
.name("AccountIdEndpointModeResolvedConfig")
.build()
)
.resolveFunction(
Symbol.builder()
.namespace(
AwsDependency.AWS_SDK_CORE.getPackageName() + "/account-id-endpoint", "/"
)
.name("resolveAccountIdEndpointModeConfig")
.build()
)
.servicePredicate((m, s) -> hasAccountIdEndpointParam(s))
.build()
);
}

@Override
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
TypeScriptSettings settings,
Model model,
SymbolProvider symbolProvider,
LanguageTarget target
) {
ServiceShape service = settings.getService(model);
Map<String, Consumer<TypeScriptWriter>> runtimeConfigs = new HashMap<>();
if (isAwsService(settings, model) || isSigV4Service(settings, model)) {
Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class);
if (endpointRuleSetTrait.isPresent()) {
RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service);
if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) {
switch (target) {
case BROWSER:
runtimeConfigs.put("accountIdEndpointMode", writer -> {
writer.addDependency(AwsDependency.AWS_SDK_CORE);
// TODO: change to addImportSubmodule when available
writer.addImport("DEFAULT_ACCOUNT_ID_ENDPOINT_MODE", "DEFAULT_ACCOUNT_ID_ENDPOINT_MODE",
"@aws-sdk/core/account-id-endpoint");
writer.write("(() => Promise.resolve(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE))");
});
break;
case NODE:
runtimeConfigs.put("accountIdEndpointMode", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addDependency(AwsDependency.AWS_SDK_CORE);
// TODO: change to addImportSubmodule when available
writer.addImport("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS",
"NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS",
"@aws-sdk/core/account-id-endpoint");
writer.write(
"loadNodeConfig(NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS)");
});
break;
default:
LOGGER.warning("AccountIdEndpointMode config not supported for target: " + target);
break;
}
}
}
}
return runtimeConfigs;
}
}
private boolean hasAccountIdEndpointParam(ServiceShape service) {
Optional<EndpointRuleSetTrait> endpointRuleSetTrait = service.getTrait(EndpointRuleSetTrait.class);
if (endpointRuleSetTrait.isPresent()) {
RuleSetParameterFinder ruleSetParameterFinder = new RuleSetParameterFinder(service);
if (ruleSetParameterFinder.getBuiltInParams().containsKey("AccountIdEndpointMode")) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export interface AccountIdEndpointModeResolvedConfig {
export const resolveAccountIdEndpointModeConfig = <T>(
input: T & AccountIdEndpointModeInputConfig & PreviouslyResolved
): T & AccountIdEndpointModeResolvedConfig => {
const accountIdEndpointModeProvider = normalizeProvider(
input.accountIdEndpointMode ?? DEFAULT_ACCOUNT_ID_ENDPOINT_MODE
);
return {
...input,
accountIdEndpointMode: async () => {
const accountIdEndpointModeProvider = normalizeProvider(
input.accountIdEndpointMode ?? DEFAULT_ACCOUNT_ID_ENDPOINT_MODE
);
const accIdMode = await accountIdEndpointModeProvider();
if (!validateAccountIdEndpointMode(accIdMode)) {
throw new Error(
Expand Down

0 comments on commit fc14586

Please sign in to comment.