created method for adding nameservers

This commit is contained in:
dinsmol 2021-09-15 21:55:11 +03:00
parent 5d9692784f
commit d36ef24170
3 changed files with 69 additions and 7 deletions

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)
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.nil?
current_user.registrar.nameservers.find_by!(hostname: hostname)
end
end

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
@ -175,6 +176,36 @@ class Registrar < ApplicationRecord
end
end
def add_nameservers(new_attributes, domains: [])
transaction do
domain_scope = domains.dup
domain_list = []
failed_list = []
return if domains.empty?
domain_scope.each do |domain_name|
domain = self.domains.find_by('name = ? OR name_puny = ?', domain_name, domain_name)
if !domain.present? || domain_not_updatable?(hostname: new_attributes[:hostname], domain: domain)
failed_list << domain_name
next
end
new_nameserver = Nameserver.new
new_nameserver.domain = domain
new_nameserver.attributes = new_attributes
new_nameserver.save!
domain_scope.delete_if { |i| i == domain.name || i == domain.name_puny }
domain_list << domain_name
end
self.domains.where(name: domain_list).find_each(&:update_whois_record) if domain_list.any?
[domain_list.uniq.sort, (domain_scope + failed_list).uniq.sort]
end
end
def vat_country=(country)
self.address_country_code = country.alpha2
end
@ -198,6 +229,7 @@ class Registrar < ApplicationRecord
def billing_email
return contact_email if self[:billing_email].blank?
self[:billing_email]
end

View file

@ -34,6 +34,29 @@ class ReppV1RegistrarNameserversTest < ActionDispatch::IntegrationTest
assert json[:data][:affected_domains].include? 'shop.test'
end
def test_add_nameserver_values
nameserver = nameservers(:shop_ns1)
payload = {
"data": {
"type": 'nameserver',
"domains": ['shop.test'],
"attributes": {
"hostname": "#{nameserver.hostname}.testtest",
"ipv4": ['2.2.2.2']
}
}
}
put '/repp/v1/registrar/nameservers', headers: @auth_headers, params: payload
json = JSON.parse(response.body, symbolize_names: true)
assert_response :ok
assert_equal 1000, json[:code]
assert_equal 'Command completed successfully', json[:message]
assert_equal({ hostname: "#{nameserver.hostname}.testtest", ipv4: ['2.2.2.2'] }, json[:data][:attributes])
assert json[:data][:affected_domains].include? 'shop.test'
end
def test_fails_to_update_if_prohibited
domain = domains(:shop)
domain.update(statuses: [DomainStatus::CLIENT_UPDATE_PROHIBITED])