Reflect provider change in puppet-openstacklib

With the creation of the new openstack_config provider, some processing
that was done in monasca_config has been centralized in
openstack_config.

This also applies for agent_config.

Impacted methods are :

  * section
  * setting
  * separator

Also, this commit adds the fact that, when passing a specific string
(ensure_absent_val) the provider will behave as if ensure => absent was
specified. '<SERVICE DEFAULT>' is the default value for
ensure_absent_val.

The use case is the following :

monasca_config { 'DEFAULT/foo' : value => 'bar' } # will work as usual

monasca_config { 'DEFAULT/foo' : value => '<SERVICE DEFAULT>' } # will mean absent

That means that all the current :

if $myvar {
  monasca_config { 'DEFAULT/foo' : value => $myvar }
} else {
  monasca_config { 'DEFAULT/foo' : ensure => absent }
}

can be removed in favor of :

monasca_config { 'DEFAULT/foo' : value => $myvar }

If for any reason '<SERVICE DEFAULT>' turns out to be a valid value for
a specific parameter. One could by pass that doing the following :

monasca_config { 'DEFAULT/foo' : value => '<SERVICE DEFAULT>',
ensure_absent_val => 'foo' }

Change-Id: I7b1ced3f5e2d3e9b685a4ae4122f83bea73bc877
Depends-On: I0eeebde3aac2662cc7e69bfad7f8d2481463a218
This commit is contained in:
Yanis Guenane 2015-08-06 13:06:01 +02:00
parent a69dd3de18
commit b29b0f1da5
17 changed files with 520 additions and 56 deletions

View File

@ -1,6 +1,7 @@
fixtures:
repositories:
'keystone': 'git://github.com/openstack/puppet-keystone.git'
'inifile': 'git://github.com/puppetlabs/puppetlabs-inifile'
'openstacklib': 'git://github.com/openstack/puppet-openstacklib.git'
'stdlib': 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
'python': 'git://github.com/stankevich/puppet-python.git'

View File

@ -36,6 +36,64 @@ Implementation
monasca is a combination of Puppet manifest that configures the monasca client and server configuration, as well as monasca's dependent services.
### Types
#### monasca_config
The `monasca_config` provider is a children of the ini_setting provider. It allows one to write an entry in the `/etc/monasca/monasca.conf` file.
```puppet
monasca_config { 'DEFAULT/verbose' :
value => true,
}
```
This will write `verbose=true` in the `[DEFAULT]` section.
##### name
Section/setting name to manage from `monasca.conf`
##### value
The value of the setting to be defined.
##### secret
Whether to hide the value from Puppet logs. Defaults to `false`.
##### ensure_absent_val
If value is equal to ensure_absent_val then the resource will behave as if `ensure => absent` was specified. Defaults to `<SERVICE DEFAULT>`
#### agent_config
The `agent_config` provider is a children of the ini_setting provider. It allows one to write an entry in the `/etc/monasca/agent/agent.conf` file.
```puppet
agent_config { 'DEFAULT/verbose' :
value => true,
}
```
This will write `verbose=true` in the `[DEFAULT]` section.
##### name
Section/setting name to manage from `agent.conf`
##### value
The value of the setting to be defined.
##### secret
Whether to hide the value from Puppet logs. Defaults to `false`.
##### ensure_absent_val
If value is equal to ensure_absent_val then the resource will behave as if `ensure => absent` was specified. Defaults to `<SERVICE DEFAULT>`
Limitations
-----------
This module currently only supports debian based installs.

View File

@ -1,27 +1,10 @@
Puppet::Type.type(:agent_config).provide(
:ini_setting,
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
:parent => Puppet::Type.type(:openstack_config).provider(:ini_setting)
) do
def section
resource[:name].split('/', 2).first
end
def setting
resource[:name].split('/', 2).last
end
def separator
'='
end
def self.file_path
'/etc/monasca/agent/agent.conf'
end
# added for backwards compatibility with older versions of inifile
def file_path
self.class.file_path
end
end

View File

@ -1,27 +1,10 @@
Puppet::Type.type(:monasca_config).provide(
:ini_setting,
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
:parent => Puppet::Type.type(:openstack_config).provider(:ini_setting)
) do
def section
resource[:name].split('/', 2).first
end
def setting
resource[:name].split('/', 2).last
end
def separator
'='
end
def self.file_path
'/etc/monasca/monasca.conf'
end
# added for backwards compatibility with older versions of inifile
def file_path
self.class.file_path
end
end

View File

