Skip to content

Commit d01472d

Browse files
authored
Merge pull request #588 from yakatz/feature/defaults_mergeconfig
Add `merge_options` for `haproxy::defaults`
2 parents 2aefadd + faeffea commit d01472d

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

REFERENCE.md

+75
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ file on an haproxy load balancer.
4949
* `haproxy::mailer::collect_exported`
5050
* `haproxy::service`: HAProxy service
5151

52+
### Functions
53+
54+
* [`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.
55+
* [`haproxy::sort_bind`](#haproxy--sort_bind)
56+
* [`haproxy::validate_ip_addr`](#haproxy--validate_ip_addr)
57+
5258
## Classes
5359

5460
### <a name="haproxy"></a>`haproxy`
@@ -714,6 +720,7 @@ The following parameters are available in the `haproxy::defaults` defined type:
714720

715721
* [`options`](#-haproxy--defaults--options)
716722
* [`sort_options_alphabetic`](#-haproxy--defaults--sort_options_alphabetic)
723+
* [`merge_options`](#-haproxy--defaults--merge_options)
717724
* [`instance`](#-haproxy--defaults--instance)
718725

719726
##### <a name="-haproxy--defaults--options"></a>`options`
@@ -733,6 +740,16 @@ Defaults to true.
733740

734741
Default value: `true`
735742

743+
##### <a name="-haproxy--defaults--merge_options"></a>`merge_options`
744+
745+
Data type: `Boolean`
746+
747+
Whether to merge the user-supplied `options` hash with the
748+
`default_options` values set in params.pp. Merging allows to change
749+
or add options without having to recreate the entire hash.
750+
751+
Default value: `$haproxy::params::merge_options`
752+
736753
##### <a name="-haproxy--defaults--instance"></a>`instance`
737754

738755
Data type: `String`
@@ -2010,3 +2027,61 @@ Optional. Defaults to 'haproxy'
20102027

20112028
Default value: `'haproxy'`
20122029

2030+
## Functions
2031+
2032+
### <a name="haproxy--generate_error_message"></a>`haproxy::generate_error_message`
2033+
2034+
Type: Ruby 4.x API
2035+
2036+
Function created to generate error message. Any string as error message can be passed and the function can
2037+
be called in epp templates.
2038+
2039+
#### `haproxy::generate_error_message(String $error_message)`
2040+
2041+
Function created to generate error message. Any string as error message can be passed and the function can
2042+
be called in epp templates.
2043+
2044+
Returns: `Any`
2045+
2046+
##### `error_message`
2047+
2048+
Data type: `String`
2049+
2050+
2051+
2052+
### <a name="haproxy--sort_bind"></a>`haproxy::sort_bind`
2053+
2054+
Type: Ruby 4.x API
2055+
2056+
The haproxy::sort_bind function.
2057+
2058+
#### `haproxy::sort_bind(Hash $bind)`
2059+
2060+
The haproxy::sort_bind function.
2061+
2062+
Returns: `Array`
2063+
2064+
##### `bind`
2065+
2066+
Data type: `Hash`
2067+
2068+
2069+
2070+
### <a name="haproxy--validate_ip_addr"></a>`haproxy::validate_ip_addr`
2071+
2072+
Type: Ruby 4.x API
2073+
2074+
The haproxy::validate_ip_addr function.
2075+
2076+
#### `haproxy::validate_ip_addr(String $virtual_ip)`
2077+
2078+
The haproxy::validate_ip_addr function.
2079+
2080+
Returns: `Boolean`
2081+
2082+
##### `virtual_ip`
2083+
2084+
Data type: `String`
2085+
2086+
2087+

manifests/defaults.pp

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
# Sort options either alphabetic or custom like haproxy internal sorts them.
1717
# Defaults to true.
1818
#
19+
# @param merge_options
20+
# Whether to merge the user-supplied `options` hash with the
21+
# `default_options` values set in params.pp. Merging allows to change
22+
# or add options without having to recreate the entire hash.
23+
#
1924
# @param instance
2025
# Optional. Defaults to 'haproxy'.
2126
#
2227
define haproxy::defaults (
2328
Hash $options = {},
2429
Boolean $sort_options_alphabetic = true,
30+
Boolean $merge_options = $haproxy::params::merge_options,
2531
String $instance = 'haproxy',
2632
) {
2733
if $instance == 'haproxy' {
@@ -36,9 +42,16 @@
3642
include haproxy::globals
3743
$_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::globals::sort_options_alphabetic)
3844

45+
$defaults_options = pick($options, $haproxy::params::defaults_options)
46+
if $merge_options {
47+
$_defaults_options = $haproxy::params::defaults_options + $defaults_options
48+
} else {
49+
$_defaults_options = $defaults_options
50+
}
51+
3952
$parameters = {
4053
'_sort_options_alphabetic' => $_sort_options_alphabetic,
41-
'options' => $options,
54+
'options' => $_defaults_options,
4255
'name' => $name,
4356
}
4457

spec/defines/defaults_spec.rb

+35-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
context 'with a single option' do
2626
let(:params) do
2727
{
28-
options: { 'balance' => 'roundrobin' }
28+
options: { 'balance' => 'roundrobin' },
29+
merge_options: false
2930
}
3031
end
3132

@@ -37,4 +38,37 @@
3738
)
3839
}
3940
end
41+
42+
context 'with merge defaults true' do
43+
let(:params) do
44+
{
45+
options: { 'balance' => 'roundrobin' }
46+
}
47+
end
48+
49+
defaults_output = <<~EXPECTEDOUTPUT
50+
51+
52+
defaults test
53+
balance roundrobin
54+
log global
55+
maxconn 8000
56+
option redispatch
57+
retries 3
58+
stats enable
59+
timeout http-request 10s
60+
timeout queue 1m
61+
timeout connect 10s
62+
timeout client 1m
63+
timeout server 1m
64+
timeout check 10s
65+
EXPECTEDOUTPUT
66+
it {
67+
is_expected.to contain_concat__fragment('haproxy-test_defaults_block').with(
68+
'order' => '25-test',
69+
'target' => '/tmp/haproxy.cfg',
70+
'content' => defaults_output,
71+
)
72+
}
73+
end
4074
end

0 commit comments

Comments
 (0)