Merge pull request #16 from bodepd/master

changes for child provider usage
This commit is contained in:
Chris Price 2012-10-03 09:29:27 -07:00
commit 842b2f06b2
3 changed files with 65 additions and 34 deletions

View file

@ -1,20 +1,45 @@
require File.expand_path('../../../util/ini_file', __FILE__) require File.expand_path('../../../util/ini_file', __FILE__)
Puppet::Type.type(:ini_setting).provide(:ruby) do Puppet::Type.type(:ini_setting).provide(:ruby) do
def exists? def exists?
ini_file.get_value(resource[:section], resource[:setting]) == resource[:value].to_s ini_file.get_value(section, setting)
end end
def create def create
ini_file.set_value(resource[:section], resource[:setting], resource[:value]) ini_file.set_value(section, setting, resource[:value])
ini_file.save ini_file.save
@ini_file = nil @ini_file = nil
end end
def value
ini_file.get_value(section, setting)
end
def value=(value)
ini_file.set_value(section, setting, resource[:value])
ini_file.save
end
def section
resource[:section]
end
def setting
resource[:setting]
end
def file_path
resource[:path]
end
def separator
resource[:key_val_separator] || '='
end
private private
def ini_file def ini_file
@ini_file ||= Puppet::Util::IniFile.new(resource[:path], @ini_file ||= Puppet::Util::IniFile.new(file_path, separator)
resource[:key_val_separator])
end end
end end

View file

@ -17,10 +17,6 @@ Puppet::Type.newtype(:ini_setting) do
desc 'The name of the setting to be defined.' desc 'The name of the setting to be defined.'
end end
newparam(:value) do
desc 'The value of the setting to be defined.'
end
newparam(:path) do newparam(:path) do
desc 'The ini file Puppet will ensure contains the specified setting.' desc 'The ini file Puppet will ensure contains the specified setting.'
validate do |value| validate do |value|
@ -43,4 +39,9 @@ Puppet::Type.newtype(:ini_setting) do
end end
end end
newproperty(:value) do
desc 'The value of the setting to be defined.'
end
end end

View file

@ -54,7 +54,7 @@ subby=bar
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:setting => 'yahoo', :value => 'yippee')) :setting => 'yahoo', :value => 'yippee'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
@ -82,7 +82,7 @@ subby=bar
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => 'section:sub', :setting => 'yahoo', :value => 'yippee')) :section => 'section:sub', :setting => 'yahoo', :value => 'yippee'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
@ -110,8 +110,8 @@ yahoo = yippee
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:setting => 'baz', :value => 'bazvalue2')) :setting => 'baz', :value => 'bazvalue2'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should == 'bazvalue'
provider.create provider.value=('bazvalue2')
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
[section1] [section1]
@ -137,8 +137,9 @@ subby=bar
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => 'section:sub', :setting => 'subby', :value => 'foo')) :section => 'section:sub', :setting => 'subby', :value => 'foo'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should == 'bar'
provider.create provider.value.should == 'bar'
provider.value=('foo')
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
[section1] [section1]
@ -164,8 +165,9 @@ subby = foo
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:setting => 'url', :value => 'http://192.168.0.1:8080')) :setting => 'url', :value => 'http://192.168.0.1:8080'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should == 'http://192.168.1.1:8080'
provider.create provider.value.should == 'http://192.168.1.1:8080'
provider.value=('http://192.168.0.1:8080')
validate_file( <<-EOS validate_file( <<-EOS
# This is a comment # This is a comment
@ -192,14 +194,14 @@ subby=bar
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:setting => 'baz', :value => 'bazvalue')) :setting => 'baz', :value => 'bazvalue'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == true provider.exists?.should == 'bazvalue'
end end
it "should add a new section if the section does not exist" do it "should add a new section if the section does not exist" do
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => "section3", :setting => 'huzzah', :value => 'shazaam')) :section => "section3", :setting => 'huzzah', :value => 'shazaam'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
@ -229,7 +231,7 @@ huzzah = shazaam
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => "section:subsection", :setting => 'huzzah', :value => 'shazaam')) :section => "section:subsection", :setting => 'huzzah', :value => 'shazaam'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
@ -259,7 +261,7 @@ huzzah = shazaam
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile)) :section => "section1", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(" validate_file("
[section1] [section1]
@ -271,7 +273,7 @@ setting1 = hellowworld
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile)) :section => "section:subsection", :setting => 'setting1', :value => 'hellowworld', :path => emptyfile))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(" validate_file("
[section:subsection] [section:subsection]
@ -283,8 +285,8 @@ setting1 = hellowworld
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => "section1", :setting => 'master', :value => true)) :section => "section1", :setting => 'master', :value => true))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == true provider.exists?.should == 'true'
provider.create provider.value.should == 'true'
end end
end end
@ -305,7 +307,7 @@ foo = http://192.168.1.1:8080
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => '', :setting => 'bar', :value => 'yippee')) :section => '', :setting => 'bar', :value => 'yippee'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
@ -322,8 +324,9 @@ foo = http://192.168.1.1:8080
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => '', :setting => 'foo', :value => 'yippee')) :section => '', :setting => 'foo', :value => 'yippee'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should == 'blah'
provider.create provider.value.should == 'blah'
provider.value=('yippee')
validate_file(<<-EOS validate_file(<<-EOS
# This is a comment # This is a comment
foo = yippee foo = yippee
@ -338,7 +341,7 @@ foo = http://192.168.1.1:8080
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => '', :setting => 'foo', :value => 'blah')) :section => '', :setting => 'foo', :value => 'blah'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == true provider.exists?.should == 'blah'
end end
end end
@ -354,7 +357,7 @@ foo = http://192.168.1.1:8080
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => '', :setting => 'foo', :value => 'yippee')) :section => '', :setting => 'foo', :value => 'yippee'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
foo = yippee foo = yippee
@ -368,8 +371,9 @@ foo = http://192.168.1.1:8080
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => 'section2', :setting => 'foo', :value => 'yippee')) :section => 'section2', :setting => 'foo', :value => 'yippee'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should == 'http://192.168.1.1:8080'
provider.create provider.value.should == 'http://192.168.1.1:8080'
provider.value=('yippee')
validate_file(<<-EOS validate_file(<<-EOS
[section2] [section2]
foo = yippee foo = yippee
@ -381,7 +385,7 @@ foo = yippee
resource = Puppet::Type::Ini_setting.new(common_params.merge( resource = Puppet::Type::Ini_setting.new(common_params.merge(
:section => 'section2', :setting => 'bar', :value => 'baz')) :section => 'section2', :setting => 'bar', :value => 'baz'))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
[section2] [section2]
@ -427,8 +431,9 @@ foo=bar
:value => 'yippee', :value => 'yippee',
:key_val_separator => '=')) :key_val_separator => '='))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should == 'bar'
provider.create provider.value.should == 'bar'
provider.value=('yippee')
validate_file(<<-EOS validate_file(<<-EOS
[section2] [section2]
foo=yippee foo=yippee
@ -443,7 +448,7 @@ foo=yippee
:value => 'baz', :value => 'baz',
:key_val_separator => '=')) :key_val_separator => '='))
provider = described_class.new(resource) provider = described_class.new(resource)
provider.exists?.should == false provider.exists?.should be_nil
provider.create provider.create
validate_file(<<-EOS validate_file(<<-EOS
[section2] [section2]