Skip to content

Commit

Permalink
fix: parse nested yaml GetAtt refs correctly (#7220)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfoyle committed Apr 30, 2021
1 parent 1397162 commit 0b20951
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
18 changes: 17 additions & 1 deletion packages/amplify-cli-core/src/__tests__/cfnUtilities.test.ts
@@ -1,5 +1,4 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { CFNTemplateFormat, JSONUtilities, readCFNTemplate, writeCFNTemplate } from '../../lib';

jest.mock('fs-extra');
Expand Down Expand Up @@ -48,6 +47,23 @@ describe('readCFNTemplate', () => {
expect(result.templateFormat).toEqual(CFNTemplateFormat.YAML);
expect(result.cfnTemplate).toEqual(testTemplate);
});

it('reads yaml template with nested GetAtt refs', async () => {
const yamlContent = `
!GetAtt myResource.output.someProp
`;
((fs_mock.readFile as unknown) as jest.MockedFunction<TwoArgReadFile>).mockResolvedValueOnce(yamlContent);

const result = await readCFNTemplate(testPath);
expect(result.cfnTemplate).toMatchInlineSnapshot(`
Object {
"Fn::GetAtt": Array [
"myResource",
"output.someProp",
],
}
`);
});
});

describe('writeCFNTemplate', () => {
Expand Down
22 changes: 20 additions & 2 deletions packages/amplify-cli-core/src/cfnUtilities.ts
Expand Up @@ -112,13 +112,31 @@ const CF_SCHEMA = new yaml.Schema([
new yaml.Type('!GetAtt', {
kind: 'scalar',
construct: function (data) {
return { 'Fn::GetAtt': Array.isArray(data) ? data : data.split('.') };
if (Array.isArray(data)) {
return {
'Fn::GetAtt': data,
};
}
// data is a string
const firstPeriodIdx = data.indexOf('.');
return {
'Fn::GetAtt': [data.slice(0, firstPeriodIdx), data.slice(firstPeriodIdx + 1)],
};
},
}),
new yaml.Type('!GetAtt', {
kind: 'sequence',
construct: function (data) {
return { 'Fn::GetAtt': Array.isArray(data) ? data : data.split('.') };
if (Array.isArray(data)) {
return {
'Fn::GetAtt': data,
};
}
// data is a string
const firstPeriodIdx = data.indexOf('.');
return {
'Fn::GetAtt': [data.slice(0, firstPeriodIdx), data.slice(firstPeriodIdx + 1)],
};
},
}),
new yaml.Type('!GetAZs', {
Expand Down

0 comments on commit 0b20951

Please sign in to comment.