Skip to content

Commit

Permalink
Merge pull request #10 from remerge/CORE-15-simplify-netbox-dns
Browse files Browse the repository at this point in the history
Simplify resource creation in Netbox and DNS
  • Loading branch information
hollow committed Jun 29, 2023
2 parents 26fb364 + 8ae5d8f commit 777f2b7
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 31 deletions.
2 changes: 1 addition & 1 deletion google/container/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module "vm" {
}]
volumeMounts = [for name, value in var.volumes : {
name = name
mountPath = value.path
mountPath = coalesce(value.path, "/${name}")
}]
}]
volumes = [for name, value in var.volumes : {
Expand Down
2 changes: 1 addition & 1 deletion google/container/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ variable "volumes" {
type = map(object({
type = optional(string, "pd-ssd")
size = optional(number, 10)
path = string
path = optional(string, null)
}))
default = {}
}
Expand Down
18 changes: 18 additions & 0 deletions google/sql/postgresql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ resource "google_sql_database_instance" "main" {
# checkov:skip=CKV2_GCP_13:Don't log SQL statement duration
}

module "netbox-vm" {
source = "../../../netbox/vm"

project = var.project
domain = var.domain

name = coalesce(var.hostname, var.name)

role = "PostgreSQL"
platform = "Cloud SQL"
site = var.site
cluster = var.cluster
tags = [var.project]

interface = "internal"
ip_address = google_sql_database_instance.main.private_ip_address
}

data "google_compute_default_service_account" "default" {}

resource "google_project_iam_member" "compute_default_service_account" {
Expand Down
40 changes: 30 additions & 10 deletions google/sql/postgresql/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
variable "project" {
type = string
default = null
}

variable "region" {
type = string
default = null
}

variable "name" {
type = string
}

variable "hostname" {
type = string
default = null
}

variable "domain" {
type = any
default = null
}

variable "site" {
type = any
default = null
}

variable "cluster" {
type = any
default = null
}

variable "database_version" {
type = string
default = "POSTGRES_15"
Expand All @@ -22,13 +52,3 @@ variable "backup" {
})
default = {}
}

variable "project" {
type = string
default = null
}

variable "region" {
type = string
default = null
}
31 changes: 14 additions & 17 deletions google/vm/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
locals {
domain = var.domain != null ? trimsuffix(var.domain.dns_name, ".") : null
hostname = var.domain != null ? "${coalesce(var.hostname, var.name)}.${local.domain}" : null
metadata = var.metadata != null ? var.metadata : local.template.metadata
hostname = coalesce(var.hostname, var.name)
domain = var.domain != null ? trimsuffix(var.domain.dns_name, ".") : "local"
fqdn = "${local.hostname}.${local.domain}"
metadata = coalesce(var.metadata, local.template.metadata)
template = data.google_compute_instance_template.main
}

Expand All @@ -16,7 +17,7 @@ resource "google_compute_instance_from_template" "main" {
zone = var.zone

name = var.name
hostname = local.hostname
hostname = local.fqdn
metadata = merge(local.metadata, {
# https://docs.bridgecrew.io/docs/bc_gcp_networking_8
block-project-ssh-keys = true
Expand Down Expand Up @@ -47,28 +48,24 @@ resource "google_compute_instance_from_template" "main" {
resource "google_compute_disk" "main" {
for_each = var.volumes
project = var.project
zone = var.zone
name = "${var.name}-${each.key}"
type = each.value.type
size = each.value.size
}

resource "google_dns_record_set" "instance" {
count = var.domain != null ? 1 : 0
project = var.project
managed_zone = var.domain.name
name = "${google_compute_instance_from_template.main.hostname}."
type = "A"
ttl = 300
rrdatas = [google_compute_instance_from_template.main.network_interface[0].network_ip]
moved {
from = google_dns_record_set.instance
to = module.netbox-vm.google_dns_record_set.main
}

module "netbox-vm" {
source = "../../netbox/vm"

name = coalesce(
google_compute_instance_from_template.main.hostname,
google_compute_instance_from_template.main.name,
)
project = var.project
domain = var.domain

name = local.hostname

role = var.role
platform = var.platform
Expand All @@ -77,5 +74,5 @@ module "netbox-vm" {
tags = [var.project]

interface = var.interface
ip_address = "${google_compute_instance_from_template.main.network_interface[0].network_ip}/32"
ip_address = google_compute_instance_from_template.main.network_interface[0].network_ip
}
22 changes: 20 additions & 2 deletions netbox/vm/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
locals {
hostname = coalesce(var.hostname, var.name)
fqdn = try("${local.hostname}.${trimsuffix(var.domain.dns_name, ".")}", local.hostname)
ip_address = var.ip_address != null ? split("/", var.ip_address)[0] : null
ip_prefix = var.ip_address != null ? try(split("/", var.ip_address)[1], "32") : null
}

resource "netbox_virtual_machine" "main" {
name = var.name
name = local.fqdn
role_id = data.netbox_device_role.main.id
platform_id = data.netbox_platform.main.id
site_id = var.site != null ? data.netbox_site.main[0].id : null
Expand Down Expand Up @@ -40,7 +47,8 @@ resource "netbox_interface" "main" {
resource "netbox_ip_address" "main" {
count = var.interface != null ? 1 : 0
interface_id = netbox_interface.main[0].id
ip_address = var.ip_address
ip_address = "${local.ip_address}/${local.ip_prefix}"
dns_name = local.fqdn
status = "active"
tags = var.tags
}
Expand All @@ -50,3 +58,13 @@ resource "netbox_primary_ip" "main" {
virtual_machine_id = netbox_virtual_machine.main.id
ip_address_id = netbox_ip_address.main[0].id
}

resource "google_dns_record_set" "main" {
count = var.domain != null ? 1 : 0
project = var.project
managed_zone = var.domain.name
name = "${local.fqdn}."
type = "A"
ttl = 300
rrdatas = [local.ip_address]
}
15 changes: 15 additions & 0 deletions netbox/vm/variables.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
variable "project" {
type = string
default = null
}

variable "name" {
type = string
}

variable "hostname" {
type = string
default = null
}

variable "domain" {
type = any
default = null
}

variable "role" {
type = string
default = "Other"
Expand Down
5 changes: 5 additions & 0 deletions netbox/vm/versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
terraform {
required_providers {
# https://registry.terraform.io/providers/hashicorp/google/latest
google = {
source = "hashicorp/google"
version = "~> 4.64"
}
# https://registry.terraform.io/providers/e-breuninger/netbox/latest
netbox = {
source = "e-breuninger/netbox"
Expand Down

0 comments on commit 777f2b7

Please sign in to comment.