diff --git a/lib/puppet/util/nagios_maker.rb b/lib/puppet/util/nagios_maker.rb index 88402838f..a7aae4e70 100644 --- a/lib/puppet/util/nagios_maker.rb +++ b/lib/puppet/util/nagios_maker.rb @@ -1,56 +1,57 @@ require 'puppet/external/nagios' require 'puppet/external/nagios/base' require 'puppet/provider/naginator' module Puppet::Util::NagiosMaker # Create a new nagios type, using all of the parameters # from the parser. def self.create_nagios_type(name) name = name.to_sym full_name = ("nagios_" + name.to_s).to_sym raise(Puppet::DevError, "No nagios type for %s" % name) unless nagtype = Nagios::Base.type(name) type = Puppet::Type.newtype(full_name) {} type.ensurable type.newparam(nagtype.namevar, :namevar => true) do desc "The name parameter for Nagios type %s" % nagtype.name end # We deduplicate the parameters because it makes sense to allow Naginator to have dupes. nagtype.parameters.uniq.each do |param| next if param == nagtype.namevar # We can't turn these parameter names into constants, so at least for now they aren't # supported. next if param.to_s =~ /^[0-9]/ type.newproperty(param) do desc "Nagios configuration file parameter." end end type.newproperty(:target) do desc 'target' defaultto do resource.class.defaultprovider.default_target end end - provider = type.provide(:naginator, :parent => Puppet::Provider::Naginator, :default_target => "/etc/nagios/#{full_name.to_s}.cfg") {} + target = "/etc/nagios/#{full_name.to_s}.cfg" + provider = type.provide(:naginator, :parent => Puppet::Provider::Naginator, :default_target => target) {} type.desc "The Nagios type #{name.to_s}. This resource type is autogenerated using the model developed in Naginator_, and all of the Nagios types are generated using the same code and the same library. This type generates Nagios configuration statements in Nagios-parseable configuration - files. By default, the statements will be added to ``#{provider.default_target}``, but + files. By default, the statements will be added to ``#{target}``, but you can send them to a different file by setting their ``target`` attribute. .. _naginator: http://reductivelabs.com/trac/naginator " end end diff --git a/spec/unit/util/nagios_maker.rb b/spec/unit/util/nagios_maker.rb index 0454b6503..b75439400 100755 --- a/spec/unit/util/nagios_maker.rb +++ b/spec/unit/util/nagios_maker.rb @@ -1,128 +1,128 @@ #!/usr/bin/env ruby # # Created by Luke Kanies on 2007-11-18. # Copyright (c) 2007. All rights reserved. require File.dirname(__FILE__) + '/../../spec_helper' require 'puppet/util/nagios_maker' describe Puppet::Util::NagiosMaker do before do @module = Puppet::Util::NagiosMaker @nagtype = stub 'nagios type', :parameters => [], :namevar => :name Nagios::Base.stubs(:type).with(:test).returns(@nagtype) end it "should be able to create a new nagios type" do @module.should respond_to(:create_nagios_type) end it "should fail if it cannot find the named Naginator type" do Nagios::Base.stubs(:type).returns(nil) lambda { @module.create_nagios_type(:no_such_type) }.should raise_error(Puppet::DevError) end it "should create a new RAL type with the provided name prefixed with 'nagios_'" do - type = stub 'type', :newparam => nil, :newproperty => nil, :ensurable => nil, :provide => nil + type = stub 'type', :newparam => nil, :newproperty => nil, :ensurable => nil, :provide => nil, :desc => nil Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end it "should mark the created type as ensurable" do - type = stub 'type', :newparam => nil, :newproperty => nil, :provide => nil + type = stub 'type', :newparam => nil, :newproperty => nil, :provide => nil, :desc => nil type.expects(:ensurable) Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end it "should create a namevar parameter for the nagios type's name parameter" do - type = stub 'type', :newproperty => nil, :ensurable => nil, :provide => nil + type = stub 'type', :newproperty => nil, :ensurable => nil, :provide => nil, :desc => nil type.expects(:newparam).with(:name, :namevar => true) Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end it "should create a property for all non-namevar parameters" do - type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil + type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil, :desc => nil @nagtype.stubs(:parameters).returns([:one, :two]) type.expects(:newproperty).with(:one) type.expects(:newproperty).with(:two) type.expects(:newproperty).with(:target) Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end it "should skip parameters that start with integers" do - type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil + type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil, :desc => nil @nagtype.stubs(:parameters).returns(["2dcoords".to_sym, :other]) type.expects(:newproperty).with(:other) type.expects(:newproperty).with(:target) Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end it "should deduplicate the parameter list" do - type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil + type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil, :desc => nil @nagtype.stubs(:parameters).returns([:one, :one]) type.expects(:newproperty).with(:one) type.expects(:newproperty).with(:target) Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end it "should create a target property" do - type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil + type = stub 'type', :newparam => nil, :ensurable => nil, :provide => nil, :desc => nil type.expects(:newproperty).with(:target) Puppet::Type.expects(:newtype).with(:nagios_test).returns(type) @module.create_nagios_type(:test) end end describe Puppet::Util::NagiosMaker, " when creating the naginator provider" do before do @module = Puppet::Util::NagiosMaker @nagtype = stub 'nagios type', :parameters => [], :namevar => :name Nagios::Base.stubs(:type).with(:test).returns(@nagtype) - @type = stub 'type', :newparam => nil, :ensurable => nil, :newproperty => nil + @type = stub 'type', :newparam => nil, :ensurable => nil, :newproperty => nil, :desc => nil Puppet::Type.stubs(:newtype).with(:nagios_test).returns(@type) end it "should add a naginator provider" do @type.expects(:provide).with { |name, options| name == :naginator } @module.create_nagios_type(:test) end it "should set Puppet::Provider::Naginator as the parent class of the provider" do @type.expects(:provide).with { |name, options| options[:parent] == Puppet::Provider::Naginator } @module.create_nagios_type(:test) end it "should use /etc/nagios/$name.cfg as the default target" do @type.expects(:provide).with { |name, options| options[:default_target] == "/etc/nagios/nagios_test.cfg" } @module.create_nagios_type(:test) end end