Skip to content

Commit

Permalink
Use EPP template in nginx::resource::map
Browse files Browse the repository at this point in the history
  • Loading branch information
jay7x committed Sep 29, 2023
1 parent da6b41c commit 127332d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 28 deletions.
14 changes: 13 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* [`Nginx::GzipProxied`](#Nginx--GzipProxied): custom type for gzip_proxied
* [`Nginx::LogFormat`](#Nginx--LogFormat)
* [`Nginx::Size`](#Nginx--Size)
* [`Nginx::StringMappings`](#Nginx--StringMappings): custom type for `map` variable mapping
* [`Nginx::Time`](#Nginx--Time)
* [`Nginx::UpstreamCustomParameters`](#Nginx--UpstreamCustomParameters)
* [`Nginx::UpstreamDefaults`](#Nginx--UpstreamDefaults)
Expand Down Expand Up @@ -3170,10 +3171,12 @@ Source string or variable to provide mapping for

##### <a name="-nginx--resource--map--mappings"></a>`mappings`

Data type: `Variant[Array, Hash]`
Data type: `Nginx::StringMappings`

Hash of map lookup keys and resultant values

Default value: `[]`

##### <a name="-nginx--resource--map--hostnames"></a>`hostnames`

Data type: `Boolean`
Expand Down Expand Up @@ -5189,6 +5192,15 @@ The Nginx::Size data type.

Alias of `Variant[Integer[0], Pattern[/\A\d+[k|K|m|M]?\z/]]`

### <a name="Nginx--StringMappings"></a>`Nginx::StringMappings`

custom type for `map` variable mapping

* **See also**
* http://nginx.org/en/docs/http/ngx_http_map_module.html

Alias of `Variant[Array[Struct[{ 'key' => String[1], 'value' => String }]], Hash[String[1], String]]`

### <a name="Nginx--Time"></a>`Nginx::Time`

The Nginx::Time data type.
Expand Down
11 changes: 9 additions & 2 deletions manifests/resource/map.pp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
#
define nginx::resource::map (
String[2] $string,
Variant[Array, Hash] $mappings,
Nginx::StringMappings $mappings = [],
Optional[String] $default = undef,
Enum['absent', 'present'] $ensure = 'present',
Array[String] $include_files = [],
Expand Down Expand Up @@ -93,7 +93,14 @@
owner => 'root',
group => $root_group,
mode => $nginx::global_mode,
content => template('nginx/conf.d/map.erb'),
content => epp('nginx/conf.d/map.epp', {
'default' => $default,
'hostnames' => $hostnames,
'include_files' => $include_files,
'mappings' => $mappings,
'name' => $name,
'string' => $string,
}),
notify => Class['nginx::service'],
tag => 'nginx_config_file',
}
Expand Down
31 changes: 31 additions & 0 deletions templates/conf.d/map.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%- |
Optional[String] $default = undef,
Boolean $hostnames,
Array[String] $include_files,
Nginx::StringMappings $mappings,
String $name,
String[2] $string,
| -%>
# MANAGED BY PUPPET
map <%= $string %> $<%= $name %> {
<% if $hostnames { -%>
hostnames;
<% } -%>
<% if $default { -%>
default <%= $default %>;
<% } -%>
<%- $include_files.each |$h| { -%>
include <%= $h %>;
<%- } -%>

<%-
$m = $mappings ? {
Hash => $mappings.keys.sort.map |$k| { { key => $k, value => $mappings[$k] } },
default => $mappings,
}
$field_width = $m.map |$x| { $x['key'].length }.max
-%>
<%- $m.each |$h| { -%>
<%= sprintf("%-*s %s", $field_width, $h['key'], $h['value']) %>;
<%- } -%>
}
25 changes: 0 additions & 25 deletions templates/conf.d/map.erb

This file was deleted.

6 changes: 6 additions & 0 deletions types/stringmappings.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# @summary custom type for `map` variable mapping
# @see http://nginx.org/en/docs/http/ngx_http_map_module.html
type Nginx::StringMappings = Variant[
Array[Struct[{ 'key' => String[1], 'value' => String }]],
Hash[String[1], String]
]

0 comments on commit 127332d

Please sign in to comment.