From 8115642f4d85cbe11ea470d9a09422c1d1e28e3f Mon Sep 17 00:00:00 2001 From: Chris Hoge Date: Thu, 1 May 2014 14:51:23 -0700 Subject: [PATCH] Vendor any2array function to support stdlib 3.2 Many installations are pinned to stdlib 3.2. This change vendors the any2arry function from stdlib 4 until Puppet Enterprise catches up. At that point the vendored function will be removed. Change-Id: Ide8a9c526ba36a0641c0e118c3280be0e766bd14 --- Modulefile | 2 +- lib/puppet/parser/functions/os_any2array.rb | 34 ++++++++++++ manifests/wsgi/apache.pp | 2 +- .../parser/functions/os_any2array_spec.rb | 55 +++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 lib/puppet/parser/functions/os_any2array.rb create mode 100644 spec/unit/puppet/parser/functions/os_any2array_spec.rb diff --git a/Modulefile b/Modulefile index 2cbcebe2..aecc0c3f 100644 --- a/Modulefile +++ b/Modulefile @@ -8,5 +8,5 @@ description 'Puppet module to install and configure the OpenStack Horizon dashb project_page 'https://launchpad.net/puppet-horizon' dependency 'puppetlabs/apache', '>= 1.0.0 <2.0.0' -dependency 'puppetlabs/stdlib', '>= 4.0.0' +dependency 'puppetlabs/stdlib', '>= 3.2.0' dependency 'saz/memcached', '>= 2.0.2 <3.0.0' diff --git a/lib/puppet/parser/functions/os_any2array.rb b/lib/puppet/parser/functions/os_any2array.rb new file mode 100644 index 00000000..a3877a0b --- /dev/null +++ b/lib/puppet/parser/functions/os_any2array.rb @@ -0,0 +1,34 @@ +# +# os_any2array.rb +# +# TODO: Remove this function when puppetlabs-stdlib 4.0.0 is in wider use + +module Puppet::Parser::Functions + newfunction(:os_any2array, :type => :rvalue, :doc => <<-EOS +This converts any object to an array containing that object. Empty argument +lists are converted to an empty array. Arrays are left untouched. Hashes are +converted to arrays of alternating keys and values. + EOS + ) do |arguments| + + if arguments.empty? + return [] + end + + if arguments.length == 1 + if arguments[0].kind_of?(Array) + return arguments[0] + elsif arguments[0].kind_of?(Hash) + result = [] + arguments[0].each do |key, value| + result << key << value + end + return result + end + end + + return arguments + end +end + +# vim: set ts=2 sw=2 et : diff --git a/manifests/wsgi/apache.pp b/manifests/wsgi/apache.pp index 5aed5368..6a4cf6b7 100644 --- a/manifests/wsgi/apache.pp +++ b/manifests/wsgi/apache.pp @@ -123,7 +123,7 @@ class horizon::wsgi::apache ( $default_vhost_conf = { ip => $bind_address, servername => $servername, - serveraliases => any2array($fqdn), + serveraliases => os_any2array($fqdn), docroot => '/var/www/', access_log_file => 'horizon_access.log', error_log_file => 'horizon_error.log', diff --git a/spec/unit/puppet/parser/functions/os_any2array_spec.rb b/spec/unit/puppet/parser/functions/os_any2array_spec.rb new file mode 100644 index 00000000..dd5bbe2c --- /dev/null +++ b/spec/unit/puppet/parser/functions/os_any2array_spec.rb @@ -0,0 +1,55 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +describe "the os_any2array function" do + let(:scope) { PuppetlabsSpec::PuppetInternals.scope } + + it "should exist" do + Puppet::Parser::Functions.function("os_any2array").should == "function_os_any2array" + end + + it "should return an empty array if there is less than 1 argument" do + result = scope.function_os_any2array([]) + result.should(eq([])) + end + + it "should convert boolean true to [ true ] " do + result = scope.function_os_any2array([true]) + result.should(eq([true])) + end + + it "should convert one object to [object]" do + result = scope.function_os_any2array(['one']) + result.should(eq(['one'])) + end + + it "should convert multiple objects to [objects]" do + result = scope.function_os_any2array(['one', 'two']) + result.should(eq(['one', 'two'])) + end + + it "should return empty array it was called with" do + result = scope.function_os_any2array([[]]) + result.should(eq([])) + end + + it "should return one-member array it was called with" do + result = scope.function_os_any2array([['string']]) + result.should(eq(['string'])) + end + + it "should return multi-member array it was called with" do + result = scope.function_os_any2array([['one', 'two']]) + result.should(eq(['one', 'two'])) + end + + it "should return members of a hash it was called with" do + result = scope.function_os_any2array([{ 'key' => 'value' }]) + result.should(eq(['key', 'value'])) + end + + it "should return an empty array if it was called with an empty hash" do + result = scope.function_os_any2array([{ }]) + result.should(eq([])) + end +end