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
This commit is contained in:
Alex Schultz 2018-10-25 14:16:34 -06:00
parent 8d889af7d4
commit 4a576293c1
13 changed files with 144 additions and 79 deletions

View File

@ -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(":")

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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