Skip to content

Commit

Permalink
refactor(ssm): API cleanups (#2904)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `ParameterStoreString` has been removed. Use `StringParameter.fromStringParameterAttributes`.
* **ssm:** `ParameterStoreSecureString` has been removed. Use `StringParameter.fromSecureStringParameterAttributes`.
* **ssm:** `ParameterOptions.name` was renamed to `parameterName`.
  • Loading branch information
Elad Ben-Israel authored Jun 18, 2019
1 parent 06221ac commit bd1bcf5
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 117 deletions.
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-ssm/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './parameter';
export * from './parameter-store-string';

// AWS::SSM CloudFormation Resources:
export * from './ssm.generated';
90 changes: 0 additions & 90 deletions packages/@aws-cdk/aws-ssm/lib/parameter-store-string.ts

This file was deleted.

73 changes: 65 additions & 8 deletions packages/@aws-cdk/aws-ssm/lib/parameter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import iam = require('@aws-cdk/aws-iam');
import { CfnDynamicReference, CfnDynamicReferenceService, Construct, Fn, IResource, Resource, Stack, Token } from '@aws-cdk/cdk';
import { CfnDynamicReference, CfnDynamicReferenceService, CfnParameter, Construct, Fn, IResource, Resource, Stack, Token } from '@aws-cdk/cdk';
import ssm = require('./ssm.generated');

/**
Expand Down Expand Up @@ -86,9 +86,9 @@ export interface ParameterOptions {
/**
* The name of the parameter.
*
* @default a name will be generated by CloudFormation
* @default - a name will be generated by CloudFormation
*/
readonly name?: string;
readonly parameterName?: string;
}

/**
Expand Down Expand Up @@ -145,22 +145,79 @@ abstract class ParameterBase extends Resource implements IParameter {
}

const STRING_PARAM_TYPE = 'String';
const SECURE_STRING_PARAM_TYPE = 'SecureString';
const STRINGLIST_PARAM_TYPE = 'StringList';

export interface StringParameterAttributes {
/**
* The name of the parameter store value
*/
readonly parameterName: string;

/**
* The version number of the value you wish to retrieve.
*
* @default The latest version will be retrieved.
*/
readonly version?: number;
}

export interface SecureStringParameterAttributes {
/**
* The name of the parameter store value
*/
readonly parameterName: string;

/**
* The version number of the value you wish to retrieve. This is required for secure strings.
*/
readonly version: number;
}

