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

[POC] feature: manage stack mappings to unblock api expansion over time #1430

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion packages/amplify-category-api/amplify-plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"rebuild",
"remove",
"update",
"help"
"help",
"snapshot-stack-mappings",
"assign-stack-mappings"
],
"commandAliases": {
"configure": "update"
Expand Down
8 changes: 8 additions & 0 deletions packages/amplify-category-api/src/commands/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ export const run = async (context: $TSContext) => {
name: 'override',
description: 'Generates overrides file to apply custom modifications to CloudFormation',
},
{
name: 'snapshot-stack-mappings',
description: 'Snapshots the stack mappings for the current project',
},
{
name: 'assign-stack-mappings',
description: 'Assign stack mappings for the newly built resources',
},
];

context.amplify.showHelp(header, commands);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { $TSContext} from '@aws-amplify/amplify-cli-core';
import { assignStackMappings } from '../../provider-utils/awscloudformation/stack-mapping-manager';

/**
* Pull all resolver and functions from the current cloud backend and snapshot them in the transform.conf.json file.
* `amplify pull` may need to be run first.
*/
export const run = async (context: $TSContext): Promise<void> => {
const stackName = context.parameters?.options?.['stack-name'];
if (!stackName) {
throw new Error('need to provide --stack-name <STACK NAME>');
}

return assignStackMappings(stackName);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { $TSContext } from '@aws-amplify/amplify-cli-core';
import { snapshotStackMappings } from '../../provider-utils/awscloudformation/stack-mapping-manager';

/**
* Pull all resolver and functions from the current cloud backend and snapshot them in the transform.conf.json file.
* `amplify pull` may need to be run first.
*/
export const run = async (context: $TSContext): Promise<void> => snapshotStackMappings();
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { pathManager, stateManager } from '@aws-amplify/amplify-cli-core';
import { getAppSyncResourceName } from '../../provider-utils/awscloudformation/utils/amplify-meta-utils';
import { readFromPath } from 'graphql-transformer-core/lib/util/fileUtils';
import { loadConfig, writeConfig } from 'graphql-transformer-core';
import * as fs from 'fs-extra';
import * as path from 'path';

/**
* Pull all resolver and functions from the current cloud backend and snapshot them in the transform.conf.json file.
* `amplify pull` may need to be run first.
*/
export const snapshotStackMappings = async (): Promise<void> => {
const apiName = getAppSyncResourceName(stateManager.getMeta());
if (!apiName) {
throw new Error('Could not find api name.');
}

const currentApiStacksPath = path.join(pathManager.getCurrentCloudBackendDirPath(), 'api', apiName, 'build', 'stacks');
if (!fs.pathExistsSync(currentApiStacksPath)) {
throw new Error('Could not find current cloud backend api stacks path.');
}

const apiPath = path.join(pathManager.getAmplifyDirPath(), 'backend', 'api', apiName);
if (!fs.pathExistsSync(apiPath)) {
throw new Error('Could not find api path.');
}

const currentApiStacks: Record<string, string> = await readFromPath(currentApiStacksPath);

const getResourceIdsForTypes = (stackDefinition: any, resourceTypes: string[]): string[] => {
const resourceTypeSet = new Set(resourceTypes);
return Object.entries(stackDefinition.Resources)
.filter(([_, resource]: [string, any]) => resourceTypeSet.has(resource.Type))
.map(([resourceName, _]) => resourceName);
};

const stackMappings = Object.fromEntries(Object.entries(currentApiStacks).flatMap(([stackFileName, stackContentsString]) => {
const stackName = stackFileName.split('.')[0];
const stackContents = JSON.parse(stackContentsString);
return getResourceIdsForTypes(stackContents, ['AWS::AppSync::FunctionConfiguration', 'AWS::AppSync::Resolver'])
.map(id => [id, stackName]);
}));

const config: any = await loadConfig(apiPath);

Object.entries(stackMappings).forEach(([resourceId, stackName]) => {
if (!config.StackMapping) {
config.StackMapping = {};
}
if (!(resourceId in config.StackMapping)) {
config.StackMapping[resourceId] = stackName;
}
});

await writeConfig(apiPath, config);
};

/**
* Pull all resolver and functions from the current cloud backend and snapshot them in the transform.conf.json file.
* `amplify pull` may need to be run first.
*/
export const assignStackMappings = async (stackNameAssignment: string): Promise<void> => {
await snapshotStackMappings();

const apiName = getAppSyncResourceName(stateManager.getMeta());
if (!apiName) {
throw new Error('Could not find api name.');
}

const apiPath = path.join(pathManager.getAmplifyDirPath(), 'backend', 'api', apiName);
if (!fs.pathExistsSync(apiPath)) {
throw new Error('Could not find api path.');
}

const buildApiStacksPath = path.join(apiPath, 'build', 'stacks');
if (!fs.pathExistsSync(buildApiStacksPath)) {
throw new Error('need to build first, run `amplify api gql-compile`');
}

const apiStacks: Record<string, string> = await readFromPath(buildApiStacksPath);

const getResourceIdsForTypes = (stackDefinition: any, resourceTypes: string[]): string[] => {
const resourceTypeSet = new Set(resourceTypes);
return Object.entries(stackDefinition.Resources)
.filter(([_, resource]: [string, any]) => resourceTypeSet.has(resource.Type))
.map(([resourceName, _]) => resourceName);
};

const stackMappings = Object.fromEntries(Object.entries(apiStacks).flatMap(([stackFileName, stackContentsString]) => {
const stackName = stackFileName.split('.')[0];
const stackContents = JSON.parse(stackContentsString);
return getResourceIdsForTypes(stackContents, ['AWS::AppSync::FunctionConfiguration', 'AWS::AppSync::Resolver'])
.map(id => [id, stackName]);
}));

const config: any = await loadConfig(apiPath);

Object.entries(stackMappings).forEach(([resourceId, stackName]) => {
if (!config.StackMapping) {
config.StackMapping = {};
}
if (!(resourceId in config.StackMapping)) {
config.StackMapping[resourceId] = stackNameAssignment;
}
});

await writeConfig(apiPath, config);
};