Skip to content

Commit

Permalink
Lazily create constructs
Browse files Browse the repository at this point in the history
Fix rebase errors
  • Loading branch information
mnapoli committed Jun 21, 2021
1 parent 6364ad5 commit a25c7cc
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 65 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-arguments": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/switch-exhaustiveness-check": "error"
"@typescript-eslint/switch-exhaustiveness-check": "error",
// We intentionally use this so that constructs can declare their command handlers
"@typescript-eslint/unbound-method": "off"
}
}
]
Expand Down
2 changes: 0 additions & 2 deletions src/classes/AwsConstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export abstract class AwsConstruct extends CdkConstruct implements ConstructInte

abstract outputs(): Record<string, () => Promise<string | undefined>>;

abstract commands(): Record<string, () => void | Promise<void>>;

/**
* CloudFormation references
*/
Expand Down
16 changes: 14 additions & 2 deletions src/classes/Construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { AwsProvider } from "./AwsProvider";
export interface ConstructInterface {
outputs(): Record<string, () => Promise<string | undefined>>;

commands(): Record<string, () => void | Promise<void>>;

/**
* CloudFormation references
*/
Expand Down Expand Up @@ -39,5 +37,19 @@ export interface StaticConstructInterface {
type: "object";
[k: string]: unknown;
};
commands?(): ConstructCommands;
create(provider: AwsProvider, id: string, configuration: Record<string, unknown>): ConstructInterface;
}

export type ConstructCommands = Record<string, ConstructCommandDefinition>;
type ConstructCommandDefinition = {
usage: string;
handler: (opt: Record<string, string>) => void | Promise<void>;
options?: {
[name: string]: {
usage: string;
required: boolean;
shortcut?: string;
};
};
};
25 changes: 17 additions & 8 deletions src/constructs/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AwsConstruct, AwsProvider } from "../classes";
import { pollMessages, retryMessages } from "./queue/sqs";
import { sleep } from "../utils/sleep";
import { PolicyStatement } from "../CloudFormation";
import { ConstructCommands } from "../classes/Construct";

const QUEUE_DEFINITION = {
type: "object",
Expand Down Expand Up @@ -41,6 +42,22 @@ type Configuration = FromSchema<typeof QUEUE_DEFINITION>;
export class Queue extends AwsConstruct {
public static type = "queue";
public static schema = QUEUE_DEFINITION;
public static commands(): ConstructCommands {
return {
failed: {
usage: "List failed messages from the dead letter queue.",
handler: Queue.prototype.listDlq,
},
"failed:purge": {
usage: "Purge failed messages from the dead letter queue.",
handler: Queue.prototype.purgeDlq,
},
"failed:retry": {
usage: "Retry failed messages from the dead letter queue by moving them to the main queue.",
handler: Queue.prototype.retryDlq,
},
};
}

private readonly queue: CdkQueue;
private readonly queueArnOutput: CfnOutput;
Expand Down Expand Up @@ -130,14 +147,6 @@ export class Queue extends AwsConstruct {
this.appendFunctions();
}

commands(): Record<string, () => void | Promise<void>> {
return {
failed: () => this.listDlq(),
"failed:purge": () => this.purgeDlq(),
"failed:retry": () => this.retryDlq(),
};
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {
queueUrl: () => this.getQueueUrl(),
Expand Down
15 changes: 9 additions & 6 deletions src/constructs/StaticWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { flatten } from "lodash";
import { log } from "../utils/logger";
import { s3Sync } from "../utils/s3-sync";
import { AwsConstruct, AwsProvider } from "../classes";
import { ConstructCommands } from "../classes/Construct";
import ServerlessError from "../utils/error";

const STATIC_WEBSITE_DEFINITION = {
Expand Down Expand Up @@ -59,6 +60,14 @@ type Configuration = FromSchema<typeof STATIC_WEBSITE_DEFINITION>;
export class StaticWebsite extends AwsConstruct {
public static type = "static-website";
public static schema = STATIC_WEBSITE_DEFINITION;
public static commands(): ConstructCommands {
return {
upload: {
usage: "Upload files directly to S3 without going through a CloudFormation deployment.",
handler: StaticWebsite.prototype.uploadWebsite,
},
};
}

private readonly bucketNameOutput: CfnOutput;
private readonly domainOutput: CfnOutput;
Expand Down Expand Up @@ -157,12 +166,6 @@ export class StaticWebsite extends AwsConstruct {
});
}

commands(): Record<string, () => Promise<void>> {
return {
upload: this.uploadWebsite.bind(this),
};
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {
url: () => this.getUrl(),
Expand Down
4 changes: 0 additions & 4 deletions src/constructs/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ export class Storage extends AwsConstruct {
];
}

commands(): Record<string, () => void | Promise<void>> {
return {};
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {
bucketName: () => this.getBucketName(),
Expand Down
4 changes: 0 additions & 4 deletions src/constructs/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ export class Webhook extends AwsConstruct {
this.appendFunctions();
}

commands(): Record<string, () => void | Promise<void>> {
return {};
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {
httpMethod: () => this.getHttpMethod(),
Expand Down
Loading

0 comments on commit a25c7cc

Please sign in to comment.