defmodule ArcadiaCloud.Sync.SnapshotsWorker do @moduledoc """ Full sync of DO snapshots — both droplet snapshots and volume snapshots. DO returns them together with a `resource_type` field that we use to differentiate; both land as kind="snapshot" with `attrs.resource_type`. """ use Oban.Worker, queue: :cloud_sync_full, max_attempts: 3 alias ArcadiaCloud.Cloud alias ArcadiaCloud.DigitalOcean.Client @kind "snapshot" @provider "digitalocean" @impl Oban.Worker def perform(_job) do now = DateTime.utc_now() |> DateTime.truncate(:second) with {:ok, snapshots} <- Client.list_snapshots() do Enum.each(snapshots, fn s -> Cloud.upsert_resource(normalize(s, now)) end) Cloud.mark_stale(@kind, now) :ok end end defp normalize(s, now) do region = case s["regions"] do [first | _] when is_binary(first) -> first _ -> nil end %{ provider: @provider, provider_id: to_string(s["id"]), kind: @kind, name: s["name"], region: region, status: "active", tags: s["tags"] || [], attrs: %{ resource_type: s["resource_type"], resource_id: s["resource_id"], size_gigabytes: s["size_gigabytes"], min_disk_size: s["min_disk_size"], regions: s["regions"], do_created_at: s["created_at"] }, first_seen_at: now, last_seen_at: now } end end