-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat(Model): fetch tableName from (SSM) Parameter Store #291
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a407fc0
feat(Model): fetch tableName from (SSM) Parameter Store
SajidHamza9 8b5b70d
fix: fix tests & rewrite SSMParam decorator
SajidHamza9 768cd00
fix: add missing dependency
SajidHamza9 dc03695
docs(readme): add SSM parameter feature
SajidHamza9 639d2e2
fix(batchWrite): await tableName
SajidHamza9 dd3abe7
Merge branch 'main' into feat/280-ssm-parameter
ThibaultRuby 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,11 @@ | ||
import { HashKeyEntity } from "./hashkey"; | ||
import documentClient from './common'; | ||
; | ||
import Model from "../../src/base-model"; | ||
export default class SSMParamModel extends Model<HashKeyEntity> { | ||
protected tableName = 'arn:aws:ssm:us-east-1:617599655210:parameter/tableName'; | ||
|
||
protected pk = 'hashkey'; | ||
|
||
protected documentClient = documentClient; | ||
} |
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,75 @@ | ||
import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm'; | ||
import { clearTables } from './hooks/create-tables'; | ||
import * as modelApi from '../src/base-model'; | ||
import SSMParamModel from './models/ssm-param'; | ||
|
||
jest.mock('@aws-sdk/client-ssm'); | ||
|
||
describe('fetchTableName', () => { | ||
afterEach(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('should throw an error', async () => { | ||
const mockSend = jest.fn(); | ||
SSMClient.prototype.send = mockSend; | ||
mockSend.mockRejectedValueOnce(new Error()); | ||
try { | ||
await modelApi.fetchTableName('arn:aws:ssm:us-east-1:617599655210:parameter/tableName'); | ||
} catch (e) { | ||
expect((e as Error).message.includes('Invalid SSM Parameter')).toBe(true); | ||
} | ||
}); | ||
test('should fetch tableName from SSM parameter store', async () => { | ||
const mockSend = jest.fn(); | ||
mockSend.mockResolvedValueOnce({ Parameter: { Value: 'table_test_hashkey' } }); | ||
SSMClient.prototype.send = mockSend; | ||
const tableName = await modelApi.fetchTableName('arn:aws:ssm:us-east-1:617599655210:parameter/tableName'); | ||
expect(GetParameterCommand).toHaveBeenCalledWith({ Name: 'tableName' }); | ||
expect(mockSend).toHaveBeenCalledWith(expect.any(GetParameterCommand)); | ||
expect(tableName).toEqual('table_test_hashkey'); | ||
}); | ||
test('should return the provided tableName', async () => { | ||
const mockSend = jest.fn(); | ||
SSMClient.prototype.send = mockSend; | ||
const mockResponse = { Parameter: { Value: 'table_test_hashkey' } }; | ||
mockSend.mockResolvedValueOnce(mockResponse); | ||
const tableName = await modelApi.fetchTableName('tableName'); | ||
expect(mockSend).not.toHaveBeenCalled(); | ||
expect(GetParameterCommand).not.toHaveBeenCalled(); | ||
expect(tableName).toEqual('tableName'); | ||
}); | ||
}); | ||
|
||
describe('SSM parameter ARN', () => { | ||
const item = { | ||
hashkey: 'bar', | ||
string: 'whatever', | ||
stringmap: { foo: 'bar' }, | ||
stringset: ['bar, bar'], | ||
number: 43, | ||
bool: true, | ||
list: ['foo', 42], | ||
}; | ||
beforeEach(async () => { | ||
const mockSend = jest.fn(); | ||
mockSend.mockResolvedValueOnce({ Parameter: { Value: 'table_test_hashkey' } }); | ||
SSMClient.prototype.send = mockSend; | ||
await clearTables(); | ||
}); | ||
afterEach(async () => { | ||
jest.clearAllMocks(); | ||
}); | ||
test('Should fetch tableName and save the item', async () => { | ||
const model = new SSMParamModel(); | ||
await model.save(item); | ||
const saved = await model.get('bar'); | ||
expect(saved).toEqual(item); | ||
}); | ||
test('Should fetch tableName once and cache its value for subsequent requests', async () => { | ||
jest.spyOn(modelApi, 'fetchTableName'); | ||
const model = new SSMParamModel(); | ||
await model.save(item); | ||
await model.save(item); | ||
expect(modelApi.fetchTableName).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use case for a decorator 💯