From ea24090227b76429b1dbd2ef578151f2d098d6a6 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 17 Oct 2023 15:06:51 -0400 Subject: [PATCH 1/7] Add `merge_defaults` option to `defaults` config section --- manifests/defaults.pp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/manifests/defaults.pp b/manifests/defaults.pp index 636d33fc..77ed4d76 100644 --- a/manifests/defaults.pp +++ b/manifests/defaults.pp @@ -16,13 +16,20 @@ # Sort options either alphabetic or custom like haproxy internal sorts them. # Defaults to true. # +# @param merge_options +# Whether to merge the user-supplied `options` hash with the +# `default_options` values set in params.pp. Merging allows to change +# or add options without having to recreate the entire hash. Defaults to +# false, but will default to true in future releases. +# # @param instance # Optional. Defaults to 'haproxy'. # define haproxy::defaults ( - Hash $options = {}, - Boolean $sort_options_alphabetic = true, - String $instance = 'haproxy', + Optional[Hash] $options = {}, + Boolean $sort_options_alphabetic = true, + Boolean $merge_options = $haproxy::params::merge_options, + String $instance = 'haproxy', ) { if $instance == 'haproxy' { include haproxy @@ -36,9 +43,17 @@ include haproxy::globals $_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::globals::sort_options_alphabetic) + $defaults_options = pick($options, $haproxy::params::defaults_options) + if $merge_options { + $_defaults_options = $haproxy::params::defaults_options + $defaults_options + } else { + $_defaults_options = $defaults_options + warning("${module_name}: The \$merge_options parameter will default to true in the next major release. Please review the documentation regarding the implications.") # lint:ignore:140chars + } + $parameters = { '_sort_options_alphabetic' => $_sort_options_alphabetic, - 'options' => $options, + 'options' => $_defaults_options, 'name' => $name, } From 0bd3487bda4f85a58046e0ed0a04fe58d14ec01e Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 17 Oct 2023 15:20:38 -0400 Subject: [PATCH 2/7] Add tests for defaults merge --- spec/defines/defaults_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/defines/defaults_spec.rb b/spec/defines/defaults_spec.rb index 302b8e41..1144f4d3 100644 --- a/spec/defines/defaults_spec.rb +++ b/spec/defines/defaults_spec.rb @@ -37,4 +37,38 @@ ) } end + + context 'with merge defaults true' do + let(:params) do + { + options: { 'balance' => 'roundrobin' }, + merge_options: true + } + end + + defaults_output = <<~EXPECTEDOUTPUT + + + defaults test + balance roundrobin + log global + maxconn 8000 + option redispatch + retries 3 + stats enable + timeout http-request 10s + timeout queue 1m + timeout connect 10s + timeout client 1m + timeout server 1m + timeout check 10s + EXPECTEDOUTPUT + it { + is_expected.to contain_concat__fragment('haproxy-test_defaults_block').with( + 'order' => '25-test', + 'target' => '/tmp/haproxy.cfg', + 'content' => defaults_output, + ) + } + end end From 5c8a7b29ff01e7bde56286d0c5cc4135906fcbaf Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 17 Oct 2023 15:27:07 -0400 Subject: [PATCH 3/7] Can't use optional without breaking backwards compatibility --- manifests/defaults.pp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/manifests/defaults.pp b/manifests/defaults.pp index 77ed4d76..a6468678 100644 --- a/manifests/defaults.pp +++ b/manifests/defaults.pp @@ -26,10 +26,10 @@ # Optional. Defaults to 'haproxy'. # define haproxy::defaults ( - Optional[Hash] $options = {}, - Boolean $sort_options_alphabetic = true, - Boolean $merge_options = $haproxy::params::merge_options, - String $instance = 'haproxy', + Hash $options = {}, + Boolean $sort_options_alphabetic = true, + Boolean $merge_options = $haproxy::params::merge_options, + String $instance = 'haproxy', ) { if $instance == 'haproxy' { include haproxy From cc366b65716c824cdc194b4e6f87e8007e22b1ed Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 17 Oct 2023 18:14:00 -0400 Subject: [PATCH 4/7] Add documentation --- REFERENCE.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/REFERENCE.md b/REFERENCE.md index 9e53411f..8bb3aa2a 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -714,6 +714,7 @@ The following parameters are available in the `haproxy::defaults` defined type: * [`options`](#-haproxy--defaults--options) * [`sort_options_alphabetic`](#-haproxy--defaults--sort_options_alphabetic) +* [`merge_options`](#-haproxy--defaults--merge_options) * [`instance`](#-haproxy--defaults--instance) ##### `options` @@ -733,6 +734,17 @@ Defaults to true. Default value: `true` +##### `merge_options` + +Data type: `Boolean` + +Whether to merge the user-supplied `options` hash with the +`default_options` values set in params.pp. Merging allows to change +or add options without having to recreate the entire hash. Defaults to +false, but will default to true in future releases. + +Default value: `$haproxy::params::merge_options` + ##### `instance` Data type: `String` From 1d87dafdd8c218f3d392739ef23c23b82e4e50d5 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 20 Nov 2023 14:02:11 -0500 Subject: [PATCH 5/7] merge_options now defaults to true, so don't warn anymore --- manifests/defaults.pp | 1 - 1 file changed, 1 deletion(-) diff --git a/manifests/defaults.pp b/manifests/defaults.pp index a6468678..1a5899c2 100644 --- a/manifests/defaults.pp +++ b/manifests/defaults.pp @@ -48,7 +48,6 @@ $_defaults_options = $haproxy::params::defaults_options + $defaults_options } else { $_defaults_options = $defaults_options - warning("${module_name}: The \$merge_options parameter will default to true in the next major release. Please review the documentation regarding the implications.") # lint:ignore:140chars } $parameters = { From 5b522f55169875312e7ef8aa227d920321c29158 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 20 Nov 2023 14:19:25 -0500 Subject: [PATCH 6/7] Fix tests broken by #592 --- spec/defines/defaults_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/defines/defaults_spec.rb b/spec/defines/defaults_spec.rb index 1144f4d3..47ed6e44 100644 --- a/spec/defines/defaults_spec.rb +++ b/spec/defines/defaults_spec.rb @@ -25,7 +25,8 @@ context 'with a single option' do let(:params) do { - options: { 'balance' => 'roundrobin' } + options: { 'balance' => 'roundrobin' }, + merge_options: false } end @@ -41,8 +42,7 @@ context 'with merge defaults true' do let(:params) do { - options: { 'balance' => 'roundrobin' }, - merge_options: true + options: { 'balance' => 'roundrobin' } } end From faeffea542cc9d64ee7058418905c7f3f7749a62 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 20 Nov 2023 14:36:22 -0500 Subject: [PATCH 7/7] Update documentation --- REFERENCE.md | 67 +++++++++++++++++++++++++++++++++++++++++-- manifests/defaults.pp | 3 +- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index 8bb3aa2a..3aada6ab 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -49,6 +49,12 @@ file on an haproxy load balancer. * `haproxy::mailer::collect_exported` * `haproxy::service`: HAProxy service +### Functions + +* [`haproxy::generate_error_message`](#haproxy--generate_error_message): Function created to generate error message. Any string as error message can be passed and the function can be called in epp templates. +* [`haproxy::sort_bind`](#haproxy--sort_bind) +* [`haproxy::validate_ip_addr`](#haproxy--validate_ip_addr) + ## Classes ### `haproxy` @@ -740,8 +746,7 @@ Data type: `Boolean` Whether to merge the user-supplied `options` hash with the `default_options` values set in params.pp. Merging allows to change -or add options without having to recreate the entire hash. Defaults to -false, but will default to true in future releases. +or add options without having to recreate the entire hash. Default value: `$haproxy::params::merge_options` @@ -2022,3 +2027,61 @@ Optional. Defaults to 'haproxy' Default value: `'haproxy'` +## Functions + +### `haproxy::generate_error_message` + +Type: Ruby 4.x API + +Function created to generate error message. Any string as error message can be passed and the function can +be called in epp templates. + +#### `haproxy::generate_error_message(String $error_message)` + +Function created to generate error message. Any string as error message can be passed and the function can +be called in epp templates. + +Returns: `Any` + +##### `error_message` + +Data type: `String` + + + +### `haproxy::sort_bind` + +Type: Ruby 4.x API + +The haproxy::sort_bind function. + +#### `haproxy::sort_bind(Hash $bind)` + +The haproxy::sort_bind function. + +Returns: `Array` + +##### `bind` + +Data type: `Hash` + + + +### `haproxy::validate_ip_addr` + +Type: Ruby 4.x API + +The haproxy::validate_ip_addr function. + +#### `haproxy::validate_ip_addr(String $virtual_ip)` + +The haproxy::validate_ip_addr function. + +Returns: `Boolean` + +##### `virtual_ip` + +Data type: `String` + + + diff --git a/manifests/defaults.pp b/manifests/defaults.pp index 1a5899c2..a3bb838c 100644 --- a/manifests/defaults.pp +++ b/manifests/defaults.pp @@ -19,8 +19,7 @@ # @param merge_options # Whether to merge the user-supplied `options` hash with the # `default_options` values set in params.pp. Merging allows to change -# or add options without having to recreate the entire hash. Defaults to -# false, but will default to true in future releases. +# or add options without having to recreate the entire hash. # # @param instance # Optional. Defaults to 'haproxy'.