@ -1,27 +1,10 @@
Puppet::Type.type(:monasca_ini).provide(
:ini_setting,
:parent => Puppet::Type.type(:ini_setting).provider(:ruby)
:parent => Puppet::Type.type(:openstack_config).provider(:ini_setting)
) do
def section
resource[:name].split('/', 2).first
end
def setting
resource[:name].split('/', 2).last
end
def separator
'='
end
def self.file_path
'/etc/monasca/monasca.ini'
end
# added for backwards compatibility with older versions of inifile
def file_path
self.class.file_path
end
end

View File

@ -14,6 +14,7 @@ Puppet::Type.newtype(:agent_config) do
value.capitalize! if value =~ /^(true|false)$/i
value
end
newvalues(/^[\S ]*$/)
def is_to_s( currentvalue )
if resource.secret?
@ -39,4 +40,10 @@ Puppet::Type.newtype(:agent_config) do
defaultto false
end
newparam(:ensure_absent_val) do
desc 'A value that is specified as the value property will behave as if ensure => absent was specified'
defaultto('<SERVICE DEFAULT>')
end
end

View File

@ -14,6 +14,7 @@ Puppet::Type.newtype(:monasca_config) do
value.capitalize! if value =~ /^(true|false)$/i
value
end
newvalues(/^[\S ]*$/)
def is_to_s( currentvalue )
if resource.secret?
@ -39,4 +40,10 @@ Puppet::Type.newtype(:monasca_config) do
defaultto false
end
newparam(:ensure_absent_val) do
desc 'A value that is specified as the value property will behave as if ensure => absent was specified'
defaultto('<SERVICE DEFAULT>')
end
end

View File

@ -39,4 +39,10 @@ Puppet::Type.newtype(:monasca_ini) do
defaultto false
end
newparam(:ensure_absent_val) do
desc 'A value that is specified as the value property will behave as if ensure => absent was specified'
defaultto('<SERVICE DEFAULT>')
end
end

View File

@ -0,0 +1,84 @@
require 'spec_helper_acceptance'
describe 'basic monasca_config resource' do
context 'default parameters' do
it 'should work with no errors' do
pp= <<-EOS
Exec { logoutput => 'on_failure' }
File <||> -> Monasca_config <||>
File <||> -> Agent_config <||>
file { '/etc/monasca' :
ensure => directory,
}
file { '/etc/monasca/monasca.conf' :
ensure => file,
}
file { '/etc/monasca/agent/agent.conf' :
ensure => file,
}
monasca_config { 'DEFAULT/thisshouldexist' :
value => 'foo',
}
monasca_config { 'DEFAULT/thisshouldnotexist' :
value => '<SERVICE DEFAULT>',
}
monasca_config { 'DEFAULT/thisshouldexist2' :
value => '<SERVICE DEFAULT>',
ensure_absent_val => 'toto',
}
monasca_config { 'DEFAULT/thisshouldnotexist2' :
value => 'toto',
ensure_absent_val => 'toto',
}
agent_config { 'DEFAULT/thisshouldexist' :
value => 'foo',
}
agent_config { 'DEFAULT/thisshouldnotexist' :
value => '<SERVICE DEFAULT>',
}
agent_config { 'DEFAULT/thisshouldexist2' :
value => '<SERVICE DEFAULT>',
ensure_absent_val => 'toto',
}
agent_config { 'DEFAULT/thisshouldnotexist2' :
value => 'toto',
ensure_absent_val => 'toto',
}
EOS
# Run it twice and test for idempotency
apply_manifest(pp, :catch_failures => true)
apply_manifest(pp, :catch_changes => true)
end
describe file('/etc/monasca/monasca.conf') do
it { should exist }
it { should contain('thisshouldexist=foo') }
it { should contain('thisshouldexist2=<SERVICE DEFAULT>') }
its(:content) { should_not match /thisshouldnotexist/ }
end
describe file('/etc/monasca/agent/agent.conf') do
it { should exist }
it { should contain('thisshouldexist=foo') }
it { should contain('thisshouldexist2=<SERVICE DEFAULT>') }
its(:content) { should_not match /thisshouldnotexist/ }
end
end
end

5
spec/shared_examples.rb Normal file
View File

@ -0,0 +1,5 @@
shared_examples_for "a Puppet::Error" do |description|
it "with message matching #{description.inspect}" do
expect { is_expected.to have_class_count(1) }.to raise_error(Puppet::Error, description)
end
end

View File

