summaryrefslogtreecommitdiff
path: root/lib/powerdnsex/models/zone.ex
blob: 96089e03477bd85bf0bb3a211c4e9a178548e178 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
defmodule PowerDNSex.Models.Zone do
  @moduledoc """
  Model for PowerDns zones, create and validate format
  """

  @body_attrs ~w(account dns kind masters name nameservers records serial
                 soa_edit soa_edit_api)a

  defstruct name: nil,
            kind: "Native",
            masters: [],
            nameservers: [],
            rrsets: [],
            account: nil,
            comments: [],
            dnssec: false,
            id: nil,
            last_check: 0,
            notified_serial: 0,
            serial: nil,
            soa_edit: "",
            soa_edit_api: "",
            url: nil

  @type t :: %__MODULE__{}

  def as_body(%__MODULE__{} = zone) do
    get_valid_attrs = fn {attr, value}, body ->
      if Enum.member?(@body_attrs, attr) do
        Map.merge(body, %{attr => value})
      else
        body
      end
    end

    zone
    |> Map.from_struct()
    |> Enum.reduce(%{}, get_valid_attrs)
    |> Poison.encode!()
  end
end