Skip to content
Merged
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
27 changes: 26 additions & 1 deletion backend/src/controllers/terraform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {rootBlockSplitBackend} from "../terraform/terraform";
import {internalErrorHandler} from "../types/errorHandler";
import {TerraformResource} from "../types/terraform";
import {jsonToHcl} from "../util";
import {AwsLoadBalancer} from "../terraform/awsLoadBalancer";

export const createTerraformSettings = (req: Request, res: Response): void => {
const provider = req.body.settings?.provider as "aws" | "google" | "azure";
Expand All @@ -31,6 +32,7 @@ export const createTerraformSettings = (req: Request, res: Response): void => {
const allowSsh = req.body.settings.allowSsh ?? false;
const allowEgressWeb = req.body.settings.allowEgressWeb ?? false;
const allowIngressWeb = req.body.settings.allowIngressWeb ?? false;
const autoLoadBalance = req.body.settings.autoLoadBalance ?? false;

//Only needed for google
const project =
Expand Down Expand Up @@ -88,7 +90,30 @@ export const createTerraformSettings = (req: Request, res: Response): void => {
return;
}

const [gce, lambda, networkedResources] = splitForPrefab(resources);
/* eslint-disable prefer-const */
let [gce, lambda, networkedResources] = splitForPrefab(resources);
/* eslint-enable prefer-const */

if (autoLoadBalance) {
networkedResources = [
...networkedResources,
new AwsLoadBalancer(
`http_load_balancer`,
"TBD",
"application",
true,
"TBD",
"TBD",
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
"http-load-balancer"
)
];
}

const network =
networkedResources.length > 0 && provider === "aws"
Expand Down
75 changes: 36 additions & 39 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,43 @@ server.serve("/about");
server.serve("/contact");
server.route("/", mainRouter);

// import {testToFileAws} from "./util";
// import {Ec2} from "./terraform/ec2";
// import {prefabNetwork} from "./terraform/prefab";
// import {S3} from "./terraform/s3";
// import {GlacierVault} from "./terraform/glacierVault";
// import {DynamoDb} from "./terraform/DynamoDb";
import {testToFileAws} from "./util";
import {Ec2} from "./terraform/ec2";
import {prefabNetwork} from "./terraform/prefab";
import {AwsLoadBalancer} from "./terraform/awsLoadBalancer";

// testToFileAws(
// "/home/brennan/aws_test/devxp.tf",
// prefabNetwork(
// {
// ec2: [new Ec2("AUTO_UBUNTU", "t2.micro", "instance_a", true)],
// s3: [
// new S3(
// "devxp_test_bucket_a",
// false,
// false,
// "devxp-test-bucket-a"
// )
// ],
// glacier: new GlacierVault(
// "devxp_test_vault",
// false,
// "devxp-test-vault"
// ),
// dynamo: new DynamoDb("devxp_test_dynamo_db", [
// {
// name: "field1",
// type: "S",
// isHash: true
// }
// ])
// },
// {
// ssh: true,
// webEgress: true,
// webIngress: true
// }
// )
// );
testToFileAws(
"/home/brennan/aws_test/devxp.tf",
prefabNetwork(
{
ec2: [
new Ec2("AUTO_UBUNTU", "t2.micro", "instance_a", true),
new Ec2("AUTO_UBUNTU", "t2.micro", "instance_b", true),
new Ec2("AUTO_UBUNTU", "t2.micro", "instance_c", true)
],
load_balancer: new AwsLoadBalancer(
`http_load_balancer`,
"TBD",
"application",
true,
"TBD",
"TBD",
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
"http-load-balancer"
)
},
{
ssh: true,
webEgress: true,
webIngress: true
}
)
);

mongoose.connection.on(
"error",
Expand Down
97 changes: 97 additions & 0 deletions backend/src/terraform/awsLoadBalancer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {jsonRoot} from "./util";
import {Resource} from "./resource";
import {load_balancer_type} from "../types/terraform";
import {arr} from "../util";
import {Ec2} from "./ec2";

export interface AwsLoadBalancer {
vpc: string;
internal: boolean;
type: load_balancer_type;
securityGroups: string[];
subnet: string[];
protocol: string;
port: number;
instances: Ec2[];
enable_http2: boolean;
enable_deletion_protection: boolean;
}
export class AwsLoadBalancer
extends Resource<AwsLoadBalancer>
implements AwsLoadBalancer
{
constructor(
id: string,

vpc: string,
type: load_balancer_type,
internal: boolean,
securityGroups: string[] | string,
subnet: string | string[],
protocol = "HTTP",
port = 80,

instances: Ec2[] = [],

enable_http2 = false,
enable_deletion_protection = true,

autoIam?: boolean,
name?: string
) {
super(id, "AwsLoadBalancer", autoIam, name);

this.vpc = vpc;
this.type = type;
this.internal = internal;
this.securityGroups = arr(securityGroups);
this.subnet = arr(subnet);
this.protocol = protocol;
this.port = port;
this.instances = instances;
this.enable_http2 = enable_http2;
this.enable_deletion_protection = enable_deletion_protection;
}

//Returns a resource block
toJSON() {
return [
jsonRoot("aws_lb", this.id, {
name: this.name,
internal: this.internal,
security_groups: this.securityGroups.map(
g => `\${aws_security_group.${g}.id}`
),
subnets: this.subnet.map(s => `\${aws_subnet.${s}.id}`),
enable_http2: this.enable_http2,
enable_deletion_protection: this.enable_deletion_protection
}),
jsonRoot("aws_lb_target_group", `${this.id}_target`, {
name: `${this.name}-target`,
port: this.port,
protocol: this.protocol,
vpc_id: `\${aws_vpc.${this.vpc}.id}`
}),
jsonRoot("aws_lb_listener", `${this.id}_listener`, {
port: this.port,
protocol: this.protocol,
load_balancer_arn: `\${aws_lb.${this.id}.id}`,
default_action: {
type: "forward",
target_group_arn: `\${aws_lb_target_group.${this.id}_target.arn}`
}
}),
...this.instances.map(ec2 =>
jsonRoot(
"aws_lb_target_group_attachment",
`${this.id}_${ec2.id}_attachment`,
{
target_group_arn: `\${aws_lb_target_group.${this.id}_target.arn}`,
target_id: `\${aws_instance.${ec2.id}.id}`,
port: this.port
}
)
)
];
}
}
33 changes: 20 additions & 13 deletions backend/src/terraform/awsVpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import {AwsSubnet} from "./awsSubnet";
import {AwsInternetGateway} from "./AwsInternetGateway";
import {AwsRouteTable} from "./AwsRouteTable";
import {AwsRoute} from "./AwsRoute";
import {awsRegion} from "../types/terraform";

export interface AwsVpc {
cidr_block: string;
private_cidr: string;
public_cidr: string;
public_cidr: string[];
privateDns: boolean;
zones: awsRegion[];
}

export class AwsVpc extends Resource<AwsVpc> implements AwsVpc {
constructor(
cidr_block: string,
private_cidr: string,
public_cidr: string,
public_cidr: string[],
id: string,
zones: awsRegion[] = ["us-west-2a"],
autoIam?: boolean,
name?: string,
privateDns = false
Expand All @@ -27,19 +30,20 @@ export class AwsVpc extends Resource<AwsVpc> implements AwsVpc {
this.private_cidr = private_cidr;
this.public_cidr = public_cidr;
this.privateDns = privateDns;
this.zones = zones;
}

//Returns an array of resource blocks
toJSON() {
const gatewayId = `${this.id}_internetgateway`;
const publicRouteTableId = `${this.id}_routetable_pub`;
const privateRouteTableId = `${this.id}_routetable_priv`;
//const privateRouteTableId = `${this.id}_routetable_priv`;
const publicSubetId = `${this.id}_subnet_public`;
const privateSubnetId = `${this.id}_subnet_private`;
//const privateSubnetId = `${this.id}_subnet_private`;

return [
//PRIVATE
new AwsSubnet(
/*new AwsSubnet(
this.id,
this.private_cidr,
false,
Expand All @@ -53,17 +57,20 @@ export class AwsVpc extends Resource<AwsVpc> implements AwsVpc {
subnet_id: `\${aws_subnet.${privateSubnetId}.id}`,
route_table_id: `\${aws_route_table.${privateRouteTableId}.id}`
}
),
),*/

//----------------------------------------------------------------//

//PUBLIC
new AwsSubnet(
this.id,
this.public_cidr,
true,
publicSubetId
).toJSON(),
...this.zones.map((zone, i) =>
new AwsSubnet(
this.id,
this.public_cidr[i] ?? this.public_cidr[0],
true,
`${publicSubetId}${i}`,
zone
).toJSON()
),
new AwsInternetGateway(gatewayId, this.id).toJSON(),
new AwsRouteTable(
publicRouteTableId,
Expand All @@ -86,7 +93,7 @@ export class AwsVpc extends Resource<AwsVpc> implements AwsVpc {
"aws_route_table_association",
`${this.id}_subnet_public_assoc`,
{
subnet_id: `\${aws_subnet.${publicSubetId}.id}`,
subnet_id: `\${aws_subnet.${publicSubetId}0.id}`,
route_table_id: `\${aws_route_table.${publicRouteTableId}.id}`
}
),
Expand Down
Loading