This one will contain the currently active remote network device that puppet
is currently managing.
This one will be set by an upcoming puppet device application.

Signed-off-by: Brice Figureau <[email protected]>
---
 lib/puppet/util/network_device.rb           |   12 ++++++-
 lib/puppet/util/network_device/base.rb      |   32 +++++++++----------
 lib/puppet/util/network_device/transport.rb |    4 +--
 spec/unit/util/network_device_spec.rb       |   45 +++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 21 deletions(-)
 create mode 100644 spec/unit/util/network_device_spec.rb

diff --git a/lib/puppet/util/network_device.rb 
b/lib/puppet/util/network_device.rb
index bca6601..d9c1aa3 100644
--- a/lib/puppet/util/network_device.rb
+++ b/lib/puppet/util/network_device.rb
@@ -1,2 +1,12 @@
-module Puppet::Util::NetworkDevice
+class Puppet::Util::NetworkDevice
+  class << self
+    attr_reader :current
+  end
+
+  def self.init(device)
+    require "puppet/util/network_device/#{device.provider}/device"
+    @current = 
Puppet::Util::NetworkDevice.const_get(device.provider.capitalize).const_get(:Device).new(device.url)
+  rescue => detail
+    raise "Can't load #{device.provider} for #{device.name}: #{detail}"
+  end
 end
\ No newline at end of file
diff --git a/lib/puppet/util/network_device/base.rb 
b/lib/puppet/util/network_device/base.rb
index ff96c86..7d6c3fc 100644
--- a/lib/puppet/util/network_device/base.rb
+++ b/lib/puppet/util/network_device/base.rb
@@ -3,27 +3,25 @@ require 'uri'
 require 'puppet/util/network_device/transport'
 require 'puppet/util/network_device/transport/base'
 
-module Puppet::Util::NetworkDevice
-  class Base
+class Puppet::Util::NetworkDevice::Base
 
-    attr_accessor :url, :transport
+  attr_accessor :url, :transport
 
-    def initialize(url)
-      @url = URI.parse(url)
+  def initialize(url)
+    @url = URI.parse(url)
 
-      @autoloader = Puppet::Util::Autoload.new(
-        self,
-        "puppet/util/network_device/transport",
-        :wrap => false
-      )
+    @autoloader = Puppet::Util::Autoload.new(
+      self,
+      "puppet/util/network_device/transport",
+      :wrap => false
+    )
 
-      if @autoloader.load(@url.scheme)
-        @transport = 
Puppet::Util::NetworkDevice::Transport.const_get(@url.scheme.capitalize).new
-        @transport.host = @url.host
-        @transport.port = @url.port || case @url.scheme ; when "ssh" ; 22 ; 
when "telnet" ; 23 ; end
-        @transport.user = @url.user
-        @transport.password = @url.password
-      end
+    if @autoloader.load(@url.scheme)
+      @transport = 
Puppet::Util::NetworkDevice::Transport.const_get(@url.scheme.capitalize).new
+      @transport.host = @url.host
+      @transport.port = @url.port || case @url.scheme ; when "ssh" ; 22 ; when 
"telnet" ; 23 ; end
+      @transport.user = @url.user
+      @transport.password = @url.password
     end
   end
 end
\ No newline at end of file
diff --git a/lib/puppet/util/network_device/transport.rb 
b/lib/puppet/util/network_device/transport.rb
index e64fe9b..cef8f38 100644
--- a/lib/puppet/util/network_device/transport.rb
+++ b/lib/puppet/util/network_device/transport.rb
@@ -1,5 +1,3 @@
 # stub
-module Puppet::Util::NetworkDevice
-  module Transport
-  end
+module Puppet::Util::NetworkDevice::Transport
 end
\ No newline at end of file
diff --git a/spec/unit/util/network_device_spec.rb 
b/spec/unit/util/network_device_spec.rb
new file mode 100644
index 0000000..7b97b2e
--- /dev/null
+++ b/spec/unit/util/network_device_spec.rb
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+
+Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? 
require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") }
+
+require 'ostruct'
+require 'puppet/util/network_device'
+
+describe Puppet::Util::NetworkDevice do
+
+  before(:each) do
+    @device = OpenStruct.new(:name => "name", :provider => "test")
+  end
+
+  class Puppet::Util::NetworkDevice::Test::Device
+    def initialize(device)
+    end
+  end
+
+  describe "when initializing the remote network device singleton" do
+    it "should load the network device code" do
+      Puppet::Util::NetworkDevice.expects(:require)
+      Puppet::Util::NetworkDevice.init(@device)
+    end
+
+    it "should create a network device instance" do
+      Puppet::Util::NetworkDevice.stubs(:require)
+      Puppet::Util::NetworkDevice::Test::Device.expects(:new)
+      Puppet::Util::NetworkDevice.init(@device)
+    end
+
+    it "should raise an error if the remote device instance can't be created" 
do
+      Puppet::Util::NetworkDevice.stubs(:require).raises("error")
+      lambda { Puppet::Util::NetworkDevice.init(@device) }.should raise_error
+    end
+
+    it "should let caller to access the singleton device" do
+      device = stub 'device'
+      Puppet::Util::NetworkDevice.stubs(:require)
+      Puppet::Util::NetworkDevice::Test::Device.expects(:new).returns(device)
+      Puppet::Util::NetworkDevice.init(@device)
+
+      Puppet::Util::NetworkDevice.current.should == device
+    end
+  end
+end
\ No newline at end of file
-- 
1.7.2.1

-- 
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