Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse nested yaml GetAtt refs correctly #7220

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/amplify-cli-core/src/__tests__/cfnUtilities.test.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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('.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no . in the string, this will generate the wrong output. I'm not sure if it actually matters though since the GetAtt won't work properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I don't think we need to cover that case here as it's already invalid

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