terraform {
  required_providers {
    ovh = {
      source = "ovh/ovh"
      version = "2.3.0"
    }
  }
}

provider "ovh" {
  endpoint      = "ovh-eu"
}

variable project_id {
  type = string
  default = "5a08aa20856c4c549ed18d3fd5c7418a"
}
variable region {
  type = string
  default = "SBG5"
}
# Ubuntu 22.04
variable image_id {
  type = string
  default = "ecf71715-17f5-44f6-8511-624de05f3041"
}
data "ovh_cloud_project_flavors" "flavors" {
  service_name = var.project_id
  name_filter = "r3-16-flex"
  region = var.region
}

resource "ovh_cloud_project_instance" "simpl_vm" {
  service_name  = var.project_id
  region = var.region
  billing_period = "hourly"
  boot_from {
    image_id = var.image_id
  }
  flavor {
    flavor_id = tolist(data.ovh_cloud_project_flavors.flavors.flavors)[0].id
  }
  name = "offering-enterprise-server-{UUID}-terraform"
  ssh_key_create {
    name       = "default-user-key"
    public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDjSFhpjNNDz3olIFuLdROifKJXekAu2CKQ7UyziJJOAPD2wP4eYEgTR6R12TnDHMDerNBLo8d/zIhkmmvF8zr4vBgB8nLHHV/hX6J8b1hg9o9ZtFY1czrdgEzjKtEEwqltQANWQqbg2i4cFDC96ZQRm8p/2XlKvdPkudHwnOqa6NtnE5vfBV0tk9uBbvr1mR8f9yMKpeTxyZUqOkhTZq8FIqS1h0n8Sh8CUYpTpsPilSspAs4xd/oR1FdINLGG4g9pJ/iRgsgMesQHb1qs8h/jws/XOE3Dg/+eDR5AsMz1DIuOjfVrT+xSFVtNxRQu5i6/mqad8NjeI+nR/TJjdnyS8k/JQEXWqOYIYOiozeueaP0fuulJuk0hf34dKrQqFp84Mqd7vE/Z4srdbyC1OfxCMhs3j7Ja9eujpAcp0aLl+mUGV9GKKANJlGmnt76YX5SPCKWEQb5plSXEbOkXPNCnJqF/x99AbPluSc3dYqBJmfhtu2w83jo6m+K50/F6eEU= cbeti@LXBU-2N877S3"
  }
  network {
    public = true
  }
  user_data = "I2Nsb3VkLWNvbmZpZwpob3N0bmFtZTogZGVmYXVsdC1zZXJ2ZXIKc3NoX3B3YXV0aDogdHJ1ZQpjaHBhc3N3ZDoKICBleHBpcmU6IGZhbHNlCgp1c2VyczoKICAtIG5hbWU6IGRlZmF1bHQKICAtIG5hbWU6IHZtdXNlcgogICAgcGFzc3dkOiAkNiRaVkJDNmJoeDZRWE1OdHM3JC82YTBhVFU2c3BDQmZWU1FPbHZCbWtKVzd0N0VtWDFJVnJZUEFtamluVlM3RFRRS0E5azBwM0MycDVNWk5zeWxjSjZyQ0VPeEV2bkdyeXQyV1AyTWgvCiAgICBzaGVsbDogL2Jpbi9iYXNoCiAgICBsb2NrX3Bhc3N3ZDogZmFsc2UKICAgIHN1ZG86IEFMTD0oQUxMKSBOT1BBU1NXRDpBTEwKICAgIGdyb3VwczogdXNlcnMsIGFkbWluLCBzdWRvCgpydW5jbWQ6CiAgLSBlY2hvICJyZWdlbmVyYXRpbmcgaG9zdCBrZXlzIgogIC0gcm0gLWYgL2V0Yy9zc2gvc3NoX2hvc3RfKgogIC0gc3NoLWtleWdlbiAtQQogIC0gZWNobyAicmVzdGFydGluZyBzc2hkIgogIC0gc3lzdGVtY3RsIHJlc3RhcnQgc3NoZAoKb3V0cHV0OgogIGFsbDogInwgdGVlIC1hIC92YXIvbG9nL2Nsb3VkLWluaXQtZGVidWcubG9nIgpmaW5hbF9tZXNzYWdlOiAiRGVmYXVsdCBWTSBDbG91ZGluaXQgZG9uZSI="
}

output "vmIps" {
  depends_on = [ovh_cloud_project_instance.simpl_vm]
  value = tolist(ovh_cloud_project_instance.simpl_vm.addresses[*].ip)
}

# data ovh_cloud_project_images image{
#   service_name  = var.project_id
#   region = "SBG5"
# }
#
# data ovh_cloud_project_regions regions {
#   service_name  = var.project_id
# }
