Skip to content

Commit c6a0f1b

Browse files
committed
Add custom charsets to fqdn_rand_string()
1 parent 52bfccb commit c6a0f1b

5 files changed

Lines changed: 47 additions & 124 deletions

File tree

README.markdown

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,30 +195,17 @@ also appear in the second array. For example, `difference(["a","b","c"],["b","c"
195195
* `floor`: Returns the largest integer less than or equal to the argument.
196196
Takes a single numeric value as an argument. *Type*: rvalue
197197

198-
* `fqdn_rand_base64`: Generates a random base64 string, combining the `$fqdn` fact and an optional seed for repeatable randomness.
198+
* `fqdn_rand_string`: Generates a random string using an optionally-specified character set (default is alphanumeric), combining the `$fqdn` fact and an optional seed for repeatable randomness.
199199

200200
*Usage:*
201201
```
202-
fqdn_rand_base64(LENGTH, [SEED])
203-
```
204-
*Examples:*
205-
```
206-
fqdn_rand_base64(10)
207-
fqdn_rand_base64(10, 'custom seed')
208-
```
209-
210-
*Type*: rvalue
211-
212-
* `fqdn_rand_string`: Generates a random alphanumeric string, combining the `$fqdn` fact and an optional seed for repeatable randomness.
213-
214-
*Usage:*
215-
```
216-
fqdn_rand_string(LENGTH, [SEED])
202+
fqdn_rand_string(LENGTH, [CHARSET], [SEED])
217203
```
218204
*Examples:*
219205
```
220206
fqdn_rand_string(10)
221-
fqdn_rand_string(10, 'custom seed')
207+
fqdn_rand_string(10, 'ABCDEF!@#$%^')
208+
fqdn_rand_string(10, '', 'custom seed')
222209
```
223210

224211
*Type*: rvalue

lib/puppet/parser/functions/fqdn_rand_base64.rb

Lines changed: 0 additions & 30 deletions
This file was deleted.

lib/puppet/parser/functions/fqdn_rand_string.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22
:fqdn_rand_string,
33
:arity => -2,
44
:type => :rvalue,
5-
:doc => "Usage: `fqdn_rand_string(LENGTH, [SEED])`. LENGTH is required and
6-
must be a positive integer; SEED is optional and may be any number or string.
5+
:doc => "Usage: `fqdn_rand_string(LENGTH, [CHARSET], [SEED])`. LENGTH is
6+
required and must be a positive integer. CHARSET is optional and may be
7+
`undef` or a string. SEED is optional and may be any number or string.
78
8-
Generates a random alphanumeric string LENGTH characters long, combining the
9-
`$fqdn` fact and the value of SEED for repeatable randomness. (That is, each
10-
node will get a different random string from this function, but a given
11-
node's result will be the same every time unless its hostname changes.)
12-
Adding a SEED can be useful if you need more than one unrelated string.") do |args|
9+
Generates a random string LENGTH characters long using the character set
10+
provided by CHARSET, combining the `$fqdn` fact and the value of SEED for
11+
repeatable randomness. (That is, each node will get a different random
12+
string from this function, but a given node's result will be the same every
13+
time unless its hostname changes.) Adding a SEED can be useful if you need
14+
more than one unrelated string. CHARSET will default to alphanumeric if
15+
`undef` or an empty string.") do |args|
1316
raise(ArgumentError, "fqdn_rand_string(): wrong number of arguments (0 for 1)") if args.size == 0
1417
Puppet::Parser::Functions.function('is_integer')
1518
raise(ArgumentError, "fqdn_rand_base64(): first argument must be a positive integer") unless function_is_integer([args[0]]) and args[0].to_i > 0
19+
raise(ArgumentError, "fqdn_rand_base64(): second argument must be undef or a string") unless args[1].nil? or args[1].is_a? String
1620

1721
Puppet::Parser::Functions.function('fqdn_rand')
1822

1923
length = args.shift.to_i
24+
charset = args.shift.to_s.chars.to_a
2025

21-
charset = (0..9).map { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a
26+
charset = (0..9).map { |i| i.to_s } + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty?
2227

2328
rand_string = ''
2429
for current in 1..length

spec/functions/fqdn_rand_base64_spec.rb

Lines changed: 0 additions & 65 deletions
This file was deleted.

spec/functions/fqdn_rand_string_spec.rb

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,29 @@
1818
expect { fqdn_rand_string(0.5) }.to( raise_error(ArgumentError, /first argument must be a positive integer/))
1919
end
2020

21-
it "provides a valid alphanumeric string" do
22-
string = %r{\A[a-zA-Z0-9]*\z}
23-
expect(fqdn_rand_string(100).match(string)).not_to eq(nil)
21+
it "provides a valid alphanumeric string when no character set is provided" do
22+
length = 100
23+
string = %r{\A[a-zA-Z0-9]{#{length}}\z}
24+
expect(fqdn_rand_string(length).match(string)).not_to eq(nil)
25+
end
26+
27+
it "provides a valid alphanumeric string when an :undef character set is provided" do
28+
length = 100
29+
string = %r{\A[a-zA-Z0-9]{#{length}}\z}
30+
expect(fqdn_rand_string(length, :charset => nil).match(string)).not_to eq(nil)
31+
end
32+
33+
it "provides a valid alphanumeric string when an empty character set is provided" do
34+
length = 100
35+
string = %r{\A[a-zA-Z0-9]{#{length}}\z}
36+
expect(fqdn_rand_string(length, :charset => '').match(string)).not_to eq(nil)
37+
end
38+
39+
it "uses a provided character set" do
40+
length = 100
41+
charset = '!@#$%^&*()-_=+'
42+
string = %r{\A[#{charset}]{#{length}}\z}
43+
expect(fqdn_rand_string(length, :charset => charset).match(string)).not_to eq(nil)
2444
end
2545

2646
it "provides a random string exactly as long as the given length" do
@@ -54,12 +74,18 @@
5474

5575
def fqdn_rand_string(max, args = {})
5676
host = args[:host] || '127.0.0.1'
77+
charset = args[:charset]
5778
extra = args[:extra_identifier] || []
5879

5980
scope = PuppetlabsSpec::PuppetInternals.scope
6081
scope.stubs(:[]).with("::fqdn").returns(host)
6182
scope.stubs(:lookupvar).with("::fqdn").returns(host)
6283

63-
scope.function_fqdn_rand_string([max] + extra)
84+
function_args = [max]
85+
if args.has_key?(:charset) or !extra.empty?
86+
function_args << charset
87+
end
88+
function_args += extra
89+
scope.function_fqdn_rand_string(function_args)
6490
end
6591
end

0 commit comments

Comments
 (0)