-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read service specific endpoints from env/config (#1014)
- Loading branch information
Showing
9 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"@smithy/middleware-endpoint": minor | ||
--- | ||
|
||
Read service specific endpoints from env/config |
This file contains 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
1 change: 1 addition & 0 deletions
1
packages/middleware-endpoint/src/adaptors/getEndpointFromConfig.browser.ts
This file contains 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 @@ | ||
export const getEndpointFromConfig = async (serviceId: string) => undefined; |
5 changes: 5 additions & 0 deletions
5
packages/middleware-endpoint/src/adaptors/getEndpointFromConfig.ts
This file contains 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,5 @@ | ||
import { loadConfig } from "@smithy/node-config-provider"; | ||
|
||
import { getEndpointUrlConfig } from "./getEndpointUrlConfig"; | ||
|
||
export const getEndpointFromConfig = async (serviceId: string) => loadConfig(getEndpointUrlConfig(serviceId))(); |
This file contains 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
89 changes: 89 additions & 0 deletions
89
packages/middleware-endpoint/src/adaptors/getEndpointUrlConfig.spec.ts
This file contains 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,89 @@ | ||
import { CONFIG_PREFIX_SEPARATOR } from "@smithy/shared-ini-file-loader"; | ||
|
||
import { getEndpointUrlConfig } from "./getEndpointUrlConfig"; | ||
|
||
const ENV_ENDPOINT_URL = "AWS_ENDPOINT_URL"; | ||
const CONFIG_ENDPOINT_URL = "endpoint_url"; | ||
|
||
describe(getEndpointUrlConfig.name, () => { | ||
const serviceId = "foo"; | ||
const endpointUrlConfig = getEndpointUrlConfig(serviceId); | ||
|
||
const mockEndpoint = "https://mock-endpoint.com"; | ||
const ORIGINAL_ENV = process.env; | ||
|
||
beforeEach(() => { | ||
process.env = {}; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = ORIGINAL_ENV; | ||
}); | ||
|
||
describe("environmentVariableSelector", () => { | ||
beforeEach(() => { | ||
process.env[ENV_ENDPOINT_URL] = mockEndpoint; | ||
}); | ||
|
||
it.each([ | ||
["foo", `${ENV_ENDPOINT_URL}_FOO`], | ||
["foobar", `${ENV_ENDPOINT_URL}_FOOBAR`], | ||
["foo bar", `${ENV_ENDPOINT_URL}_FOO_BAR`], | ||
])("returns endpoint for '%s' from environment variable %s", (serviceId, envKey) => { | ||
const serviceMockEndpoint = `${mockEndpoint}/${envKey}`; | ||
process.env[envKey] = serviceMockEndpoint; | ||
|
||
const endpointUrlConfig = getEndpointUrlConfig(serviceId); | ||
expect(endpointUrlConfig.environmentVariableSelector(process.env)).toEqual(serviceMockEndpoint); | ||
}); | ||
|
||
it(`returns endpoint from environment variable ${ENV_ENDPOINT_URL}`, () => { | ||
expect(endpointUrlConfig.environmentVariableSelector(process.env)).toEqual(mockEndpoint); | ||
}); | ||
|
||
it("returns undefined, if endpoint not available in environment variables", () => { | ||
process.env[ENV_ENDPOINT_URL] = undefined; | ||
expect(endpointUrlConfig.environmentVariableSelector(process.env)).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("configFileSelector", () => { | ||
it.each([ | ||
["foo", "foo"], | ||
["foobar", "foobar"], | ||
["foo bar", "foo_bar"], | ||
])("returns endpoint for '%s' from config file '%s'", (serviceId, serviceConfigId) => { | ||
const servicesSectionPrefix = "services"; | ||
const servicesSectionName = "config-services"; | ||
const serviceMockEndpoint = `${mockEndpoint}/${serviceConfigId}`; | ||
|
||
const profile = { | ||
[servicesSectionPrefix]: servicesSectionName, | ||
[CONFIG_ENDPOINT_URL]: mockEndpoint, | ||
}; | ||
|
||
const config = { | ||
[serviceId]: profile, | ||
[[servicesSectionPrefix, servicesSectionName].join(CONFIG_PREFIX_SEPARATOR)]: { | ||
[[serviceConfigId, CONFIG_ENDPOINT_URL].join(CONFIG_PREFIX_SEPARATOR)]: serviceMockEndpoint, | ||
}, | ||
}; | ||
|
||
const endpointUrlConfig = getEndpointUrlConfig(serviceId); | ||
expect(endpointUrlConfig.configFileSelector(profile, config)).toEqual(serviceMockEndpoint); | ||
}); | ||
|
||
it(`returns endpoint from config ${CONFIG_ENDPOINT_URL}`, () => { | ||
const profile = { [CONFIG_ENDPOINT_URL]: mockEndpoint }; | ||
expect(endpointUrlConfig.configFileSelector(profile)).toEqual(mockEndpoint); | ||
}); | ||
|
||
it("returns undefined, if endpoint not available in config", () => { | ||
expect(endpointUrlConfig.environmentVariableSelector({})).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
it("returns undefined by default", () => { | ||
expect(endpointUrlConfig.default).toBeUndefined(); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
packages/middleware-endpoint/src/adaptors/getEndpointUrlConfig.ts
This file contains 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,41 @@ | ||
import { LoadedConfigSelectors } from "@smithy/node-config-provider"; | ||
import { CONFIG_PREFIX_SEPARATOR } from "@smithy/shared-ini-file-loader"; | ||
|
||
const ENV_ENDPOINT_URL = "AWS_ENDPOINT_URL"; | ||
const CONFIG_ENDPOINT_URL = "endpoint_url"; | ||
|
||
export const getEndpointUrlConfig = (serviceId: string): LoadedConfigSelectors<string | undefined> => ({ | ||
environmentVariableSelector: (env) => { | ||
// The value provided by a service-specific environment variable. | ||
const serviceSuffixParts = serviceId.split(" ").map((w) => w.toUpperCase()); | ||
const serviceEndpointUrl = env[[ENV_ENDPOINT_URL, ...serviceSuffixParts].join("_")]; | ||
if (serviceEndpointUrl) return serviceEndpointUrl; | ||
|
||
// The value provided by the global endpoint environment variable. | ||
const endpointUrl = env[ENV_ENDPOINT_URL]; | ||
if (endpointUrl) return endpointUrl; | ||
|
||
return undefined; | ||
}, | ||
|
||
configFileSelector: (profile, config) => { | ||
// The value provided by a service-specific parameter from a services definition section | ||
if (config && profile.services) { | ||
const servicesSection = config[["services", profile.services].join(CONFIG_PREFIX_SEPARATOR)]; | ||
if (servicesSection) { | ||
const servicePrefixParts = serviceId.split(" ").map((w) => w.toLowerCase()); | ||
const endpointUrl = | ||
servicesSection[[servicePrefixParts.join("_"), CONFIG_ENDPOINT_URL].join(CONFIG_PREFIX_SEPARATOR)]; | ||
if (endpointUrl) return endpointUrl; | ||
} | ||
} | ||
|
||
// The value provided by the global parameter from a profile in the shared configuration file. | ||
const endpointUrl = profile[CONFIG_ENDPOINT_URL]; | ||
if (endpointUrl) return endpointUrl; | ||
|
||
return undefined; | ||
}, | ||
|
||
default: undefined, | ||
}); |
This file contains 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 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