Skip to content

Commit 390d47a

Browse files
authored
Merge pull request #1356 from smortex/namespace-puppet4-functions
Namespace Puppet 4.x functions
2 parents 2c61c1b + de89c7f commit 390d47a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+951
-653
lines changed

.rubocop_todo.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Lint/MissingCopEnableDirective:
3737
# Configuration parameters: AllowComments, AllowNil.
3838
Lint/SuppressedException:
3939
Exclude:
40-
- 'lib/puppet/functions/merge.rb'
40+
- 'lib/puppet/functions/stdlib/merge.rb'
4141
- 'lib/puppet/parser/functions/has_interface_with.rb'
4242

4343
# Offense count: 5
@@ -93,7 +93,7 @@ Naming/VariableNumber:
9393
# Configuration parameters: MinSize.
9494
Performance/CollectionLiteralInLoop:
9595
Exclude:
96-
- 'lib/puppet/functions/ensure_packages.rb'
96+
- 'lib/puppet/functions/stdlib/ensure_packages.rb'
9797

9898
# Offense count: 36
9999
# Configuration parameters: Prefixes, AllowedPatterns.
@@ -192,5 +192,5 @@ Style/MixinUsage:
192192
# AllowedMethods: respond_to_missing?
193193
Style/OptionalBooleanParameter:
194194
Exclude:
195-
- 'lib/puppet/functions/to_json_pretty.rb'
195+
- 'lib/puppet/functions/stdlib/to_json_pretty.rb'
196196
- 'lib/puppet_x/stdlib/toml_dumper.rb'

Rakefile

+30
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,33 @@ EOM
8787
end
8888
end
8989

