Skip to content

Commit

Permalink
change to add resource walkshrough using CDK
Browse files Browse the repository at this point in the history
  • Loading branch information
fossamagna committed Jan 25, 2022
1 parent f53770e commit 961262f
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 724 deletions.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
},
"scripts": {
"build": "tsc",
"clean": "rimraf ./lib"
"clean": "rimraf ./lib",
"generateSchemas": "ts-node ./scripts/generateApiSchemas.ts"
},
"dependencies": {
"@aws-amplify/cli-extensibility-helper": "^2.2.1",
Expand All @@ -27,8 +28,9 @@
"@types/fs-extra": "^9.0.13",
"@types/inquirer": "^7.3.1",
"@types/uuid": "^8.3.0",
"amplify-cli-core": "^2.3.0",
"amplify-cli-core": "^2.4.8",
"rimraf": "^3.0.2",
"ts-node": "^10.4.0",
"typescript": "^4.1.2"
},
"keywords": [
Expand Down
3 changes: 1 addition & 2 deletions resources/overrides-resource/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"amplify-category-console-notification": "^0.3.0",
"@aws-amplify/cli-extensibility-helper": "^2.2.1"
},
"devDependencies": {
"typescript": "^4.2.4"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"type": "object",
"properties": {
"version": {
"description": "The schema version.",
"type": "number",
"enum": [
1
]
},
"sendToSlackFuncton": {
"type": "string"
}
},
"required": [
"sendToSlackFuncton",
"version"
],
"$schema": "http://json-schema.org/draft-07/schema#"
}
12 changes: 12 additions & 0 deletions scripts/generateApiSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CLIInputSchemaGenerator, TypeDef } from 'amplify-cli-core';

const ConsoleNotificationTypeDef: TypeDef = {
typeName: 'ConsoleNotificationCLIInputs',
service: 'AmplifyConsoleNotification',
};

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

