Skip to content

Commit

Permalink
Merge pull request #268 from horike37/feature/intrinsic_in_array
Browse files Browse the repository at this point in the history
fix: intrinsics in array not detected
  • Loading branch information
theburningmonk committed Oct 11, 2019
2 parents 4c0103a + fa135d8 commit ef79a6a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
13 changes: 10 additions & 3 deletions lib/deploy/stepFunctions/compileStateMachines.js
Expand Up @@ -42,9 +42,16 @@ function* getIntrinsicFunctions(obj) {
if (Array.isArray(value)) {
// eslint-disable-next-line guard-for-in, no-restricted-syntax
for (const idx in value) {
const innerFuncs = Array.from(getIntrinsicFunctions(value[idx]));
for (const x of innerFuncs) {
yield x;
const element = value[idx];
if (isIntrinsic(element)) {
const paramName = randomName();
value[idx] = `\${${paramName}}`;
yield [paramName, element];
} else {
const innerFuncs = Array.from(getIntrinsicFunctions(element));
for (const x of innerFuncs) {
yield x;
}
}
}
} else if (isIntrinsic(value)) {
Expand Down
83 changes: 55 additions & 28 deletions lib/deploy/stepFunctions/compileStateMachines.test.js
@@ -1,5 +1,6 @@
'use strict';

const _ = require('lodash');
const expect = require('chai').expect;
const Serverless = require('serverless/lib/Serverless');
const AwsProvider = require('serverless/lib/plugins/aws/provider/awsProvider');
Expand Down Expand Up @@ -596,6 +597,28 @@ describe('#compileStateMachines', () => {
},
MessageBody: 'This is a static message',
},
Next: 'Fargate',
},
Fargate: {
Type: 'Task',
Resource: 'arn:aws:states:::ecs:runTask.waitForTaskToken',
Parameters: {
LaunchType: 'FARGATE',
Cluster: {
Ref: 'ActivityCluster',
},
NetworkConfiguration: {
AwsvpcConfiguration: {
AssignPublicIp: 'ENABLED',
SecurityGroups: [{
Ref: 'ActivitySecurityGroup',
}],
Subnets: [{
Ref: 'ActivitySubnet',
}],
},
},
},
Next: 'Parallel',
},
Parallel: {
Expand Down Expand Up @@ -633,34 +656,38 @@ describe('#compileStateMachines', () => {
const [json, params] = stateMachine.Properties.DefinitionString['Fn::Sub'];
const modifiedDefinition = JSON.parse(json);

const lambda = modifiedDefinition.States.Lambda;
expect(lambda.Resource.startsWith('${')).to.eq(true);
const functionParam = lambda.Resource.replace(/[${}]/g, '');
expect(params).to.haveOwnProperty(functionParam);
expect(params[functionParam]).to.eql({ Ref: 'MyFunction' });

const sns = modifiedDefinition.States.Sns;
expect(sns.Parameters.Message.startsWith('${')).to.eq(true);
const topicNameParam = sns.Parameters.Message.replace(/[${}]/g, '');
expect(params).to.haveOwnProperty(topicNameParam);
expect(params[topicNameParam]).to.eql({ 'Fn::GetAtt': ['MyTopic', 'TopicName'] });
expect(sns.Parameters.TopicArn.startsWith('${')).to.eq(true);
const topicArnParam = sns.Parameters.TopicArn.replace(/[${}]/g, '');
expect(params).to.haveOwnProperty(topicArnParam);
expect(params[topicArnParam]).to.eql({ Ref: 'MyTopic' });

const sqs = modifiedDefinition.States.Sqs;
expect(sqs.Parameters.QueueUrl.startsWith('${')).to.eq(true);
const queueUrlParam = sqs.Parameters.QueueUrl.replace(/[${}]/g, '');
expect(params[queueUrlParam]).to.eql({ Ref: 'MyQueue' });

const parallel = modifiedDefinition.States.Parallel;
expect(parallel.Branches).to.have.lengthOf(1);
const lambda2 = parallel.Branches[0].States.Lambda2;
expect(lambda2.Resource.startsWith('${')).to.eq(true);
const functionParam2 = lambda2.Resource.replace(/[${}]/g, '');
expect(params).to.haveOwnProperty(functionParam2);
expect(params[functionParam2]).to.eql({ Ref: 'MyFunction2' });
const hasParam = (state, path, expected) => {
const attr = _.get(state, path);
expect(attr.startsWith('${')).to.eq(true);
const paramName = attr.replace(/[${}]/g, '');
expect(params).to.haveOwnProperty(paramName);
expect(params[paramName]).to.eql(expected);
};

hasParam(modifiedDefinition.States.Lambda, 'Resource', {
Ref: 'MyFunction',
});
hasParam(modifiedDefinition.States.Sns, 'Parameters.Message', {
'Fn::GetAtt': ['MyTopic', 'TopicName'],
});
hasParam(modifiedDefinition.States.Sns, 'Parameters.TopicArn', {
Ref: 'MyTopic',
});
hasParam(modifiedDefinition.States.Sqs, 'Parameters.QueueUrl', {
Ref: 'MyQueue',
});
hasParam(modifiedDefinition.States.Fargate, 'Parameters.Cluster', {
Ref: 'ActivityCluster',
});
hasParam(modifiedDefinition.States.Fargate, 'Parameters.NetworkConfiguration.AwsvpcConfiguration.SecurityGroups.0', {
Ref: 'ActivitySecurityGroup',
});
hasParam(modifiedDefinition.States.Fargate, 'Parameters.NetworkConfiguration.AwsvpcConfiguration.Subnets.0', {
Ref: 'ActivitySubnet',
});
hasParam(modifiedDefinition.States.Parallel, 'Branches.0.States.Lambda2.Resource', {
Ref: 'MyFunction2',
});
});

it('should allow null values #193', () => {
Expand Down

0 comments on commit ef79a6a

Please sign in to comment.