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.
This commit is contained in:
Dan Bode 2013-03-05 14:42:17 -08:00
parent 16bbb76f48
commit ac5fb1ca8a
6 changed files with 83 additions and 144 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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