285 lines
10 KiB
HCL
285 lines
10 KiB
HCL
locals {
|
|
target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id
|
|
network_vcn_id = trimspace(var.existing_vcn_id) != "" ? var.existing_vcn_id : oci_core_vcn.homelab[0].id
|
|
public_subnet_id = trimspace(var.existing_public_subnet_id) != "" ? var.existing_public_subnet_id : oci_core_subnet.public[0].id
|
|
public_route_table_id = try(oci_core_route_table.public[0].id, null)
|
|
private_route_table_id = try(oci_core_route_table.private[0].id, null)
|
|
base_tags = merge(var.freeform_tags, { managed_by = "jeannie", purpose = "homelab-learning" })
|
|
}
|
|
|
|
resource "oci_identity_compartment" "homelab" {
|
|
count = var.enabled && var.create_compartment ? 1 : 0
|
|
compartment_id = var.tenancy_id
|
|
name = var.compartment_name
|
|
description = var.compartment_description
|
|
enable_delete = true
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_vcn" "homelab" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
cidr_blocks = [var.vcn_cidr]
|
|
display_name = var.vcn_display_name
|
|
dns_label = "homelab"
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_internet_gateway" "homelab" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
display_name = "homelab-internet-gateway"
|
|
enabled = true
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_nat_gateway" "homelab" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
display_name = "homelab-nat-gateway"
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_route_table" "public" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
display_name = "homelab-public-routes"
|
|
route_rules {
|
|
destination = "0.0.0.0/0"
|
|
destination_type = "CIDR_BLOCK"
|
|
network_entity_id = oci_core_internet_gateway.homelab[0].id
|
|
}
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_route_table" "private" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
display_name = "homelab-private-routes"
|
|
route_rules {
|
|
destination = "0.0.0.0/0"
|
|
destination_type = "CIDR_BLOCK"
|
|
network_entity_id = oci_core_nat_gateway.homelab[0].id
|
|
}
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_subnet" "public" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
cidr_block = var.public_subnet_cidr
|
|
display_name = "homelab-public-subnet"
|
|
dns_label = "public"
|
|
route_table_id = local.public_route_table_id
|
|
prohibit_public_ip_on_vnic = false
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_subnet" "private" {
|
|
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
cidr_block = var.private_subnet_cidr
|
|
display_name = "homelab-private-subnet"
|
|
dns_label = "private"
|
|
route_table_id = local.private_route_table_id
|
|
prohibit_public_ip_on_vnic = true
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_network_security_group" "edge" {
|
|
count = var.enabled && var.edge_enabled ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
display_name = "homelab-edge-nsg"
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_network_security_group" "email" {
|
|
count = var.enabled && var.email_enabled ? 1 : 0
|
|
compartment_id = local.target_compartment_id
|
|
vcn_id = local.network_vcn_id
|
|
display_name = "homelab-email-nsg"
|
|
freeform_tags = local.base_tags
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "edge_ssh" {
|
|
count = var.enabled && var.edge_enabled ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.edge[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = var.admin_cidr
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 22
|
|
max = 22
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "edge_web" {
|
|
count = var.enabled && var.edge_enabled ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.edge[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = "0.0.0.0/0"
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 80
|
|
max = 80
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "edge_https" {
|
|
count = var.enabled && var.edge_enabled ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.edge[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = "0.0.0.0/0"
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 443
|
|
max = 443
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "email_ssh" {
|
|
count = var.enabled && var.email_enabled ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.email[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = var.admin_cidr
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 22
|
|
max = 22
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "email_ports" {
|
|
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.email[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = "0.0.0.0/0"
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 25
|
|
max = 25
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "email_submission" {
|
|
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.email[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = "0.0.0.0/0"
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 465
|
|
max = 465
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "email_submission_starttls" {
|
|
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.email[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = "0.0.0.0/0"
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 587
|
|
max = 587
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "oci_core_network_security_group_security_rule" "email_imaps" {
|
|
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
|
|
network_security_group_id = oci_core_network_security_group.email[0].id
|
|
direction = "INGRESS"
|
|
protocol = "6"
|
|
source = "0.0.0.0/0"
|
|
source_type = "CIDR_BLOCK"
|
|
tcp_options {
|
|
destination_port_range {
|
|
min = 993
|
|
max = 993
|
|
}
|
|
}
|
|
}
|
|
|
|
data "oci_core_instance" "existing_edge" {
|
|
count = var.enabled && var.edge_enabled && trimspace(var.edge_existing_instance_id) != "" ? 1 : 0
|
|
instance_id = var.edge_existing_instance_id
|
|
}
|
|
|
|
resource "oci_core_instance" "edge" {
|
|
count = var.enabled && var.edge_enabled && trimspace(var.edge_existing_instance_id) == "" ? 1 : 0
|
|
availability_domain = var.edge_availability_domain
|
|
compartment_id = local.target_compartment_id
|
|
display_name = var.edge_display_name
|
|
shape = var.edge_shape
|
|
shape_config {
|
|
ocpus = var.edge_ocpus
|
|
memory_in_gbs = var.edge_memory_in_gbs
|
|
}
|
|
source_details {
|
|
source_type = "image"
|
|
source_id = var.edge_image_id
|
|
boot_volume_size_in_gbs = var.edge_boot_volume_size_in_gbs
|
|
}
|
|
create_vnic_details {
|
|
subnet_id = local.public_subnet_id
|
|
assign_public_ip = true
|
|
hostname_label = var.edge_hostname_label
|
|
nsg_ids = [oci_core_network_security_group.edge[0].id]
|
|
}
|
|
metadata = { ssh_authorized_keys = var.ssh_public_key }
|
|
freeform_tags = merge(local.base_tags, { role = "edge" })
|
|
}
|
|
|
|
resource "oci_core_instance" "email" {
|
|
count = var.enabled && var.email_enabled ? 1 : 0
|
|
availability_domain = var.email_availability_domain
|
|
compartment_id = local.target_compartment_id
|
|
display_name = var.email_display_name
|
|
shape = var.email_shape
|
|
shape_config {
|
|
ocpus = var.email_ocpus
|
|
memory_in_gbs = var.email_memory_in_gbs
|
|
}
|
|
source_details {
|
|
source_type = "image"
|
|
source_id = var.email_image_id
|
|
boot_volume_size_in_gbs = var.email_boot_volume_size_in_gbs
|
|
}
|
|
create_vnic_details {
|
|
subnet_id = local.public_subnet_id
|
|
assign_public_ip = true
|
|
hostname_label = var.email_hostname_label
|
|
nsg_ids = [oci_core_network_security_group.email[0].id]
|
|
}
|
|
metadata = { ssh_authorized_keys = var.ssh_public_key }
|
|
freeform_tags = merge(local.base_tags, { role = "email" })
|
|
}
|