From 4a576293c1e8edc73008b3a344445aa97ac66dad Mon Sep 17 00:00:00 2001 From: Alex Schultz Date: Thu, 25 Oct 2018 14:16:34 -0600 Subject: [PATCH] Update parser functions to 4.x api This change updates additional parser functions we have to use teh puppet 4.x function api. This includes some basic unit tests to ensure they continue to function as expected. Change-Id: Iebeb82b2890216bed139219441718fffc4004391 Related-Bug: #1799786 --- .../docker_volumes_to_storage_maps.rb | 22 ++++------- .../{parser => }/functions/extract_id.rb | 11 ++++-- .../{parser => }/functions/list_to_hash.rb | 18 ++++----- .../functions/list_to_zookeeper_hash.rb | 12 +++--- .../{parser => }/functions/noop_resource.rb | 14 ++++--- lib/puppet/functions/tripleo_swift_devices.rb | 27 +++++++++++++ .../parser/functions/tripleo_swift_devices.rb | 39 ------------------- .../docker_volumes_to_storage_maps_spec.rb | 19 +++++++++ spec/functions/extract_id_spec.rb | 6 +++ spec/functions/list_to_hash_spec.rb | 11 ++++++ spec/functions/list_to_zookeeper_hash_spec.rb | 15 +++++++ spec/functions/noop_resource_spec.rb | 16 ++++++++ spec/functions/tripleo_swift_devices_spec.rb | 13 +++++++ 13 files changed, 144 insertions(+), 79 deletions(-) rename lib/puppet/{parser => }/functions/docker_volumes_to_storage_maps.rb (59%) rename lib/puppet/{parser => }/functions/extract_id.rb (60%) rename lib/puppet/{parser => }/functions/list_to_hash.rb (68%) rename lib/puppet/{parser => }/functions/list_to_zookeeper_hash.rb (68%) rename lib/puppet/{parser => }/functions/noop_resource.rb (77%) create mode 100644 lib/puppet/functions/tripleo_swift_devices.rb delete mode 100644 lib/puppet/parser/functions/tripleo_swift_devices.rb create mode 100644 spec/functions/docker_volumes_to_storage_maps_spec.rb create mode 100644 spec/functions/extract_id_spec.rb create mode 100644 spec/functions/list_to_hash_spec.rb create mode 100644 spec/functions/list_to_zookeeper_hash_spec.rb create mode 100644 spec/functions/noop_resource_spec.rb create mode 100644 spec/functions/tripleo_swift_devices_spec.rb diff --git a/lib/puppet/parser/functions/docker_volumes_to_storage_maps.rb b/lib/puppet/functions/docker_volumes_to_storage_maps.rb similarity index 59% rename from lib/puppet/parser/functions/docker_volumes_to_storage_maps.rb rename to lib/puppet/functions/docker_volumes_to_storage_maps.rb index 1325640c8..306357cfe 100644 --- a/lib/puppet/parser/functions/docker_volumes_to_storage_maps.rb +++ b/lib/puppet/functions/docker_volumes_to_storage_maps.rb @@ -18,22 +18,14 @@ # "options" => "ro", # } # } -module Puppet::Parser::Functions - newfunction(:docker_volumes_to_storage_maps, :arity => 2, :type => :rvalue, - :doc => <<-EOS - This function converts an array of docker volumes (SOURCE:TARGET[:OPTIONS]) - to a pacemaker::resource::bundle storage_map (a hash). - EOS - ) do |argv| - docker_volumes = argv[0] - prefix = argv[1] +Puppet::Functions.create_function(:'docker_volumes_to_storage_maps') do + dispatch :docker_volumes_to_storage_maps do + param 'Array', :docker_volumes + param 'String', :prefix + return_type 'Hash' + end - unless docker_volumes.is_a?(Array) - raise Puppet::ParseError, "docker_volumes_to_storage_maps: Argument 'docker_volumes' must be an array. The value given was: #{docker_volumes}" - end - unless prefix.is_a?(String) - raise Puppet::ParseError, "docker_volumes_to_storage_maps: Argument 'prefix' must be an string. The value given was: #{prefix}" - end + def docker_volumes_to_storage_maps(docker_volumes, prefix) storage_maps = Hash.new docker_volumes.each do |docker_vol| source, target, options = docker_vol.split(":") diff --git a/lib/puppet/parser/functions/extract_id.rb b/lib/puppet/functions/extract_id.rb similarity index 60% rename from lib/puppet/parser/functions/extract_id.rb rename to lib/puppet/functions/extract_id.rb index 61734abfa..e52d8961c 100644 --- a/lib/puppet/parser/functions/extract_id.rb +++ b/lib/puppet/functions/extract_id.rb @@ -1,13 +1,16 @@ # Custom function to extract the index from a list. # The list are a list of hostname, and the index is the n'th # position of the host in list -module Puppet::Parser::Functions - newfunction(:extract_id, :type => :rvalue) do |argv| - hosts = argv[0] +Puppet::Functions.create_function(:extract_id) do + dispatch :extract_id do + param 'Variant[Array, String]', :hosts + param 'String', :hostname + end + + def extract_id(hosts, hostname) if hosts.class != Array hosts = [hosts] end - hostname = argv[1] hash = Hash[hosts.map.with_index.to_a] return hash[hostname].to_i + 1 end diff --git a/lib/puppet/parser/functions/list_to_hash.rb b/lib/puppet/functions/list_to_hash.rb similarity index 68% rename from lib/puppet/parser/functions/list_to_hash.rb rename to lib/puppet/functions/list_to_hash.rb index c6449a9f7..b0d456c99 100644 --- a/lib/puppet/parser/functions/list_to_hash.rb +++ b/lib/puppet/functions/list_to_hash.rb @@ -18,14 +18,14 @@ # Puppet parser. # -module Puppet::Parser::Functions - newfunction(:list_to_hash, :type => :rvalue, :doc => <<-EOS - This function returns an hash from a specified array - EOS - ) do |argv| - arr1 = argv[0] - arr2 = argv[1] - h = arr1.each_with_object({}) { |v,h| h[v] = arr2 } - return h +Puppet::Functions.create_function(:list_to_hash) do + dispatch :list_to_hash do + param 'Array', :arr1 + param 'Array', :arr2 + end + + def list_to_hash(arr1, arr2) + hh = arr1.each_with_object({}) { |v,h| h[v] = arr2 } + return hh end end diff --git a/lib/puppet/parser/functions/list_to_zookeeper_hash.rb b/lib/puppet/functions/list_to_zookeeper_hash.rb similarity index 68% rename from lib/puppet/parser/functions/list_to_zookeeper_hash.rb rename to lib/puppet/functions/list_to_zookeeper_hash.rb index 814326e84..f8d123215 100644 --- a/lib/puppet/parser/functions/list_to_zookeeper_hash.rb +++ b/lib/puppet/functions/list_to_zookeeper_hash.rb @@ -3,12 +3,12 @@ # because a not-so-good design of the puppet-midonet module # and we hope to deprecate it soon. -module Puppet::Parser::Functions - newfunction(:list_to_zookeeper_hash, :type => :rvalue, :doc => <<-EOS - This function returns Zookeper configuration list of hash - EOS - ) do |argv| - zk_list = argv[0] +Puppet::Functions.create_function(:list_to_zookeeper_hash) do + dispatch :list_to_zookeeper_hash do + param 'Variant[Array, String]', :zk_list + end + + def list_to_zookeeper_hash(zk_list) if zk_list.class != Array zk_list = [zk_list] end diff --git a/lib/puppet/parser/functions/noop_resource.rb b/lib/puppet/functions/noop_resource.rb similarity index 77% rename from lib/puppet/parser/functions/noop_resource.rb rename to lib/puppet/functions/noop_resource.rb index 02db63340..a80b40a0b 100644 --- a/lib/puppet/parser/functions/noop_resource.rb +++ b/lib/puppet/functions/noop_resource.rb @@ -40,12 +40,14 @@ class Puppet::Provider::Noop < Puppet::Provider end -module Puppet::Parser::Functions - newfunction(:noop_resource, :type => :rvalue, :doc => "Create a default noop provider for the specified resource.") do |arg| - if arg[0].class == String - Puppet::Type.type(arg[0].downcase.to_sym).provide(:noop, :parent => Puppet::Provider::Noop) do - defaultfor :osfamily => :redhat - end +Puppet::Functions.create_function(:noop_resource) do + dispatch :noop_resource do + param 'String', :res + end + + def noop_resource(res) + Puppet::Type.type(res.downcase.to_sym).provide(:noop, :parent => Puppet::Provider::Noop) do + defaultfor :osfamily => :redhat end return true end diff --git a/lib/puppet/functions/tripleo_swift_devices.rb b/lib/puppet/functions/tripleo_swift_devices.rb new file mode 100644 index 000000000..8b2db7784 --- /dev/null +++ b/lib/puppet/functions/tripleo_swift_devices.rb @@ -0,0 +1,27 @@ +# Build Swift devices list from the parts, e.g. for: +# raw_disk_prefix = 'r1z1-' +# swift_storage_node_ips = ['192.168.1.12', '192.168.1.13'] +# raw_disks = [':%PORT%/device1', ':%PORT%/device2'] +# +# devices will be ['r1z1-192.168.1.12:%PORT%/device1', +# 'r1z1-192.168.1.12:%PORT%/device2' +# 'r1z1-192.168.1.13:%PORT%/device1' +# 'r1z1-192.168.1.13:%PORT%/device2'] +Puppet::Functions.create_function(:tripleo_swift_devices) do + dispatch :tripleo_swift_devices do + param 'String', :raw_disk_prefix + param 'Array', :swift_node_ips + param 'Array', :raw_disks + end + + def tripleo_swift_devices(raw_disk_prefix, swift_node_ips, raw_disks) + devices = [] + for ip in swift_node_ips do + for disk in raw_disks do + devices << "#{raw_disk_prefix}#{ip}#{disk}" + end + end + + return devices + end +end diff --git a/lib/puppet/parser/functions/tripleo_swift_devices.rb b/lib/puppet/parser/functions/tripleo_swift_devices.rb deleted file mode 100644 index b320d6296..000000000 --- a/lib/puppet/parser/functions/tripleo_swift_devices.rb +++ /dev/null @@ -1,39 +0,0 @@ -# Build Swift devices list from the parts, e.g. for: -# raw_disk_prefix = 'r1z1-' -# swift_storage_node_ips = ['192.168.1.12', '192.168.1.13'] -# raw_disks = [':%PORT%/device1', ':%PORT%/device2'] -# -# devices will be ['r1z1-192.168.1.12:%PORT%/device1', -# 'r1z1-192.168.1.12:%PORT%/device2' -# 'r1z1-192.168.1.13:%PORT%/device1' -# 'r1z1-192.168.1.13:%PORT%/device2'] -module Puppet::Parser::Functions - newfunction(:tripleo_swift_devices, :arity =>3, :type => :rvalue, - :doc => ("Build list of swift devices the TripleO way:" + - "from a raw disk prefix, a list of swift storage" + - "node IPs, and a list of raw disks.")) do |args| - - raw_disk_prefix = args[0] - swift_node_ips = args[1] - raw_disks = args[2] - - unless raw_disk_prefix.is_a?(String) - raise Puppet::ParseError, "tripleo_swift_devices: Argument 'raw_disk_prefix' must be a string. The value given was: #{raw_disk_prefix}" - end - unless swift_node_ips.is_a?(Array) - raise Puppet::ParseError, "tripleo_swift_devices: Argument 'swift_node_ips' must be an array. The value given was: #{swift_node_ips}" - end - unless raw_disks.is_a?(Array) - raise Puppet::ParseError, "tripleo_swift_devices: Argument 'raw_disks' must be an array. The value given was: #{raw_disks}" - end - - devices = [] - for ip in swift_node_ips do - for disk in raw_disks do - devices << "#{raw_disk_prefix}#{ip}#{disk}" - end - end - - return devices - end -end diff --git a/spec/functions/docker_volumes_to_storage_maps_spec.rb b/spec/functions/docker_volumes_to_storage_maps_spec.rb new file mode 100644 index 000000000..1a1a02e5a --- /dev/null +++ b/spec/functions/docker_volumes_to_storage_maps_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe 'docker_volumes_to_storage_maps' do + it { + should run.with_params(["/src/vol1:/tgt/vol1", "/src/vol2:/tgt/vol2:ro"], "my-prefix") + .and_return({ + "my-prefix-src-vol1" => { + "source-dir" => "/src/vol1", + "target-dir" => "/tgt/vol1", + "options" => "rw", + }, + "my-prefix-src-vol2" => { + "source-dir" => "/src/vol2", + "target-dir" => "/tgt/vol2", + "options" => "ro", + } + }) + } +end diff --git a/spec/functions/extract_id_spec.rb b/spec/functions/extract_id_spec.rb new file mode 100644 index 000000000..4fb6229ac --- /dev/null +++ b/spec/functions/extract_id_spec.rb @@ -0,0 +1,6 @@ +require 'spec_helper' + +describe 'extract_id' do + it { should run.with_params('127.0.0.1', '127.0.0.1').and_return(1) } + it { should run.with_params(["127.0.0.1", "127.0.0.2"], "127.0.0.2").and_return(2) } +end diff --git a/spec/functions/list_to_hash_spec.rb b/spec/functions/list_to_hash_spec.rb new file mode 100644 index 000000000..4225006ab --- /dev/null +++ b/spec/functions/list_to_hash_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe 'list_to_hash' do + it { + should run.with_params(['192.168.0.1:5000', '192.168.0.2:5000'], ['transparent']) + .and_return({ + '192.168.0.1:5000' => ['transparent'], + '192.168.0.2:5000' => ['transparent'], + }) + } +end diff --git a/spec/functions/list_to_zookeeper_hash_spec.rb b/spec/functions/list_to_zookeeper_hash_spec.rb new file mode 100644 index 000000000..15a7d9123 --- /dev/null +++ b/spec/functions/list_to_zookeeper_hash_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe 'list_to_zookeeper_hash' do + it { + should run.with_params('127.0.0.1').and_return([ + { 'ip' => '127.0.0.1', 'port' => 2181 } + ]) + } + it { + should run.with_params(['127.0.0.1', '127.0.0.2']).and_return([ + { 'ip' => '127.0.0.1', 'port' => 2181 }, + { 'ip' => '127.0.0.2', 'port' => 2181 } + ]) + } +end diff --git a/spec/functions/noop_resource_spec.rb b/spec/functions/noop_resource_spec.rb new file mode 100644 index 000000000..9bee175e5 --- /dev/null +++ b/spec/functions/noop_resource_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe 'noop_resource' do + it { + should run.with_params('nova_config').and_return(true) + } + context 'noop a puppet resource' do + let (:pre_condition) { + 'noop_resource("file") + file { "bar": path => "/baz" }' + } + it { + expect(-> {catalogue}).to contain_file('bar') + } + end +end diff --git a/spec/functions/tripleo_swift_devices_spec.rb b/spec/functions/tripleo_swift_devices_spec.rb new file mode 100644 index 000000000..7fb7762e6 --- /dev/null +++ b/spec/functions/tripleo_swift_devices_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe 'tripleo_swift_devices' do + it { + should run.with_params('r1z1-', ['192.168.1.12', '192.168.1.13'], [':%PORT%/device1', ':%PORT%/device2']) + .and_return([ + 'r1z1-192.168.1.12:%PORT%/device1', + 'r1z1-192.168.1.12:%PORT%/device2', + 'r1z1-192.168.1.13:%PORT%/device1', + 'r1z1-192.168.1.13:%PORT%/device2', + ]) + } +end