From d1d508a636f366a3cd8bb6fe0aed9ab867d4050e Mon Sep 17 00:00:00 2001 From: ZhongShengping Date: Tue, 10 May 2016 22:06:02 +0800 Subject: [PATCH] Add api_paste type/provider for aodh Add aodh_api_paste_ini type/provider for aodh. Also add the capability to configure api_paste.ini with config.pp. Closes-Bug:1483371 Change-Id: Ia083df99901ccae646d5cc3c6414a3f126f1eac2 --- .../aodh_api_paste_ini/ini_setting.rb | 27 ++++++++++ lib/puppet/type/aodh_api_paste_ini.rb | 52 +++++++++++++++++++ manifests/config.pp | 8 ++- .../aodh_api_paste_ini/ini_setting_spec.rb | 29 +++++++++++ spec/unit/type/aodh_api_paste_init.spec.rb | 34 ++++++++++++ 5 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 lib/puppet/provider/aodh_api_paste_ini/ini_setting.rb create mode 100644 lib/puppet/type/aodh_api_paste_ini.rb create mode 100644 spec/unit/provider/aodh_api_paste_ini/ini_setting_spec.rb create mode 100644 spec/unit/type/aodh_api_paste_init.spec.rb diff --git a/lib/puppet/provider/aodh_api_paste_ini/ini_setting.rb b/lib/puppet/provider/aodh_api_paste_ini/ini_setting.rb new file mode 100644 index 00000000..b191feba --- /dev/null +++ b/lib/puppet/provider/aodh_api_paste_ini/ini_setting.rb @@ -0,0 +1,27 @@ +Puppet::Type.type(:aodh_api_paste_ini).provide( + :ini_setting, + :parent => Puppet::Type.type(:ini_setting).provider(:ruby) +) 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/aodh/api_paste.ini' + end + + # added for backwards compatibility with older versions of inifile + def file_path + self.class.file_path + end + +end diff --git a/lib/puppet/type/aodh_api_paste_ini.rb b/lib/puppet/type/aodh_api_paste_ini.rb new file mode 100644 index 00000000..1f5df52f --- /dev/null +++ b/lib/puppet/type/aodh_api_paste_ini.rb @@ -0,0 +1,52 @@ +Puppet::Type.newtype(:aodh_api_paste_ini) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from /etc/aodh/api_paste.ini' + newvalues(/\S+\/\S+/) + end + + newproperty(:value) do + desc 'The value of the setting to be defined.' + munge do |value| + value = value.to_s.strip + value.capitalize! if value =~ /^(true|false)$/i + value + end + + def is_to_s( currentvalue ) + if resource.secret? + return '[old secret redacted]' + else + return currentvalue + end + end + + def should_to_s( newvalue ) + if resource.secret? + return '[new secret redacted]' + else + return newvalue + end + end + end + + newparam(:secret, :boolean => true) do + desc 'Whether to hide the value from Puppet logs. Defaults to `false`.' + + newvalues(:true, :false) + + 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('') + end + + autorequire(:package) do + 'aodh-common' + end + +end diff --git a/manifests/config.pp b/manifests/config.pp index 0d212ff6..5ef19ac1 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -17,14 +17,20 @@ # DEFAULT/bar: # value: barValue # +# [*aodh_api_paste_ini*] +# (optional) Allow configuration of /etc/aodh/api_paste.ini options. +# # NOTE: The configuration MUST NOT be already handled by this module # or Puppet catalog compilation will fail with duplicate resources. # class aodh::config ( - $aodh_config = {}, + $aodh_config = {}, + $aodh_api_paste_ini = {}, ) { validate_hash($aodh_config) + validate_hash($aodh_api_paste_ini) create_resources('aodh_config', $aodh_config) + create_resources('aodh_api_paste_ini', $aodh_api_paste_ini) } diff --git a/spec/unit/provider/aodh_api_paste_ini/ini_setting_spec.rb b/spec/unit/provider/aodh_api_paste_ini/ini_setting_spec.rb new file mode 100644 index 00000000..3c578fea --- /dev/null +++ b/spec/unit/provider/aodh_api_paste_ini/ini_setting_spec.rb @@ -0,0 +1,29 @@ +# +# 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(:aodh_api_paste_ini).provider(:ini_setting) +describe provider_class do + + it 'should allow setting to be set explicitly' do + resource = Puppet::Type::Aodh_api_paste_ini.new( + {:name => 'dude/foo', :value => 'bar'} + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('dude') + expect(provider.setting).to eq('foo') + end +end diff --git a/spec/unit/type/aodh_api_paste_init.spec.rb b/spec/unit/type/aodh_api_paste_init.spec.rb new file mode 100644 index 00000000..5bdc009d --- /dev/null +++ b/spec/unit/type/aodh_api_paste_init.spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' +# this hack is required for now to ensure that the path is set up correctly +# to retrive the parent provider +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + 'fixtures', + 'modules', + 'inifile', + 'lib') +) +require 'puppet/type/aodh_api_paste_ini' +describe 'Puppet::Type.type(:aodh_api_paste_ini)' do + before :each do + @aodh_api_paste_ini = Puppet::Type.type(:aodh_api_paste_ini).new(:name => 'DEFAULT/foo', :value => 'bar') + end + it 'should accept a valid value' do + @aodh_api_paste_ini[:value] = 'bar' + expect(@aodh_api_paste_ini[:value]).to eq('bar') + end + + it 'should autorequire the package that install the file' do + catalog = Puppet::Resource::Catalog.new + package = Puppet::Type.type(:package).new(:name => 'aodh-common') + catalog.add_resource package, @aodh_api_paste_ini + dependency = @aodh_api_paste_ini.autorequire + expect(dependency.size).to eq(1) + expect(dependency[0].target).to eq(@aodh_api_paste_ini) + expect(dependency[0].source).to eq(package) + end + +end