Skip to content

Commit

Permalink
feat: ddb overrides and flow refactor
Browse files Browse the repository at this point in the history
* feat: ddb overrides

* feat: add migration logic

* fix: remove older files post migration

* fix: address PR comments

* fix: remove inquirer and use amplify prompts instead

Co-authored-by: Ghosh <kaustavg@3c22fb229ff6.ant.amazon.com>
  • Loading branch information
kaustavghosh06 and Ghosh authored Oct 4, 2021
1 parent 1b2cd07 commit 89b4f0a
Show file tree
Hide file tree
Showing 33 changed files with 2,072 additions and 1,003 deletions.
2 changes: 1 addition & 1 deletion packages/amplify-category-storage/amplify-plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "storage",
"type": "category",
"commands": ["add", "import", "console", "push", "remove", "update", "help"],
"commands": ["add", "import", "console", "push", "remove", "update", "help", "override"],
"commandAliases": {
"configure": "update"
},
Expand Down
16 changes: 12 additions & 4 deletions packages/amplify-category-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amplify-category-storage",
"version": "2.12.9",
"name": "@aws-amplify/amplify-category-storage",
"version": "1.0.0",
"description": "amplify-cli storage plugin",
"repository": {
"type": "git",
Expand All @@ -15,7 +15,8 @@
"build": "tsc",
"test": "jest",
"clean": "rimraf lib tsconfig.tsbuildinfo",
"watch": "tsc -w"
"watch": "tsc -w",
"generateSchemas": "ts-node ./resources/genInputSchema.ts"
},
"keywords": [
"amplify",
Expand All @@ -27,6 +28,9 @@
"amplify-util-import": "1.5.12",
"chalk": "^4.1.1",
"cloudform-types": "^4.2.0",
"@aws-cdk/aws-s3": "~1.124.0",
"@aws-cdk/core": "~1.124.0",
"@aws-cdk/aws-dynamodb": "~1.124.0",
"enquirer": "^2.3.6",
"fs-extra": "^8.1.0",
"inquirer": "^7.3.3",
Expand All @@ -52,6 +56,10 @@
"json",
"node"
],
"collectCoverage": true
"collectCoverage": true,
"coverageReporters": [
"json",
"html"
]
}
}
17 changes: 17 additions & 0 deletions packages/amplify-category-storage/resources/genInputSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { TypeDef, CLIInputSchemaGenerator } from 'amplify-cli-core';

//ResourceProvider TypeDefs
const DDBStorageTypeDef: TypeDef = {
typeName: 'DynamoDBCLIInputs',
service: 'DynamoDB',
};
const S3StorageTypeDef: TypeDef = {
typeName: 'S3UserInputs',
service: 'S3',
};

// Defines the type names and the paths to the TS files that define them
const storageCategoryTypeDefs: TypeDef[] = [DDBStorageTypeDef];

const schemaGenerator = new CLIInputSchemaGenerator(storageCategoryTypeDefs);
schemaGenerator.generateJSONSchemas(); //convert CLI input data into json schemas.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Add Amplify Helper dependencies */

