data "docker_registry_image" "unifi_controller" {
  name = "jacobalberty/unifi"
}
resource "docker_service" "unifi_controller" {
  name     = "unifi_controller"
  provider = docker.unifi
  task_spec {
    container_spec {
      image = "${data.docker_registry_image.unifi_controller.name}@${data.docker_registry_image.unifi_controller.sha256_digest}"
      env = {
        TZ = "Europe/Amsterdam"
      }
      mounts {
        target = "/unifi"
        source = "/home/techinc/unifi"
        type   = "bind"
      }
    }
    restart_policy {
      condition = "any"
      delay     = "0s"
      window    = "0s"
    }
  }
  endpoint_spec {
    ports {
      target_port    = 8443
      published_port = 443
      publish_mode   = "ingress"
    }
    ports {
      target_port    = 8443
      published_port = 8443
      publish_mode   = "ingress"
    }
    ports {
      target_port    = 3478
      published_port = 3478
      publish_mode   = "ingress"
      protocol       = "udp"
    }
    ports {
      target_port    = 10001
      published_port = 10001
      publish_mode   = "ingress"
      protocol       = "udp"
    }
    ports {
      target_port    = 8080
      published_port = 8080
      publish_mode   = "ingress"
    }
  }
}

resource "docker_service" "unifi_controller_restored_from_backup" {
  name     = "unifi_controller_restored_from_backup"
  provider = docker.unifi
  task_spec {
    container_spec {
      image = "${data.docker_registry_image.unifi_controller.name}@${data.docker_registry_image.unifi_controller.sha256_digest}"
      env = {
        TZ = "Europe/Amsterdam"
      }
      mounts {
        target = "/unifi"
        source = "/home/techinc/unifi_restored_from_backup"
        type   = "bind"
      }
    }
    restart_policy {
      condition = "any"
      delay     = "0s"
      window    = "0s"
    }
  }
  endpoint_spec {
    ports {
      target_port    = 443
      published_port = 444
      publish_mode   = "ingress"
    }
  }
}

data "docker_registry_image" "http2https" {
  name = "articulate/http-to-https"
}
resource "docker_service" "unifi_http2https" {
  name     = "http2https"
  provider = docker.unifi

  task_spec {
    container_spec {
      image = "${data.docker_registry_image.http2https.name}@${data.docker_registry_image.http2https.sha256_digest}"
    }
    restart_policy {
      condition = "any"
      delay     = "0s"
      window    = "0s"
    }
  }
  endpoint_spec {
    ports {
      target_port    = 80
      published_port = 80
      publish_mode   = "ingress"
    }
  }
}

data "docker_registry_image" "iperf" {
  name     = "loganmarchione/docker-iperf3:latest"
  provider = docker.unifi
}
resource "docker_service" "unifi_iperf" {
  name     = "iperf"
  provider = docker.unifi
  task_spec {
    container_spec {
      image = "${data.docker_registry_image.iperf.name}@${data.docker_registry_image.iperf.sha256_digest}"
    }
    restart_policy {
      condition = "any"
      delay     = "0s"
      window    = "0s"
    }
  }
  endpoint_spec {
    ports {
      target_port    = 5201
      published_port = 5201
      publish_mode   = "ingress"
      protocol       = "tcp"
    }
    ports {
      target_port    = 5201
      published_port = 5201
      publish_mode   = "ingress"
      protocol       = "udp"
    }
  }
}