nameserver replacement: scope only to selected domains

This commit is contained in:
Karl Erik Õunapuu 2020-11-06 15:51:03 +02:00
parent ed7181e060
commit 451e1f79af
No known key found for this signature in database
GPG key ID: C9DD647298A34764
4 changed files with 30 additions and 3 deletions

View file

@ -8,6 +8,7 @@ module Repp
requires :data, type: Hash, allow_blank: false do
requires :type, type: String, allow_blank: false
requires :id, type: String, allow_blank: false
optional :domains, type: Array
requires :attributes, type: Hash, allow_blank: false do
requires :hostname, type: String, allow_blank: false
requires :ipv4, type: Array
@ -28,8 +29,10 @@ module Repp
ipv6: params[:data][:attributes][:ipv6],
}
domains = params[:data][:domains] || []
begin
affected_domains = current_user.registrar.replace_nameservers(hostname, new_attributes)
affected_domains = current_user.registrar.replace_nameservers(hostname, new_attributes, domains)
rescue ActiveRecord::RecordInvalid => e
error!({ errors: e.record.errors.full_messages.map { |error| { title: error } } }, 400)
end

View file

@ -6,9 +6,13 @@ class Registrar
ipv4 = params[:ipv4].split("\r\n")
ipv6 = params[:ipv6].split("\r\n")
domains = domain_list_from_csv
puts "Domains are #{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
@ -55,5 +59,13 @@ class Registrar
render file: 'registrar/bulk_change/new', locals: { active_tab: :nameserver }
end
end
def domain_list_from_csv
return [] if params[:batch_file].blank?
domains = []
CSV.read(params[:batch_file].path, headers: true).each { |b| domains << b['domain_name'] }
domains
end
end
end

View file

@ -141,11 +141,13 @@ class Registrar < ApplicationRecord
end
# Audit log is needed, therefore no raw SQL
def replace_nameservers(hostname, new_attributes)
def replace_nameservers(hostname, new_attributes, domains)
transaction do
domain_list = []
nameservers.where(hostname: hostname).find_each do |original_nameserver|
next unless domains.include?(original_nameserver.domain.name_puny) || domains.empty?
new_nameserver = Nameserver.new
new_nameserver.domain = original_nameserver.domain
new_nameserver.attributes = new_attributes

View file

@ -1,4 +1,4 @@
<%= form_tag registrar_nameservers_path, method: :patch, class: 'form-horizontal' do %>
<%= form_tag registrar_nameservers_path, multipart: true, method: :patch, class: 'form-horizontal' do %>
<%= render 'registrar/domain_transfers/form/api_errors' %>
<div class="form-group">
@ -44,6 +44,16 @@
</div>
</div>
<div class="form-group">
<div class="col-md-2 control-label">
<%= label_tag 'List of domains' %>
</div>
<div class="col-md-4">
<%= file_field_tag :batch_file, required: false, accept: 'text/csv' %>
<span class="help-block">CSV format, must have domain_name field. List of domains that nameserver change should be scoped to.</span>
</div>
</div>
<div class="form-group">
<div class="col-md-4 col-md-offset-2 text-right">
<button class="btn btn-warning">