Skip to content

Commit

Permalink
feat: getParameters fn
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamorosi committed Dec 13, 2022
1 parent 580634c commit 642b9bb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
13 changes: 11 additions & 2 deletions packages/parameters/src/SSMProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseProvider, DEFAULT_PROVIDERS } from './BaseProvider';
import { SSMClient, GetParameterCommand, paginateGetParametersByPath } from '@aws-sdk/client-ssm';
import type { SSMClientConfig, GetParameterCommandInput, GetParametersByPathCommandInput } from '@aws-sdk/client-ssm';
import type { SSMGetOptionsInterface } from 'types/SSMProvider';
import { PaginationConfiguration } from '@aws-sdk/types';
import type { SSMGetMultipleOptionsInterface, SSMGetOptionsInterface } from 'types/SSMProvider';
import type { PaginationConfiguration } from '@aws-sdk/types';

class SSMProvider extends BaseProvider {
public client: SSMClient;
Expand Down Expand Up @@ -71,7 +71,16 @@ const getParameter = (name: string, options?: SSMGetOptionsInterface): Promise<u
return DEFAULT_PROVIDERS.ssm.get(name, options);
};

const getParameters = (path: string, options?: SSMGetMultipleOptionsInterface): Promise<undefined | Record<string, unknown>> => {
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
DEFAULT_PROVIDERS.ssm = new SSMProvider();
}

return DEFAULT_PROVIDERS.ssm.getMultiple(path, options);
};

export {
SSMProvider,
getParameter,
getParameters,
};
8 changes: 4 additions & 4 deletions packages/parameters/tests/unit/SSMProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ describe('Class: SSMProvider', () => {
mockClient(SSMClient).on(GetParametersByPathCommand)
.resolvesOnce({
Parameters: [{
'Name':'/foo/bar',
'Value':'bar',
Name:'/foo/bar',
Value:'bar',
}],
NextToken: 'someToken',
})
.resolves({
Parameters: [{
'Name':'/foo/baz',
'Value':'baz',
Name:'/foo/baz',
Value:'baz',
}]
});
const parameterPath = '/foo';
Expand Down
71 changes: 71 additions & 0 deletions packages/parameters/tests/unit/getParameters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Test getParameters function
*
* @group unit/parameters/SSMProvider/getParameters/function
*/
import { DEFAULT_PROVIDERS } from '../../src/BaseProvider';
import { SSMProvider, getParameters } from '../../src/SSMProvider';
import { SSMClient, GetParametersByPathCommand } from '@aws-sdk/client-ssm';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';

describe('Function: getParameters', () => {

beforeEach(() => {
jest.clearAllMocks();
});

test('when called and a default provider doesn\'t exist, it instantiates one and returns the value', async () => {

// Prepare
const parameterPath = '/foo';
const parameterValue = 'bar';
const client = mockClient(SSMClient).on(GetParametersByPathCommand).resolves({
Parameters: [{
Name: '/foo/bar',
Value: parameterValue,
}],
});

// Act
const parameters = await getParameters(parameterPath);

// Assess
expect(client).toReceiveCommandWith(GetParametersByPathCommand, {
Path: parameterPath,
});
expect(parameters).toEqual({
bar: parameterValue,
});

});

test('when called and a default provider exists, it uses it and returns the value', async () => {

// Prepare
const provider = new SSMProvider();
DEFAULT_PROVIDERS.ssm = provider;
const parameterPath = '/foo';
const parameterValue = 'bar';
const client = mockClient(SSMClient).on(GetParametersByPathCommand).resolves({
Parameters: [{
Name: '/foo/bar',
Value: parameterValue,
}],
});

// Act
const parameters = await getParameters(parameterPath);

// Assess
expect(client).toReceiveCommandWith(GetParametersByPathCommand, {
Path: parameterPath,
});
expect(parameters).toEqual({
'bar': parameterValue,
});
expect(DEFAULT_PROVIDERS.ssm).toBe(provider);

});

});

0 comments on commit 642b9bb

Please sign in to comment.