-
Notifications
You must be signed in to change notification settings - Fork 156
feat(parameters): AppConfigProvider #1200
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
dreamorosi
merged 11 commits into
aws-powertools:main
from
shdq:1177-feature-implement-appconfig-provider
Jan 6, 2023
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ad876a4
feat(parameters): add appconfig provider with types
shdq 58cecef
feat: save the latest retrived configuration
shdq f36b06f
refactor: refactor interface
shdq bdfc9b8
Merge branch 'awslabs:main' into 1177-feature-implement-appconfig-pro…
shdq 9a82e7b
Merge remote-tracking branch 'origin/1177-feature-implement-appconfig…
shdq 184d4b3
feat: AppConfigProvider
shdq 96434b4
feat(appconfig): add getAddConfig utility function, types, and tests
shdq 6298d2a
feat(appconfig): update AppConfigProvider, tests
shdq c4c62cd
Merge remote-tracking branch 'origin/main' into 1177-feature-implemen…
shdq f6025a4
resolve merge conflicts
shdq 5f20b0a
fix interface
shdq 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,115 @@ | ||
import { BaseProvider, DEFAULT_PROVIDERS } from './BaseProvider'; | ||
import { | ||
AppConfigDataClient, | ||
StartConfigurationSessionCommand, | ||
GetLatestConfigurationCommand, | ||
} from '@aws-sdk/client-appconfigdata'; | ||
import type { | ||
StartConfigurationSessionCommandInput, | ||
GetLatestConfigurationCommandInput, | ||
} from '@aws-sdk/client-appconfigdata'; | ||
import type { AppConfigGetOptionsInterface } from './types/AppConfigProvider'; | ||
|
||
class AppConfigProvider extends BaseProvider { | ||
public client: AppConfigDataClient; | ||
private application: string; | ||
private environment: string; | ||
private latestConfiguration: Uint8Array | undefined; | ||
private token: string | undefined; | ||
|
||
/** | ||
* It initializes the AppConfigProvider class'. | ||
* * | ||
* @param {AppConfigGetOptionsInterface} config | ||
*/ | ||
public constructor(options: AppConfigGetOptionsInterface) { | ||
super(); | ||
this.client = new AppConfigDataClient(options.clientConfig || {}); | ||
this.application = options?.sdkOptions?.application || 'app_undefined'; // TODO: make it optional when we add retrieving from env var | ||
this.environment = options?.sdkOptions?.environment || 'env_undefined'; | ||
} | ||
|
||
public async get(name: string, options?: AppConfigGetOptionsInterface): Promise<undefined | string | Uint8Array | Record<string, unknown>> { | ||
return super.get(name, options); | ||
} | ||
|
||
/** | ||
* Retrieve a parameter value from AWS App config. | ||
* | ||
* @param {string} name - Name of the configuration | ||
* @param {AppConfigGetOptionsInterface} options - SDK options to propagate to `StartConfigurationSession` API call | ||
* @returns {Promise<Uint8Array | undefined>} | ||
*/ | ||
protected async _get( | ||
name: string, | ||
options?: AppConfigGetOptionsInterface | ||
): Promise<Uint8Array | undefined> { | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* The new AppConfig APIs require two API calls to return the configuration | ||
* First we start the session and after that we retrieve the configuration | ||
* We need to store the token to use in the next execution | ||
**/ | ||
if (!this.token) { | ||
const sessionOptions: StartConfigurationSessionCommandInput = { | ||
ConfigurationProfileIdentifier: name, | ||
EnvironmentIdentifier: this.application, | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ApplicationIdentifier: this.environment, | ||
}; | ||
|
||
if (options?.sdkOptions) { | ||
Object.assign(sessionOptions, options.sdkOptions); | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const sessionCommand = new StartConfigurationSessionCommand( | ||
sessionOptions | ||
); | ||
|
||
const session = await this.client.send(sessionCommand); | ||
this.token = session.InitialConfigurationToken; | ||
} | ||
|
||
const getConfigurationCommand = new GetLatestConfigurationCommand({ | ||
ConfigurationToken: this.token, | ||
}); | ||
const response = await this.client.send(getConfigurationCommand); | ||
|
||
this.token = response.NextPollConfigurationToken; | ||
|
||
const configuration = response.Configuration; | ||
|
||
if (configuration) { | ||
this.latestConfiguration = configuration; | ||
} | ||
|
||
return this.latestConfiguration; | ||
} | ||
|
||
/** | ||
* Retrieving multiple parameter values is not supported with AWS App Config Provider. | ||
* | ||
* @throws Not Implemented Error. | ||
*/ | ||
protected async _getMultiple( | ||
_path: string, | ||
_sdkOptions?: Partial<GetLatestConfigurationCommandInput> | ||
shdq marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): Promise<Record<string, string | undefined>> { | ||
return this._notImplementedError(); | ||
} | ||
|
||
private _notImplementedError(): never { | ||
throw new Error('Not Implemented'); | ||
} | ||
} | ||
|
||
const getAppConfig = ( | ||
name: string, | ||
options: AppConfigGetOptionsInterface | ||
): Promise<undefined | string | Uint8Array | Record<string, unknown>> => { | ||
if (!DEFAULT_PROVIDERS.hasOwnProperty('appconfig')) { | ||
DEFAULT_PROVIDERS.appconfig = new AppConfigProvider(options); | ||
} | ||
|
||
return DEFAULT_PROVIDERS.appconfig.get(name, options); | ||
}; | ||
|
||
export { AppConfigProvider, getAppConfig }; |
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,23 @@ | ||
import type { | ||
AppConfigDataClientConfig, | ||
} from '@aws-sdk/client-appconfigdata'; | ||
import type { GetOptionsInterface } from 'types/BaseProvider'; | ||
|
||
/** | ||
* Options for the AppConfigProvider get method. | ||
* | ||
* @interface AppConfigGetOptionsInterface | ||
* @extends {GetOptionsInterface} | ||
* @property {} [clientConfig] - optional configuration to pass during client initialization | ||
* @property {} sdkOptions - required options to start configuration session. | ||
*/ | ||
interface AppConfigGetOptionsInterface | ||
extends Omit<GetOptionsInterface, 'sdkOptions'> { | ||
clientConfig?: AppConfigDataClientConfig | ||
sdkOptions?: { | ||
application: string | ||
environment: string | ||
} | ||
} | ||
|
||
export { AppConfigGetOptionsInterface }; |
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,60 @@ | ||
/** | ||
* Test AppConfigProvider class | ||
* | ||
* @group unit/parameters/AppConfigProvider/class | ||
*/ | ||
import { AppConfigProvider } from '../../src/AppConfigProvider'; | ||
import { | ||
AppConfigDataClient, | ||
StartConfigurationSessionCommand, | ||
GetLatestConfigurationCommand, | ||
} from '@aws-sdk/client-appconfigdata'; | ||
import { mockClient } from 'aws-sdk-client-mock'; | ||
import 'aws-sdk-client-mock-jest'; | ||
|
||
const encoder = new TextEncoder(); | ||
|
||
describe('Class: AppConfigProvider', () => { | ||
const client = mockClient(AppConfigDataClient); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('Method: _get', () => { | ||
test('when called with name and options, it gets binary configuration', async () => { | ||
// Prepare | ||
const options = { | ||
sdkOptions: { | ||
application: 'MyApp', | ||
environment: 'MyAppProdEnv', | ||
}, | ||
}; | ||
const provider = new AppConfigProvider(options); | ||
const name = 'MyAppFeatureFlag'; | ||
|
||
const mockInitialToken = | ||
'AYADeNgfsRxdKiJ37A12OZ9vN2cAXwABABVhd3MtY3J5cHRvLXB1YmxpYy1rZXkAREF1RzlLMTg1Tkx2Wjk4OGV2UXkyQ1'; | ||
const mockNextToken = | ||
'ImRmyljpZnxt7FfxeEOE5H8xQF1SfOlWZFnHujbzJmIvNeSAAA8/qA9ivK0ElRMwpvx96damGxt125XtMkmYf6a0OWSqnBw=='; | ||
const mockData = encoder.encode('myAppConfiguration'); | ||
|
||
client | ||
.on(StartConfigurationSessionCommand) | ||
.resolves({ | ||
InitialConfigurationToken: mockInitialToken, | ||
}) | ||
.on(GetLatestConfigurationCommand) | ||
.resolves({ | ||
Configuration: mockData, | ||
NextPollConfigurationToken: mockNextToken, | ||
}); | ||
|
||
// Act | ||
const result = await provider.get(name); | ||
|
||
// Assess | ||
expect(result).toBe(mockData); | ||
}); | ||
}); | ||
}); |
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.