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__)
Puppet::Type.type(:ini_setting).provide(:ruby) do
def exists?
ini_file.get_value(resource[:section], resource[:setting]) == resource[:value].to_s
ini_file.get_value(section, setting)
end
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 = nil
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
def ini_file
@ini_file ||= Puppet::Util::IniFile.new(resource[:path],
resource[:key_val_separator])
@ini_file ||= Puppet::Util::IniFile.new(file_path, separator)
end
end

View file

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

View file

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