From 3577f4b86d557aed37eaabce0d88d4dc07eac9ac Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Sat, 4 Jan 2020 12:50:35 -0800 Subject: [PATCH] Porting functions to the modern Puppet 4.x API --- lib/puppet/functions/guess_apache_version.rb | 68 ++++++++++++++++++++ lib/puppet/functions/htpasswd_sha1.rb | 37 +++++++++++ spec/functions/_guess_apache_version_spec.rb | 41 ++++++++++++ spec/functions/_htpasswd_sha1_spec.rb | 41 ++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 lib/puppet/functions/guess_apache_version.rb create mode 100644 lib/puppet/functions/htpasswd_sha1.rb create mode 100644 spec/functions/_guess_apache_version_spec.rb create mode 100644 spec/functions/_htpasswd_sha1_spec.rb diff --git a/lib/puppet/functions/guess_apache_version.rb b/lib/puppet/functions/guess_apache_version.rb new file mode 100644 index 0000000..36e9e11 --- /dev/null +++ b/lib/puppet/functions/guess_apache_version.rb @@ -0,0 +1,68 @@ +# 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 ---- +# Try to guess the version of apache to be installed. +# Certain apache modules depend on each other, so we +# need to evaluate the apache version before it gets +# installed. This function decides which apache version +# is going to be installed based on the `operatingsystemrelease` +# fact. +# ---- original file header ---- +# +# @summary +# Summarise what the function does here +# +Puppet::Functions.create_function(:'guess_apache_version') do + # @param args + # 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', :args + end + + + def default_impl(*args) + + release = lookupvar('operatingsystemrelease') + unknown = 'unknown' + + case lookupvar('operatingsystem') + + when 'Debian' + case release + when /^7.*/ + version = '2.2' + when /^8.*|^9.*/ + version = '2.4' + else + version = unknown + end + + when 'Ubuntu' + case release + when /(12.04|12.10|13.04|13.10)/ + version = '2.2' + when /(14.04|14.10|15.04|15.10|16.04)/ + version = '2.4' + else + version = unknown + end + + else + version = unknown + end + version + + end +end diff --git a/lib/puppet/functions/htpasswd_sha1.rb b/lib/puppet/functions/htpasswd_sha1.rb new file mode 100644 index 0000000..2d5f210 --- /dev/null +++ b/lib/puppet/functions/htpasswd_sha1.rb @@ -0,0 +1,37 @@ +# 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 ---- +require 'digest/sha1' +require 'base64' + +# ---- original file header ---- +# +# @summary +# Summarise what the function does here +# +Puppet::Functions.create_function(:'htpasswd_sha1') do + # @param args + # 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', :args + end + + + def default_impl(*args) + + "{SHA}" + Base64.encode64(Digest::SHA1.digest(args[0])) + + end +end diff --git a/spec/functions/_guess_apache_version_spec.rb b/spec/functions/_guess_apache_version_spec.rb new file mode 100644 index 0000000..d08d354 --- /dev/null +++ b/spec/functions/_guess_apache_version_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'guess_apache_version' 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 diff --git a/spec/functions/_htpasswd_sha1_spec.rb b/spec/functions/_htpasswd_sha1_spec.rb new file mode 100644 index 0000000..0f5fe31 --- /dev/null +++ b/spec/functions/_htpasswd_sha1_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'htpasswd_sha1' 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