From ff6ffb2ffdb556c0ea32f324bd5e4e57b7fe7ed6 Mon Sep 17 00:00:00 2001 From: Ben Ford Date: Sat, 4 Jan 2020 13:12:28 -0800 Subject: [PATCH] Porting functions to the modern Puppet 4.x API --- .../neutron/validate_network_vlan_ranges.rb | 76 +++++++++++++++++++ .../neutron/validate_tunnel_id_ranges.rb | 73 ++++++++++++++++++ .../functions/neutron/validate_vni_ranges.rb | 76 +++++++++++++++++++ .../neutron/validate_vxlan_udp_port.rb | 61 +++++++++++++++ ...utron_validate_network_vlan_ranges_spec.rb | 41 ++++++++++ .../neutron_validate_tunnel_id_ranges_spec.rb | 41 ++++++++++ .../neutron_validate_vni_ranges_spec.rb | 41 ++++++++++ .../neutron_validate_vxlan_udp_port_spec.rb | 41 ++++++++++ 8 files changed, 450 insertions(+) create mode 100644 lib/puppet/functions/neutron/validate_network_vlan_ranges.rb create mode 100644 lib/puppet/functions/neutron/validate_tunnel_id_ranges.rb create mode 100644 lib/puppet/functions/neutron/validate_vni_ranges.rb create mode 100644 lib/puppet/functions/neutron/validate_vxlan_udp_port.rb create mode 100644 spec/functions/neutron_validate_network_vlan_ranges_spec.rb create mode 100644 spec/functions/neutron_validate_tunnel_id_ranges_spec.rb create mode 100644 spec/functions/neutron_validate_vni_ranges_spec.rb create mode 100644 spec/functions/neutron_validate_vxlan_udp_port_spec.rb diff --git a/lib/puppet/functions/neutron/validate_network_vlan_ranges.rb b/lib/puppet/functions/neutron/validate_network_vlan_ranges.rb new file mode 100644 index 0000000..0595dd6 --- /dev/null +++ b/lib/puppet/functions/neutron/validate_network_vlan_ranges.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) 2013 eNovance SAS +# +# Author: Emilien Macchi +# Martin Magr +# +# 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. +# +# Advanced validation for VLAN configuration +# + +# ---- original file header ---- +# +# @summary +# Summarise what the function does here +# +Puppet::Functions.create_function(:'neutron::validate_network_vlan_ranges') 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) + + value = args[0] + if not value.kind_of?(Array) + value = [value] + end + + value.each do |range| + if m = /^(.+:)?(\d+):(\d+)$/.match(range) + first_id = Integer(m[-2]) + second_id = Integer(m[-1]) + if (first_id > 4094) || (second_id > 4094) + raise Puppet::Error, "vlan id are invalid." + end + if ((second_id - first_id) < 0 ) + raise Puppet::Error, "network vlan ranges are invalid." + end + elsif m = /^([^:]+)?(:\d+)?$/.match(range) + # Either only name of physical network or single vlan id has + # been passed. This is also correct. + elsif range + raise Puppet::Error, "network vlan ranges are invalid." + end + end + + end +end diff --git a/lib/puppet/functions/neutron/validate_tunnel_id_ranges.rb b/lib/puppet/functions/neutron/validate_tunnel_id_ranges.rb new file mode 100644 index 0000000..4409bbc --- /dev/null +++ b/lib/puppet/functions/neutron/validate_tunnel_id_ranges.rb @@ -0,0 +1,73 @@ +# 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) 2013 eNovance SAS +# +# Author: Emilien Macchi +# Martin Magr +# +# 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. +# +# Advanced validation when using GRE +# + +# ---- original file header ---- +# +# @summary +# Summarise what the function does here +# +Puppet::Functions.create_function(:'neutron::validate_tunnel_id_ranges') 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) + + value = args[0] + if not value.kind_of?(Array) + value = [value] + end + + value.each do |range| + if m = /^(\d+):(\d+)$/.match(range) + first_id = Integer(m[1]) + second_id = Integer(m[2]) + if ((second_id - first_id) > 1000000) + raise Puppet::Error, "tunnel id ranges are to large." + end + if ((second_id - first_id) < 0 ) + raise Puppet::Error, "tunnel id ranges are invalid." + end + elsif range + raise Puppet::Error, "tunnel id ranges are invalid." + end + end + + end +end diff --git a/lib/puppet/functions/neutron/validate_vni_ranges.rb b/lib/puppet/functions/neutron/validate_vni_ranges.rb new file mode 100644 index 0000000..b8220f5 --- /dev/null +++ b/lib/puppet/functions/neutron/validate_vni_ranges.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) 2013 eNovance SAS +# +# Author: Emilien Macchi +# Martin Magr +# +# 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. +# +# Advanced validation when using VXLAN +# + +# ---- original file header ---- +# +# @summary +# Summarise what the function does here +# +Puppet::Functions.create_function(:'neutron::validate_vni_ranges') 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) + + value = args[0] + if not value.kind_of?(Array) + value = [value] + end + + value.each do |range| + if m = /^(\d+):(\d+)$/.match(range) + first_id = Integer(m[1]) + second_id = Integer(m[2]) + if not (0 <= first_id && first_id <= 16777215) + raise Puppet::Error, "vni ranges are invalid." + end + if not (0 <= second_id && second_id <= 16777215) + raise Puppet::Error, "vni ranges are invalid." + end + if (second_id < first_id) + raise Puppet::Error, "vni ranges are invalid." + end + elsif range + raise Puppet::Error, "vni ranges are invalid." + end + end + + end +end diff --git a/lib/puppet/functions/neutron/validate_vxlan_udp_port.rb b/lib/puppet/functions/neutron/validate_vxlan_udp_port.rb new file mode 100644 index 0000000..df0c1ea --- /dev/null +++ b/lib/puppet/functions/neutron/validate_vxlan_udp_port.rb @@ -0,0 +1,61 @@ +# 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) 2013 eNovance SAS +# +# Author: Emilien Macchi +# Martin Magr +# +# 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. +# +# Advanced validation for VXLAN UDP port configuration +# + +# ---- original file header ---- +# +# @summary +# Summarise what the function does here +# +Puppet::Functions.create_function(:'neutron::validate_vxlan_udp_port') 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) + + value = Integer(args[0]) + + # check if port is either default value or one of the private ports + # according to http://tools.ietf.org/html/rfc6056 + if value != 4789 or (49151 >= value and value > 65535) + raise Puppet::Error, "vxlan udp port is invalid." + end + + end +end diff --git a/spec/functions/neutron_validate_network_vlan_ranges_spec.rb b/spec/functions/neutron_validate_network_vlan_ranges_spec.rb new file mode 100644 index 0000000..3e2e565 --- /dev/null +++ b/spec/functions/neutron_validate_network_vlan_ranges_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'neutron::validate_network_vlan_ranges' 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/neutron_validate_tunnel_id_ranges_spec.rb b/spec/functions/neutron_validate_tunnel_id_ranges_spec.rb new file mode 100644 index 0000000..4ba40f1 --- /dev/null +++ b/spec/functions/neutron_validate_tunnel_id_ranges_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'neutron::validate_tunnel_id_ranges' 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/neutron_validate_vni_ranges_spec.rb b/spec/functions/neutron_validate_vni_ranges_spec.rb new file mode 100644 index 0000000..86fdc22 --- /dev/null +++ b/spec/functions/neutron_validate_vni_ranges_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'neutron::validate_vni_ranges' 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/neutron_validate_vxlan_udp_port_spec.rb b/spec/functions/neutron_validate_vxlan_udp_port_spec.rb new file mode 100644 index 0000000..66f9f94 --- /dev/null +++ b/spec/functions/neutron_validate_vxlan_udp_port_spec.rb @@ -0,0 +1,41 @@ +require 'spec_helper' + +describe 'neutron::validate_vxlan_udp_port' 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