diff --git a/lib/puppet/functions/bind/bind_check_hostname.rb b/lib/puppet/functions/bind/bind_check_hostname.rb new file mode 100644 index 0000000..6b204f4 --- /dev/null +++ b/lib/puppet/functions/bind/bind_check_hostname.rb @@ -0,0 +1,67 @@ +# 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 ---- +# +# bind_check_hostname.rb +# +# ---- original file header ---- +# +# @summary +# Prepare checked string for *is_domain_name()* (from stdlib) by removing /^*.?/ +#if present. *is_domain_name()* doesn't want any wildcard, which makes sense in +#most cases. +#Usage: bind_check_hostname(hostname, type) +# +# +Puppet::Functions.create_function(:'bind::bind_check_hostname') 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) + + + if (arguments.size != 2) then + raise(Puppet::ParseError, "bind_check_hostname(): Wrong number of arguments "+ + "given #{arguments.size} for 2") + end + + record = arguments[0] + type = arguments[1] + + # Allows '@' + return true if record == '@' + + # All is allowed for SRV and TXT record types + return true if type == 'SRV' + return true if type == 'TXT' + + # Allow wildcard only at the begining + # As we're calling stdlib's *is_domain_name()* + # which doesn't accept wildcards, we just clean it + # from the record, and pass this new string to + # *is_domain_name()* + domain = record.sub(/^\*\.?/, '') + + # Nothing left to check, and is_domain_name fails empty + return true if domain == '' + + return function_is_domain_name([domain]) + + end +end diff --git a/spec/functions/bind_bind_check_hostname_spec.rb b/spec/functions/bind_bind_check_hostname_spec.rb new file mode 100644 index 0000000..856cc23 --- /dev/null +++ b/spec/functions/bind_bind_check_hostname_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'bind::bind_check_hostname' 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