Permalink
Browse files

Merge pull request #110 from cloudfoundry/dns_model_fix

[director] Fix update PTR records when IP changed in same subnet
  • Loading branch information...
2 parents a6ad8f3 + 5405739 commit 21d9feb991210ccbc58dc316b5462bd83e223e58 @pmenglund pmenglund committed Apr 3, 2013
Showing with 32 additions and 12 deletions.
  1. +13 −9 director/lib/director/dns_helper.rb
  2. +19 −3 director/spec/unit/dns_helper_spec.rb
@@ -102,34 +102,38 @@ def update_dns_a_record(domain, name, ip_address)
# create/update DNS PTR records (for reverse lookups)
def update_dns_ptr_record(name, ip_address)
- reverse = reverse_domain(ip_address)
- rdomain = Models::Dns::Domain.safe_find_or_create(:name => reverse,
- :type => "NATIVE")
+ reverse_domain = reverse_domain(ip_address)
+ reverse_host = reverse_host(ip_address)
+
+ rdomain = Models::Dns::Domain.safe_find_or_create(:name => reverse_domain,
+ :type => "NATIVE")
Models::Dns::Record.find_or_create(:domain_id => rdomain.id,
- :name => reverse,
+ :name => reverse_domain,
:type =>'SOA', :content => SOA,
:ttl => TTL_4H)
Models::Dns::Record.find_or_create(:domain_id => rdomain.id,
- :name => reverse,
+ :name => reverse_domain,
:type =>'NS', :ttl => TTL_4H,
:content => dns_ns_record)
record = Models::Dns::Record.find(:content => name, :type =>'PTR')
# delete the record if the IP address changed
- if record && record.name != reverse
+ if record && record.name != reverse_host
id = record.domain_id
record.destroy
record = nil
- delete_empty_domain(Models::Dns::Domain[id])
+ # delete the domain if the domain id changed and it's empty
+ if id != rdomain.id
+ delete_empty_domain(Models::Dns::Domain[id])
+ end
end
- reverse = reverse_host(ip_address)
unless record
record = Models::Dns::Record.new(:domain_id => rdomain.id,
- :name => reverse,
+ :name => reverse_host,
:type =>'PTR', :ttl => TTL_5M)
end
record.content = name
@@ -153,13 +153,29 @@
record = BDM::Dns::Record.find(:name => "4.3.2.1.in-addr.arpa")
record.content.should == "0.foo.default.bosh"
record.type.should == "PTR"
+ BDM::Dns::Domain.all.size.should == 1
+ BDM::Dns::Record.all.size.should == 3
end
- it "should update existing record" do
+ it "should update existing record on a different subnet" do
update_dns_ptr_record("0.foo.default.bosh", "1.2.3.4")
update_dns_ptr_record("0.foo.default.bosh", "5.6.7.8")
- record = BDM::Dns::Record.find(:name => "8.7.6.5.in-addr.arpa")
- record.content.should == "0.foo.default.bosh"
+ old_record = BDM::Dns::Record.find(:name => "4.3.2.1.in-addr.arpa")
+ old_record.should be_nil
+ new_record = BDM::Dns::Record.find(:name => "8.7.6.5.in-addr.arpa")
+ new_record.content.should == "0.foo.default.bosh"
+ BDM::Dns::Domain.all.size.should == 1
+ BDM::Dns::Record.all.size.should == 3
+ end
+
+ it "should update existing record on the same subnet" do
+ update_dns_ptr_record("0.foo.default.bosh", "1.2.3.4")
+ update_dns_ptr_record("0.foo.default.bosh", "1.2.3.5")
+ old_record = BDM::Dns::Record.find(:name => "4.3.2.1.in-addr.arpa")
+ old_record.should be_nil
+ new_record = BDM::Dns::Record.find(:name => "5.3.2.1.in-addr.arpa")
+ new_record.content.should == "0.foo.default.bosh"
+ BDM::Dns::Domain.all.size.should == 1
BDM::Dns::Record.all.size.should == 3
end
end

0 comments on commit 21d9feb

Please sign in to comment.