Two patterns added: 1. ProjectsWorker now does URN-discover for kinds without a dedicated sync worker (spaces_bucket, managed_db, k8s_cluster, etc.). For these, it inserts a minimal placeholder row when the URN points to something not yet in inventory. Kinds with dedicated workers (droplet, snapshot, volume, etc.) still get attribution-only — the worker is source of truth for richer attrs. Implemented by splitting attribute_or_discover/4 on a @dedicated_kinds whitelist. 2. New BackupsWorker pulls /v2/droplets/:id/backups for each active droplet. DO automated backups aren't in /v2/snapshots; they live per droplet. Cron: hourly at :41. Kind="droplet_backup". URN normalization extended for two more aliases DO emits: "volumesnapshot" → snapshot (was creating a duplicate row) "image" → snapshot (DO droplet snapshots show as do:image:id) Billing.find_resource/1 gets a kind-specific clause for droplet_backup that matches to the parent droplet by name, since invoice lines for backups read "<droplet-name> (Weekly Backup Services)" — the line is a per-droplet subscription, not a per-backup-snapshot fee. Live verified on the same April 2026 invoice: - 6 Spaces buckets discovered via URN (account has 6, only 1 visible in the invoice as the $5 subscription line — that's account-level so it can't tie to a specific bucket, expected). - 4 droplet backups discovered via BackupsWorker; the git.sky-ai.com backup line now matches (repo.sky-ai.com backup line can't match — that droplet was destroyed). - Of 16 unmatched lines: 11 are destroyed historic resources, 1 is GST, 1 is the account-level Spaces subscription, 3 are likely tiny snapshot name variances. Effectively ~100% of currently-existing billable resources match. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
163 lines
5.1 KiB
Elixir
163 lines
5.1 KiB
Elixir
defmodule ArcadiaCloud.DigitalOcean.Client do
|
|
@moduledoc """
|
|
Thin Req wrapper over the DigitalOcean v2 API.
|
|
|
|
Token resolution: per-purpose, looked up via `ArcadiaCloud.DigitalOcean.Tokens`.
|
|
Phase 0/1: env var `DO_API_TOKEN`. Phase 2: from the secrets vault.
|
|
|
|
Paginated list endpoints stream all pages by default.
|
|
"""
|
|
|
|
alias ArcadiaCloud.DigitalOcean.Tokens
|
|
|
|
@base "https://api.digitalocean.com/v2"
|
|
@page_size 100
|
|
|
|
# ---- public ---------------------------------------------------------------
|
|
|
|
def list_droplets(opts \\ []), do: list_paginated("/droplets", "droplets", opts)
|
|
def list_projects(opts \\ []), do: list_paginated("/projects", "projects", opts)
|
|
def list_domains(opts \\ []), do: list_paginated("/domains", "domains", opts)
|
|
def list_volumes(opts \\ []), do: list_paginated("/volumes", "volumes", opts)
|
|
def list_floating_ips(opts \\ []), do: list_paginated("/floating_ips", "floating_ips", opts)
|
|
def list_snapshots(opts \\ []), do: list_paginated("/snapshots", "snapshots", opts)
|
|
def list_firewalls(opts \\ []), do: list_paginated("/firewalls", "firewalls", opts)
|
|
def list_load_balancers(opts \\ []), do: list_paginated("/load_balancers", "load_balancers", opts)
|
|
|
|
def list_droplet_backups(droplet_id, opts \\ []) do
|
|
list_paginated("/droplets/#{droplet_id}/backups", "backups", opts)
|
|
end
|
|
|
|
# ---- billing --------------------------------------------------------------
|
|
|
|
def get_balance(opts \\ []) do
|
|
request(:get, "/customers/my/balance", purpose: opts[:purpose] || "billing")
|
|
end
|
|
|
|
def list_billing_history(opts \\ []) do
|
|
list_paginated("/customers/my/billing_history", "billing_history",
|
|
Keyword.put(opts, :purpose, opts[:purpose] || "billing"))
|
|
end
|
|
|
|
def get_invoice_summary(invoice_uuid, opts \\ []) do
|
|
request(:get, "/customers/my/invoices/#{invoice_uuid}/summary",
|
|
purpose: opts[:purpose] || "billing")
|
|
end
|
|
|
|
@doc """
|
|
Fetch the CSV body for an invoice. Returns {:ok, csv_string} | {:error, _}.
|
|
"""
|
|
def fetch_invoice_csv(invoice_uuid, opts \\ []) do
|
|
purpose = opts[:purpose] || "billing"
|
|
|
|
with {:ok, token} <- Tokens.fetch(purpose) do
|
|
case Req.request(
|
|
method: :get,
|
|
url: @base <> "/customers/my/invoices/#{invoice_uuid}/csv",
|
|
headers: [
|
|
{"authorization", "Bearer " <> token},
|
|
{"accept", "text/csv"}
|
|
],
|
|
retry: :transient,
|
|
max_retries: 3,
|
|
decode_body: false
|
|
) do
|
|
{:ok, %Req.Response{status: 200, body: body}} when is_binary(body) ->
|
|
{:ok, body}
|
|
|
|
{:ok, %Req.Response{status: status, body: body}} ->
|
|
{:error, {:http, status, body}}
|
|
|
|
{:error, e} ->
|
|
{:error, {:transport, e}}
|
|
end
|
|
end
|
|
end
|
|
|
|
def create_project(name, purpose, description \\ "", opts \\ []) do
|
|
body = %{
|
|
name: name,
|
|
purpose: purpose,
|
|
description: description,
|
|
environment: "Development"
|
|
}
|
|
|
|
request(:post, "/projects", body: body, purpose: opts[:purpose] || "provisioning")
|
|
|> case do
|
|
{:ok, %{"project" => project}} -> {:ok, project}
|
|
other -> other
|
|
end
|
|
end
|
|
|
|
def list_project_resources(project_id, opts \\ []) do
|
|
list_paginated("/projects/#{project_id}/resources", "resources", opts)
|
|
end
|
|
|
|
def assign_to_project(project_id, urns, opts \\ []) when is_list(urns) do
|
|
request(:post, "/projects/#{project_id}/resources",
|
|
body: %{resources: urns},
|
|
purpose: opts[:purpose] || "provisioning"
|
|
)
|
|
end
|
|
|
|
# ---- core -----------------------------------------------------------------
|
|
|
|
defp list_paginated(path, root_key, opts) do
|
|
purpose = opts[:purpose] || "sync_full"
|
|
do_paginate(path, root_key, purpose, [], 1)
|
|
end
|
|
|
|
defp do_paginate(path, root_key, purpose, acc, page) do
|
|
params = [page: page, per_page: @page_size]
|
|
|
|
case request(:get, path, params: params, purpose: purpose) do
|
|
{:ok, %{} = body} ->
|
|
items = Map.get(body, root_key, [])
|
|
new_acc = acc ++ items
|
|
|
|
if has_next?(body) do
|
|
do_paginate(path, root_key, purpose, new_acc, page + 1)
|
|
else
|
|
{:ok, new_acc}
|
|
end
|
|
|
|
err ->
|
|
err
|
|
end
|
|
end
|
|
|
|
defp has_next?(%{"links" => %{"pages" => %{"next" => _}}}), do: true
|
|
defp has_next?(_), do: false
|
|
|
|
defp request(method, path, opts) do
|
|
purpose = Keyword.fetch!(opts, :purpose)
|
|
|
|
with {:ok, token} <- Tokens.fetch(purpose) do
|
|
req_opts =
|
|
[
|
|
method: method,
|
|
url: @base <> path,
|
|
headers: [{"authorization", "Bearer " <> token}],
|
|
retry: :transient,
|
|
max_retries: 3
|
|
]
|
|
|> maybe_put(:params, opts[:params])
|
|
|> maybe_put(:json, opts[:body])
|
|
|
|
case Req.request(req_opts) do
|
|
{:ok, %Req.Response{status: status, body: body}} when status in 200..299 ->
|
|
{:ok, body}
|
|
|
|
{:ok, %Req.Response{status: status, body: body}} ->
|
|
{:error, {:http, status, body}}
|
|
|
|
{:error, exception} ->
|
|
{:error, {:transport, exception}}
|
|
end
|
|
end
|
|
end
|
|
|
|
defp maybe_put(opts, _key, nil), do: opts
|
|
defp maybe_put(opts, key, value), do: Keyword.put(opts, key, value)
|
|
end
|