Skip to content

Commit

Permalink
Merge pull request #588 from yakatz/feature/defaults_mergeconfig
Browse files Browse the repository at this point in the history
Add `merge_options` for `haproxy::defaults`
  • Loading branch information
Ramesh7 authored Nov 21, 2023
2 parents 2aefadd + faeffea commit d01472d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
75 changes: 75 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

### <a name="haproxy"></a>`haproxy`
Expand Down Expand Up @@ -714,6 +720,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)

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

Default value: `true`

##### <a name="-haproxy--defaults--merge_options"></a>`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.

Default value: `$haproxy::params::merge_options`

##### <a name="-haproxy--defaults--instance"></a>`instance`

Data type: `String`
Expand Down Expand Up @@ -2010,3 +2027,61 @@ Optional. Defaults to 'haproxy'

Default value: `'haproxy'`

## Functions

### <a name="haproxy--generate_error_message"></a>`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`



### <a name="haproxy--sort_bind"></a>`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`



### <a name="haproxy--validate_ip_addr"></a>`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`



15 changes: 14 additions & 1 deletion manifests/defaults.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
# 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.
#
# @param instance
# Optional. Defaults to 'haproxy'.
#
define haproxy::defaults (
Hash $options = {},
Boolean $sort_options_alphabetic = true,
Boolean $merge_options = $haproxy::params::merge_options,
String $instance = 'haproxy',
) {
if $instance == 'haproxy' {
Expand All @@ -36,9 +42,16 @@
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
}

$parameters = {
'_sort_options_alphabetic' => $_sort_options_alphabetic,
'options' => $options,
'options' => $_defaults_options,
'name' => $name,
}

Expand Down
36 changes: 35 additions & 1 deletion spec/defines/defaults_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
context 'with a single option' do
let(:params) do
{
options: { 'balance' => 'roundrobin' }
options: { 'balance' => 'roundrobin' },
merge_options: false
}
end

Expand All @@ -37,4 +38,37 @@
)
}
end

context 'with merge defaults true' do
let(:params) do
{
options: { 'balance' => 'roundrobin' }
}
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

0 comments on commit d01472d

Please sign in to comment.