So that when they're applied on behalf of a puppet device run they inherit the correct device.
The other system of specifying the device url in the resource for use with puppet apply is still supported. Signed-off-by: Brice Figureau <[email protected]> --- lib/puppet/provider/cisco.rb | 9 +++++++++ lib/puppet/provider/interface/cisco.rb | 18 ++++++------------ lib/puppet/provider/network_device.rb | 21 +++++++++++++++------ lib/puppet/provider/vlan/cisco.rb | 14 ++++---------- spec/unit/provider/cisco_spec.rb | 16 ++++++++++++++++ spec/unit/provider/interface/cisco_spec.rb | 20 +++++++------------- spec/unit/provider/network_device_spec.rb | 21 +++++++++++++-------- spec/unit/provider/vlan/cisco_spec.rb | 20 +++++++------------- 8 files changed, 77 insertions(+), 62 deletions(-) create mode 100644 lib/puppet/provider/cisco.rb create mode 100644 spec/unit/provider/cisco_spec.rb diff --git a/lib/puppet/provider/cisco.rb b/lib/puppet/provider/cisco.rb new file mode 100644 index 0000000..918982f --- /dev/null +++ b/lib/puppet/provider/cisco.rb @@ -0,0 +1,9 @@ +require 'puppet/util/network_device/cisco/device' +require 'puppet/provider/network_device' + +# This is the base class of all prefetched cisco device providers +class Puppet::Provider::Cisco < Puppet::Provider::NetworkDevice + def self.device(url) + Puppet::Util::NetworkDevice::Cisco::Device.new(url) + end +end diff --git a/lib/puppet/provider/interface/cisco.rb b/lib/puppet/provider/interface/cisco.rb index f3bd202..795a7f1 100644 --- a/lib/puppet/provider/interface/cisco.rb +++ b/lib/puppet/provider/interface/cisco.rb @@ -1,22 +1,20 @@ -require 'puppet/util/network_device/cisco/device' -require 'puppet/provider/network_device' +require 'puppet/provider/cisco' -Puppet::Type.type(:interface).provide :cisco, :parent => Puppet::Provider::NetworkDevice do +Puppet::Type.type(:interface).provide :cisco, :parent => Puppet::Provider::Cisco do desc "Cisco switch/router provider for interface." mk_resource_methods - def self.lookup(url, name) + def self.lookup(device, name) interface = nil - network_gear = Puppet::Util::NetworkDevice::Cisco::Device.new(url) - network_gear.command do |ng| - interface = network_gear.interface(name) + device.command do |ng| + interface = device.interface(name) end interface end - def initialize(*args) + def initialize(device, *args) super end @@ -26,8 +24,4 @@ Puppet::Type.type(:interface).provide :cisco, :parent => Puppet::Provider::Netwo end super end - - def device - @device ||= Puppet::Util::NetworkDevice::Cisco::Device.new(resource[:device_url]) - end end diff --git a/lib/puppet/provider/network_device.rb b/lib/puppet/provider/network_device.rb index 58865fd..b178df9 100644 --- a/lib/puppet/provider/network_device.rb +++ b/lib/puppet/provider/network_device.rb @@ -2,17 +2,22 @@ # This is the base class of all prefetched network device provider class Puppet::Provider::NetworkDevice < Puppet::Provider - def self.lookup(url, name) + def self.device(url) + raise "This provider doesn't implement the necessary device method" + end + + def self.lookup(device, name) raise "This provider doesn't implement the necessary lookup method" end def self.prefetch(resources) resources.each do |name, resource| - if result = lookup(resource[:device_url], name) + device = Puppet::Util::NetworkDevice.current || device(resource[:device_url]) + if result = lookup(device, name) result[:ensure] = :present - resource.provider = new(result) + resource.provider = new(device, result) else - resource.provider = new(:ensure => :absent) + resource.provider = new(device, :ensure => :absent) end end end @@ -21,8 +26,12 @@ class Puppet::Provider::NetworkDevice < Puppet::Provider @property_hash[:ensure] != :absent end - def initialize(*args) - super + attr_accessor :device + + def initialize(device, *args) + super(*args) + + @device = device # Make a duplicate, so that we have a copy for comparison # at the end. diff --git a/lib/puppet/provider/vlan/cisco.rb b/lib/puppet/provider/vlan/cisco.rb index 46e172c..3421d35 100644 --- a/lib/puppet/provider/vlan/cisco.rb +++ b/lib/puppet/provider/vlan/cisco.rb @@ -1,22 +1,20 @@ -require 'puppet/util/network_device/cisco/device' -require 'puppet/provider/network_device' +require 'puppet/provider/cisco' -Puppet::Type.type(:vlan).provide :cisco, :parent => Puppet::Provider::NetworkDevice do +Puppet::Type.type(:vlan).provide :cisco, :parent => Puppet::Provider::Cisco do desc "Cisco switch/router provider for vlans." mk_resource_methods - def self.lookup(url, id) + def self.lookup(device, id) vlans = {} - device = Puppet::Util::NetworkDevice::Cisco::Device.new(url) device.command do |d| vlans = d.parse_vlans || {} end vlans[id] end - def initialize(*args) + def initialize(device, *args) super end @@ -27,8 +25,4 @@ Puppet::Type.type(:vlan).provide :cisco, :parent => Puppet::Provider::NetworkDev end super end - - def device - @device ||= Puppet::Util::NetworkDevice::Cisco::Device.new(resource[:device_url]) - end end diff --git a/spec/unit/provider/cisco_spec.rb b/spec/unit/provider/cisco_spec.rb new file mode 100644 index 0000000..0832073 --- /dev/null +++ b/spec/unit/provider/cisco_spec.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'puppet/provider/cisco' + +describe Puppet::Provider::Cisco do + it "should implement a device class method" do + Puppet::Provider::Cisco.should respond_to(:device) + end + + it "should create a cisco device instance" do + Puppet::Util::NetworkDevice::Cisco::Device.expects(:new).returns :device + Puppet::Provider::Cisco.device(:url).should == :device + end +end \ No newline at end of file diff --git a/spec/unit/provider/interface/cisco_spec.rb b/spec/unit/provider/interface/cisco_spec.rb index d1f7060..c18f87c 100755 --- a/spec/unit/provider/interface/cisco_spec.rb +++ b/spec/unit/provider/interface/cisco_spec.rb @@ -8,12 +8,13 @@ provider_class = Puppet::Type.type(:interface).provider(:cisco) describe provider_class do before do + @device = stub_everything 'device' @resource = stub("resource", :name => "Fa0/1") - @provider = provider_class.new(@resource) + @provider = provider_class.new(@device, @resource) end - it "should have a parent of Puppet::Provider::NetworkDevice" do - provider_class.should < Puppet::Provider::NetworkDevice + it "should have a parent of Puppet::Provider::Cisco" do + provider_class.should < Puppet::Provider::Cisco end it "should have an instances method" do @@ -22,31 +23,24 @@ describe provider_class do describe "when looking up instances at prefetch" do before do - @device = stub_everything 'device' - Puppet::Util::NetworkDevice::Cisco::Device.stubs(:new).returns(@device) @device.stubs(:command).yields(@device) end - it "should initialize the network device with the given url" do - Puppet::Util::NetworkDevice::Cisco::Device.expects(:new).with(:url).returns(@device) - provider_class.lookup(:url, "Fa0/1") - end - it "should delegate to the device interface fetcher" do @device.expects(:interface) - provider_class.lookup("", "Fa0/1") + provider_class.lookup(@device, "Fa0/1") end it "should return the given interface data" do @device.expects(:interface).returns({ :description => "thisone", :mode => :access}) - provider_class.lookup("", "Fa0").should == {:description => "thisone", :mode => :access } + provider_class.lookup(@device, "Fa0").should == {:description => "thisone", :mode => :access } end end describe "when an instance is being flushed" do it "should call the device interface update method with current and past properties" do - @instance = provider_class.new(:ensure => :present, :name => "Fa0/1", :description => "myinterface") + @instance = provider_class.new(@device, :ensure => :present, :name => "Fa0/1", :description => "myinterface") @instance.description = "newdesc" @instance.resource = @resource @resource.stubs(:[]).with(:name).returns("Fa0/1") diff --git a/spec/unit/provider/network_device_spec.rb b/spec/unit/provider/network_device_spec.rb index 83d2bdc..e2a87cf 100755 --- a/spec/unit/provider/network_device_spec.rb +++ b/spec/unit/provider/network_device_spec.rb @@ -3,10 +3,15 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/provider/network_device' +require 'ostruct' Puppet::Type.type(:vlan).provide :test, :parent => Puppet::Provider::NetworkDevice do mk_resource_methods - def self.lookup(device_url, name) + def self.lookup(device, name) + end + + def self.device(url) + :device end end @@ -34,7 +39,7 @@ describe provider_class do end it "should lookup an entry for each passed resource" do - provider_class.expects(:lookup).with(nil, "200").returns nil + provider_class.expects(:lookup).with{ |device,value| value == "200" }.returns nil provider_class.stubs(:new) @resource.stubs(:provider=) @@ -44,7 +49,7 @@ describe provider_class do describe "resources that do not exist" do it "should create a provider with :ensure => :absent" do provider_class.stubs(:lookup).returns(nil) - provider_class.expects(:new).with(:ensure => :absent).returns "myprovider" + provider_class.expects(:new).with(:device, :ensure => :absent).returns "myprovider" @resource.expects(:provider=).with("myprovider") provider_class.prefetch(@resources) end @@ -54,7 +59,7 @@ describe provider_class do it "should create a provider with the results of the find and ensure at present" do provider_class.stubs(:lookup).returns({ :name => "200", :description => "myvlan"}) - provider_class.expects(:new).with(:name => "200", :description => "myvlan", :ensure => :present).returns "myprovider" + provider_class.expects(:new).with(:device, :name => "200", :description => "myvlan", :ensure => :present).returns "myprovider" @resource.expects(:provider=).with("myprovider") provider_class.prefetch(@resources) @@ -74,7 +79,7 @@ describe provider_class do end it "should store a copy of the hash as its vlan_properties" do - instance = provider_class.new(:one => :two) + instance = provider_class.new(:device, :one => :two) instance.former_properties.should == {:one => :two} end end @@ -82,7 +87,7 @@ describe provider_class do describe "when an instance" do before do - @instance = provider_class.new + @instance = provider_class.new(:device) @property_class = stub 'property_class', :array_matching => :all, :superclass => Puppet::Property @resource_class = stub 'resource_class', :attrclass => @property_class, :valid_parameter? => true, :validproperties => [:description] @@ -98,12 +103,12 @@ describe provider_class do end it "should indicate when the instance already exists" do - @instance = provider_class.new(:ensure => :present) + @instance = provider_class.new(:device, :ensure => :present) @instance.exists?.should be_true end it "should indicate when the instance does not exist" do - @instance = provider_class.new(:ensure => :absent) + @instance = provider_class.new(:device, :ensure => :absent) @instance.exists?.should be_false end diff --git a/spec/unit/provider/vlan/cisco_spec.rb b/spec/unit/provider/vlan/cisco_spec.rb index bb243a7..a67290e 100755 --- a/spec/unit/provider/vlan/cisco_spec.rb +++ b/spec/unit/provider/vlan/cisco_spec.rb @@ -8,12 +8,13 @@ provider_class = Puppet::Type.type(:vlan).provider(:cisco) describe provider_class do before do + @device = stub_everything 'device' @resource = stub("resource", :name => "200") - @provider = provider_class.new(@resource) + @provider = provider_class.new(@device, @resource) end - it "should have a parent of Puppet::Provider::NetworkDevice" do - provider_class.should < Puppet::Provider::NetworkDevice + it "should have a parent of Puppet::Provider::Cisco" do + provider_class.should < Puppet::Provider::Cisco end it "should have an instances method" do @@ -22,31 +23,24 @@ describe provider_class do describe "when looking up instances at prefetch" do before do - @device = stub_everything 'device' - Puppet::Util::NetworkDevice::Cisco::Device.stubs(:new).returns(@device) @device.stubs(:command).yields(@device) end - it "should initialize the network device with the given url" do - Puppet::Util::NetworkDevice::Cisco::Device.expects(:new).with(:url).returns(@device) - provider_class.lookup(:url, "200") - end - it "should delegate to the device vlans" do @device.expects(:parse_vlans) - provider_class.lookup("", "200") + provider_class.lookup(@device, "200") end it "should return only the given vlan" do @device.expects(:parse_vlans).returns({"200" => { :description => "thisone" }, "1" => { :description => "nothisone" }}) - provider_class.lookup("", "200").should == {:description => "thisone" } + provider_class.lookup(@device, "200").should == {:description => "thisone" } end end describe "when an instance is being flushed" do it "should call the device update_vlan method with its vlan id, current attributes, and desired attributes" do - @instance = provider_class.new(:ensure => :present, :name => "200", :description => "myvlan") + @instance = provider_class.new(@device, :ensure => :present, :name => "200", :description => "myvlan") @instance.description = "myvlan2" @instance.resource = @resource @resource.stubs(:[]).with(:name).returns("200") -- 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.
