From ac5fb1ca8a1984bdcd89784e0a913c8a833f95be Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Tue, 5 Mar 2013 14:42:17 -0800 Subject: [PATCH] change nova_config to use ini_file This commit refactors the nova_config type to use the ini_file provider. This will ensure that this type is consistent with other types. It also completely removes the parsed and parsed_config types. The usage of inifile from nova_config is slightly difference from other similar types. It does not require a section name, and assumes that settings supplied without a section name belong in the default section. This is for backwards compatibility with the other types and will likely change in the next major version. this commit also updates nova_paste_int to support purging. --- .../provider/nova_config/ini_setting.rb | 34 ++++++++ lib/puppet/provider/nova_config/parsed.rb | 38 --------- .../provider/nova_config/parsed_config.rb | 43 ---------- .../nova_paste_api_ini/ini_setting.rb | 7 +- lib/puppet/type/nova_config.rb | 20 ++--- spec/unit/provider/nova_config/parsed_spec.rb | 85 +++++++------------ 6 files changed, 83 insertions(+), 144 deletions(-) create mode 100644 lib/puppet/provider/nova_config/ini_setting.rb delete mode 100644 lib/puppet/provider/nova_config/parsed.rb delete mode 100644 lib/puppet/provider/nova_config/parsed_config.rb diff --git a/lib/puppet/provider/nova_config/ini_setting.rb b/lib/puppet/provider/nova_config/ini_setting.rb new file mode 100644 index 000000000..aa5e56d09 --- /dev/null +++ b/lib/puppet/provider/nova_config/ini_setting.rb @@ -0,0 +1,34 @@ +Puppet::Type.type(:nova_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:ini_setting).provider(:ruby) +) do + + # the setting is always default + # this if for backwards compat with the old puppet providers for nova_config + def section + section_setting = resource[:name].split('/', 2) + section_setting.size == 2 ? section_setting.first : 'DEFAULT' + end + + # assumes that the name was the setting + # this is to maintain backwards compat with the the older + # stuff + def setting + section_setting = resource[:name].split('/', 2) + section_setting.size == 2 ? section_setting.last : resource[:name] + end + + def separator + '=' + end + + def self.file_path + '/etc/nova/nova.conf' + end + + # this needs to be removed. This has been replaced with the class method + def file_path + self.class.file_path + end + +end diff --git a/lib/puppet/provider/nova_config/parsed.rb b/lib/puppet/provider/nova_config/parsed.rb deleted file mode 100644 index 841b2020a..000000000 --- a/lib/puppet/provider/nova_config/parsed.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'puppet/provider/parsedfile' - -Puppet::Type.type(:nova_config).provide( - :parsed, - :parent => Puppet::Provider::ParsedFile, - :default_target => Puppet::Type.type(:nova_config).default_target, - :filetype => :flat -) do - - - #confine :exists => novaconf - text_line :comment, :match => /^\s*#/; - text_line :blank, :match => /^\s*$/; - - record_line :parsed, - :fields => %w{line}, - :match => /--(.*)/ , - :post_parse => proc { |hash| - Puppet.debug("nova config line:#{hash[:line]} has been parsed") - if hash[:line] =~ /^\s*(\S+?)\s*=\s*([\S ]+)\s*$/ - hash[:name]=$1 - hash[:value]=$2 - elsif hash[:line] =~ /^\s*no(\S+)\s*$/ - hash[:name]=$1 - hash[:value]=false - elsif hash[:line] =~ /^\s*(\S+)\s*$/ - hash[:name]=$1 - hash[:value]=true - else - raise Puppet::Error, "Invalid line: #{hash[:line]}" - end - } - - def self.to_line(hash) - "--#{hash[:name]}=#{hash[:value]}" - end - -end diff --git a/lib/puppet/provider/nova_config/parsed_config.rb b/lib/puppet/provider/nova_config/parsed_config.rb deleted file mode 100644 index ec8d6af0c..000000000 --- a/lib/puppet/provider/nova_config/parsed_config.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'puppet/provider/parsedfile' - -Puppet::Type.type(:nova_config).provide( - :configfile, - :parent => Puppet::Provider::ParsedFile, - :default_target => Puppet::Type.type(:nova_config).default_target, - :filetype => :flat -) do - - confine :osfamily => [:debian, :redhat] - defaultfor :operatingsystem => :debian - - #confine :exists => novaconf - text_line :comment, :match => /#|\[.*/; - text_line :blank, :match => /^\s*$/; - - record_line :parsed, - :fields => %w{line}, - :match => /(.*)/ , - :post_parse => proc { |hash| - Puppet.debug("nova config line:#{hash[:line]} has been parsed") - if hash[:line] =~ /^\s*(\S+?)\s*=\s*([\S ]+?)\s*$/ - hash[:name]=$1 - hash[:value]=$2 - elsif hash[:line] =~ /^\s*(\S+)\s*$/ - hash[:name]=$1 - hash[:value]=false - else - raise Puppet::Error, "Invalid line: #{hash[:line]}" - end - } - - def self.to_line(hash) - if hash[:name] and hash[:value] - "#{hash[:name]}=#{hash[:value]}" - end - end - - def self.header - "# Auto Genarated Nova Config File\n[DEFAULT]\n" - end - -end diff --git a/lib/puppet/provider/nova_paste_api_ini/ini_setting.rb b/lib/puppet/provider/nova_paste_api_ini/ini_setting.rb index cf715ff31..339d32af0 100644 --- a/lib/puppet/provider/nova_paste_api_ini/ini_setting.rb +++ b/lib/puppet/provider/nova_paste_api_ini/ini_setting.rb @@ -15,8 +15,13 @@ Puppet::Type.type(:nova_paste_api_ini).provide( '=' end - def file_path + def self.file_path '/etc/nova/api-paste.ini' end + # this needs to be removed. This has been replaced with the class method + def file_path + self.class.file_path + end + end diff --git a/lib/puppet/type/nova_config.rb b/lib/puppet/type/nova_config.rb index df84376cc..05acad0c2 100644 --- a/lib/puppet/type/nova_config.rb +++ b/lib/puppet/type/nova_config.rb @@ -1,13 +1,13 @@ Puppet::Type.newtype(:nova_config) do - def self.default_target - "/etc/nova/nova.conf" - end +# def self.default_target +# "/etc/nova/nova.conf" +# end ensurable newparam(:name, :namevar => true) do - newvalues(/^\S+$/) + newvalues(/^\S+$/, /\S+\/\S+/) end newproperty(:value) do @@ -17,12 +17,12 @@ Puppet::Type.newtype(:nova_config) do newvalues(/^[\S ]+$/) end - newproperty(:target) do - desc "Path to our nova config file" - defaultto { - Puppet::Type.type(:nova_config).default_target - } - end + #newproperty(:target) do + # desc "Path to our nova config file" + # defaultto { + # Puppet::Type.type(:nova_config).default_target + # } + #end validate do if self[:ensure] == :present diff --git a/spec/unit/provider/nova_config/parsed_spec.rb b/spec/unit/provider/nova_config/parsed_spec.rb index 5dbd7d997..926901d42 100644 --- a/spec/unit/provider/nova_config/parsed_spec.rb +++ b/spec/unit/provider/nova_config/parsed_spec.rb @@ -1,57 +1,38 @@ -require 'puppet' -require 'mocha' -require 'tempfile' -RSpec.configure do |config| - config.mock_with :mocha -end -provider_class = Puppet::Type.type(:nova_config).provider(:parsed) +# +# these tests are a little concerning b/c they are hacking around the +# modulepath, so these tests will not catch issues that may eventually arise +# related to loading these plugins. +# I could not, for the life of me, figure out how to programatcally set the modulepath +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'inifile', + 'lib') +) +require 'spec_helper' +provider_class = Puppet::Type.type(:nova_config).provider(:ini_setting) describe provider_class do - before :each do - @nova_config = Tempfile.new('nova.conf') - @resource = Puppet::Type::Nova_config.new( - {:target => @nova_config, :name => 'foo', :value => 'bar'} + + it 'should default to the default setting when no other one is specified' do + resource = Puppet::Type::Nova_config.new( + {:name => 'foo', :value => 'bar'} ) - @provider = provider_class.new(@resource) + provider = provider_class.new(resource) + provider.section.should == 'DEFAULT' + provider.setting.should == 'foo' end - it 'should be able to parse lines into records' do - record = @provider.class.parse('--foo = bar').first - record[:name].should == 'foo' - record[:value].should == 'bar' - record[:record_type].should == :parsed - end - it 'should be able to parse settings without values' do - record = @provider.class.parse('--foo').first - record[:name].should == 'foo' - record[:value].should == true - record[:record_type].should == :parsed - end - it 'should be able to parse negated settings without values' do - record = @provider.class.parse('--nofoo').first - record[:name].should == 'foo' - record[:value].should == false - record[:record_type].should == :parsed - end - it 'should be able to parse values that have spaces' do - record = @provider.class.parse('--foo = bar or baz').first - record[:name].should == 'foo' - record[:value].should == 'bar or baz' - record[:record_type].should == :parsed - end - it 'should be able to parse values with equal signs' do - record = @provider.class.parse('--foo = bar=baz').first - record[:name].should == 'foo' - record[:value].should == 'bar=baz' - record[:record_type].should == :parsed - end - it 'should be able to create a valid line from a resource' do - provider_class.to_line({:name => 'foo', :value => 'bar'}).should == '--foo=bar' - end - it 'should parse empty lines' do - record = @provider.class.parse(' ').first - record[:record_type].should == :blank - end - it 'should parse comment lines' do - record = @provider.class.parse(' #--foo = bar').first - record[:record_type].should == :comment + + it 'should allow setting to be set explicitly' do + resource = Puppet::Type::Nova_config.new( + {:name => 'dude/foo', :value => 'bar'} + ) + provider = provider_class.new(resource) + provider.section.should == 'dude' + provider.setting.should == 'foo' end end