Skip to content

Commit

Permalink
FIX: convert stack output Version format
Browse files Browse the repository at this point in the history
Use the Visitor to ensure no timestamp is added
to AWSTEmplateFormatVersion/Version tags

fixes: #189
  • Loading branch information
frodeaa authored and tavisrudd committed Oct 15, 2019
1 parent 8d7cacf commit 9ae7cd8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/cfn/convertStackToIidy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import def from '../default';
import {getKMSAliasForParameter} from '../params';
import {SUCCESS} from '../statusCodes';
import * as yaml from '../yaml';
import {Visitor} from '../preprocess/visitor';
import {getStackDescription} from './getStackDescription';
import {parseTemplateBody} from "./parseTemplateBody";
import {StackArgs} from "./types";
Expand Down Expand Up @@ -106,6 +107,23 @@ export type ConvertStackArguments = GenericCLIArguments & {
moveParamsToSsm: boolean;
};

export function readTemplateObj(templateBody: string, sortkeys: boolean): Object {
const templateObj0 = parseTemplateBody(templateBody);
let templateObj;
if (sortkeys) {
templateObj = deepMapValues(templateObj0, sortCallback, 'root');
} else {
templateObj = templateObj0;
}

const visitor = new Visitor();
return visitor.visitNode(templateObj, 'Root', {
GlobalAccumulator: {},
$envValues: {},
Stack: []
});
};

export async function convertStackToIIDY(argv0: GenericCLIArguments): Promise<number> {
const argv = argv0 as ConvertStackArguments;
await configureAWS(argv);
Expand All @@ -119,14 +137,7 @@ export async function convertStackToIIDY(argv0: GenericCLIArguments): Promise<nu
throw new Error(`Invalid cfn template found for ${StackName}`);
}

const templateObj0 = parseTemplateBody(TemplateBody!);
let templateObj;
if (argv.sortkeys) {
templateObj = deepMapValues(templateObj0, sortCallback, 'root');
} else {
templateObj = templateObj0;
}

const templateObj = readTemplateObj(TemplateBody!, argv.sortkeys);
const stack = await getStackDescription(StackName);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir);
Expand Down
21 changes: 21 additions & 0 deletions src/tests/test-cfn.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {expect} from 'chai';
import {loadStackArgs} from "../cfn/loadStackArgs";
import {readTemplateObj} from "../cfn/convertStackToIidy";
import * as yaml from "../yaml";
import * as aws from 'aws-sdk'

describe('cfn', () => {
Expand All @@ -19,4 +21,23 @@ describe('cfn', () => {
expect(args.ServiceRoleARN).to.equal('arn:aws:iam::001234567890:role/name');
});
});

describe('readTemplateObj', () => {
it('handles AWSTemplateFormatVersion strings properly', async function() {
const templateBody = 'AWSTemplateFormatVersion: 2010-09-09';
const templateObj = readTemplateObj(templateBody, true);
expect(yaml.dump(templateObj)).to.equal(
"AWSTemplateFormatVersion: '2010-09-09'\n"
);
});

it('handles Version strings properly', async function() {
const templateBody = 'Version: 2010-09-09\n';
const templateObj = readTemplateObj(templateBody, false);
expect(yaml.dump(templateObj)).to.equal(
"Version: '2010-09-09'\n"
);
});
});

});

0 comments on commit 9ae7cd8

Please sign in to comment.