From 9130db3a86d15ae165b7d6dee7334ce366dd0e4a Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Fri, 3 Jan 2020 17:15:18 -0800 Subject: [PATCH] Porting functions to the modern Puppet 4.x API --- .../functions/cobblersystems_to_unbound.rb | 72 +++++++++++++++++++ .../_cobblersystems_to_unbound_spec.rb | 41 +++++++++++ 2 files changed, 113 insertions(+) create mode 100644 lib/puppet/functions/cobblersystems_to_unbound.rb create mode 100644 spec/functions/_cobblersystems_to_unbound_spec.rb diff --git a/lib/puppet/functions/cobblersystems_to_unbound.rb b/lib/puppet/functions/cobblersystems_to_unbound.rb new file mode 100644 index 0000000..cef8acc --- /dev/null +++ b/lib/puppet/functions/cobblersystems_to_unbound.rb @@ -0,0 +1,72 @@ +# This is an autogenerated function, ported from the original legacy version. +# It /should work/ as is, but will not have all the benefits of the modern +# function API. You should see the function docs to learn how to add function +# signatures for type safety and to document this function using puppet-strings. +# +# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html +# +# ---- original file header ---- +# Copyright 2014 Brainsware +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# +# cobblersystems to unbound transformation +# + +# takes an Hash of cobblersystems and transforms them to a Hash of unbound record hashes + +# ---- original file header ---- +# +# @summary +# takes an Hash of cobblersystems and transforms them to a Hash of unbound record hashes +# +Puppet::Functions.create_function(:'cobblersystems_to_unbound') do + # @param arguments + # The original array of arguments. Port this to individually managed params + # to get the full benefit of the modern function API. + # + # @return [Data type] + # Describe what the function returns here + # + dispatch :default_impl do + # Call the method named 'default_impl' when this is matched + # Port this to match individual params for better type safety + repeated_param 'Any', :arguments + end + + + def default_impl(*arguments) + + + input = arguments[0] + output = {} + + get_ip = lambda { |data, key| data['interfaces']['eth0'][key] } + + raise(TypeError, "cobblersystems_to_unbound(): first argument must be a Hash. " + + "Given an argument of class #{input.class}.") unless input.is_a? Hash + + input.each do |host, data| + hostname = data['hostname'] || host + + output[hostname] = { + 'ip' => get_ip[data, 'ip_address'], + 'ipv6' => get_ip[data, 'ipv6_address'] + } + end + + output + + end +end diff --git a/spec/functions/_cobblersystems_to_unbound_spec.rb b/spec/functions/_cobblersystems_to_unbound_spec.rb new file mode 100644 index 0000000..20d66f3 --- /dev/null +++ b/spec/functions/_cobblersystems_to_unbound_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'cobblersystems_to_unbound' do + # without knowing details about the implementation, this is the only test + # case that we can autogenerate. You should add more examples below! + it { is_expected.not_to eq(nil) } + +################################# +# Below are some example test cases. You may uncomment and modify them to match +# your needs. Notice that they all expect the base error class of `StandardError`. +# This is because the autogenerated function uses an untyped array for parameters +# and relies on your implementation to do the validation. As you convert your +# function to proper dispatches and typed signatures, you should change the +# expected error of the argument validation examples to `ArgumentError`. +# +# Other error types you might encounter include +# +# * StandardError +# * ArgumentError +# * Puppet::ParseError +# +# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/ +# +# it 'raises an error if called with no argument' do +# is_expected.to run.with_params.and_raise_error(StandardError) +# end +# +# it 'raises an error if there is more than 1 arguments' do +# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError) +# end +# +# it 'raises an error if argument is not the proper type' do +# is_expected.to run.with_params('foo').and_raise_error(StandardError) +# end +# +# it 'returns the proper output' do +# is_expected.to run.with_params(123).and_return('the expected output') +# end +################################# + +end