dotfiles/app/lib/prymn/servers/server.ex

50 lines
1.3 KiB
Elixir
Raw Normal View History

2023-06-09 19:13:27 +00:00
defmodule Prymn.Servers.Server do
@moduledoc false
2023-06-09 19:13:27 +00:00
use Ecto.Schema
import Ecto.Changeset
@typedoc """
A `Server` describes a server resource that was created in the application.
Its status can be one of:
- `:unregistered`: when the resource was just created and the `Prymn Agent`
is not yet installed or active.
- `:registered`: the Agent was installed, and now the resource MUST have a
`public_ip` that we can connect to.
"""
@type t :: %__MODULE__{
id: integer(),
name: String.t(),
public_ip: String.t(),
provider: term(),
registration_token: binary(),
status: term()
}
2023-06-09 19:13:27 +00:00
schema "servers" do
field :name, :string
2023-06-23 07:05:10 +00:00
field :public_ip, :string
2023-06-19 20:28:24 +00:00
field :provider, Ecto.Enum, values: [:Hetzner, :Custom]
2023-06-23 07:05:10 +00:00
field :registration_token, :binary, redact: true
2023-07-09 16:41:41 +00:00
field :status, Ecto.Enum,
values: [:unregistered, :registered],
default: :unregistered
has_many :apps, Prymn.Apps.App
2023-06-09 19:13:27 +00:00
timestamps()
end
@doc false
def changeset(server, attrs) do
server
2023-07-09 16:41:41 +00:00
|> cast(attrs, [:name, :public_ip, :provider, :registration_token, :status])
2023-06-23 07:05:10 +00:00
|> validate_required([:name, :provider])
2023-06-19 20:28:24 +00:00
|> validate_inclusion(:provider, [:Custom], message: "Provider not available (yet)")
2023-07-09 16:41:41 +00:00
|> unique_constraint([:public_ip])
2023-06-09 19:13:27 +00:00
end
end