-
Notifications
You must be signed in to change notification settings - Fork 618
feat(core): add AccountIdEndpointMode config option #6036
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8ac04e5
feat(core): add AccountIdEndpointMode config
siddsriv 21ae9eb
chore(core): add deps for @aws-sdk/core package.json
siddsriv 5c03ea1
chore(codegen): minor refactor and imports addition
siddsriv 56fc147
chore(core): refactor accountId for submodules
siddsriv 006fb9b
chore(core): refactor update
siddsriv 96f4a31
Merge branch 'main' into accId-config
siddsriv eca886e
chore(core): rm blankspace
siddsriv 3323360
chore(core): package.json update
siddsriv 02b7909
fix(codegen): minor fixes for accIdEpMode code generator
siddsriv 704a66c
chore(core): accountId module export field
siddsriv 7fc38b3
chore(core): add accountId dir entry point
siddsriv 7ce0b61
chore(codegen): checkstyle fixes
siddsriv c9adc06
chore(codegen): checkstyle fixes
siddsriv 66f4181
chore(core): tsconfig files updates
siddsriv 1189e97
chore: checkstyle fix
siddsriv 665331c
chore(core): add core deps
siddsriv 034c256
chore(codegen): add TypeScript Integration for codegen
siddsriv b9469d5
chore(codegen): add ad-hoc import for account-id-endpoints in core
siddsriv f804aef
chore(codegen): ci fix
siddsriv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
.../software/amazon/smithy/aws/typescript/codegen/AddAccountIdEndpointModeRuntimeConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
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 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; | ||
|
||
siddsriv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* 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); | ||
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"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@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; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
/** | ||
* Do not edit: | ||
* This is a compatibility redirect for contexts that do not understand package.json exports field. | ||
*/ | ||
module.exports = require("./dist-cjs/submodules/account-id-endpoint/index.js"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/core/src/submodules/account-id-endpoint/AccountIdEndpointModeConfigResolver.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Provider } from "@smithy/types"; | ||
import { normalizeProvider } from "@smithy/util-middleware"; | ||
|
||
import { | ||
AccountIdEndpointMode, | ||
DEFAULT_ACCOUNT_ID_ENDPOINT_MODE, | ||
validateAccountIdEndpointMode, | ||
} from "./AccountIdEndpointModeConstants"; | ||
|
||
/** | ||
* @public | ||
*/ | ||
export interface AccountIdEndpointModeInputConfig { | ||
/** | ||
* The account ID endpoint mode to use. | ||
*/ | ||
accountIdEndpointMode?: AccountIdEndpointMode | Provider<AccountIdEndpointMode>; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface PreviouslyResolved {} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface AccountIdEndpointModeResolvedConfig { | ||
/** | ||
* Resolved value for input config {config.accountIdEndpointMode} | ||
*/ | ||
accountIdEndpointMode: Provider<AccountIdEndpointMode>; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const resolveAccountIdEndpointModeConfig = <T>( | ||
input: T & AccountIdEndpointModeInputConfig & PreviouslyResolved | ||
): T & AccountIdEndpointModeResolvedConfig => { | ||
return { | ||
...input, | ||
accountIdEndpointMode: async () => { | ||
const accountIdEndpointModeProvider = normalizeProvider( | ||
input.accountIdEndpointMode ?? DEFAULT_ACCOUNT_ID_ENDPOINT_MODE | ||
); | ||
const accIdMode = await accountIdEndpointModeProvider(); | ||
if (!validateAccountIdEndpointMode(accIdMode)) { | ||
throw new Error( | ||
`Invalid value for accountIdEndpointMode: ${accIdMode}. Valid values are: "required", "preferred", "disabled".` | ||
); | ||
} | ||
return accIdMode; | ||
}, | ||
}; | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/core/src/submodules/account-id-endpoint/AccountIdEndpointModeConstants.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { validateAccountIdEndpointMode } from "./AccountIdEndpointModeConstants"; | ||
|
||
describe("validateAccountIdEndpointMode", () => { | ||
it('should return true for "disabled"', () => { | ||
const result = validateAccountIdEndpointMode("disabled"); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true for "preferred"', () => { | ||
const result = validateAccountIdEndpointMode("preferred"); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true for "required"', () => { | ||
const result = validateAccountIdEndpointMode("required"); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it("should return false for an invalid value", () => { | ||
const result = validateAccountIdEndpointMode("invalidValue"); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return false for an empty string", () => { | ||
const result = validateAccountIdEndpointMode(""); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return false for a number", () => { | ||
const result = validateAccountIdEndpointMode(123); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return false for null", () => { | ||
const result = validateAccountIdEndpointMode(null); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it("should return false for undefined", () => { | ||
const result = validateAccountIdEndpointMode(undefined); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/core/src/submodules/account-id-endpoint/AccountIdEndpointModeConstants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export type AccountIdEndpointMode = "disabled" | "preferred" | "required"; | ||
|
||
export const DEFAULT_ACCOUNT_ID_ENDPOINT_MODE = "preferred"; | ||
|
||
export const ACCOUNT_ID_ENDPOINT_MODE_VALUES: AccountIdEndpointMode[] = ["disabled", "preferred", "required"]; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function validateAccountIdEndpointMode(value: any): value is AccountIdEndpointMode { | ||
return ACCOUNT_ID_ENDPOINT_MODE_VALUES.includes(value); | ||
} |
68 changes: 68 additions & 0 deletions
68
...es/core/src/submodules/account-id-endpoint/NodeAccountIdEndpointModeConfigOptions.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { DEFAULT_ACCOUNT_ID_ENDPOINT_MODE } from "./AccountIdEndpointModeConstants"; | ||
import { | ||
CONFIG_ACCOUNT_ID_ENDPOINT_MODE, | ||
ENV_ACCOUNT_ID_ENDPOINT_MODE, | ||
NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS, | ||
} from "./NodeAccountIdEndpointModeConfigOptions"; | ||
|
||
describe("NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS", () => { | ||
const originalEnv = process.env; | ||
|
||
beforeEach(() => { | ||
jest.resetModules(); | ||
process.env = { ...originalEnv }; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
describe("environmentVariableSelector", () => { | ||
it("should return the value set in environment variables", () => { | ||
const testValue = "preferred"; | ||
process.env[ENV_ACCOUNT_ID_ENDPOINT_MODE] = testValue; | ||
const selector = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.environmentVariableSelector; | ||
expect(selector(process.env)).toEqual(testValue); | ||
}); | ||
|
||
it("should throw an error if the environment variable is set to an invalid value", () => { | ||
process.env[ENV_ACCOUNT_ID_ENDPOINT_MODE] = "InvalidValue"; | ||
const selector = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.environmentVariableSelector; | ||
expect(() => selector(process.env)).toThrow("Invalid AccountIdEndpointMode value"); | ||
}); | ||
|
||
it("should not throw an error if the environment variable is not set", () => { | ||
delete process.env[ENV_ACCOUNT_ID_ENDPOINT_MODE]; | ||
const selector = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.environmentVariableSelector; | ||
expect(() => selector(process.env)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe("configFileSelector", () => { | ||
it("should return the value set in the configuration file", () => { | ||
const testValue = "required"; | ||
const profile = { [CONFIG_ACCOUNT_ID_ENDPOINT_MODE]: testValue }; | ||
const selector = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.configFileSelector; | ||
expect(selector(profile)).toEqual(testValue); | ||
}); | ||
|
||
it("should throw an error if the configuration file contains an invalid value", () => { | ||
const profile = { [CONFIG_ACCOUNT_ID_ENDPOINT_MODE]: "InvalidValue" }; | ||
const selector = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.configFileSelector; | ||
expect(() => selector(profile)).toThrow("Invalid AccountIdEndpointMode value"); | ||
}); | ||
|
||
it("should not throw an error if the configuration file does not contain the setting", () => { | ||
const profile = {}; | ||
const selector = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.configFileSelector; | ||
expect(() => selector(profile)).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe("default", () => { | ||
it("should return the default value", () => { | ||
const defaultValue = NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS.default; | ||
expect(defaultValue).toEqual(DEFAULT_ACCOUNT_ID_ENDPOINT_MODE); | ||
}); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/core/src/submodules/account-id-endpoint/NodeAccountIdEndpointModeConfigOptions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { LoadedConfigSelectors } from "@smithy/node-config-provider"; | ||
|
||
import { | ||
AccountIdEndpointMode, | ||
DEFAULT_ACCOUNT_ID_ENDPOINT_MODE, | ||
validateAccountIdEndpointMode, | ||
} from "./AccountIdEndpointModeConstants"; | ||
|
||
const err = "Invalid AccountIdEndpointMode value"; | ||
|
||
const _throw = (message: string): never => { | ||
throw new Error(message); | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ENV_ACCOUNT_ID_ENDPOINT_MODE = "AWS_ACCOUNT_ID_ENDPOINT_MODE"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const CONFIG_ACCOUNT_ID_ENDPOINT_MODE = "account_id_endpoint_mode"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const NODE_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_OPTIONS: LoadedConfigSelectors<AccountIdEndpointMode> = { | ||
environmentVariableSelector: (env) => { | ||
const value = env[ENV_ACCOUNT_ID_ENDPOINT_MODE]; | ||
if (value && !validateAccountIdEndpointMode(value)) { | ||
_throw(err); | ||
} | ||
return value as AccountIdEndpointMode; | ||
}, | ||
configFileSelector: (profile) => { | ||
const value = profile[CONFIG_ACCOUNT_ID_ENDPOINT_MODE]; | ||
if (value && !validateAccountIdEndpointMode(value)) { | ||
_throw(err); | ||
} | ||
return value as AccountIdEndpointMode; | ||
}, | ||
default: DEFAULT_ACCOUNT_ID_ENDPOINT_MODE, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# @aws-sdk/core/account-id-endpoint | ||
|
||
> An internal package | ||
|
||
This submodule provides functionality for AccountId based endpoint routing. | ||
|
||
## Usage | ||
|
||
You probably shouldn't, at least directly. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.