Skip to content

Commit

Permalink
feat(backend): S3 support
Browse files Browse the repository at this point in the history
Closes #129
  • Loading branch information
brennanwilkes committed Feb 23, 2022
1 parent c5ada8d commit afc7de7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
23 changes: 17 additions & 6 deletions backend/src/controllers/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {Ec2} from "../terraform/ec2";
import {Gce} from "../terraform/gce";
import {NamedGoogleBackend} from "../terraform/googleBackend";
import {GoogleProvider} from "../terraform/googleProvider";
import {S3} from "../terraform/s3";
import {rootBlockSplitBackend} from "../terraform/terraform";
import {internalErrorHandler} from "../types/errorHandler";
import {TerraformResource} from "../types/terraform";
Expand All @@ -25,22 +26,32 @@ export const createTerraformSettings = (req: Request, res: Response): void => {
provider === "google" ? (req.body.settings?.project as string) : "";

const resourcesRaw = req.body.settings?.resources as (TerraformResource & {
type: "ec2" | "gce";
type: "ec2" | "gce" | "s3";
})[];
const repo = req.body.repo as string;
const token = req.headers?.token as string;

let flag = false;

const resources = resourcesRaw.map(resource => {
if (resource.type == "ec2") {
if (resource.type === "ec2") {
const ec2: Ec2 = resource as Ec2;
return new Ec2(ec2.ami, ec2.instance_type, ec2.id);
}
//else if(resource.type == "gce"){
else {
} else if (resource.type === "gce") {
const gce: Gce = resource as Gce;
return new Gce(project, gce.id, gce.machine_type, gce.disk_image);
} else if (resource.type === "s3") {
const s3: S3 = resource as S3;
return new S3(s3.id, s3.acl);
} else {
flag = true;
}
});
}) as TerraformResource[];

if (flag) {
internalErrorHandler(req, res)(new Error("Unknown resource type"));
return;
}

const [root, backend] = rootBlockSplitBackend(
provider === "aws" ? new AwsProvider() : new GoogleProvider(project),
Expand Down
40 changes: 40 additions & 0 deletions backend/src/terraform/s3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {model} from "mongoose";
import {DatabaseModel, generateSchema} from "../types/database";
import {acl} from "../types/terraform";

export interface S3 {
acl: acl;
id: string;
}
export class S3 implements S3, DatabaseModel<S3> {
constructor(id: string, acl: acl = "private") {
this.id = id;
this.acl = acl;
}

toSchema() {
return generateSchema<S3>(this);
}
toModel() {
return model("S3", this.toSchema());
}

toJSON() {
const resource: any = {};
resource[this.id] = [
{
acl: this.acl,
bucket: this.id,
versioning: [
{
enabled: true
}
]
}
];

return {
aws_s3_bucket: [resource]
};
}
}
9 changes: 4 additions & 5 deletions backend/src/terraform/terraform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {
NamedRequiredProvider,
namedTerraformBackend,
RequiredProvider
RequiredProvider,
TerraformResource
} from "../types/terraform";
import {NamedAwsBackend} from "./awsBackend";
import {AwsProvider} from "./awsProvider";
import {Ec2} from "./ec2";
import {Gce} from "./gce";
import {NamedGoogleBackend} from "./googleBackend";
import {GoogleProvider} from "./googleProvider";

Expand Down Expand Up @@ -34,7 +33,7 @@ export const terraformBlock = (
export const rootBlock = (
providers: NamedRequiredProvider[] | NamedRequiredProvider,
backend: namedTerraformBackend,
resources: (Ec2 | Gce)[] = []
resources: TerraformResource[] = []
) => {
return {
terraform: terraformBlock(providers, backend),
Expand All @@ -51,7 +50,7 @@ export const rootBlock = (
export const rootBlockSplitBackend = (
providers: NamedRequiredProvider[] | NamedRequiredProvider,
backend: namedTerraformBackend,
resources: (Ec2 | Gce)[] = []
resources: TerraformResource[] = []
) => {
const root = rootBlock(providers, backend, resources);
const backendBlock = root.terraform[0].backend;
Expand Down
7 changes: 6 additions & 1 deletion backend/src/types/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Ec2} from "../terraform/ec2";
import {Gce} from "../terraform/gce";
import {NamedGoogleBackend} from "../terraform/googleBackend";
import {GoogleProvider} from "../terraform/googleProvider";
import {S3} from "../terraform/s3";
import {DatabaseModel, generateSchemaInternals} from "./database";

// ---------------------------------Variable---------------------------------- //
Expand Down Expand Up @@ -150,9 +151,13 @@ export type source_image =
| "windows-server-2019-dc-v20220210"
| "fedora-coreos-35-20220116-3-0-gcp-x86-64";

// ----------------------------------S3-------------------------------------- //

export type acl = "private" | "public-read" | "public-read-write";

// ---------------------------------MISC------------------------------------- //

export type TerraformResource = Ec2 | Gce;
export type TerraformResource = Ec2 | Gce | S3;

// ----------------------------Terraform Root-------------------------------- //

Expand Down
15 changes: 14 additions & 1 deletion backend/src/validators/resourceValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CustomValidator} from "express-validator";

export const resourceTypes = /^(ec2|gce)$/;
export const resourceTypes = /^(ec2|gce|s3)$/;

const hasAllKeys = (obj: any, keys: string[]) => {
let retVal = true;
Expand Down Expand Up @@ -39,6 +39,19 @@ const resourceValidator: CustomValidator = (resource: any) => {
if ("zone" in resource && !/^[a-zA-Z]*-?[0-9]*$/.test(resource.zone)) {
return false;
}
} else if (resource.type === "s3") {
if (!hasAllKeys(resource, ["id"])) {
return false;
}
if (
resource.acl &&
!(resource.acl in ["private", "public-read", "public-read-write"])
) {
return false;
}
if (!/^[a-z][-a-z0-9]*[a-z0-9]$/.test(resource.id)) {
return false;
}
}

return true;
Expand Down

0 comments on commit afc7de7

Please sign in to comment.