From 52904772452f0e570b3816d7dedba5ded477e045 Mon Sep 17 00:00:00 2001 From: Yury Bushmelev Date: Fri, 29 Sep 2023 22:01:58 +0800 Subject: [PATCH] Use EPP template in nginx::resource::map --- REFERENCE.md | 14 +++++++++++++- manifests/resource/map.pp | 11 +++++++++-- templates/conf.d/map.epp | 31 +++++++++++++++++++++++++++++++ templates/conf.d/map.erb | 25 ------------------------- types/stringmappings.pp | 6 ++++++ 5 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 templates/conf.d/map.epp delete mode 100644 templates/conf.d/map.erb create mode 100644 types/stringmappings.pp diff --git a/REFERENCE.md b/REFERENCE.md index 1564eb84a..ee9405b72 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -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 the `map` variable mapping * [`Nginx::Time`](#Nginx--Time) * [`Nginx::UpstreamCustomParameters`](#Nginx--UpstreamCustomParameters) * [`Nginx::UpstreamDefaults`](#Nginx--UpstreamDefaults) @@ -3170,10 +3171,12 @@ Source string or variable to provide mapping for ##### `mappings` -Data type: `Variant[Array, Hash]` +Data type: `Nginx::StringMappings` Hash of map lookup keys and resultant values +Default value: `[]` + ##### `hostnames` Data type: `Boolean` @@ -5189,6 +5192,15 @@ The Nginx::Size data type. Alias of `Variant[Integer[0], Pattern[/\A\d+[k|K|m|M]?\z/]]` +### `Nginx::StringMappings` + +custom type for the `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]]` + ### `Nginx::Time` The Nginx::Time data type. diff --git a/manifests/resource/map.pp b/manifests/resource/map.pp index 929ab4628..0fee18eba 100644 --- a/manifests/resource/map.pp +++ b/manifests/resource/map.pp @@ -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 = [], @@ -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', } diff --git a/templates/conf.d/map.epp b/templates/conf.d/map.epp new file mode 100644 index 000000000..612bc5eed --- /dev/null +++ b/templates/conf.d/map.epp @@ -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']) %>; +<%- } -%> +} diff --git a/templates/conf.d/map.erb b/templates/conf.d/map.erb deleted file mode 100644 index 07213d9fa..000000000 --- a/templates/conf.d/map.erb +++ /dev/null @@ -1,25 +0,0 @@ -# MANAGED BY PUPPET -map <%= @string %> $<%= @name %> { -<% if @hostnames -%> - hostnames; -<% end -%> -<% if @default -%> - default <%= @default %>; -<% end -%> -<%- @include_files.each do |h| -%> - include <%= h %>; -<%- end -%> - -<% if @mappings.is_a?(Hash) -%> - <%- field_width = @mappings.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%> - <%- @mappings.sort_by{|k,v| k}.each do |key,value| -%> - <%= sprintf("%-*s", field_width, key) %> <%= value %>; - <%- end -%> -<% end -%> -<% if @mappings.is_a?(Array) -%> - <%- field_width = @mappings.inject(0) { |l,h| h['key'].size > l ? h['key'].size : l } -%> - <%- @mappings.each do |h| -%> - <%= sprintf("%-*s", field_width, h['key']) %> <%= h['value'] %>; - <%- end -%> -<% end -%> -} diff --git a/types/stringmappings.pp b/types/stringmappings.pp new file mode 100644 index 000000000..f0b0ec85d --- /dev/null +++ b/types/stringmappings.pp @@ -0,0 +1,6 @@ +# @summary custom type for the `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] +]