diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/powerdnsex/converter.ex | 19 | ||||
-rw-r--r-- | lib/powerdnsex/gen_server/server.ex | 14 | ||||
-rw-r--r-- | lib/powerdnsex/managers/records_manager.ex | 21 | ||||
-rw-r--r-- | lib/powerdnsex/models/record.ex | 10 | ||||
-rw-r--r-- | lib/powerdnsex/models/resource_record_set.ex | 24 |
5 files changed, 55 insertions, 33 deletions
diff --git a/lib/powerdnsex/converter.ex b/lib/powerdnsex/converter.ex new file mode 100644 index 0000000..507c119 --- /dev/null +++ b/lib/powerdnsex/converter.ex @@ -0,0 +1,19 @@ +defmodule PowerDNSex.Converter do + def keys_to_atom(struct) when is_list(struct) do + for item <- struct, do: keys_to_atom(item) + end + + def keys_to_atom(%{} = struct) do + for {key, value} <- struct, into: %{} do + n_key = if is_binary(key), do: String.to_atom(key), else: key + + n_value = if is_map(value) or is_list(value) do + keys_to_atom(value) + else value end + + {n_key, n_value} + end + end + + def keys_to_atom(item), do: item +end diff --git a/lib/powerdnsex/gen_server/server.ex b/lib/powerdnsex/gen_server/server.ex index bed016b..a91dc76 100644 --- a/lib/powerdnsex/gen_server/server.ex +++ b/lib/powerdnsex/gen_server/server.ex @@ -2,7 +2,7 @@ defmodule PowerDNSex.Server do use GenServer alias PowerDNSex.Managers.{ZonesManager, RecordsManager} - alias PowerDNSex.Models.Zone + alias PowerDNSex.{Models.Zone, Converter} def start_link(name) do GenServer.start_link(__MODULE__, :ok, name: name) @@ -25,18 +25,22 @@ defmodule PowerDNSex.Server do end def handle_call({:create_record, zone, rrset_attrs}, _from, state) do - {:reply, RecordsManager.create(zone, rrset_attrs), state} + attrs = Converter.keys_to_atom(rrset_attrs) + {:reply, RecordsManager.create(zone, attrs), state} end def handle_call({:show_record, zone_name, rrset_attrs}, _from, state) do - {:reply, RecordsManager.show(zone_name, rrset_attrs), state} + attrs = Converter.keys_to_atom(rrset_attrs) + {:reply, RecordsManager.show(zone_name, attrs), state} end def handle_call({:update_record, zone, rrset_attrs}, _from, state) do - {:reply, RecordsManager.update(zone, rrset_attrs), state} + attrs = Converter.keys_to_atom(rrset_attrs) + {:reply, RecordsManager.update(zone, attrs), state} end def handle_call({:delete_record, zone, rrset_attrs}, _from, state) do - {:reply, RecordsManager.delete(zone, rrset_attrs), state} + attrs = Converter.keys_to_atom(rrset_attrs) + {:reply, RecordsManager.delete(zone, attrs), state} end end diff --git a/lib/powerdnsex/managers/records_manager.ex b/lib/powerdnsex/managers/records_manager.ex index c7c586d..a0ec317 100644 --- a/lib/powerdnsex/managers/records_manager.ex +++ b/lib/powerdnsex/managers/records_manager.ex @@ -8,8 +8,7 @@ defmodule PowerDNSex.Managers.RecordsManager do def create(%Zone{} = zone, %{} = rrset_attrs) do - rrset_attrs = Map.merge(rrset_attrs, %{"changetype" => "REPLACE"}) - IO.puts "RRSet create: #{inspect rrset_attrs}" + rrset_attrs = Map.merge(rrset_attrs, %{changetype: "REPLACE"}) patch(zone, rrset_attrs) end @@ -21,12 +20,12 @@ defmodule PowerDNSex.Managers.RecordsManager do end def update(%Zone{} = zone, %{} = rrset_attrs) do - rrset_attrs = Map.merge(rrset_attrs, %{"changetype" => "REPLACE"}) + rrset_attrs = Map.merge(rrset_attrs, %{changetype: "REPLACE"}) patch(zone, rrset_attrs) end def delete(%Zone{} = zone, %{} = rrset_attrs) do - rrset_attrs = Map.merge(rrset_attrs, %{"changetype" => "DELETE"}) + rrset_attrs = Map.merge(rrset_attrs, %{changetype: "DELETE"}) patch(zone, rrset_attrs) end @@ -38,7 +37,6 @@ defmodule PowerDNSex.Managers.RecordsManager do case status do s when s == 204 -> :ok s when s < 300 -> - IO.puts "Response status: #{s}" :ok s when s >= 300 -> error = Poison.decode!(body, as: %Error{}) @@ -50,17 +48,16 @@ defmodule PowerDNSex.Managers.RecordsManager do raise "[Records Manager] Zone URL attribute is empty!" end - defp patch(%Zone{} = zone, %{} = rrset_attrs) do - IO.puts "Patch rrset_attrs: #{inspect rrset_attrs}" - rrset_struct = RRSet.build(rrset_attrs) - IO.puts "[Patch] Struct: #{inspect rrset_struct}" - IO.puts "[Patch] Body: #{inspect RRSet.as_body(rrset_struct)}" - + defp patch(%Zone{} = zone, %RRSet{} = rrset) do zone.url - |> HttpClient.patch!(RRSet.as_body(RRSet.build(rrset_attrs))) + |> HttpClient.patch!(RRSet.as_body(rrset)) |> process_request_response end + defp patch(%Zone{} = zone, %{} = rrset_attrs) do + patch(zone, RRSet.build(rrset_attrs)) + end + defp has_attrs?(rrset, attrs) do Map.keys(attrs) |> Enum.all?(&(equal_attr?(&1, attrs[&1], rrsets))) diff --git a/lib/powerdnsex/models/record.ex b/lib/powerdnsex/models/record.ex index b334a9b..0c40e65 100644 --- a/lib/powerdnsex/models/record.ex +++ b/lib/powerdnsex/models/record.ex @@ -31,11 +31,13 @@ defmodule PowerDNSex.Models.Record do # PRIVATE ### - defp build_item(attrs) when is_tuple(attrs) do - %__MODULE__{content: elem(attrs, 0), disabled: elem(attrs, 1)} + defp build_item(attrs) when is_map(attrs) do + disabled = attrs[:disabled] || false + record = %__MODULE__{content: attrs.content, disabled: disabled} + record end - defp build_item(attrs) when is_map(attrs) do - %__MODULE__{content: attrs["content"], disabled: attrs["disabled"]} + defp build_item(attrs) when is_tuple(attrs) do + %__MODULE__{content: elem(attrs, 0), disabled: elem(attrs, 1)} end end diff --git a/lib/powerdnsex/models/resource_record_set.ex b/lib/powerdnsex/models/resource_record_set.ex index 89973a8..928848c 100644 --- a/lib/powerdnsex/models/resource_record_set.ex +++ b/lib/powerdnsex/models/resource_record_set.ex @@ -4,17 +4,11 @@ defmodule PowerDNSex.Models.ResourceRecordSet do defstruct [:name, :type, :ttl, :records, :changetype] - def build(%{"records" => records} = rrset_attrs) when is_list(records) do - rrset = %__MODULE__{} - - rrset - |> Map.keys - |> Enum.reduce(rrset, &(set_attrs(&2, &1, rrset_attrs))) + def build(%{records: records} = rrset_attrs) when is_list(records) do + build_rrset(rrset_attrs) end def as_body(%__MODULE__{} = rrset) do - IO.puts "RRSet: #{inspect rrset}" - %{ rrsets: [ %{ name: rrset.name, @@ -44,20 +38,26 @@ defmodule PowerDNSex.Models.ResourceRecordSet do ### defp set_attrs(rrset, attr_name, attrs) do - if is_atom(attr_name), do: attr_name = Atom.to_string(attr_name) if Map.has_key?(attrs, attr_name) do attr_value = case attr_name do - "records" -> Record.build(Map.fetch!(attrs, attr_name)) + :records -> Record.build(Map.fetch!(attrs, attr_name)) _ -> Map.fetch!(attrs, attr_name) end - %{ rrset | String.to_atom(attr_name) => attr_value } + %{ rrset | attr_name => attr_value } else rrset end end defp equal_attr?(attr, attr_value, rrset) do - Map.get(rrset, attr) == attr_value + attr_atom = if is_binary(attr), do: String.to_atom(attr), else: attr + Map.get(rrset, attr_atom) == attr_value + end + + defp build_rrset(rrset_attrs) do + %__MODULE__{} + |> Map.keys + |> Enum.reduce(%__MODULE__{}, &(set_attrs(&2, &1, rrset_attrs))) end end |