// A small fleet on DigitalOcean: a droplet made and waited for until it has an address, rebooted,
// listed past one page, a domain with its records, and a Spaces key made and revoked.
//
//   DIGITALOCEAN_TOKEN=... quillo run digitalocean.qwe      then open http://localhost:8080/droplets, /action, /dns, /spaces and /failures
//
// The token needs write scope. These routes spend money on a real account. DIGITALOCEAN_API_URL
// reaches a test server.

use "digitalocean.qwer"

route GET /droplets {
    let made = digitalocean_droplet_create({ name: "web-9", region: "ams3", size: "s-1vcpu-1gb", image: "ubuntu-24-04-x64", tags: ["web"] }, { wait: true, every_ms: 100 })?
    let all = digitalocean_droplets({})?
    let web = digitalocean_droplets({ tag: "web" })?
    { status: made.status, ip: made.public_ip, total: len(all), tagged: len(web), one: digitalocean_droplet(made.id, {})?.name, rate: digitalocean_rate().remaining != null }
}

route GET /action {
    let done = digitalocean_droplet_action(1001, "reboot", null, { wait: true, every_ms: 100 })?
    let queued = digitalocean_droplet_action(1001, "snapshot", { name: "before-upgrade" }, {})?
    let spare = digitalocean_droplet_create({ name: "spare", region: "ams3", size: "s-1vcpu-1gb", image: "ubuntu-24-04-x64" }, {})?
    { reboot: done.status, snapshot: queued.status, failed: digitalocean_droplet_action(1002, "power_on", null, { wait: true, every_ms: 50 }).error, deleted: digitalocean_droplet_delete(spare.id, {})? }
}

route GET /dns {
    digitalocean_domain_create("shop.example", "192.0.2.10", {})?
    let rec = digitalocean_record_create("shop.example", { type: "A", name: "www", data: "192.0.2.11", ttl: 300 }, {})?
    let txt = digitalocean_record_create("shop.example", { type: "TXT", name: "@", data: "v=spf1 -all", ttl: 3600 }, {})?
    let changed = digitalocean_record_update("shop.example", rec.id, { data: "192.0.2.12" }, {})?
    digitalocean_record_delete("shop.example", txt.id, {})?
    {
        domains: map(digitalocean_domains({})?, (d) => d.name),
        www: changed.data,
        records: len(digitalocean_records("shop.example", {})?),
        only_a: map(digitalocean_records("shop.example", { type: "A", name: "www.shop.example" })?, (r) => r.name),
        taken: digitalocean_domain_create("shop.example", "", {}).error,
        removed: digitalocean_domain_delete("shop.example", {})?,
    }
}

route GET /spaces {
    let key = digitalocean_spaces_key_create("backup", [{ bucket: "photos", permission: "read" }], {})?
    let listed = digitalocean_spaces_keys({})?
    { access: key.access_key, secret_shown: len(key.secret_key) > 0, listed: len(listed), hides_secret: listed[0].secret_key == "", bad: digitalocean_spaces_key_create("x", [{ bucket: "a", permission: "write" }], {}).error, revoked: digitalocean_spaces_key_delete(key.access_key, {})? }
}

route GET /failures = {
    missing: digitalocean_droplet(404404, {}).error,
    incomplete: digitalocean_droplet_create({ name: "x" }, {}).error,
    limited: digitalocean_request("GET", "/v2/account", null, {})?.account.status,
}