90+
desc 'Regenerate the deprecated unamespaced shims'
91+
task :regenerate_unamespaced_shims do
92+
Dir['lib/puppet/functions/*.rb'].each do |filename|
93+
content = File.read(filename)
94+
95+
unless content =~ /@summary DEPRECATED. Use the namespaced function/
96+
warn("#{filename} does not look like a deprecation shim (skipping)")
97+
next
98+
end
99+
100+
function_name = File.basename(filename, '.rb')
101+
102+
File.write(filename, <<~CODE)
103+
# frozen_string_literal: true
104+
105+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
106+
107+
# @summary DEPRECATED. Use the namespaced function [`stdlib::#{function_name}`](#stdlib#{function_name}) instead.
108+
Puppet::Functions.create_function(:#{function_name}) do
109+
dispatch :deprecation_gen do
110+
repeated_param 'Any', :args
111+
end
112+
def deprecation_gen(*args)
113+
call_function('deprecation', '#{function_name}', 'This function is deprecated, please use stdlib::#{function_name} instead.')
114+
call_function('stdlib::#{function_name}', *args)
115+
end
116+
end
117+
CODE
118+
end
119+
end

lib/puppet/functions/batch_escape.rb

+8-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Escapes a string so that it can be safely used in a batch shell command line.
5-
#
6-
# >* Note:* that the resulting string should be used unquoted and is not intended for use in double quotes nor in single
7-
# quotes.
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::batch_escape`](#stdlibbatch_escape) instead.
86
Puppet::Functions.create_function(:batch_escape) do
9-
# @param string
10-
# The string to escape
11-
#
12-
# @return
13-
# An escaped string that can be safely used in a batch command line.
14-
dispatch :batch_escape do
15-
param 'Any', :string
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
169
end
17-
18-
def batch_escape(string)
19-
result = ''
20-
21-
string.to_s.chars.each do |char|
22-
result += case char
23-
when '"' then '""'
24-
when '$', '\\' then "\\#{char}"
25-
else char
26-
end
27-
end
28-
29-
%("#{result}")
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'batch_escape', 'This function is deprecated, please use stdlib::batch_escape instead.')
12+
call_function('stdlib::batch_escape', *args)
3013
end
3114
end
+8-55
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary Takes a list of packages and only installs them if they don't already exist.
4-
#
5-
# It optionally takes a hash as a second parameter that will be passed as the
6-
# third argument to the ensure_resource() function.
7-
Puppet::Functions.create_function(:ensure_packages, Puppet::Functions::InternalFunction) do
8-
# @param packages
9-
# The packages to ensure are installed.
10-
# @param default_attributes
11-
# Default attributes to be passed to the `ensure_resource()` function
12-
# @return [Undef] Returns nothing.
13-
dispatch :ensure_packages do
14-
scope_param
15-
param 'Variant[String[1], Array[String[1]]]', :packages
16-
optional_param 'Hash', :default_attributes
17-
return_type 'Undef'
18-
end
19-
20-
# @param packages
21-
# The packages to ensure are installed. The keys are packages and values are the attributes specific to that package.
22-
# @param default_attributes
23-
# Default attributes. Package specific attributes from the `packages` parameter will take precedence.
24-
# @return [Undef] Returns nothing.
25-
dispatch :ensure_packages_hash do
26-
scope_param
27-
param 'Hash[String[1], Any]', :packages
28-
optional_param 'Hash', :default_attributes
29-
return_type 'Undef'
30-
end
31-
32-
def ensure_packages(scope, packages, default_attributes = {})
33-
Array(packages).each do |package_name|
34-
defaults = { 'ensure' => 'installed' }.merge(default_attributes)
35-
36-
# `present` and `installed` are aliases for the `ensure` attribute. If `ensure` is set to either of these values replace
37-
# with `installed` by default but `present` if this package is already in the catalog with `ensure => present`
38-
defaults['ensure'] = default_ensure(package_name) if ['present', 'installed'].include?(defaults['ensure'])
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
394

40-
scope.call_function('ensure_resource', ['package', package_name, defaults])
41-
end
42-
nil
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::ensure_packages`](#stdlibensure_packages) instead.
6+
Puppet::Functions.create_function(:ensure_packages) do
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
439
end
44-
45-
def ensure_packages_hash(scope, packages, default_attributes = {})
46-
packages.each do |package, attributes|
47-
ensure_packages(scope, package, default_attributes.merge(attributes))
48-
end
49-
nil
50-
end
51-
52-
private
53-
54-
def default_ensure(package_name)
55-
if call_function('defined_with_params', "Package[#{package_name}]", { 'ensure' => 'present' })
56-
'present'
57-
else
58-
'installed'
59-
end
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'ensure_packages', 'This function is deprecated, please use stdlib::ensure_packages instead.')
12+
call_function('stdlib::ensure_packages', *args)
6013
end
6114
end
+8-31
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Generates a random alphanumeric string. Combining the `$fqdn` fact and an
5-
# optional seed for repeatable randomness.
6-
#
7-
# Optionally, you can specify a character set for the function (defaults to alphanumeric).
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::fqdn_rand_string`](#stdlibfqdn_rand_string) instead.
86
Puppet::Functions.create_function(:fqdn_rand_string) do
9-
# @param length The length of the resulting string.
10-
# @param charset The character set to use.
11-
# @param The seed for repeatable randomness.
12-
#
13-
# @return [String]
14-
#
15-
# @example Example Usage:
16-
# fqdn_rand_string(10)
17-
# fqdn_rand_string(10, 'ABCDEF!@$%^')
18-
# fqdn_rand_string(10, '', 'custom seed')
19-
dispatch :fqdn_rand_string do
20-
param 'Integer[1]', :length
21-
optional_param 'String', :charset
22-
optional_repeated_param 'Any', :seed
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
239
end
24-
25-
def fqdn_rand_string(length, charset = '', *seed)
26-
charset = charset.chars.to_a
27-
28-
charset = (0..9).map(&:to_s) + ('A'..'Z').to_a + ('a'..'z').to_a if charset.empty?
29-
30-
rand_string = ''
31-
length.times do |current|
32-
rand_string += charset[call_function('fqdn_rand', charset.size, (seed + [current + 1]).join(':'))]
33-
end
34-
35-
rand_string
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'fqdn_rand_string', 'This function is deprecated, please use stdlib::fqdn_rand_string instead.')
12+
call_function('stdlib::fqdn_rand_string', *args)
3613
end
3714
end
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# frozen_string_literal: true
22

3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
35
# @summary DEPRECATED. Use the namespaced function [`stdlib::has_interface_with`](#stdlibhas_interface_with) instead.
46
Puppet::Functions.create_function(:has_interface_with) do
57
dispatch :deprecation_gen do
68
repeated_param 'Any', :args
79
end
810
def deprecation_gen(*args)
9-
call_function('deprecation', 'has_interface_with', 'This method is deprecated, please use stdlib::has_interface_with instead.')
11+
call_function('deprecation', 'has_interface_with', 'This function is deprecated, please use stdlib::has_interface_with instead.')
1012
call_function('stdlib::has_interface_with', *args)
1113
end
1214
end

lib/puppet/functions/merge.rb

+8-106
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Merges two or more hashes together or hashes resulting from iteration, and returns
5-
# the resulting hash.
6-
#
7-
# @example Using merge()
8-
# $hash1 = {'one' => 1, 'two', => 2}
9-
# $hash2 = {'two' => 'dos', 'three', => 'tres'}
10-
# $merged_hash = merge($hash1, $hash2) # $merged_hash = {'one' => 1, 'two' => 'dos', 'three' => 'tres'}
11-
#
12-
# When there is a duplicate key, the key in the rightmost hash will "win."
13-
#
14-
# Note that since Puppet 4.0.0 the same merge can be achieved with the + operator.
15-
# `$merged_hash = $hash1 + $hash2`
16-
#
17-
# If merge is given a single Iterable (Array, Hash, etc.) it will call a given block with
18-
# up to three parameters, and merge each resulting Hash into the accumulated result. All other types
19-
# of values returned from the block (typically undef) are skipped (not merged).
20-
#
21-
# The codeblock can take 2 or three parameters:
22-
# * with two, it gets the current hash (as built to this point), and each value (for hash the value is a [key, value] tuple)
23-
# * with three, it gets the current hash (as built to this point), the key/index of each value, and then the value
24-
#
25-
# If the iterable is empty, or no hash was returned from the given block, an empty hash is returned. In the given block, a call to `next()`
26-
# will skip that entry, and a call to `break()` will end the iteration.
27-
#
28-
# @example counting occurrences of strings in an array
29-
# ['a', 'b', 'c', 'c', 'd', 'b'].merge | $hsh, $v | { { $v => $hsh[$v].lest || { 0 } + 1 } } # results in { a => 1, b => 2, c => 2, d => 1 }
30-
#
31-
# @example skipping values for entries that are longer than 1 char
32-
# ['a', 'b', 'c', 'c', 'd', 'b', 'blah', 'blah'].merge | $hsh, $v | { if $v =~ String[1,1] { { $v => $hsh[$v].lest || { 0 } + 1 } } } # results in { a => 1, b => 2, c => 2, d => 1 }
33-
#
34-
# The iterative `merge()` has an advantage over doing the same with a general `reduce()` in that the constructed hash
35-
# does not have to be copied in each iteration and thus will perform much better with large inputs.
36-
Puppet::Functions.create_function(:merge) do
37-
# @param args
38-
# Repeated Param - The hashes that are to be merged
39-
#
40-
# @return
41-
# The merged hash
42-
dispatch :merge2hashes do
43-
repeated_param 'Variant[Hash[Scalar,Any], Undef, String[0,0]]', :args # this strange type is backwards compatible
44-
return_type 'Hash[Scalar,Any]'
45-
end
46-
47-
# @param args
48-
# Repeated Param - The hashes that are to be merged
49-
#
50-
# @param block
51-
# A block placed on the repeatable param `args`
52-
#
53-
# @return
54-
# The merged hash
55-
dispatch :merge_iterable3 do
56-
repeated_param 'Iterable', :args
57-
block_param 'Callable[3,3]', :block
58-
return_type 'Hash'
59-
end
60-
61-
# @param args
62-
# Repeated Param - The hashes that are to be merged
63-
#
64-
# @param block
65-
# A block placed on the repeatable param `args`
66-
#
67-
# @return
68-
# The merged hash
69-
dispatch :merge_iterable2 do
70-
repeated_param 'Iterable', :args
71-
block_param 'Callable[2,2]', :block
72-
return_type 'Hash'
73-
end
74-
75-
def merge2hashes(*hashes)
76-
accumulator = {}
77-
hashes.each { |h| accumulator.merge!(h) if h.is_a?(Hash) }
78-
accumulator
79-
end
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
804

81-
def merge_iterable2(iterable)
82-
accumulator = {}
83-
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable)
84-
enum.each do |v|
85-
r = yield(accumulator, v)
86-
accumulator.merge!(r) if r.is_a?(Hash)
87-
end
88-
accumulator
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::merge`](#stdlibmerge) instead.
6+
Puppet::Functions.create_function(:merge) do
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
899
end
90-
91-
def merge_iterable3(iterable)
92-
accumulator = {}
93-
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, iterable)
94-
if enum.hash_style?
95-
enum.each do |entry|
96-
r = yield(accumulator, *entry)
97-
accumulator.merge!(r) if r.is_a?(Hash)
98-
end
99-
else
100-
begin
101-
index = 0
102-
loop do
103-
r = yield(accumulator, index, enum.next)
104-
accumulator.merge!(r) if r.is_a?(Hash)
105-
index += 1
106-
end
107-
rescue StopIteration
108-
end
109-
end
110-
accumulator
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'merge', 'This function is deprecated, please use stdlib::merge instead.')
12+
call_function('stdlib::merge', *args)
11113
end
11214
end
+8-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
# frozen_string_literal: true
22

