Skip to content

Commit

Permalink
Simplify SSM's parameter API to avoid proxy object
Browse files Browse the repository at this point in the history
  • Loading branch information
oleiade committed Oct 17, 2022
1 parent 93893ee commit 3dff3e5
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export default function () {

// Let's get its value
// getParameter returns an parameter object: e.g. {parameter: {name: string, value: string...}}
const parameter = systemsManager.getParameter(testParameterName).parameter // directly assign the parameter contents
const parameter = systemsManager.getParameter(testParameterName)
if (parameter.value !== testParameterValue) {
exec.test.abort('test parameter not found')
}
Expand Down
2 changes: 1 addition & 1 deletion build/aws.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/aws.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/kms.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ssm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/ssm.min.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions examples/ssm.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ export default function () {
// Currently the parameter needs to be created before hand

// Let's get its value
// getParameter returns an parameter object: e.g. {parameter: {name: string, value: string...}}
const parameter = systemsManager.getParameter(testParameterName).parameter // directly assign the parameter contents
// getParameter returns a parameter object: e.g. {name: string, value: string...}
const parameter = systemsManager.getParameter(testParameterName)
if (parameter.value !== testParameterValue) {
exec.test.abort('test parameter not found')
}

// Let's get the secret value with decryption
// destructure the parameter object to get to the values you want
const {
parameter: { value: encryptedParameterValue },
} = systemsManager.getParameter(testParameterSecretName, true)
const { value: encryptedParameterValue } = systemsManager.getParameter(
testParameterSecretName,
true
)
if (encryptedParameterValue !== testParameterSecretValue) {
exec.test.abort('encrypted test parameter not found')
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/kms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class KMSServiceError extends AWSError {
*/
enum KMSOperation {
GenerateDataKey = 'GenerateDataKey',
ListKeys = 'ListKeys'
ListKeys = 'ListKeys',
}

/**
Expand Down
90 changes: 51 additions & 39 deletions src/internal/ssm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class SystemsManagerClient extends AWSClient {
const res = http.request(this.method, signedRequest.url, body, {
headers: signedRequest.headers,
})
this._handle_error(SystemsManagerOperation.GetParameter, res);
this._handle_error(SystemsManagerOperation.GetParameter, res)

return SystemsManagerParameter.fromJSON(res.json() as JSONObject)
}
Expand Down Expand Up @@ -107,46 +107,56 @@ export class SystemsManagerClient extends AWSClient {
* Class representing a Systems Manager's Parameter
*/
export class SystemsManagerParameter {
parameter: SystemsManagerParameterItem

/**
* Constructs a Systems Manager's Parameter
*
* @param {JSONObject} parameter - The response object content for the AWS System Manager.
* The Amazon Resource Name (ARN) of the parameter.
*/
constructor(parameter: JSONObject) {
this.parameter = SystemsManagerParameterItem.fromJSON(parameter)
}
arn: string

/**
* Parses and constructs a Systems Manager's Parameter from the content
* of a JSON response returned by the AWS service
*
* @param {Object} json - JSON object as returned and parsed from
* the AWS service's API call.
* @returns {SystemsManagerParameter}
* The data type of the parameter, such as text or aws:ec2:image.
* The default is text.
*/
static fromJSON(json: JSONObject): SystemsManagerParameter {
return new SystemsManagerParameter(json.Parameter as JSONObject)
}
}

/**
* Class representing a Systems Manager's Parameter Object content
*/
class SystemsManagerParameterItem {
arn: string
dataType: string

/**
* Date the parameter was last changed or updated and the parameter version was created.
*/
lastModifiedDate: number

/**
* The friendly name of the parameter.
*/
name: string

/**
* Either the version number or the label used to retrieve the parameter value. Specify selectors by using one of the following formats:
* parameter_name:version
* parameter_name:label
*/
selector: string

/**
* plies to parameters that reference information in other AWS services. SourceResult is the raw result or response from the source.
*/
sourceResult: string

/**
* The type of parameter. Valid values include the following: String, StringList, and SecureString.
*/
type: string

/**
* The parameter value.
*/
value: string

/**
* The parameter version.
*/
version: number

/**
* Constructs a Systems Manager's Parameter object contents
* Constructs a Systems Manager's Parameter
*
* @param {string} arn - The Amazon Resource Name (ARN) of the parameter.
* @param {string} dataType - The data type of the parameter, such as text or aws:ec2:image. The default is text.
Expand Down Expand Up @@ -183,24 +193,26 @@ class SystemsManagerParameterItem {
}

/**
* Parses and constructs a Systems Manager's Parameter object from the content
* Parses and constructs a Systems Manager's Parameter from the content
* of a JSON response returned by the AWS service
*
* @param {Object} json - JSON object as returned and parsed from
* the AWS service's API call.
* @returns {SystemsManagerParameterItem}
* @returns {SystemsManagerParameter}
*/
static fromJSON(json: JSONObject): SystemsManagerParameterItem {
return new SystemsManagerParameterItem(
json.ARN as string,
json.DataType as string,
json.LastModifiedDate as number,
json.Name as string,
json.Selector as string,
json.SourceResult as string,
json.Type as string,
json.Value as string,
json.Version as number
static fromJSON(json: JSONObject): SystemsManagerParameter {
const parameter = json.Parameter as JSONObject

return new SystemsManagerParameter(
parameter.ARN as string,
parameter.DataType as string,
parameter.LastModifiedDate as number,
parameter.Name as string,
parameter.Selector as string,
parameter.SourceResult as string,
parameter.Type as string,
parameter.Value as string,
parameter.Version as number
)
}
}
Expand Down
18 changes: 10 additions & 8 deletions tests/internal/ssm.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.1/index.js'
import {
SystemsManagerClient,
SystemsManagerServiceError,
} from '../../build/ssm.min.js'
import { SystemsManagerClient, SystemsManagerServiceError } from '../../build/ssm.min.js'

export function ssmTestSuite(data) {
const systemsManagerClient = new SystemsManagerClient(data.awsConfig)

describe('get parameter', () => {
// Act
// getParameter returns an parameter object: e.g. {parameter: {name: string, value: string...}}
const parameterObject = systemsManagerClient.getParameter(data.systemsManager.testParameter.name)
const parameterObject = systemsManagerClient.getParameter(
data.systemsManager.testParameter.name
)
const nonExistingParameterFn = () =>
systemsManagerClient.getParameter('non-existing-parameter')

// Assert
expect(parameterObject).to.be.an('object')
expect(parameterObject.parameter.value).to.be.an('string')
expect(parameterObject.parameter.value).to.equal(data.systemsManager.testParameter.value)
expect(parameterObject.value).to.be.an('string')
expect(parameterObject.value).to.equal(data.systemsManager.testParameter.value)
expect(nonExistingParameterFn).to.throw(SystemsManagerServiceError)
})

describe('get secret parameter', () => {
// Act
// destructure the object to get the values you want directly
const { parameter: { value: parameterValue } } = systemsManagerClient.getParameter(data.systemsManager.testParameterSecret.name, true)
const { value: parameterValue } = systemsManagerClient.getParameter(
data.systemsManager.testParameterSecret.name,
true
)
const nonExistingParameterFn = () =>
systemsManagerClient.getParameter('non-existing-parameter', true)

Expand Down

0 comments on commit 3dff3e5

Please sign in to comment.