diff --git a/lib/puppet/provider/glance_swift_config/ini_setting.rb b/lib/puppet/provider/glance_swift_config/ini_setting.rb new file mode 100644 index 00000000..9d2d7ece --- /dev/null +++ b/lib/puppet/provider/glance_swift_config/ini_setting.rb @@ -0,0 +1,10 @@ +Puppet::Type.type(:glance_swift_config).provide( + :ini_setting, + :parent => Puppet::Type.type(:openstack_config).provider(:ini_setting) +) do + + def self.file_path + '/etc/glance/glance-swift.conf' + end + +end diff --git a/lib/puppet/type/glance_swift_config.rb b/lib/puppet/type/glance_swift_config.rb new file mode 100644 index 00000000..592b22f7 --- /dev/null +++ b/lib/puppet/type/glance_swift_config.rb @@ -0,0 +1,57 @@ +Puppet::Type.newtype(:glance_swift_config) do + + ensurable + + newparam(:name, :namevar => true) do + desc 'Section/setting name to manage from glance-api.conf' + 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 + newvalues(/^[\S ]*$/) + + 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 + if Facter.value(:osfamily) == 'Debian' + 'glance-api' + elsif Facter.value(:osfamily) == 'RedHat' + 'openstack-glance' + end + end + +end diff --git a/manifests/backend/swift.pp b/manifests/backend/swift.pp index 3a1adda4..72750be7 100644 --- a/manifests/backend/swift.pp +++ b/manifests/backend/swift.pp @@ -62,8 +62,11 @@ class glance::backend::swift( 'glance_store/swift_store_endpoint_type': value => $swift_store_endpoint_type; - 'glance_store/swift_store_config_file': value => '/etc/glance/glance-api.conf'; + 'glance_store/swift_store_config_file': value => '/etc/glance/glance-swift.conf'; 'glance_store/default_swift_reference': value => $default_swift_reference; + } + + glance_swift_config { "${default_swift_reference}/user": value => $swift_store_user; "${default_swift_reference}/key": value => $swift_store_key; "${default_swift_reference}/auth_address": value => $swift_store_auth_address; diff --git a/spec/classes/glance_backend_swift_spec.rb b/spec/classes/glance_backend_swift_spec.rb index 7ca3fa32..d4531b30 100644 --- a/spec/classes/glance_backend_swift_spec.rb +++ b/spec/classes/glance_backend_swift_spec.rb @@ -27,12 +27,12 @@ describe 'glance::backend::swift' do is_expected.to contain_glance_api_config('glance_store/swift_store_create_container_on_put').with_value(false) is_expected.to contain_glance_api_config('glance_store/swift_store_endpoint_type').with_value('internalURL') is_expected.to contain_glance_api_config('glance_store/swift_store_region').with_value(nil) - is_expected.to contain_glance_api_config('glance_store/swift_store_config_file').with_value('/etc/glance/glance-api.conf') + is_expected.to contain_glance_api_config('glance_store/swift_store_config_file').with_value('/etc/glance/glance-swift.conf') is_expected.to contain_glance_api_config('glance_store/default_swift_reference').with_value('ref1') - is_expected.to contain_glance_api_config('ref1/key').with_value('key') - is_expected.to contain_glance_api_config('ref1/user').with_value('user') - is_expected.to contain_glance_api_config('ref1/auth_version').with_value('2') - is_expected.to contain_glance_api_config('ref1/auth_address').with_value('127.0.0.1:5000/v2.0/') + is_expected.to contain_glance_swift_config('ref1/key').with_value('key') + is_expected.to contain_glance_swift_config('ref1/user').with_value('user') + is_expected.to contain_glance_swift_config('ref1/auth_version').with_value('2') + is_expected.to contain_glance_swift_config('ref1/auth_address').with_value('127.0.0.1:5000/v2.0/') end end @@ -60,10 +60,10 @@ describe 'glance::backend::swift' do is_expected.to contain_glance_api_config('glance_store/swift_store_endpoint_type').with_value('publicURL') is_expected.to contain_glance_api_config('glance_store/swift_store_region').with_value('RegionTwo') is_expected.to contain_glance_api_config('glance_store/default_swift_reference').with_value('swift_creds') - is_expected.to contain_glance_api_config('swift_creds/key').with_value('key2') - is_expected.to contain_glance_api_config('swift_creds/user').with_value('user2') - is_expected.to contain_glance_api_config('swift_creds/auth_version').with_value('1') - is_expected.to contain_glance_api_config('swift_creds/auth_address').with_value('127.0.0.2:8080/v1.0/') + is_expected.to contain_glance_swift_config('swift_creds/key').with_value('key2') + is_expected.to contain_glance_swift_config('swift_creds/user').with_value('user2') + is_expected.to contain_glance_swift_config('swift_creds/auth_version').with_value('1') + is_expected.to contain_glance_swift_config('swift_creds/auth_address').with_value('127.0.0.2:8080/v1.0/') end end diff --git a/spec/unit/provider/glance_swift_config/ini_setting_spec.rb b/spec/unit/provider/glance_swift_config/ini_setting_spec.rb new file mode 100644 index 00000000..90cd0ceb --- /dev/null +++ b/spec/unit/provider/glance_swift_config/ini_setting_spec.rb @@ -0,0 +1,60 @@ +$LOAD_PATH.push( + File.join( + File.dirname(__FILE__), + '..', + '..', + '..', + 'fixtures', + 'modules', + 'openstacklib', + 'lib') +) + +require 'spec_helper' + +provider_class = Puppet::Type.type(:glance_swift_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::Glance_swift_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::Glance_swift_config.new( + { + :name => 'dude/foo', + :value => 'bar' + } + ) + provider = provider_class.new(resource) + expect(provider.section).to eq('dude') + expect(provider.setting).to eq('foo') + end + + it 'should ensure absent when is specified as a value' do + resource = Puppet::Type::Glance_swift_config.new( + {:name => 'dude/foo', :value => ''} + ) + 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::Glance_swift_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 diff --git a/spec/unit/type/glance_swift_config_spec.rb b/spec/unit/type/glance_swift_config_spec.rb new file mode 100644 index 00000000..f72b8966 --- /dev/null +++ b/spec/unit/type/glance_swift_config_spec.rb @@ -0,0 +1,41 @@ +require 'puppet' +require 'puppet/type/glance_swift_config' + +describe 'Puppet::Type.type(:glance_swift_config)' do + before :each do + Puppet::Type.rmtype(:glance_swift_config) + Facter.fact(:osfamily).stubs(:value).returns(platform_params[:osfamily]) + @glance_swift_config = Puppet::Type.type(:glance_swift_config).new(:name => 'DEFAULT/foo', :value => 'bar') + end + + shared_examples_for 'glance_swift_config' do + it 'should autorequire the package that install the file' do + catalog = Puppet::Resource::Catalog.new + package = Puppet::Type.type(:package).new(:name => platform_params[:package_name]) + catalog.add_resource package, @glance_swift_config + dependency = @glance_swift_config.autorequire + expect(dependency.size).to eq(1) + expect(dependency[0].target).to eq(@glance_swift_config) + expect(dependency[0].source).to eq(package) + end + end + + context 'on Debian platforms' do + let :platform_params do + { :package_name => 'glance-api', + :osfamily => 'Debian' } + end + + it_behaves_like 'glance_swift_config' + end + + context 'on RedHat platforms' do + let :platform_params do + { :package_name => 'openstack-glance', + :osfamily => 'RedHat'} + end + + it_behaves_like 'glance_swift_config' + end + +end