Merge pull request #2161 from internetee/2158-nameserver-bulk-addition

REPP: bulk nameservers addition
This commit is contained in:
Timo Võhmar 2021-09-29 18:41:18 +03:00 committed by GitHub
commit 61df405560
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 191 additions and 34 deletions

View file

@ -32,7 +32,8 @@ class Registrar
end
def compose_notice_message(res)
notices = ["#{t('.replaced')}. #{t('.affected_domains')}: " \
action_text = params[:old_hostname].blank? ? t('.added') : t('.replaced')
notices = ["#{action_text}. #{t('.affected_domains')}: " \
"#{res[:data][:affected_domains].join(', ')}"]
notices << "#{t('.skipped_domains')}: #{res[:data][:skipped_domains].join(', ')}" if res[:data][:skipped_domains]
@ -42,7 +43,7 @@ class Registrar
def csv_list_empty_guard
notice = 'CSV scoped domain list seems empty. Make sure that domains are added and ' \
'"domain_name" header is present.'
'"Domain" header is present.'
redirect_to(registrar_domains_url, flash: { notice: notice })
end
@ -52,9 +53,9 @@ class Registrar
domains = []
csv = CSV.read(params[:puny_file].path, headers: true)
return [] if csv['domain_name'].blank?
return [] if csv['Domain'].blank?
csv.map { |b| domains << b['domain_name'] }
csv.map { |b| domains << b['Domain'] }
domains.compact
rescue CSV::MalformedCSVError

View file

@ -8,7 +8,7 @@ module Repp
desc 'bulk nameserver change'
param :data, Hash, required: true, desc: 'Object holding nameserver changes' do
param :type, String, required: true, desc: 'Always set as "nameserver"'
param :id, String, required: true, desc: 'Hostname of replacable nameserver'
param :id, String, required: false, desc: 'Hostname of replacable nameserver'
param :domains, Array, required: false, desc: 'Array of domain names qualified for ' \
'nameserver replacement'
param :attributes, Hash, required: true, desc: 'Object holding new nameserver values' do
@ -17,11 +17,16 @@ module Repp
param :ipv6, Array, of: String, required: false, desc: 'Array of fixed IPv6 addresses'
end
end
def update
affected, errored = current_user.registrar
.replace_nameservers(hostname,
hostname_params[:data][:attributes],
domains: domains_from_params)
def update # rubocop:disable Metrics/MethodLength
affected, errored = if hostname.present?
current_user.registrar.replace_nameservers(hostname,
hostname_params[:data][:attributes],
domains: domains_from_params)
else
current_user.registrar.add_nameservers(hostname_params[:data][:attributes],
domains: domains_from_params)
end
render_success(data: data_format_for_success(affected, errored))
rescue ActiveRecord::RecordInvalid => e
@ -47,7 +52,7 @@ module Repp
end
def hostname_params
params.require(:data).require(%i[type id])
params.require(:data).require(%i[type])
params.require(:data).require(:attributes).require([:hostname])
params.permit(data: [
@ -58,10 +63,12 @@ module Repp
end
def hostname
hostname_params[:data][:id]
hostname_params[:data][:id] || nil
end
def verify_nameserver_existance
return true if hostname.blank?
current_user.registrar.nameservers.find_by!(hostname: hostname)
end
end

View file

@ -2,7 +2,7 @@ module Domain::BulkUpdatable
extend ActiveSupport::Concern
def bulk_update_prohibited?
discarded? || statuses_blocks_update?
statuses_blocks_update?
end
def statuses_blocks_update?

View file

@ -43,6 +43,7 @@ class Registrar < ApplicationRecord
after_commit :update_whois_records
def update_whois_records
return true unless changed? && (changes.keys & WHOIS_TRIGGERS).present?
RegenerateRegistrarWhoisesJob.perform_later id
end
@ -159,10 +160,7 @@ class Registrar < ApplicationRecord
next
end
new_nameserver = Nameserver.new
new_nameserver.domain = origin.domain
new_nameserver.attributes = new_attributes
new_nameserver.save!
create_nameserver(origin.domain, new_attributes)
domain_scope.delete_if { |i| i == idn || i == puny }
domain_list << idn
@ -175,6 +173,37 @@ class Registrar < ApplicationRecord
end
end
def add_nameservers(new_attributes, domains: [])
transaction do
return if domains.empty?
approved_list = domain_list_processing(domains: domains, new_attributes: new_attributes)
self.domains.where(name: approved_list).find_each(&:update_whois_record) if approved_list.any?
[approved_list.uniq.sort, (domains - approved_list).uniq.sort]
end
end
def domain_list_processing(domains:, new_attributes:)
approved_list = []
domains.each do |domain_name|
domain = self.domains.find_by('name = ? OR name_puny = ?', domain_name, domain_name)
next if domain.blank? || domain_not_updatable?(hostname: new_attributes[:hostname], domain: domain)
create_nameserver(domain, new_attributes)
approved_list << domain_name
end
approved_list
end
def create_nameserver(domain, attributes)
new_nameserver = Nameserver.new
new_nameserver.domain = domain
new_nameserver.attributes = attributes
new_nameserver.save!
end
def vat_country=(country)
self.address_country_code = country.alpha2
end
@ -198,6 +227,7 @@ class Registrar < ApplicationRecord
def billing_email
return contact_email if self[:billing_email].blank?
self[:billing_email]
end

View file

@ -3,11 +3,11 @@
<div class="form-group">
<div class="col-md-2 control-label">
<%= label_tag :old_hostname %>
<%= label_tag :old_hostname, t('.old_hostname') %>
</div>
<div class="col-md-4">
<%= text_field_tag :old_hostname, params[:old_hostname], required: true,
<%= text_field_tag :old_hostname, params[:old_hostname], required: false,
class: 'form-control' %>
</div>
</div>