Please review pull request #192: Ticket/1.6.x/12864 windows primary dns suffix opened by (jeffweiss)

Description:

Windows does not expose the host's primary DNS suffix via WMI, only the
registry. This commit adds a registry helper class and will retrieve the
primary DNS suffix from the registry. In the case the primary DNS suffix
does not exist or is empty, the domain fact will fall back to using WMI
for any connection-specific DNS suffix.

  • Opened: Fri Apr 27 07:56:25 UTC 2012
  • Based on: puppetlabs:1.6.x (e1025c15ee3a01cbae537811679847571bacf619)
  • Requested merge: jeffweiss:ticket/1.6.x/12864_windows_primary_dns_suffix (5a49d475306b7225394e1d0968598f80fce15886)

Diff follows:

diff --git a/lib/facter/domain.rb b/lib/facter/domain.rb
index ff5e988..2ae909a 100644
--- a/lib/facter/domain.rb
+++ b/lib/facter/domain.rb
@@ -52,12 +52,17 @@
 Facter.add(:domain) do
   confine :kernel => :windows
   setcode do
-    require 'facter/util/wmi'
+    require 'facter/util/registry'
     domain = ""
-    Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
-      domain = nic.DNSDomain
-      break
-    }
+    regvalue = Facter::Util::Registry.hklm_read('SYSTEM\CurrentControlSet\Services\Tcpip\Parameters', 'Domain')
+    domain = regvalue if regvalue
+    if domain == ""
+      require 'facter/util/wmi'
+      Facter::Util::WMI.execquery("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").each { |nic|
+        domain = nic.DNSDomain
+        break
+      }
+    end
     domain
   end
 end
diff --git a/lib/facter/util/registry.rb b/lib/facter/util/registry.rb
new file mode 100644
index 0000000..412eadb
--- /dev/null
+++ b/lib/facter/util/registry.rb
@@ -0,0 +1,12 @@
+module Facter::Util::Registry
+  class << self
+    def hklm_read(key, value)
+      require 'win32/registry'
+      val = nil
+      Win32::Registry::HKEY_LOCAL_MACHINE.open(key) do |reg|
+        val = reg[value]
+      end
+      val
+    end
+  end
+end
diff --git a/spec/unit/domain_spec.rb b/spec/unit/domain_spec.rb
index 6ef416c..07195ca 100755
--- a/spec/unit/domain_spec.rb
+++ b/spec/unit/domain_spec.rb
@@ -98,19 +98,39 @@
   end
 
   describe "on Windows" do
-    it "should use the DNSDomain for the first nic where ip is enabled" do
+    before(:each) do
       Facter.fact(:kernel).stubs(:value).returns("windows")
+      require 'facter/util/registry'
+    end
+    describe "with primary dns suffix" do
+      before(:each) do
+        Facter::Util::Registry.stubs(:hklm_read).returns('baz.com')
+      end
+      it "should get the primary dns suffix" do
+        Facter.fact(:domain).value.should == 'baz.com'
+      end
+      it "should not execute the wmi query" do
+        require 'facter/util/wmi'
+        Facter::Util::WMI.expects(:execquery).never
+        Facter.fact(:domain).value
+      end
+    end
+    describe "without primary dns suffix" do
+      before(:each) do
+        Facter::Util::Registry.stubs(:hklm_read).returns('')
+      end
+      it "should use the DNSDomain for the first nic where ip is enabled" do
+        nic = stubs 'nic'
+        nic.stubs(:DNSDomain).returns("foo.com")
 
-      nic = stubs 'nic'
-      nic.stubs(:DNSDomain).returns("foo.com")
-
-      nic2 = stubs 'nic'
-      nic2.stubs(:DNSDomain).returns("bar.com")
+        nic2 = stubs 'nic'
+        nic2.stubs(:DNSDomain).returns("bar.com")
 
-      require 'facter/util/wmi'
-      Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
+        require 'facter/util/wmi'
+        Facter::Util::WMI.stubs(:execquery).with("select DNSDomain from Win32_NetworkAdapterConfiguration where IPEnabled = True").returns([nic, nic2])
 
-      Facter.fact(:domain).value.should == 'foo.com'
+        Facter.fact(:domain).value.should == 'foo.com'
+      end
     end
   end
 end

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to