Skip to content

Commit f5230c3

Browse files
authored
Merge pull request #1673 from bwitt/geo-network-rework
Reverse geo `networks` param hash
2 parents 71f3d26 + b7f2491 commit f5230c3

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed

REFERENCE.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,28 +1778,30 @@ nginx::resource::geo { 'client_network':
17781778
proxy_recursive => false,
17791779
proxies => [ '192.168.99.99' ],
17801780
networks => {
1781-
'10.0.0.0/8' => 'intra',
1782-
'172.16.0.0/12' => 'intra',
1783-
'192.168.0.0/16' => 'intra',
1781+
'intra' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'],
17841782
}
17851783
}
17861784
```
17871785

17881786
##### Hiera usage
17891787

17901788
```puppet
1789+
# Define network lists that can be reused
1790+
my_internal_networks: &internal_nets
1791+
- '10.0.0.0/8'
1792+
- '172.16.0.0/12'
1793+
- '192.168.0.0/16'
1794+
17911795
nginx::geo_mappings:
17921796
client_network:
17931797
ensure: present
17941798
ranges: false
17951799
default: 'extra'
17961800
proxy_recursive: false
17971801
proxies:
1798-
- 192.168.99.99
1802+
- 192.168.99.99
17991803
networks:
1800-
'10.0.0.0/8': 'intra'
1801-
'172.16.0.0/12': 'intra'
1802-
'192.168.0.0/16': 'intra'
1804+
intra: *internal_nets
18031805
```
18041806

18051807
#### Parameters
@@ -1817,9 +1819,9 @@ The following parameters are available in the `nginx::resource::geo` defined typ
18171819

18181820
##### <a name="-nginx--resource--geo--networks"></a>`networks`
18191821

1820-
Data type: `Hash`
1822+
Data type: `Hash[String[1], Array[String[1]]]`
18211823

1822-
Hash of geo lookup keys and resultant values
1824+
Hash where keys are geo result values and values are network CIDR arrays.
18231825

18241826
##### <a name="-nginx--resource--geo--default"></a>`default`
18251827

manifests/resource/geo.pp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# @summary Create a new geo mapping entry for NGINX
22
#
33
# @param networks
4-
# Hash of geo lookup keys and resultant values
4+
# Hash where keys are geo result values and values are network CIDR arrays.
55
#
66
# @param default
77
# Sets the resulting value if the source value fails to match any of the
@@ -35,27 +35,29 @@
3535
# proxy_recursive => false,
3636
# proxies => [ '192.168.99.99' ],
3737
# networks => {
38-
# '10.0.0.0/8' => 'intra',
39-
# '172.16.0.0/12' => 'intra',
40-
# '192.168.0.0/16' => 'intra',
38+
# 'intra' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'],
4139
# }
4240
# }
4341
#
4442
# @example Hiera usage
43+
# # Define network lists that can be reused
44+
# my_internal_networks: &internal_nets
45+
# - '10.0.0.0/8'
46+
# - '172.16.0.0/12'
47+
# - '192.168.0.0/16'
48+
#
4549
# nginx::geo_mappings:
4650
# client_network:
4751
# ensure: present
4852
# ranges: false
4953
# default: 'extra'
5054
# proxy_recursive: false
5155
# proxies:
52-
# - 192.168.99.99
56+
# - 192.168.99.99
5357
# networks:
54-
# '10.0.0.0/8': 'intra'
55-
# '172.16.0.0/12': 'intra'
56-
# '192.168.0.0/16': 'intra'
58+
# intra: *internal_nets
5759
define nginx::resource::geo (
58-
Hash $networks,
60+
Hash[String[1], Array[String[1]]] $networks,
5961
Optional[String] $default = undef,
6062
Enum['present', 'absent'] $ensure = 'present',
6163
Boolean $ranges = false,

spec/defines/resource_geo_spec.rb

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
{
2323
default: 'extra',
2424
networks: {
25-
'172.16.0.0/12' => 'intra',
26-
'192.168.0.0/16' => 'intra',
27-
'10.0.0.0/8' => 'intra',
25+
'intra' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'],
2826
},
2927
proxies: ['1.2.3.4', '4.3.2.1'],
3028
}
@@ -71,9 +69,7 @@
7169
title: 'should contain ordered network directives',
7270
attr: 'networks',
7371
value: {
74-
'192.168.0.0/16' => 'intra',
75-
'172.16.0.0/12' => 'intra',
76-
'10.0.0.0/8' => 'intra',
72+
'intra' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'],
7773
},
7874
match: [
7975
' 10.0.0.0/8 intra;',
@@ -129,6 +125,59 @@
129125
it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_ensure('absent') }
130126
end
131127
end
128+
129+
describe 'networks parameter with multiple values' do
130+
context 'with multiple geo values' do
131+
let :params do
132+
{
133+
default: 'extra',
134+
networks: {
135+
'intra' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16'],
136+
'external' => ['8.8.8.0/24'],
137+
},
138+
proxies: ['1.2.3.4'],
139+
}
140+
end
141+
142+
it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_mode('0644') }
143+
144+
it 'contains network directives for all values' do
145+
is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_content(%r{10\.0\.0\.0/8\s+intra;})
146+
is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_content(%r{172\.16\.0\.0/12\s+intra;})
147+
is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_content(%r{192\.168\.0\.0/16\s+intra;})
148+
is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_content(%r{8\.8\.8\.0/24\s+external;})
149+
end
150+
end
151+
152+
context 'with empty networks hash' do
153+
let :params do
154+
{
155+
networks: {},
156+
}
157+
end
158+
159+
it { is_expected.to compile.with_all_deps }
160+
it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf") }
161+
end
162+
163+
context 'networks are sorted by IP address' do
164+
let :params do
165+
{
166+
networks: {
167+
'external' => ['8.8.8.0/24'],
168+
'intra' => ['10.0.0.0/8', '192.168.0.0/16'],
169+
},
170+
}
171+
end
172+
173+
it 'outputs networks in ascending IP order' do
174+
# 8.8.8.0 < 10.0.0.0 < 192.168.0.0 numerically
175+
is_expected.to contain_file("/etc/nginx/conf.d/#{title}-geo.conf").with_content(
176+
%r{8\.8\.8\.0/24\s+external;.*10\.0\.0\.0/8\s+intra;.*192\.168\.0\.0/16\s+intra;}m,
177+
)
178+
end
179+
end
180+
end
132181
end
133182
end
134183
end

templates/conf.d/geo.erb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ geo <%= @address ? "#{@address} " : '' %>$<%= @name %> {
2525
<% end -%>
2626
<% if @networks -%>
2727

28-
<%- field_width = @networks.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%>
29-
<%- @networks.sort_by{|k,v| IPAddr.new(k.split('-').first).to_i }.each do |key,value| -%>
28+
<%-
29+
# Expand { 'value' => ['net1', 'net2'] } to { 'net1' => 'value', 'net2' => 'value' }
30+
expanded = {}
31+
@networks.each { |value, nets| nets.each { |net| expanded[net] = value } }
32+
field_width = expanded.inject(0) { |l,(k,v)| k.size > l ? k.size : l }
33+
-%>
34+
<%- expanded.sort_by{|k,v| IPAddr.new(k.split('-').first).to_i }.each do |key,value| -%>
3035
<%= sprintf("%-*s", field_width, key) %> <%= value %>;
3136
<%- end -%>
3237
<% end -%>

0 commit comments

Comments
 (0)