locals {
    cdi = { for k, v in var.virt.cdi : k => v if k!="enable" }
    kubevirt = { for k, v in var.virt.kubevirt : k => v if k!="enable" }
    bridges = { for k, v in var.virt.bridges : k => v if k!="enable" }
    multus = { for k, v in var.virt.multus : k => v if k!="enable" }
    whereabouts = { for k, v in var.virt.whereabouts : k => v if k!="enable" }
}

resource "kubernetes_namespace_v1" "virt-ns" {
  count = var.virt.enable && ( var.virt.bridges.enable || var.virt.multus.enable || var.virt.whereabouts.enable || var.virt.cdi.enable || var.virt.kubevirt.enable)? 1 : 0
  metadata {
    annotations = local.annotations
    labels = local.common-labels
    name = var.virt.namespace
  }
}

resource "kubectl_manifest" "cdi" {
  count = var.virt.enable && var.virt.cdi.enable ? 1 : 0
  depends_on = [kubernetes_namespace_v1.virt-ns]
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "cdi"
      namespace: "${var.virt.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "virt"
      component: "cdi"
      options: ${jsonencode(local.cdi)}
  EOF
}

resource "kubectl_manifest" "bridges" {
  count = var.virt.enable && var.virt.bridges.enable ? 1 : 0
  depends_on = [kubernetes_namespace_v1.virt-ns]
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "bridges"
      namespace: "${var.virt.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "virt"
      component: "bridges"
      options: ${jsonencode(local.bridges)}
  EOF
}

resource "kubectl_manifest" "multus" {
  count = var.virt.enable && var.virt.multus.enable ? 1 : 0
  depends_on = [kubernetes_namespace_v1.virt-ns]
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "multus"
      namespace: "${var.virt.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "virt"
      component: "multus"
      options: ${jsonencode(local.multus)}
  EOF
}

resource "kubectl_manifest" "whereabouts" {
  count = var.virt.enable && var.virt.whereabouts.enable ? 1 : 0
  depends_on = [kubernetes_namespace_v1.virt-ns]
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "whereabouts"
      namespace: "${var.virt.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "virt"
      component: "whereabouts"
      options: ${jsonencode(local.whereabouts)}
  EOF
}

resource "kubectl_manifest" "kubevirt" {
  count = var.virt.enable && var.virt.kubevirt.enable ? 1 : 0
  depends_on = [kubernetes_namespace_v1.virt-ns]
  yaml_body  = <<-EOF
    apiVersion: "vynil.solidite.fr/v1"
    kind: "Install"
    metadata:
      name: "kubevirt"
      namespace: "${var.virt.namespace}"
      labels: ${jsonencode(local.common-labels)}
    spec:
      distrib: "${var.component}"
      category: "virt"
      component: "kubevirt"
      options: ${jsonencode(local.kubevirt)}
  EOF
}
