summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Freire <code.tiago.frire@locaweb.com.br>2016-09-27 15:45:09 -0300
committerTiago Freire <code.tiago.frire@locaweb.com.br>2016-09-27 15:45:09 -0300
commitad6be210406368b83d3bcfc8c3154eb790fc3c35 (patch)
tree5b61ac6dbdac8bfbbe5db01e194e515491ec6563
parentMerge branch 'master' of github.com:akaKuruma/power_dnsex (diff)
Fix update of Records Manager
-rw-r--r--lib/powerdnsex/managers/records_manager.ex15
-rw-r--r--lib/powerdnsex/models/resource_record_set.ex14
2 files changed, 26 insertions, 3 deletions
diff --git a/lib/powerdnsex/managers/records_manager.ex b/lib/powerdnsex/managers/records_manager.ex
index a0ec317..e59a153 100644
--- a/lib/powerdnsex/managers/records_manager.ex
+++ b/lib/powerdnsex/managers/records_manager.ex
@@ -19,9 +19,18 @@ defmodule PowerDNSex.Managers.RecordsManager do
end
end
- def update(%Zone{} = zone, %{} = rrset_attrs) do
- rrset_attrs = Map.merge(rrset_attrs, %{changetype: "REPLACE"})
- patch(zone, rrset_attrs)
+ def update(%Zone{} = zone, %{name: rrset_name, type: rrset_type} = rrset_attrs) do
+ rrset_find_params = %{name: "#{rrset_name}.#{zone.name}", type: rrset_type}
+ rrset = RRSet.find(zone.rrsets, rrset_find_params)
+
+ if rrset do
+ updated_rrset = RRSet.update(rrset, rrset_attrs)
+ updated_rrset = Map.merge(updated_rrset, %{changetype: "REPLACE"})
+ patch(zone, updated_rrset)
+ else
+ error_msg = "Record #{rrset_name}, type #{rrset_type}, not found!"
+ {:error, %Error{error: error_msg, http_status_code: 404}}
+ end
end
def delete(%Zone{} = zone, %{} = rrset_attrs) do
diff --git a/lib/powerdnsex/models/resource_record_set.ex b/lib/powerdnsex/models/resource_record_set.ex
index 928848c..52dbed2 100644
--- a/lib/powerdnsex/models/resource_record_set.ex
+++ b/lib/powerdnsex/models/resource_record_set.ex
@@ -3,6 +3,7 @@ defmodule PowerDNSex.Models.ResourceRecordSet do
alias PowerDNSex.Models.Record
defstruct [:name, :type, :ttl, :records, :changetype]
+ @permited_attrs [:ttl, :records]
def build(%{records: records} = rrset_attrs) when is_list(records) do
build_rrset(rrset_attrs)
@@ -33,6 +34,19 @@ defmodule PowerDNSex.Models.ResourceRecordSet do
end)
end
+ def update(%__MODULE__{} = rrset, %{} = new_attrs) do
+ Enum.reduce(@permited_attrs, rrset, fn(attr_name, rrset)->
+ case Map.fetch(new_attrs, attr_name) do
+ {:ok, new_value} ->
+ if attr_name == :records do
+ new_value = Record.build(new_value)
+ end
+ %{rrset | attr_name => new_value}
+ _ -> rrset
+ end
+ end)
+ end
+
###
# Private
###