diff --git a/lib/puppet/provider/tempest_flavor_id_setter/openstack.rb b/lib/puppet/provider/tempest_flavor_id_setter/openstack.rb new file mode 100644 index 00000000..ab01e33f --- /dev/null +++ b/lib/puppet/provider/tempest_flavor_id_setter/openstack.rb @@ -0,0 +1,88 @@ +require File.join(File.dirname(__FILE__), '..','..','..', 'puppet/provider/tempest') + +Puppet::Type.type(:tempest_flavor_id_setter).provide( + :openstack, + :parent => Puppet::Provider::Tempest +) do + + @credentials = Puppet::Provider::Openstack::CredentialsV3.new + + def exists? + lines.find do |line| + should_line.chomp == line.chomp + end + end + + def file_path + resource[:tempest_conf_path] + end + + def create + handle_create_with_match + end + + def destroy + handle_create_with_match + end + + def get_flavor_id + if resource[:ensure] == :present or resource[:ensure].nil? + if @flavor_id.nil? + flavors = self.class.request('flavor', 'list', file_path) + flavor = flavors.detect {|flavor| flavor[:name] == resource[:flavor_name]} + if flavor.nil? + raise(Puppet::Error, "Flavor #{resource[:flavor_name]} not found!") + end + @flavor_id = flavor[:id] + end + elsif resource[:ensure] != :absent + raise(Puppet::Error, "Cannot ensure to #{resource[:ensure]}") + end + @flavor_id + end + + def should_line + "#{resource[:name]} = #{get_flavor_id}" + end + + def match + /^\s*#{resource[:name]}\s*=\s*/ + end + + def handle_create_with_match() + regex = match + match_count = lines.select { |l| regex.match(l) }.count + + file = lines + case match_count + when 1 + File.open(file_path, 'w') do |fh| + lines.each do |l| + fh.puts(regex.match(l) ? "#{should_line}" : l) + end + end + when 0 + block_pos = lines.find_index { |l| /^\[compute\]/ =~ l } + if block_pos.nil? + file += ["[compute]\n", "#{should_line}\n"] + else + file.insert(block_pos+1, "#{should_line}\n") + end + File.write(file_path, file.join) + else # cannot be negative. + raise Puppet::Error, "More than one line in file \ +'#{file_path}' matches pattern '#{regex}'" + end + end + + private + def lines + # If this type is ever used with very large files, we should + # write this in a different way, using a temp + # file; for now assuming that this type is only used on + # small-ish config files that can fit into memory without + # too much trouble. + @lines ||= File.readlines(file_path) + end + +end diff --git a/lib/puppet/type/tempest_flavor_id_setter.rb b/lib/puppet/type/tempest_flavor_id_setter.rb new file mode 100644 index 00000000..8ff00fe7 --- /dev/null +++ b/lib/puppet/type/tempest_flavor_id_setter.rb @@ -0,0 +1,30 @@ +Puppet::Type.newtype(:tempest_flavor_id_setter) do +# +# tempest_flavor_id_setter { 'flavor_id': +# tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', +# flavor_name => $name, +# } +# + + ensurable + + newparam(:name, :namevar => true) do + desc 'name of the setting to update' + end + + newparam(:tempest_conf_path) do + desc 'path to tempest conf file' + end + + newparam(:flavor_name) do + desc 'name of nova flavor' + end + + autorequire(:nova_flavor) do + [self[:flavor_name]] if self[:flavor_name] + end + + autorequire(:package) do + ['python-openstackclient', 'python3-openstackclient'] + end +end diff --git a/manifests/init.pp b/manifests/init.pp index 2dc5aebc..1086e513 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -98,6 +98,10 @@ # Defaults to undef # [*run_ssh*] # Defaults to false +# [*flavor_name*] +# Defaults to undef +# [*flavor_name_alt*] +# Defaults to undef # [*flavor_ref*] # Defaults to undef # [*flavor_ref_alt*] @@ -211,6 +215,8 @@ # [*db_flavor_ref*] # Valid primary flavor to use in Trove tests. # Defaults to $::os_service_default +# [*db_flavor_name*] +# Defaults to undef # [*baremetal_driver*] # Defaults to 'fake' # [*baremetal_enabled_hardware_types*] @@ -310,6 +316,8 @@ class tempest( $image_alt_ssh_user = undef, $flavor_ref = undef, $flavor_ref_alt = undef, + $flavor_name = undef, + $flavor_name_alt = undef, $compute_build_interval = undef, $run_ssh = false, # whitebox @@ -326,6 +334,7 @@ class tempest( $sahara_plugins = undef, # Trove config $db_flavor_ref = $::os_service_default, + $db_flavor_name = undef, # Service configuration $cinder_available = true, $cinder_backup_available = false, @@ -778,6 +787,42 @@ class tempest( } + if ! $flavor_ref and $flavor_name { + tempest_flavor_id_setter { 'flavor_ref': + ensure => present, + tempest_conf_path => $tempest_conf, + flavor_name => $flavor_name, + } + Tempest_config<||> -> Tempest_flavor_id_setter['flavor_ref'] + Keystone_user_role<||> -> Tempest_flavor_id_setter['flavor_ref'] + } elsif ($flavor_name and $flavor_ref) { + fail('flavor_ref and flavor_name are both set: please set only one of them') + } + + if ! $flavor_ref_alt and $flavor_name_alt { + tempest_flavor_id_setter { 'flavor_ref_alt': + ensure => present, + tempest_conf_path => $tempest_conf, + flavor_name => $flavor_name_alt, + } + Tempest_config<||> -> Tempest_flavor_id_setter['flavor_ref_alt'] + Keystone_user_role<||> -> Tempest_flavor_id_setter['flavor_ref_alt'] + } elsif ($flavor_name_alt and $flavor_ref_alt) { + fail('flavor_ref_alt and flavor_name_alt are both set: please set only one of them') + } + + if is_service_default($db_flavor_ref) and $db_flavor_name { + tempest_flavor_id_setter { 'db_flavor_ref': + ensure => present, + tempest_conf_path => $tempest_conf, + flavor_name => $db_flavor_name, + } + Tempest_config<||> -> Tempest_flavor_id_setter['db_flavor_ref'] + Keystone_user_role<||> -> Tempest_flavor_id_setter['db_flavor_ref'] + } elsif ($db_flavor_name and ! is_service_default($db_flavor_ref)) { + fail('db_flavor_ref and db_flavor_name are both set: please set only one of them') + } + if $configure_images { if ! $image_ref and $image_name { # If the image id was not provided, look it up via the image name diff --git a/releasenotes/notes/flavor-id-setter-aac1c1ea5bb11149.yaml b/releasenotes/notes/flavor-id-setter-aac1c1ea5bb11149.yaml new file mode 100644 index 00000000..e464e2fd --- /dev/null +++ b/releasenotes/notes/flavor-id-setter-aac1c1ea5bb11149.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + A new puppet provider makes it possible to set the ``flavor_ref``, + ``flavor_ref_alt`` and ``db_flavor_ref`` using the flavor name as + parameter instead of the ID. Use ``flavor_name``, ``flavor_name_alt`` + and ``db_flavor_name`` to do so. diff --git a/spec/classes/tempest_init_spec.rb b/spec/classes/tempest_init_spec.rb index a600ab9b..743b4c40 100644 --- a/spec/classes/tempest_init_spec.rb +++ b/spec/classes/tempest_init_spec.rb @@ -380,6 +380,35 @@ describe 'tempest' do ) end end + + context 'with flavor_name parameters' do + let :params do + { + :configure_images => false, + :flavor_name => 'm1.tiny', + :flavor_name_alt => 'm1.nano', + :db_flavor_name => 'm1.micro', + } + end + + it "sets flavor id using setter" do + is_expected.to contain_tempest_flavor_id_setter('flavor_ref').with( + :ensure => 'present', + :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', + :flavor_name => 'm1.tiny', + ) + is_expected.to contain_tempest_flavor_id_setter('flavor_ref_alt').with( + :ensure => 'present', + :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', + :flavor_name => 'm1.nano', + ) + is_expected.to contain_tempest_flavor_id_setter('db_flavor_ref').with( + :ensure => 'present', + :tempest_conf_path => '/var/lib/tempest/etc/tempest.conf', + :flavor_name => 'm1.micro', + ) + end + end end shared_examples 'tempest with plugins packages' do