diff options
author | Tiago Freire <code.tiago.frire@locaweb.com.br> | 2016-09-20 19:15:32 -0300 |
---|---|---|
committer | Tiago Freire <code.tiago.frire@locaweb.com.br> | 2016-09-20 19:15:32 -0300 |
commit | 7ee7155d7eff30e95b58f905779850e1bd9807ae (patch) | |
tree | 1144872f4ac67c45a712d3279b47b8a66baad407 | |
parent | adjust models and manager (diff) |
Change RRSet
-rw-r--r-- | lib/powerdnsex/managers/records_manager.ex | 28 | ||||
-rw-r--r-- | lib/powerdnsex/models/resource_record_set.ex | 24 |
2 files changed, 33 insertions, 19 deletions
diff --git a/lib/powerdnsex/managers/records_manager.ex b/lib/powerdnsex/managers/records_manager.ex index c7c586d..1e85ebe 100644 --- a/lib/powerdnsex/managers/records_manager.ex +++ b/lib/powerdnsex/managers/records_manager.ex @@ -8,8 +8,8 @@ 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}" + changetype = %{changetype_key(rrset_attrs) => "REPLACE"} + rrset_attrs = Map.merge(rrset_attrs, changetype) patch(zone, rrset_attrs) end @@ -21,12 +21,14 @@ defmodule PowerDNSex.Managers.RecordsManager do end def update(%Zone{} = zone, %{} = rrset_attrs) do - rrset_attrs = Map.merge(rrset_attrs, %{"changetype" => "REPLACE"}) + changetype = %{changetype_key(rrset_attrs) => "REPLACE"} + rrset_attrs = Map.merge(rrset_attrs, changetype) patch(zone, rrset_attrs) end def delete(%Zone{} = zone, %{} = rrset_attrs) do - rrset_attrs = Map.merge(rrset_attrs, %{"changetype" => "DELETE"}) + changetype = %{changetype_key(rrset_attrs) => "DELETE"} + rrset_attrs = Map.merge(rrset_attrs, changetype) patch(zone, rrset_attrs) end @@ -34,6 +36,11 @@ defmodule PowerDNSex.Managers.RecordsManager do # Private ## + defp changetype_key(attrs) do + key = "changetype" + if Map.has_key?(attrs, key), do: key, else: String.to_atom(key) + end + defp process_request_response(%Response{body: body, status_code: status}) do case status do s when s == 204 -> :ok @@ -50,17 +57,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/resource_record_set.ex b/lib/powerdnsex/models/resource_record_set.ex index 89973a8..c22f255 100644 --- a/lib/powerdnsex/models/resource_record_set.ex +++ b/lib/powerdnsex/models/resource_record_set.ex @@ -5,16 +5,15 @@ defmodule PowerDNSex.Models.ResourceRecordSet do defstruct [:name, :type, :ttl, :records, :changetype] def build(%{"records" => records} = rrset_attrs) when is_list(records) do - rrset = %__MODULE__{} + build_rrset(rrset_attrs) + end - 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}" + def as_body(%__MODULE__{} = rrset) do %{ rrsets: [ %{ name: rrset.name, @@ -30,7 +29,8 @@ defmodule PowerDNSex.Models.ResourceRecordSet do def find(rrsets, %{} = attrs) when is_list(rrsets) do Enum.find(rrsets, fn(rrset)-> Enum.all?(attrs, fn({attr, attr_value})-> - if Enum.member?(Map.keys(%__MODULE__{}), attr) do + attr_atom = if is_binary(attr), do: String.to_atom(attr), else: attr + if Enum.member?(Map.keys(%__MODULE__{}), attr_atom) do equal_attr?(attr, attr_value, rrset) else Record.find(rrset.records, %{attr => attr_value}) @@ -45,6 +45,7 @@ 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)) @@ -58,6 +59,13 @@ defmodule PowerDNSex.Models.ResourceRecordSet do 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 |