const schemaGenerator = new CLIInputSchemaGenerator(consoleNotificationCategoryTypeDefs);
schemaGenerator.generateJSONSchemas(); // convert CLI input data into json schemas.
3 changes: 1 addition & 2 deletions src/commands/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export const run = async (context: $TSContext) => {
const { amplify, parameters } = context;
const resourceName = parameters.first;
try {
// @ts-ignore
await amplify.removeResource(context, category, resourceName);
} catch (err) {
} catch (err: any) {
context.print.info(err.stack);
context.print.error('An error occurred when removing the console notification resource');
context.usageData.emitError(err);
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export async function handleAmplifyEvent(context: $TSContext, args: any) {
}

export async function transformCategoryStack(context: $TSContext, resource: IAmplifyResource): Promise<void> {
if (canResourceBeTransformed(resource.resourceName)) {
if (canResourceBeTransformed(context, resource.resourceName)) {
generateConsoleNotificationStackTemplate(context, resource.resourceName);
}
}

function canResourceBeTransformed(resourceName: string) {
const resourceInputState = new ConsoleNotificationInputState(resourceName);
function canResourceBeTransformed(context: $TSContext, resourceName: string) {
const resourceInputState = new ConsoleNotificationInputState(context, resourceName);
return resourceInputState.cliInputFileExists();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import {
pathManager,
CLIInputSchemaValidator,
CategoryInputState,
$TSContext,
} from 'amplify-cli-core';
import { category } from '../../../constants';
import { ConsoleNotificationCLIInputs } from '../service-walkthrough-types/console-notification-user-input-types';
import { ConsoleNotificationCLIInputs } from '../service-walkthrough-types/amplifyConsoleNotification-user-input-types';

export class ConsoleNotificationInputState extends CategoryInputState {

#context: $TSContext;
#cliInputsFilePath: string; //cli-inputs.json (output) filepath
#category: string; //category of the resource
#service: string; //AWS service for the resource
#buildFilePath: string;

constructor(resourceName: string) {
constructor(context: $TSContext, resourceName: string) {
super(resourceName);
this.#context = context;
this.#category = category
this.#service = "AmplifyConsoleNotification";
const projectBackendDirPath = pathManager.getBackendDirPath();
Expand All @@ -29,15 +31,15 @@ export class ConsoleNotificationInputState extends CategoryInputState {
return JSONUtilities.readJson<ConsoleNotificationCLIInputs>(this.#cliInputsFilePath, { throwIfNotExist: true })!;
}

async saveCLIInputPayload(cliInputs: any): Promise<void> {
async saveCLIInputPayload(cliInputs: ConsoleNotificationCLIInputs): Promise<void> {
if (await this.isCLIInputsValid(cliInputs)) {
fs.ensureDirSync(path.join(pathManager.getBackendDirPath(), this.#category, this._resourceName));
JSONUtilities.writeJson(this.#cliInputsFilePath, cliInputs);
}
}

isCLIInputsValid(cliInputs: any): Promise<boolean> {
const schemaValidator = new CLIInputSchemaValidator(this.#service, this.#category, 'ConsoleNotificationCLIInputs');
const schemaValidator = new CLIInputSchemaValidator(this.#context, this.#service, this.#category, 'ConsoleNotificationCLIInputs');
return schemaValidator.validateInput(JSON.stringify(cliInputs));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Function, CfnPermission } from '@aws-cdk/aws-lambda';
import { Topic } from '@aws-cdk/aws-sns';
import { LambdaSubscription } from "@aws-cdk/aws-sns-subscriptions";
import { AmplifyStackTemplate, Template } from 'amplify-cli-core';
import { ConsoleNotificationStackOptions } from '../service-walkthrough-types/console-notification-user-input-types';
import { ConsoleNotificationStackOptions } from '../service-walkthrough-types/amplifyConsoleNotification-user-input-types';
import { AmplifyConsoleNotificationStackTemplate } from '../../../amplify-cli-extensibility-helper/types';

const CFN_TEMPLATE_FORMAT_VERSION = '2010-09-09';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from 'amplify-cli-core';
import { printer, formatter } from 'amplify-prompts';
import { ConsoleNotificationInputState } from '../console-notification-inputs-manager/console-notification-input-state';
import { ConsoleNotificationStackOptions } from '../service-walkthrough-types/console-notification-user-input-types';
import { ConsoleNotificationStackOptions } from '../service-walkthrough-types/amplifyConsoleNotification-user-input-types';
import { getAppId } from '../utils/get-app-id';
import { AmplifyConsoleNotificationStack } from './console-notificatoin-stack-builder';
import { category } from '../../../constants';
Expand Down Expand Up @@ -125,7 +125,7 @@ export class AmplifyConsoleNotificationTransform extends AmplifyCategoryTransfor
}

private generateStackOptions(context: $TSContext): ConsoleNotificationStackOptions {
const cliState = new ConsoleNotificationInputState(this.resourceName);
const cliState = new ConsoleNotificationInputState(context, this.resourceName);
const input = cliState.getCLIInputPayload();
return {
appId: getAppId(context)!,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export interface ConsoleNotificationCLIInputs {
/**
* The schema version.
*/
version: 1;

sendToSlackFuncton: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Options } from "..";
import * as path from 'path';
import inquirer from 'inquirer';
import { v4 as uuid } from 'uuid';
import { ConsoleNotificationInputState } from "../console-notification-inputs-manager/console-notification-input-state";
import { generateConsoleNotificationStackTemplate } from "../utils/generate-console-notification-stack-template";
import { ConsoleNotificationCLIInputs } from "../service-walkthrough-types/amplifyConsoleNotification-user-input-types";

const templateFileName = 'amplify-console-notification-cloudformation-template.json.ejs';

Expand All @@ -16,7 +19,15 @@ export async function createWalkthrough(context: $TSContext, category: string, o
const functionName = await addTrigger(context);
options.functionName = functionName;

await copyCfnTemplate(context, category, options);
const resourceName = 'slack';
const cliInputsState = new ConsoleNotificationInputState(context, resourceName);
const cliInputs: ConsoleNotificationCLIInputs = {
version: 1,
sendToSlackFuncton: functionName
};
await cliInputsState.saveCLIInputPayload(cliInputs);

await generateConsoleNotificationStackTemplate(context, resourceName);

const backendConfigs = {
service: options.service,
Expand All @@ -31,7 +42,7 @@ export async function createWalkthrough(context: $TSContext, category: string, o
}
],
};
await context.amplify.updateamplifyMetaAfterResourceAdd(category, 'slack', backendConfigs);
await context.amplify.updateamplifyMetaAfterResourceAdd(category, resourceName, backendConfigs);
}

function copyCfnTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { migrateResourceToSupportOverride } from './migrate-override-resource';
* returns true if check goes through, false if cancelled
*/
export const checkConsoleNotificationResourceMigration = async (context: $TSContext, resourceName: string, isUpdate: boolean): Promise<boolean> => {
const cliState = new ConsoleNotificationInputState(resourceName);
const cliState = new ConsoleNotificationInputState(context, resourceName);
if (!cliState.cliInputFileExists()) {
printer.debug("cli-inputs.json doesn't exist");
// put spinner here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from 'fs-extra';
import { $TSObject, JSONUtilities, NotInitializedError, pathManager } from 'amplify-cli-core';
import { printer } from 'amplify-prompts';
import { category } from '../../../constants';
import { ConsoleNotificationCLIInputs } from '../service-walkthrough-types/console-notification-user-input-types';
import { ConsoleNotificationCLIInputs } from '../service-walkthrough-types/amplifyConsoleNotification-user-input-types';

export const migrateResourceToSupportOverride = async (resourceName: string) => {
printer.debug('Starting Migration Process');
Expand All @@ -23,6 +23,7 @@ export const migrateResourceToSupportOverride = async (resourceName: string) =>
const functionName = functionParam.replace(/^function/, '').replace(/Arn$/, '');

const cliInputs: ConsoleNotificationCLIInputs = {
version: 1,
sendToSlackFuncton: functionName
};
const cliInputsPath = path.join(resourceDirPath, 'cli-inputs.json');
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@
},
"include": [
"src/**/*"
],
]
}

0 comments on commit 961262f

Please sign in to comment.