summaryrefslogtreecommitdiff
path: root/lib/powerdnsex/models/zone.ex
blob: 62a5a2a941e635d2c7ad4226f813abaada3b1892 (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
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

  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