Skip to content
This repository has been archived by the owner on Sep 30, 2019. It is now read-only.

Commit

Permalink
Fix cfRef Resolver (#113)
Browse files Browse the repository at this point in the history
* Fix cfRef Resolver

* Return value instead of throwing Errors

* Remove unsed code

* Add tests and fixes
  • Loading branch information
bboure committed Apr 30, 2019
1 parent 3cb3521 commit 733b9ab
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 40 deletions.
Expand Up @@ -14,7 +14,10 @@ describe('cloudFormationProcessor', () => {
const refTable = 'tablefromref';
const {
custom: {
appSync: { dataSources },
appSync: {
dataSources,
userPoolConfig: { userPoolId },
},
},
} = cloudFormationProcessor(configs, {
dynamodbTables: {
Expand All @@ -35,20 +38,8 @@ describe('cloudFormationProcessor', () => {
tableName: refTable,
},
});
});

it('will throw errors messages with pathing information', () => {
expect(() => {
cloudFormationProcessor(
{
config: {
nested: {
inarray: [{ Ref: 'somethingnothere' }],
},
},
},
{},
);
}).toThrow('nested.inarray.0.Ref');
expect(userPoolId).toMatchObject({
Ref: 'UserPoolResource',
});
});
});
Expand Up @@ -19,7 +19,8 @@ custom:
userPoolConfig:
awsRegion: us-east-1
defaultAction: ALLOW
userPoolId: us-east-1_27WcML9k8
userPoolId:
Ref: UserPoolResource
region: us-east-2
mappingTemplates:
- dataSource: QuoteRequest
Expand Down
32 changes: 9 additions & 23 deletions packages/appsync-emulator-serverless/cloudFormationProcessor.js
Expand Up @@ -6,10 +6,6 @@

const DynamoDBTable = 'AWS::DynamoDB::Table';

function humanObjectPath(objectPath) {
return objectPath.join('.');
}

function lookupResourcesFromCtx(resource, ctx) {
return ctx.resources && ctx.resources.Resources[resource];
}
Expand All @@ -29,35 +25,22 @@ function lookupDynamodbTableName(cfObject, { dynamodbTables }) {
return tableName;
}

function cfRef(value, ctx, objectPath) {
function cfRef(value, ctx) {
const cfObject = lookupResourcesFromCtx(value, ctx);
if (!cfObject) {
throw new Error(
`Cannot find referenced [Ref] cloud formation resource (${humanObjectPath(
objectPath,
)})`,
);
return false;
}

const { Type: type } = cfObject;
if (!type) {
// TODO: Add path
throw new Error(
`Unknown cloud formation object reference or type (${humanObjectPath(
objectPath,
)})`,
);
return false;
}

switch (type) {
case DynamoDBTable:
return lookupDynamodbTableName(cfObject, ctx);
default:
throw new Error(
`Unable to find logical ref resource for : ${type} (${humanObjectPath(
objectPath,
)})`,
);
return false;
}
}

Expand All @@ -80,11 +63,14 @@ function processObject(object, ctx, objectPath = []) {
return entries.reduce((sum, [key, value]) => {
const newObjectPath = [...objectPath, key];
if (entries.length === 1 && cloudFormationHandlers[key]) {
return processObject(
cloudFormationHandlers[key](value, ctx, newObjectPath),
const resolvedResource = processObject(
cloudFormationHandlers[key](value, ctx),
ctx,
newObjectPath,
);
if (resolvedResource !== false) {
return resolvedResource;
}
}
return {
...sum,
Expand Down

0 comments on commit 733b9ab

Please sign in to comment.