2022-03-21 23:27:54 -04:00

172 lines
3.6 KiB
HCL

# Variables
# =====================
variable "name" {
type = string
description = "Network name"
}
variable "mode" {
description = "Network mode (route|nat)"
type = string
default = "nat"
}
variable "bridge" {
description = "Network interface name (16 char max, virbr_)"
type = string
}
variable "domain" {
description = "Network domain"
type = string
default = "local"
}
variable "gw4" {
description = "Network address"
type = string
default = "192.168.0.1"
}
variable "mask" {
description = "Netmask of the network"
type = number
default = 24
}
#
variable "nat_enable" {
description = "Enable NAT from outside of libvirt"
type = bool
default = false
}
variable "nat_driver" {
description = "Default NAT driver"
type = string
default = "ferm"
}
variable "nat_device" {
description = "Device to forward unrouted traffic (ie: eth0)"
type = string
default = "enp1s0"
}
#
variable "vip" {
description = "List of vips"
type = list
default = []
}
variable "subnets" {
description = "List of subnets"
type = list
default = []
}
# Resources
# =====================
resource "libvirt_network" "netdef" {
name = var.name
mode = var.mode
bridge = var.bridge
domain = var.domain
addresses = ["${var.gw4}/${var.mask}"]
autostart = true
dns {
enabled = true
local_only = true
hosts {
hostname = "gw"
ip = cidrhost("${var.gw4}/${var.mask}", 1)
}
dynamic "hosts" {
for_each = var.vip
content {
hostname = try(hosts.value.hostname)
ip = try(hosts.value.ip)
}
}
}
}
# Tests
# =====================
# resource "consul_keys" "app" {
# count = var.nat_enable ? 1 : 0
#
# depends_on = [libvirt_network.netdef]
# key {
# path = "tests/net/${var.bridge}/nat"
# value = "${var.nat_device} - ${ var.nat_enable }"
# delete = true
# }
# }
# TMP DISABVLED, require ferm first resource "ssh_resource" "nat_config" {
# TMP DISABVLED, require ferm first count = var.nat_enable ? 1 : 0
# TMP DISABVLED, require ferm first
# TMP DISABVLED, require ferm first host = "192.168.142.10"
# TMP DISABVLED, require ferm first user = "root"
# TMP DISABVLED, require ferm first agent = true
# TMP DISABVLED, require ferm first
# TMP DISABVLED, require ferm first depends_on = [libvirt_network.netdef]
# TMP DISABVLED, require ferm first
# TMP DISABVLED, require ferm first file {
# TMP DISABVLED, require ferm first content = templatefile("${path.module}/templates/ferm_nat.tpl", {
# TMP DISABVLED, require ferm first device = "${var.nat_device}"
# TMP DISABVLED, require ferm first driver = "${var.nat_driver}"
# TMP DISABVLED, require ferm first })
# TMP DISABVLED, require ferm first destination = "/etc/libvirt/hooks/state/net/${var.bridge}_nat.ini"
# TMP DISABVLED, require ferm first permissions = "0644"
# TMP DISABVLED, require ferm first }
# TMP DISABVLED, require ferm first }
# resource "ssh_resource" "ipvs_config" {
# count = var.nat_enable ? 1 : 0
#
# host = "192.168.142.10"
# user = "root"
# agent = true
#
# depends_on = [libvirt_network.netdef]
#
# file {
# content = templatefile("${path.module}/templates/ipvs_config.tpl", {
# vips = var.vip
# })
# destination = "/etc/libvirt/hooks/state/net/${var.bridge}_ipvs.ini"
# permissions = "0644"
# }
# }
# Output
# =====================
output "networks" {
value = resource.libvirt_network.netdef
}
output "subnets" {
value = var.subnets
}
output "vips" {
value = var.vip
}