diff --git a/lib/puppet/functions/varnish/normalize_storage_spec.rb b/lib/puppet/functions/varnish/normalize_storage_spec.rb new file mode 100644 index 0000000..289a6ad --- /dev/null +++ b/lib/puppet/functions/varnish/normalize_storage_spec.rb @@ -0,0 +1,76 @@ +# 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 (c) 2016 Finalsite +# Copyright (c) 2016 Carl P. Corliss +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +# EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +# OR OTHER DEALINGS IN THE SOFTWARE. +# +require File.expand_path('../../../../varnish_helper', __FILE__) + +# ---- original file header ---- +# +# @summary +# Normalizes the storage specification, adjusting sizes to match the size +# format requirements of varnish. +# +# Examples: +# # Assumed facts: +# # memorysize_mb: 10240.0 +# # mountpoint_a_path: /var/lib/varnish +# # mountpoint_a_size: 8589934592 # 8 GB +# +# normalize_storage_spec('malloc,10%') +# # => malloc,1g +# +# normalize_storage_spec('file,/var/lib/varnish,10%') +# # => malloc, +# +# +Puppet::Functions.create_function(:'varnish::normalize_storage_spec') 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) + + + if args.length != 1 + fail Puppet::ParseError.new("normalize_storage_spec(): expected 1 argument but found #{args.length}.") + end + + begin + return Varnish::StorageSpec.new(self, args.first).to_s + rescue Varnish::Error => e + fail Puppet::ParseError, "validate_storage_spec(): #{e.message}" + end + + end +end diff --git a/lib/puppet/functions/varnish/validate_storage_spec.rb b/lib/puppet/functions/varnish/validate_storage_spec.rb new file mode 100644 index 0000000..d96da54 --- /dev/null +++ b/lib/puppet/functions/varnish/validate_storage_spec.rb @@ -0,0 +1,76 @@ +# 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 (c) 2016 Finalsite +# Copyright (c) 2016 Carl P. Corliss +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, +# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all copies or substantial +# portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +# EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN +# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +# OR OTHER DEALINGS IN THE SOFTWARE. +# +require File.expand_path('../../../../varnish_helper', __FILE__) + +# ---- original file header ---- +# +# @summary +# Perform validation of a varnish storage specification, ensuring that it +# it is valid for any of the three possible storage types (malloc, file or persistent) +# +# The following strings will validate: +# +# validate_storage_spec('malloc,1G') +# validate_storage_spec('file,/var/tmp,20G,16k') +# validate_storage_spec('persistent,/var/tmp,20G') +# +# The following strings will fail to validate, causing compilation to abort: +# +# validate_storage_spec('foo,1G') +# validate_storage_spec('file,var/tmp,1G') +# validate_storage_spec('persistent,/var/tmp,1.2G') +# +# +Puppet::Functions.create_function(:'varnish::validate_storage_spec') 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) + + + if args.length != 1 + fail Puppet::ParseError.new("validate_storage_spec(): expected 1 argument but found #{args.length}.") + end + + begin + Varnish::StorageSpec.new(self, args.first) + rescue Varnish::Error => e + fail Puppet::ParseError, "validate_storage_spec(): #{e.message}" + end + + end +end diff --git a/spec/functions/varnish_normalize_storage_spec_spec.rb b/spec/functions/varnish_normalize_storage_spec_spec.rb new file mode 100644 index 0000000..f631d4b --- /dev/null +++ b/spec/functions/varnish_normalize_storage_spec_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'varnish::normalize_storage_spec' 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/varnish_validate_storage_spec_spec.rb b/spec/functions/varnish_validate_storage_spec_spec.rb new file mode 100644 index 0000000..cc312e9 --- /dev/null +++ b/spec/functions/varnish_validate_storage_spec_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'varnish::validate_storage_spec' 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