diff --git a/bootstrap/oci/main.tf b/bootstrap/oci/main.tf index 507c29c..7b3edad 100644 --- a/bootstrap/oci/main.tf +++ b/bootstrap/oci/main.tf @@ -1,12 +1,20 @@ locals { target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id + email_compartment_id = var.enabled && var.email_create_compartment ? oci_identity_compartment.email[0].id : (trimspace(var.email_compartment_id) != "" ? var.email_compartment_id : local.target_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 + email_subnet_id = trimspace(var.existing_email_subnet_id) != "" ? var.existing_email_subnet_id : oci_core_subnet.email[0].id + email_route_table_id = trimspace(var.existing_vcn_id) != "" ? data.oci_core_subnet.existing_public[0].route_table_id : local.public_route_table_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" }) } +data "oci_core_subnet" "existing_public" { + count = var.enabled && trimspace(var.existing_public_subnet_id) != "" ? 1 : 0 + subnet_id = var.existing_public_subnet_id +} + resource "oci_identity_compartment" "homelab" { count = var.enabled && var.create_compartment ? 1 : 0 compartment_id = var.tenancy_id @@ -16,6 +24,15 @@ resource "oci_identity_compartment" "homelab" { freeform_tags = local.base_tags } +resource "oci_identity_compartment" "email" { + count = var.enabled && var.email_enabled && var.email_create_compartment ? 1 : 0 + compartment_id = var.tenancy_id + name = var.email_compartment_name + description = "Jeannie-managed email service resources" + 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 @@ -92,6 +109,57 @@ resource "oci_core_subnet" "private" { freeform_tags = local.base_tags } +resource "oci_core_security_list" "email_subnet" { + count = var.enabled && var.email_enabled && trimspace(var.existing_email_subnet_id) == "" ? 1 : 0 + compartment_id = local.email_compartment_id + vcn_id = local.network_vcn_id + display_name = "homelab-email-subnet-security" + freeform_tags = local.base_tags + + egress_security_rules { + protocol = "all" + destination = "0.0.0.0/0" + } + + ingress_security_rules { + protocol = "6" + source = var.admin_cidr + tcp_options { + destination_port_range { + min = 22 + max = 22 + } + } + } + + dynamic "ingress_security_rules" { + for_each = var.open_mail_ports ? [25, 465, 587, 993] : [] + content { + protocol = "6" + source = "0.0.0.0/0" + tcp_options { + destination_port_range { + min = ingress_security_rules.value + max = ingress_security_rules.value + } + } + } + } +} + +resource "oci_core_subnet" "email" { + count = var.enabled && var.email_enabled && trimspace(var.existing_email_subnet_id) == "" ? 1 : 0 + compartment_id = local.target_compartment_id + vcn_id = local.network_vcn_id + cidr_block = var.email_subnet_cidr + display_name = "homelab-email-subnet" + dns_label = "email" + route_table_id = local.email_route_table_id + security_list_ids = [oci_core_security_list.email_subnet[0].id] + prohibit_public_ip_on_vnic = false + 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 @@ -102,7 +170,7 @@ resource "oci_core_network_security_group" "edge" { resource "oci_core_network_security_group" "email" { count = var.enabled && var.email_enabled ? 1 : 0 - compartment_id = local.target_compartment_id + compartment_id = local.email_compartment_id vcn_id = local.network_vcn_id display_name = "homelab-email-nsg" freeform_tags = local.base_tags @@ -261,7 +329,7 @@ resource "oci_core_instance" "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 + compartment_id = local.email_compartment_id display_name = var.email_display_name shape = var.email_shape shape_config { @@ -274,7 +342,7 @@ resource "oci_core_instance" "email" { boot_volume_size_in_gbs = var.email_boot_volume_size_in_gbs } create_vnic_details { - subnet_id = local.public_subnet_id + subnet_id = local.email_subnet_id assign_public_ip = true hostname_label = var.email_hostname_label nsg_ids = [oci_core_network_security_group.email[0].id] diff --git a/bootstrap/oci/outputs.tf b/bootstrap/oci/outputs.tf index 6db6df6..8351bfd 100644 --- a/bootstrap/oci/outputs.tf +++ b/bootstrap/oci/outputs.tf @@ -1,6 +1,7 @@ output "compartment_id" { value = local.target_compartment_id } output "vcn_id" { value = local.network_vcn_id } output "public_subnet_id" { value = local.public_subnet_id } +output "email_subnet_id" { value = local.email_subnet_id } output "private_subnet_id" { value = try(oci_core_subnet.private[0].id, null) } output "edge_instance_id" { value = try(oci_core_instance.edge[0].id, data.oci_core_instance.existing_edge[0].id, null) } output "edge_public_ip" { value = try(oci_core_instance.edge[0].public_ip, data.oci_core_instance.existing_edge[0].public_ip, null) } diff --git a/bootstrap/oci/variables.tf b/bootstrap/oci/variables.tf index d4cf85f..ca37533 100644 --- a/bootstrap/oci/variables.tf +++ b/bootstrap/oci/variables.tf @@ -41,6 +41,21 @@ variable "compartment_description" { default = "Jeannie-managed OCI learning resources" } +variable "email_create_compartment" { + type = bool + default = true +} + +variable "email_compartment_id" { + type = string + default = "" +} + +variable "email_compartment_name" { + type = string + default = "homelab-email" +} + variable "vcn_display_name" { type = string default = "homelab-vcn" @@ -73,6 +88,18 @@ variable "existing_public_subnet_id" { default = "" } +variable "existing_email_subnet_id" { + description = "Existing dedicated email subnet OCID to reuse instead of creating one." + type = string + default = "" +} + +variable "email_subnet_cidr" { + description = "CIDR for the dedicated public email subnet when one must be created." + type = string + default = "10.50.30.0/24" +} + variable "admin_cidr" { description = "CIDR allowed to SSH to the edge and email VMs. Do not leave this at 0.0.0.0/0." type = string @@ -200,6 +227,7 @@ check "foundation_inputs" { trimspace(var.region) != "" && trimspace(var.tenancy_id) != "" && (var.create_compartment || trimspace(var.compartment_id) != "") && + (!var.email_enabled || var.email_create_compartment || trimspace(var.email_compartment_id) != "") && ((trimspace(var.existing_vcn_id) == "" && trimspace(var.existing_public_subnet_id) == "") || (trimspace(var.existing_vcn_id) != "" && trimspace(var.existing_public_subnet_id) != "")) && trimspace(var.admin_cidr) != "" && (!var.edge_enabled || trimspace(var.edge_existing_instance_id) != "" || (trimspace(var.edge_availability_domain) != "" && trimspace(var.edge_image_id) != "")) && diff --git a/lib/jeannie/env.sh b/lib/jeannie/env.sh index 8cbd981..09f8e0b 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -340,8 +340,13 @@ export_oci_foundation_tf_vars() { export_if_unset TF_VAR_create_compartment "${LAB_OCI_CREATE_COMPARTMENT:-}" export_if_unset TF_VAR_compartment_id "${LAB_OCI_COMPARTMENT_ID:-}" export_if_unset TF_VAR_compartment_name "${LAB_OCI_COMPARTMENT_NAME:-}" + export_if_unset TF_VAR_email_create_compartment "${LAB_OCI_EMAIL_CREATE_COMPARTMENT:-}" + export_if_unset TF_VAR_email_compartment_id "${LAB_OCI_EMAIL_COMPARTMENT_ID:-}" + export_if_unset TF_VAR_email_compartment_name "${LAB_OCI_EMAIL_COMPARTMENT_NAME:-}" export_if_unset TF_VAR_existing_vcn_id "${LAB_OCI_EXISTING_VCN_ID:-}" export_if_unset TF_VAR_existing_public_subnet_id "${LAB_OCI_EXISTING_PUBLIC_SUBNET_ID:-}" + export_if_unset TF_VAR_existing_email_subnet_id "${LAB_OCI_EXISTING_EMAIL_SUBNET_ID:-}" + export_if_unset TF_VAR_email_subnet_cidr "${LAB_OCI_EMAIL_SUBNET_CIDR:-}" export_if_unset TF_VAR_admin_cidr "${LAB_OCI_ADMIN_CIDR:-}" export_if_unset TF_VAR_open_mail_ports "${LAB_OCI_OPEN_MAIL_PORTS:-}" export_if_unset TF_VAR_edge_enabled "${LAB_OCI_EDGE_ENABLED:-}"