Bulk NS change: Verify CSV integrity

This commit is contained in:
Karl Erik Õunapuu 2021-01-22 16:51:09 +02:00 committed by Alex Sherman
parent 067db7aa0a
commit 4170554ca1

View file

@ -7,11 +7,12 @@ class Registrar
ipv6 = params[:ipv6].split("\r\n") ipv6 = params[:ipv6].split("\r\n")
domains = domain_list_from_csv domains = domain_list_from_csv
return csv_list_empty_guard if domains == []
uri = URI.parse("#{ENV['repp_url']}registrar/nameservers") uri = URI.parse("#{ENV['repp_url']}registrar/nameservers")
request = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json') request = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
request.body = { data: { type: 'nameserver', id: params[:old_hostname], request.body = { data: { type: 'nameserver', id: params[:old_hostname],
domains: domains, domains: domains || [],
attributes: { hostname: params[:new_hostname], attributes: { hostname: params[:new_hostname],
ipv4: ipv4, ipv4: ipv4,
ipv6: ipv6 } } }.to_json ipv6: ipv6 } } }.to_json
@ -42,12 +43,20 @@ class Registrar
notices.join(', ') notices.join(', ')
end end
def csv_list_empty_guard
notice = 'CSV scoped domain list seems empty. Make sure that domains are added and ' \
'"domain_name" header is present.'
redirect_to(registrar_domains_url, flash: { notice: notice })
end
def domain_list_from_csv def domain_list_from_csv
return [] if params[:puny_file].blank? return if params[:puny_file].blank?
domains = [] domains = []
CSV.read(params[:puny_file].path, headers: true).each { |b| domains << b['domain_name'] } CSV.read(params[:puny_file].path, headers: true).map { |b| domains << b['domain_name'] }
domains domains.compact
rescue CSV::MalformedCSVError
[]
end end
end end
end end