internetee-registry/app/controllers/registrar/nameservers_controller.rb

68 lines
2.2 KiB
Ruby

class Registrar
class NameserversController < BulkChangeController
def update
authorize! :manage, :repp
ipv4 = params[:ipv4].split("\r\n")
ipv6 = params[:ipv6].split("\r\n")
domains = domain_list_from_csv
return csv_list_empty_guard if domains == []
uri = URI.parse("#{ENV['repp_url']}registrar/nameservers")
request = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
request.body = { data: { type: 'nameserver', id: params[:old_hostname],
domains: domains || [],
attributes: { hostname: params[:new_hostname],
ipv4: ipv4,
ipv6: ipv6 } } }.to_json
request.basic_auth(current_registrar_user.username,
current_registrar_user.plain_text_password)
response = do_request(request, uri)
parsed_response = JSON.parse(response.body, symbolize_names: true)
if response.code == '200'
redirect_to(registrar_domains_url,
flash: { notice: compose_notice_message(parsed_response) })
else
@api_errors = parsed_response[:message]
render file: 'registrar/bulk_change/new', locals: { active_tab: :nameserver }
end
end
def compose_notice_message(res)
notices = ["#{t('.replaced')}. #{t('.affected_domains')}: " \
"#{res[:data][:affected_domains].join(', ')}"]
if res[:data][:skipped_domains]
notices << "#{t('.skipped_domains')}: #{res[:data][:skipped_domains].join(', ')}"
end
notices.join(', ')
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
return if params[:puny_file].blank?
domains = []
csv = CSV.read(params[:puny_file].path, headers: true)
return [] if csv['domain_name'].blank?
csv.map { |b| domains << b['domain_name'] }
domains.compact
rescue CSV::MalformedCSVError
[]
end
end
end