Skip to content

Commit 37a6141

Browse files
committed
Namespace function ensure_packages()
1 parent 736aa24 commit 37a6141

File tree

4 files changed

+71
-57
lines changed

4 files changed

+71
-57
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.
Lines changed: 8 additions & 55 deletions
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
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
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(:'stdlib::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'])
39+
40+
scope.call_function('ensure_resource', ['package', package_name, defaults])
41+
end
42+
nil
43+
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
60+
end
61+
end

spec/functions/ensure_packages_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require 'spec_helper'
44

5-
describe 'ensure_packages' do
5+
describe 'stdlib::ensure_packages' do
66
it { is_expected.not_to be_nil }
77
it { is_expected.to run.with_params('packagename') }
88
it { is_expected.to run.with_params(['packagename1', 'packagename2']) }

0 commit comments

Comments
 (0)