Update upload_cirros to support multiple images

This change creates a puppet parser function to convert the
test_vm_image from hiera into a glance_image compatible array of hashes
that can be used with create_resources. Previously vmware deployments
included an extra TestVM to be uploaded but during the rewrite of the
upload_cirros task this ability was dropped.

Change-Id: I3f0f6da32134a4a15f2c4764c5334bb8e85309c8
Closes-Bug: #1635144
(cherry picked from commit 53f6229f63)
This commit is contained in:
Alex Schultz 2016-07-19 11:07:33 -06:00 committed by Alexander Arzhanov
parent 5560ee8b0b
commit 632109b5ad
3 changed files with 119 additions and 10 deletions

View File

@ -0,0 +1,32 @@
module Puppet::Parser::Functions
newfunction(
:generate_glance_images,
:type => :rvalue,
:arity => 1,
:doc => <<-EOS
Takes an array of glance images (in form used in astute.yaml) as argument.
Returns a hash compatible with the glance_image type provided by the glance
module.
EOS
) do |args|
images = args[0]
raise Puppet::ParseError, "generate_glance_images(): Requires an array to work with" unless images.is_a? Array
result = {}
images.each do |image|
raise Puppet::ParseError, "generate_glance_images(): Requires an array of hashes" unless image.is_a? Hash
params = {
'container_format' => image['container_format'],
'disk_format' => image['disk_format'],
'is_public' => image['public'],
'min_ram' => image['min_ram'],
'source' => image['img_path'],
'properties' => image['properties'],
}
result.store image['img_name'], params
end
result
end
end

View File

@ -10,19 +10,15 @@ class osnailyfacter::astute::upload_cirros {
notice('MODULAR: astute/upload_cirros.pp')
$test_vm_image = hiera_hash('test_vm_image')
$test_vm_images = hiera('test_vm_image')
$glance_images = generate_glance_images(flatten([$test_vm_images]))
$defaults = {
'ensure' => 'present',
}
include ::osnailyfacter::wait_for_glance_backends
glance_image { $test_vm_image['img_name']:
ensure => present,
container_format => $test_vm_image['container_format'],
disk_format => $test_vm_image['disk_format'],
is_public => $test_vm_image['public'],
min_ram => $test_vm_image['min_ram'],
source => $test_vm_image['img_path'],
properties => $test_vm_image['properties'],
}
create_resources(glance_image, $glance_images, $defaults)
Class['osnailyfacter::wait_for_glance_backends'] -> Glance_image<||>
}

View File

@ -0,0 +1,81 @@
require 'spec_helper'
describe 'generate_glance_images' do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
let(:subject) {
Puppet::Parser::Functions.function(:generate_glance_images)
}
let(:input) {
[
{
'container_format' => 'bare',
'disk_format' => 'vmdk',
'glance_properties' => '--property hypervisor_type=vmware --property vmware_disktype=sparse --property vmware_adaptertype=lsiLogic',
'img_name' => 'TestVM-VMDK',
'img_path' => '/usr/share/cirros-testvm/cirros-i386-disk.vmdk',
'min_ram' => '64',
'os_name' => 'cirros',
'properties' => {
'hypervisor_type' => 'vmware',
'vmware_adaptertype' => 'lsiLogic',
'vmware_disktype' => 'sparse',
},
'public' => 'true',
},
{
'container_format' => 'bare',
'disk_format' => 'qcow2',
'glance_properties' => '',
'img_name' => 'TestVM',
'img_path' => '/usr/share/cirros-testvm/cirros-x86_64-disk.img',
'min_ram' => '64',
'os_name' => 'cirros',
'properties' => {},
'public' => 'true',
},
]
}
let (:output) {
{
'TestVM-VMDK' => {
'container_format' => 'bare',
'disk_format' => 'vmdk',
'is_public' => 'true',
'min_ram' => '64',
'source' => '/usr/share/cirros-testvm/cirros-i386-disk.vmdk',
'properties' => {
'hypervisor_type' => 'vmware',
'vmware_adaptertype' => 'lsiLogic',
'vmware_disktype' => 'sparse',
},
},
'TestVM' => {
'container_format' => 'bare',
'disk_format' => 'qcow2',
'is_public' => 'true',
'min_ram' => '64',
'source' => '/usr/share/cirros-testvm/cirros-x86_64-disk.img',
'properties' => {},
},
}
}
it 'should exist' do
expect(subject).to eq 'function_generate_glance_images'
end
it 'should expect 1 argument' do
expect { scope.function_generate_glance_images([]) }.to raise_error(ArgumentError)
end
it 'should expect array as given argument' do
expect { scope.function_generate_glance_images(['foobar']) }.to raise_error(Puppet::ParseError)
end
it 'should return glance compatible hash' do
expect(scope.function_generate_glance_images([input])).to eq(output)
end
end