3-
# @summary
4-
# Checks if the OS version is at least a certain version.
5-
# > *Note:*
6-
# Only the major version is taken into account.
7-
#
8-
# @example Example usage:#
9-
# if os_version_gte('Debian', '9') { }
10-
# if os_version_gte('Ubuntu', '18.04') { }
3+
# THIS FILE WAS GENERATED BY `rake regenerate_unamespaced_shims`
4+
5+
# @summary DEPRECATED. Use the namespaced function [`stdlib::os_version_gte`](#stdlibos_version_gte) instead.
116
Puppet::Functions.create_function(:os_version_gte) do
12-
# @param os operating system
13-
# @param version
14-
#
15-
# @return [Boolean] `true` or `false
16-
dispatch :os_version_gte do
17-
param 'String[1]', :os
18-
param 'String[1]', :version
19-
return_type 'Boolean'
7+
dispatch :deprecation_gen do
8+
repeated_param 'Any', :args
209
end
21-
22-
def os_version_gte(os, version)
23-
facts = closure_scope['facts']
24-
(facts['os']['name'] == os &&
25-
Puppet::Util::Package.versioncmp(facts['os']['release']['major'], version) >= 0)
10+
def deprecation_gen(*args)
11+
call_function('deprecation', 'os_version_gte', 'This function is deprecated, please use stdlib::os_version_gte instead.')
12+
call_function('stdlib::os_version_gte', *args)
2613
end
2714
end

0 commit comments

Comments
 (0)