@ -1,6 +1,8 @@
require 'puppetlabs_spec_helper/module_spec_helper'
require 'shared_examples'
RSpec.configure do |c|
c.alias_it_should_behave_like_to :it_configures, 'configures'
c.alias_it_should_behave_like_to :it_raises, 'raises'
c.alias_it_should_behave_like_to :it_configures, 'configures'
c.alias_it_should_behave_like_to :it_raises, 'raises'
c.default_facts = { :concat_basedir => '/var/lib/puppet/concat' }
end

View File

@ -0,0 +1,63 @@
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'inifile',
'lib')
)
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'openstacklib',
'lib')
)
require 'spec_helper'
provider_class = Puppet::Type.type(:agent_config).provider(:ini_setting)
describe provider_class do
it 'should default to the default setting when no other one is specified' do
resource = Puppet::Type::Agent_config.new(
{:name => 'DEFAULT/foo', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('DEFAULT')
expect(provider.setting).to eq('foo')
end
it 'should allow setting to be set explicitly' do
resource = Puppet::Type::Agent_config.new(
{:name => 'dude/whoa', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('dude')
expect(provider.setting).to eq('whoa')
end
it 'should ensure absent when <SERVICE DEFAULT> is specified as a value' do
resource = Puppet::Type::Agent_config.new(
{:name => 'dude/foo', :value => '<SERVICE DEFAULT>'}
)
provider = provider_class.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :absent
end
it 'should ensure absent when value matches ensure_absent_val' do
resource = Puppet::Type::Agent_config.new(
{:name => 'dude/foo', :value => 'foo', :ensure_absent_val => 'foo' }
)
provider = provider_class.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :absent
end
end

View File

@ -0,0 +1,63 @@
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'inifile',
'lib')
)
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'openstacklib',
'lib')
)
require 'spec_helper'
provider_class = Puppet::Type.type(:monasca_config).provider(:ini_setting)
describe provider_class do
it 'should default to the default setting when no other one is specified' do
resource = Puppet::Type::Monasca_config.new(
{:name => 'DEFAULT/foo', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('DEFAULT')
expect(provider.setting).to eq('foo')
end
it 'should allow setting to be set explicitly' do
resource = Puppet::Type::Monasca_config.new(
{:name => 'dude/whoa', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('dude')
expect(provider.setting).to eq('whoa')
end
it 'should ensure absent when <SERVICE DEFAULT> is specified as a value' do
resource = Puppet::Type::Monasca_config.new(
{:name => 'dude/foo', :value => '<SERVICE DEFAULT>'}
)
provider = provider_class.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :absent
end
it 'should ensure absent when value matches ensure_absent_val' do
resource = Puppet::Type::Monasca_config.new(
{:name => 'dude/foo', :value => 'foo', :ensure_absent_val => 'foo' }
)
provider = provider_class.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :absent
end
end

View File

@ -0,0 +1,63 @@
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'inifile',
'lib')
)
$LOAD_PATH.push(
File.join(
File.dirname(__FILE__),
'..',
'..',
'..',
'fixtures',
'modules',
'openstacklib',
'lib')
)
require 'spec_helper'
provider_class = Puppet::Type.type(:monasca_ini).provider(:ini_setting)
describe provider_class do
it 'should default to the default setting when no other one is specified' do
resource = Puppet::Type::Monasca_ini.new(
{:name => 'DEFAULT/foo', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('DEFAULT')
expect(provider.setting).to eq('foo')
end
it 'should allow setting to be set explicitly' do
resource = Puppet::Type::Monasca_ini.new(
{:name => 'dude/whoa', :value => 'bar'}
)
provider = provider_class.new(resource)
expect(provider.section).to eq('dude')
expect(provider.setting).to eq('whoa')
end
it 'should ensure absent when <SERVICE DEFAULT> is specified as a value' do
resource = Puppet::Type::Monasca_ini.new(
{:name => 'dude/foo', :value => '<SERVICE DEFAULT>'}
)
provider = provider_class.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :absent
end
it 'should ensure absent when value matches ensure_absent_val' do
resource = Puppet::Type::Monasca_ini.new(
{:name => 'dude/foo', :value => 'foo', :ensure_absent_val => 'foo' }
)
provider = provider_class.new(resource)
provider.exists?
expect(resource[:ensure]).to eq :absent
end
end

View File

@ -0,0 +1,52 @@
require 'puppet'
require 'puppet/type/agent_config'
describe 'Puppet::Type.type(:agent_config)' do
before :each do
@agent_config = Puppet::Type.type(:agent_config).new(:name => 'DEFAULT/foo', :value => 'bar')
end
it 'should require a name' do
expect {
Puppet::Type.type(:agent_config).new({})
}.to raise_error(Puppet::Error, 'Title or name must be provided')
end
it 'should not expect a name with whitespace' do
expect {
Puppet::Type.type(:agent_config).new(:name => 'f oo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should fail when there is no section' do
expect {
Puppet::Type.type(:agent_config).new(:name => 'foo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should not require a value when ensure is absent' do
Puppet::Type.type(:agent_config).new(:name => 'DEFAULT/foo', :ensure => :absent)
end
it 'should accept a valid value' do
@agent_config[:value] = 'bar'
expect(@agent_config[:value]).to eq('bar')
end
it 'should not accept a value with whitespace' do
@agent_config[:value] = 'b ar'
expect(@agent_config[:value]).to eq('b ar')
end
it 'should accept valid ensure values' do
@agent_config[:ensure] = :present
expect(@agent_config[:ensure]).to eq(:present)
@agent_config[:ensure] = :absent
expect(@agent_config[:ensure]).to eq(:absent)
end
it 'should not accept invalid ensure values' do
expect {
@agent_config[:ensure] = :latest
}.to raise_error(Puppet::Error, /Invalid value/)
end
end

View File

@ -0,0 +1,52 @@
require 'puppet'
require 'puppet/type/monasca_config'
describe 'Puppet::Type.type(:monasca_config)' do
before :each do
@monasca_config = Puppet::Type.type(:monasca_config).new(:name => 'DEFAULT/foo', :value => 'bar')
end
it 'should require a name' do
expect {
Puppet::Type.type(:monasca_config).new({})
}.to raise_error(Puppet::Error, 'Title or name must be provided')
end
it 'should not expect a name with whitespace' do
expect {
Puppet::Type.type(:monasca_config).new(:name => 'f oo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should fail when there is no section' do
expect {
Puppet::Type.type(:monasca_config).new(:name => 'foo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should not require a value when ensure is absent' do
Puppet::Type.type(:monasca_config).new(:name => 'DEFAULT/foo', :ensure => :absent)
end
it 'should accept a valid value' do
@monasca_config[:value] = 'bar'
expect(@monasca_config[:value]).to eq('bar')
end
it 'should not accept a value with whitespace' do
@monasca_config[:value] = 'b ar'
expect(@monasca_config[:value]).to eq('b ar')
end
it 'should accept valid ensure values' do
@monasca_config[:ensure] = :present
expect(@monasca_config[:ensure]).to eq(:present)
@monasca_config[:ensure] = :absent
expect(@monasca_config[:ensure]).to eq(:absent)
end
it 'should not accept invalid ensure values' do
expect {
@monasca_config[:ensure] = :latest
}.to raise_error(Puppet::Error, /Invalid value/)
end
end

View File

@ -0,0 +1,52 @@
require 'puppet'
require 'puppet/type/monasca_ini'
describe 'Puppet::Type.type(:monasca_ini)' do
before :each do
@monasca_ini = Puppet::Type.type(:monasca_ini).new(:name => 'DEFAULT/foo', :value => 'bar')
end
it 'should require a name' do
expect {
Puppet::Type.type(:monasca_ini).new({})
}.to raise_error(Puppet::Error, 'Title or name must be provided')
end
it 'should not expect a name with whitespace' do
expect {
Puppet::Type.type(:monasca_ini).new(:name => 'f oo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should fail when there is no section' do
expect {
Puppet::Type.type(:monasca_ini).new(:name => 'foo')
}.to raise_error(Puppet::Error, /Parameter name failed/)
end
it 'should not require a value when ensure is absent' do
Puppet::Type.type(:monasca_ini).new(:name => 'DEFAULT/foo', :ensure => :absent)
end
it 'should accept a valid value' do
@monasca_ini[:value] = 'bar'
expect(@monasca_ini[:value]).to eq('bar')
end
it 'should not accept a value with whitespace' do
@monasca_ini[:value] = 'b ar'
expect(@monasca_ini[:value]).to eq('b ar')
end
it 'should accept valid ensure values' do
@monasca_ini[:ensure] = :present
expect(@monasca_ini[:ensure]).to eq(:present)
@monasca_ini[:ensure] = :absent
expect(@monasca_ini[:ensure]).to eq(:absent)
end
it 'should not accept invalid ensure values' do
expect {
@monasca_ini[:ensure] = :latest
}.to raise_error(Puppet::Error, /Invalid value/)
end
end