/* TODO: Need to change props to Root-Stack specific props when props are ready */
export function overrideProps(props: any) {
/* TODO: Add snippet of how to override in comments */
return props;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "overrides-for-root-stack",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0"
},
"devDependencies": {
"typescript": "^4.2.4"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "build"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./",
"rootDir": "../"
},
"include": ["../**/*"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* Add Amplify Helper dependencies */

/* TODO: Need to change props to Root-Stack specific props when props are ready */
export function overrideProps(props: any): void {
/* TODO: Add snippet of how to override in comments */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "overrides-for-root-stack",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@types/fs-extra": "^9.0.11",
"fs-extra": "^9.1.0"
},
"devDependencies": {
"typescript": "^4.2.4"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "build"
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"strict": false,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./",
"rootDir": "../"
},
"include": ["../**/*"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"type": "object",
"properties": {
"resourceName": {
"type": "string"
},
"tableName": {
"type": "string"
},
"partitionKey": {
"$ref": "#/definitions/DynamoDBCLIInputsKeyType"
},
"sortKey": {
"$ref": "#/definitions/DynamoDBCLIInputsKeyType"
},
"gsi": {
"type": "array",
"items": {
"$ref": "#/definitions/DynamoDBCLIInputsGSIType"
}
},
"triggerFunctions": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["partitionKey", "resourceName", "tableName"],
"definitions": {
"DynamoDBCLIInputsKeyType": {
"type": "object",
"properties": {
"fieldName": {
"type": "string"
},
"fieldType": {
"$ref": "#/definitions/FieldType"
}
},
"required": ["fieldName", "fieldType"]
},
"FieldType": {
"enum": ["binary", "boolean", "list", "map", "null", "number", "string"],
"type": "string"
},
"DynamoDBCLIInputsGSIType": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"partitionKey": {
"$ref": "#/definitions/DynamoDBCLIInputsKeyType"
},
"sortKey": {
"$ref": "#/definitions/DynamoDBCLIInputsKeyType"
}
},
"required": ["name", "partitionKey"]
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"type": "object",
"properties": {
"resourceName": {
"type": "string"
},
"bucketName": {
"type": "string"
},
"storageAccess": {
"$ref": "#/definitions/S3AccessType"
},
"selectedGuestPermissions": {
"type": "array",
"items": {
"enum": ["DELETE_OBJECT", "GET_OBJECT", "LIST_BUCKET", "PUT_OBJECT"],
"type": "string"
}
},
"selectedAuthenticatedPermissions": {
"type": "array",
"items": {
"enum": ["DELETE_OBJECT", "GET_OBJECT", "LIST_BUCKET", "PUT_OBJECT"],
"type": "string"
}
},
"isTriggerEnabled": {
"type": "boolean"
},
"triggerFunctionName": {
"type": "string"
}
},
"required": [
"bucketName",
"isTriggerEnabled",
"resourceName",
"selectedAuthenticatedPermissions",
"selectedGuestPermissions",
"storageAccess",
"triggerFunctionName"
],
"definitions": {
"S3AccessType": {
"enum": ["auth", "authAndGuest"],
"type": "string"
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
83 changes: 83 additions & 0 deletions packages/amplify-category-storage/src/commands/storage/override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
entry code for amplify override root
*/

import { generateOverrideSkeleton, $TSContext, FeatureFlags, stateManager, pathManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import * as fs from 'fs-extra';
import inquirer from 'inquirer';
import { DynamoDBInputState } from '../../provider-utils/awscloudformation/service-walkthroughs/dynamoDB-input-state';
import { DDBStackTransform } from '../../provider-utils/awscloudformation/cdk-stack-builder/ddb-stack-transform';
import * as path from 'path';
import { categoryName } from '../../constants';

export const name = 'override';

export const run = async (context: $TSContext) => {
if (FeatureFlags.getBoolean('overrides.project')) {
const { amplify } = context;
const amplifyMeta = stateManager.getMeta();

const storageResources: string[] = [];

Object.keys(amplifyMeta[categoryName]).forEach(resourceName => {
storageResources.push(resourceName);
});

if (storageResources.length === 0) {
const errMessage = 'No resources to override. You need to add a resource.';
printer.error(errMessage);
return;
}

let selectedResourceName: string = storageResources[0];

if (storageResources.length > 1) {
const resourceAnswer = await inquirer.prompt({
type: 'list',
name: 'resource',
message: 'Which resource would you like to add overrides for?',
choices: storageResources,
});
selectedResourceName = resourceAnswer.resource;
}

const destPath = pathManager.getResourceDirectoryPath(undefined, categoryName, selectedResourceName);
fs.ensureDirSync(destPath);

const srcPath = path.join(
__dirname,
'..',
'..',
'..',
'resources',
'overrides-resource',
amplifyMeta[categoryName][selectedResourceName].service,
);

// Make sure to migrate first
if (amplifyMeta[categoryName][selectedResourceName].service === 'DynamoDB') {
const resourceInputState = new DynamoDBInputState(selectedResourceName);
if (!resourceInputState.cliInputFileExists()) {
if (await amplify.confirmPrompt('File migration required to continue. Do you want to continue?', true)) {
resourceInputState.migrate();
const stackGenerator = new DDBStackTransform(selectedResourceName);
stackGenerator.transform();
} else {
return;
}
}
} else if (amplifyMeta[categoryName][selectedResourceName].service === 'S3') {
// S3 migration logic goes in here
}

await generateOverrideSkeleton(context, srcPath, destPath);
} else {
printer.info('Storage overrides is currently not turned on. In amplify/cli.json file please include the following:');
printer.info(`{
override: {
storage: true
}
}`);
}
};
Loading

0 comments on commit 89b4f0a

Please sign in to comment.