/**
* Creates a new String SSM Parameter.
* @resource AWS::SSM::Parameter
*/
export class StringParameter extends ParameterBase implements IStringParameter {

/**
* Imports an external string parameter.
* Imports an external string parameter by name.
*/
public static fromStringParameterName(scope: Construct, id: string, stringParameterName: string): IStringParameter {
return this.fromStringParameterAttributes(scope, id, { parameterName: stringParameterName });
}

/**
* Imports an external string parameter with name and optional version.
*/
public static fromStringParameterAttributes(scope: Construct, id: string, attrs: StringParameterAttributes): IStringParameter {
if (!attrs.parameterName) {
throw new Error(`parameterName cannot be an empty string`);
}

const stringValue = attrs.version
? new CfnDynamicReference(CfnDynamicReferenceService.Ssm, `${attrs.parameterName}:${attrs.version}`).toString()
: new CfnParameter(scope, `${id}.Parameter`, { type: 'AWS::SSM::Parameter::Value<String>', default: attrs.parameterName }).valueAsString;

class Import extends ParameterBase {
public readonly parameterName = stringParameterName;
public readonly parameterName = attrs.parameterName;
public readonly parameterType = STRING_PARAM_TYPE;
public readonly stringValue = new CfnDynamicReference(CfnDynamicReferenceService.Ssm, stringParameterName).toString();
public readonly stringValue = stringValue;
}

return new Import(scope, id);
}

/**
* Imports a secure string parameter from the SSM parameter store.
*/
public static fromSecureStringParameterAttributes(scope: Construct, id: string, attrs: SecureStringParameterAttributes): IStringParameter {
const stringValue = new CfnDynamicReference(CfnDynamicReferenceService.SsmSecure, `${attrs.parameterName}:${attrs.version}`).toString();

class Import extends ParameterBase {
public readonly parameterName = attrs.parameterName;
public readonly parameterType = SECURE_STRING_PARAM_TYPE;
public readonly stringValue = stringValue;
}

return new Import(scope, id);
Expand All @@ -180,7 +237,7 @@ export class StringParameter extends ParameterBase implements IStringParameter {
const resource = new ssm.CfnParameter(this, 'Resource', {
allowedPattern: props.allowedPattern,
description: props.description,
name: props.name,
name: props.parameterName,
type: STRING_PARAM_TYPE,
value: props.stringValue,
});
Expand Down Expand Up @@ -228,7 +285,7 @@ export class StringListParameter extends ParameterBase implements IStringListPar
const resource = new ssm.CfnParameter(this, 'Resource', {
allowedPattern: props.allowedPattern,
description: props.description,
name: props.name,
name: props.parameterName,
type: STRINGLIST_PARAM_TYPE,
value: props.stringListValue.join(','),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
},
{
"Parameters": {
"MyValueParameterCCC58B13": {
"MyValueParameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/My/Public/Parameter"
}
},
"Resources": {
"Dummy": {
"Type": "AWS::SNS::Topic"
}
},
"Outputs": {
"TheValue": {
"Value": {
"Ref": "MyValueParameterCCC58B13"
"Ref": "MyValueParameter"
}
}
}
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CreatingStack extends cdk.Stack {
super(scope, id);

new ssm.StringParameter(this, 'String', {
name: '/My/Public/Parameter',
parameterName: '/My/Public/Parameter',
stringValue: 'abcdef'
});
}
Expand All @@ -20,19 +20,20 @@ class UsingStack extends cdk.Stack {
/// !show
// Retrieve the latest value of the non-secret parameter
// with name "/My/String/Parameter".
const stringValue = new ssm.ParameterStoreString(this, 'MyValue', {
const stringValue = ssm.StringParameter.fromStringParameterAttributes(this, 'MyValue', {
parameterName: '/My/Public/Parameter',
// 'version' can be specified but is optional.
}).stringValue;

// Retrieve a specific version of the secret (SecureString) parameter.
// 'version' is always required.
const secretValue = new ssm.ParameterStoreSecureString({
const secretValue = ssm.StringParameter.fromSecureStringParameterAttributes(this, 'MySecureValue', {
parameterName: '/My/Secret/Parameter',
version: 5
});
/// !hide

new cdk.CfnResource(this, 'Dummy', { type: 'AWS::SNS::Topic' });
new cdk.CfnOutput(this, 'TheValue', { value: stringValue });

// Cannot be provisioned so cannot be actually used
Expand Down
16 changes: 8 additions & 8 deletions packages/@aws-cdk/aws-ssm/test/test.parameter-store-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export = {
const stack = new cdk.Stack();

// WHEN
const ref = new ssm.ParameterStoreString(stack, 'Ref', {
const ref = ssm.StringParameter.fromStringParameterAttributes(stack, 'Ref', {
parameterName: '/some/key',
version: 123
});
Expand All @@ -25,21 +25,21 @@ export = {
const stack = new cdk.Stack();

// WHEN
const ref = new ssm.ParameterStoreString(stack, 'Ref', {
const ref = ssm.StringParameter.fromStringParameterAttributes(stack, 'Ref', {
parameterName: '/some/key',
});

// THEN
expect(stack).toMatch({
Parameters: {
RefParameter407AF5C8: {
RefParameter: {
Type: "AWS::SSM::Parameter::Value<String>",
Default: "/some/key"
}
}
});

test.deepEqual(stack.resolve(ref.stringValue), { Ref: 'RefParameter407AF5C8' });
test.deepEqual(stack.resolve(ref.stringValue), { Ref: 'RefParameter' });

test.done();
},
Expand All @@ -49,10 +49,10 @@ export = {
const stack = new cdk.Stack();

// WHEN
const ref = new ssm.ParameterStoreSecureString({
const ref = ssm.StringParameter.fromSecureStringParameterAttributes(stack, 'Ref', {
parameterName: '/some/key',
version: 123
});
}).stringValue;

// THEN
test.equal(stack.resolve(ref), '{{resolve:ssm-secure:/some/key:123}}');
Expand All @@ -66,10 +66,10 @@ export = {

// WHEN
test.throws(() => {
new ssm.ParameterStoreString(stack, 'Ref', {
ssm.StringParameter.fromStringParameterAttributes(stack, 'Ref', {
parameterName: '',
});
}, /parameterName cannot be empty/);
}, /parameterName cannot be an empty string/);

test.done();
},
Expand Down
Loading

0 comments on commit bd1bcf5

Please sign in to comment.