puppet 6.2.0-x86-mingw32 → 6.3.0-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puppet might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +70 -0
- data/Gemfile.lock +7 -7
- data/lib/puppet/application.rb +1 -1
- data/lib/puppet/application/lookup.rb +1 -1
- data/lib/puppet/confine/boolean.rb +45 -0
- data/lib/puppet/confine/false.rb +7 -1
- data/lib/puppet/confine/true.rb +7 -1
- data/lib/puppet/defaults.rb +0 -18
- data/lib/puppet/functions/call.rb +2 -1
- data/lib/puppet/functions/group_by.rb +54 -0
- data/lib/puppet/functions/index.rb +167 -0
- data/lib/puppet/functions/partition.rb +54 -0
- data/lib/puppet/pops/evaluator/evaluator_impl.rb +1 -1
- data/lib/puppet/pops/issues.rb +4 -0
- data/lib/puppet/pops/model/factory.rb +38 -4
- data/lib/puppet/pops/parser/egrammar.ra +2 -2
- data/lib/puppet/pops/parser/eparser.rb +2 -2
- data/lib/puppet/pops/parser/heredoc_support.rb +17 -7
- data/lib/puppet/pops/parser/lexer2.rb +6 -1
- data/lib/puppet/pops/parser/locator.rb +106 -86
- data/lib/puppet/pops/parser/parser_support.rb +11 -2
- data/lib/puppet/pops/types/type_mismatch_describer.rb +1 -1
- data/lib/puppet/pops/validation/checker4_0.rb +2 -2
- data/lib/puppet/provider/group/windows_adsi.rb +4 -1
- data/lib/puppet/provider/package/apt.rb +2 -2
- data/lib/puppet/provider/package/dnf.rb +2 -2
- data/lib/puppet/provider/package/gem.rb +2 -2
- data/lib/puppet/provider/package/openbsd.rb +2 -2
- data/lib/puppet/provider/package/pacman.rb +1 -2
- data/lib/puppet/provider/package/pip.rb +1 -2
- data/lib/puppet/provider/package/pip3.rb +1 -2
- data/lib/puppet/provider/package/pkg.rb +2 -4
- data/lib/puppet/provider/package/portage.rb +1 -2
- data/lib/puppet/provider/package/rpm.rb +1 -2
- data/lib/puppet/provider/package/sun.rb +2 -2
- data/lib/puppet/provider/package/windows.rb +2 -2
- data/lib/puppet/provider/package/yum.rb +1 -2
- data/lib/puppet/provider/package/zypper.rb +2 -2
- data/lib/puppet/provider/service/upstart.rb +16 -6
- data/lib/puppet/transaction/resource_harness.rb +1 -0
- data/lib/puppet/type/file.rb +6 -1
- data/lib/puppet/util/log.rb +7 -2
- data/lib/puppet/util/pidlock.rb +14 -1
- data/lib/puppet/util/windows/process.rb +73 -5
- data/lib/puppet/version.rb +1 -1
- data/locales/puppet.pot +92 -92
- data/man/man5/puppet.conf.5 +2 -2
- data/man/man8/puppet-agent.8 +1 -1
- data/man/man8/puppet-apply.8 +1 -1
- data/man/man8/puppet-catalog.8 +1 -1
- data/man/man8/puppet-config.8 +1 -1
- data/man/man8/puppet-describe.8 +1 -1
- data/man/man8/puppet-device.8 +1 -1
- data/man/man8/puppet-doc.8 +1 -1
- data/man/man8/puppet-epp.8 +1 -1
- data/man/man8/puppet-facts.8 +1 -1
- data/man/man8/puppet-filebucket.8 +1 -1
- data/man/man8/puppet-generate.8 +1 -1
- data/man/man8/puppet-help.8 +1 -1
- data/man/man8/puppet-key.8 +1 -1
- data/man/man8/puppet-lookup.8 +2 -2
- data/man/man8/puppet-man.8 +1 -1
- data/man/man8/puppet-module.8 +1 -1
- data/man/man8/puppet-node.8 +1 -1
- data/man/man8/puppet-parser.8 +1 -1
- data/man/man8/puppet-plugin.8 +1 -1
- data/man/man8/puppet-report.8 +1 -1
- data/man/man8/puppet-resource.8 +1 -1
- data/man/man8/puppet-script.8 +1 -1
- data/man/man8/puppet-ssl.8 +1 -1
- data/man/man8/puppet-status.8 +1 -1
- data/man/man8/puppet.8 +2 -2
- data/spec/unit/application_spec.rb +8 -1
- data/spec/unit/confine/false_spec.rb +27 -0
- data/spec/unit/confine/true_spec.rb +27 -0
- data/spec/unit/defaults_spec.rb +0 -14
- data/spec/unit/functions/group_by_spec.rb +40 -0
- data/spec/unit/functions/index_spec.rb +184 -0
- data/spec/unit/functions/partition_spec.rb +40 -0
- data/spec/unit/pops/loaders/loaders_spec.rb +5 -0
- data/spec/unit/pops/parser/locator_spec.rb +45 -0
- data/spec/unit/pops/parser/parse_heredoc_spec.rb +111 -15
- data/spec/unit/pops/types/type_mismatch_describer_spec.rb +9 -0
- data/spec/unit/provider/group/windows_adsi_spec.rb +7 -1
- data/spec/unit/transaction/resource_harness_spec.rb +26 -0
- data/spec/unit/util/execution_spec.rb +2 -2
- data/spec/unit/util/log_spec.rb +15 -0
- data/spec/unit/util/pidlock_spec.rb +21 -1
- metadata +13 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7c03056fb261cae9f267079355947aa497bb8197647f3a5711c349c8530e99f
|
4
|
+
data.tar.gz: 070f1e085fef0d7e29f721588bfb0ebc78e6e9aa67f7f80183fa47621fdc443d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b22ef3c85a9decfcbc0ff991ec09f3349bb1b0bd97fd8f5f40b8163b42e4ccdfd3e71430dbbc42799f9f3a46c0d61e92e75e50007d9a40c8a0394bd5c87d7e67
|
7
|
+
data.tar.gz: e46983398037419137e70a505ce0887f63200b7828741c63911f3a02381e849e60b2908141074c56a390af982b32d89a56ed16485dcf9f506a3b3e0748b9d423
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Community Guidelines and Code of Conduct
|
2
|
+
|
3
|
+
We want to keep the Puppet communities awesome, and we need your help to keep it
|
4
|
+
that way. While we have specific guidelines for various tools (see links below),
|
5
|
+
in general, you should:
|
6
|
+
|
7
|
+
* **Be nice**: Be courteous, respectful and polite to fellow community members. No
|
8
|
+
offensive comments related to gender, gender identity or expression, sexual
|
9
|
+
orientation, disability, physical appearance, body size, race, religion; no
|
10
|
+
sexual images in public spaces, real or implied violence, intimidation,
|
11
|
+
oppression, stalking, following, harassing photography or recording, sustained
|
12
|
+
disruption of talks or other events, inappropriate physical contact, doxxing, or
|
13
|
+
unwelcome sexual attention will be tolerated. We like nice people way better
|
14
|
+
than mean ones!
|
15
|
+
* **Encourage diversity and participation**: Make everyone in our community feel
|
16
|
+
welcome, regardless of their background, and do everything possible to encourage
|
17
|
+
participation in our community.
|
18
|
+
* **Focus on constructive criticisms**: When offering suggestions, whether in online
|
19
|
+
discussions or as comments on a pull request, you should always use welcoming
|
20
|
+
and inclusive language. Be respectful of differing viewpoints and the fact that
|
21
|
+
others may not have the same experiences you do. Offer suggestions for
|
22
|
+
improvement, rather than focusing on mistakes. When others critique your work or
|
23
|
+
ideas, gracefully accept the criticisms and default to assuming good intentions.
|
24
|
+
* **Keep it legal**: Basically, don't get us in trouble. Share only content that you
|
25
|
+
own, do not share private or sensitive information, and don't break the law.
|
26
|
+
* **Stay on topic**: Keep conversation in a thread on topic, whether that's a pull
|
27
|
+
request or a Slack conversation or anything else. Make sure that you are posting
|
28
|
+
to the correct channel and remember that nobody likes spam.
|
29
|
+
|
30
|
+
## Guideline violations --- 3 strikes method
|
31
|
+
|
32
|
+
The point of this section is not to find opportunities to punish people, but we
|
33
|
+
do need a fair way to deal with people who do harm to our community. Extreme
|
34
|
+
violations of a threatening, abusive, destructive, or illegal nature will be
|
35
|
+
addressed immediately and are not subject to 3 strikes.
|
36
|
+
|
37
|
+
* First occurrence: We'll give you a friendly, but public, reminder that the
|
38
|
+
behavior is inappropriate according to our guidelines.
|
39
|
+
* Second occurrence: We'll send you a private message with a warning that any
|
40
|
+
additional violations will result in removal from the community.
|
41
|
+
* Third occurrence: Depending on the violation, we might need to delete or ban
|
42
|
+
your account.
|
43
|
+
|
44
|
+
Notes:
|
45
|
+
|
46
|
+
* Obvious spammers are banned on first occurrence. If we don’t do this, we’ll
|
47
|
+
have spam all over the place.
|
48
|
+
* Violations are forgiven after 6 months of good behavior, and we won’t hold a grudge.
|
49
|
+
* People who are committing minor formatting / style infractions will get some
|
50
|
+
education, rather than hammering them in the 3 strikes process.
|
51
|
+
|
52
|
+
Contact conduct@puppet.com to report abuse or appeal violations. This email list
|
53
|
+
goes to Kara Sowles (kara at puppet.com) and Katie Abbott (katie dot abbott at
|
54
|
+
puppet.com). In the case of appeals, we know that mistakes happen, and we’ll
|
55
|
+
work with you to come up with a fair solution if there has been a
|
56
|
+
misunderstanding.
|
57
|
+
|
58
|
+
## Full text
|
59
|
+
|
60
|
+
See our [full community guidelines](https://puppet.com/community/community-guidelines),
|
61
|
+
covering Slack, IRC, events and other forms of community participation.
|
62
|
+
|
63
|
+
## Credits
|
64
|
+
|
65
|
+
Credit to [01.org](https://01.org/community/participation-guidelines) and
|
66
|
+
[meego.com](http://wiki.meego.com/Community_guidelines), since they formed the
|
67
|
+
starting point for many of these guidelines.
|
68
|
+
|
69
|
+
The Event Code of Conduct is based on the [example policy from the Geek Feminism wiki](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment),
|
70
|
+
created by the Ada Initiative and other volunteers. The [PyCon Code of Conduct](https://github.com/python/pycon-code-of-conduct) also served as inspiration.
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
puppet (6.
|
4
|
+
puppet (6.3.0)
|
5
5
|
CFPropertyList (~> 2.2)
|
6
6
|
facter (>= 2.4.0, < 4)
|
7
7
|
fast_gettext (~> 1.1.2)
|
@@ -34,9 +34,9 @@ GEM
|
|
34
34
|
locale
|
35
35
|
hashdiff (0.3.8)
|
36
36
|
hiera (3.5.0)
|
37
|
-
hiera-eyaml (
|
37
|
+
hiera-eyaml (3.0.0)
|
38
38
|
highline (~> 1.6.19)
|
39
|
-
|
39
|
+
optimist
|
40
40
|
highline (1.6.21)
|
41
41
|
hocon (1.2.5)
|
42
42
|
hpricot (0.8.6)
|
@@ -53,10 +53,11 @@ GEM
|
|
53
53
|
msgpack (1.2.6)
|
54
54
|
multi_json (1.13.1)
|
55
55
|
mustache (1.1.0)
|
56
|
-
|
56
|
+
optimist (3.0.0)
|
57
|
+
packaging (0.99.23)
|
57
58
|
artifactory (~> 2)
|
58
59
|
rake (~> 12.3)
|
59
|
-
parallel (1.
|
60
|
+
parallel (1.13.0)
|
60
61
|
parser (2.6.0.0)
|
61
62
|
ast (~> 2.4.0)
|
62
63
|
powerpack (0.1.2)
|
@@ -66,7 +67,7 @@ GEM
|
|
66
67
|
public_suffix (3.0.3)
|
67
68
|
puppet-resource_api (1.6.2)
|
68
69
|
hocon (>= 1.0)
|
69
|
-
puppetserver-ca (1.
|
70
|
+
puppetserver-ca (1.3.0)
|
70
71
|
facter (>= 2.0.1, < 4)
|
71
72
|
racc (1.4.9)
|
72
73
|
rainbow (2.2.2)
|
@@ -110,7 +111,6 @@ GEM
|
|
110
111
|
safe_yaml (1.0.4)
|
111
112
|
semantic_puppet (1.0.2)
|
112
113
|
text (1.3.1)
|
113
|
-
trollop (2.9.9)
|
114
114
|
unicode-display_width (1.4.1)
|
115
115
|
vcr (2.9.3)
|
116
116
|
webmock (1.24.6)
|
data/lib/puppet/application.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'puppet/confine'
|
2
|
+
|
3
|
+
# Common module for the Boolean confines. It currently
|
4
|
+
# contains just enough code to implement PUP-9336.
|
5
|
+
class Puppet::Confine
|
6
|
+
module Boolean
|
7
|
+
# Returns the passing value for the Boolean confine.
|
8
|
+
def passing_value
|
9
|
+
raise NotImplementedError, "The Boolean confine %{confine} must provide the passing value." % { confine: self.class.name }
|
10
|
+
end
|
11
|
+
|
12
|
+
# The Boolean confines 'true' and 'false' let the user specify
|
13
|
+
# two types of values:
|
14
|
+
# * A lambda for lazy evaluation. This would be something like
|
15
|
+
# confine :true => lambda { true }
|
16
|
+
#
|
17
|
+
# * A single Boolean value, or an array of Boolean values. This would
|
18
|
+
# be something like
|
19
|
+
# confine :true => true OR confine :true => [true, false, false, true]
|
20
|
+
#
|
21
|
+
# This override distinguishes between the two cases.
|
22
|
+
def values
|
23
|
+
# Note that Puppet::Confine's constructor ensures that @values
|
24
|
+
# will always be an array, even if a lambda's passed in. This is
|
25
|
+
# why we have the length == 1 check.
|
26
|
+
unless @values.length == 1 && @values.first.respond_to?(:call)
|
27
|
+
return @values
|
28
|
+
end
|
29
|
+
|
30
|
+
# We have a lambda. Here, we want to enforce "cache positive"
|
31
|
+
# behavior, which is to cache the result _if_ it evaluates to
|
32
|
+
# the passing value (i.e. the class name).
|
33
|
+
|
34
|
+
return @cached_value unless @cached_value.nil?
|
35
|
+
|
36
|
+
# Double negate to coerce the value into a Boolean
|
37
|
+
calculated_value = !! @values.first.call
|
38
|
+
if calculated_value == passing_value
|
39
|
+
@cached_value = [calculated_value]
|
40
|
+
end
|
41
|
+
|
42
|
+
[calculated_value]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/puppet/confine/false.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
require 'puppet/confine'
|
1
|
+
require 'puppet/confine/boolean'
|
2
2
|
|
3
3
|
class Puppet::Confine::False < Puppet::Confine
|
4
|
+
include Puppet::Confine::Boolean
|
5
|
+
|
6
|
+
def passing_value
|
7
|
+
false
|
8
|
+
end
|
9
|
+
|
4
10
|
def self.summarize(confines)
|
5
11
|
confines.inject(0) { |count, confine| count + confine.summary }
|
6
12
|
end
|
data/lib/puppet/confine/true.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
require 'puppet/confine'
|
1
|
+
require 'puppet/confine/boolean'
|
2
2
|
|
3
3
|
class Puppet::Confine::True < Puppet::Confine
|
4
|
+
include Puppet::Confine::Boolean
|
5
|
+
|
6
|
+
def passing_value
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
4
10
|
def self.summarize(confines)
|
5
11
|
confines.inject(0) { |count, confine| count + confine.summary }
|
6
12
|
end
|
data/lib/puppet/defaults.rb
CHANGED
@@ -1452,30 +1452,12 @@ EOT
|
|
1452
1452
|
:server => {
|
1453
1453
|
:default => "puppet",
|
1454
1454
|
:desc => "The puppet master server to which the puppet agent should connect.",
|
1455
|
-
:call_hook => :on_initialize_and_write,
|
1456
|
-
:hook => proc { |value|
|
1457
|
-
if Puppet.settings.set_by_config?(:server) && Puppet.settings.set_by_config?(:server_list)
|
1458
|
-
#TRANSLATOR 'server' and 'server_list' are setting names and should not be translated
|
1459
|
-
message = _('Attempted to set both server and server_list.')
|
1460
|
-
message += ' ' + _('Server setting will not be used.')
|
1461
|
-
Puppet.deprecation_warning(message, :SERVER_DUPLICATION)
|
1462
|
-
end
|
1463
|
-
}
|
1464
1455
|
},
|
1465
1456
|
:server_list => {
|
1466
1457
|
:default => [],
|
1467
1458
|
:type => :server_list,
|
1468
1459
|
:desc => "The list of puppet master servers to which the puppet agent should connect,
|
1469
1460
|
in the order that they will be tried.",
|
1470
|
-
:call_hook => :on_initialize_and_write,
|
1471
|
-
:hook => proc { |value|
|
1472
|
-
if Puppet.settings.set_by_config?(:server) && Puppet.settings.set_by_config?(:server_list)
|
1473
|
-
#TRANSLATOR 'server' and 'server_list' are setting names and should not be translated
|
1474
|
-
message = _('Attempted to set both server and server_list.')
|
1475
|
-
message += ' ' + _('Server setting will not be used.')
|
1476
|
-
Puppet.deprecation_warning(message, :SERVER_DUPLICATION)
|
1477
|
-
end
|
1478
|
-
}
|
1479
1461
|
},
|
1480
1462
|
:use_srv_records => {
|
1481
1463
|
:default => false,
|
@@ -69,7 +69,8 @@ Puppet::Functions.create_function(:call, Puppet::Functions::InternalFunction) do
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def call_impl_block(scope, function_name, *args, &block)
|
72
|
-
|
72
|
+
# The call function must be able to call functions loaded by any loader visible from the calling scope.
|
73
|
+
Puppet::Pops::Parser::EvaluatingParser.new.evaluator.external_call_function(function_name, args, scope, &block)
|
73
74
|
end
|
74
75
|
|
75
76
|
def call_deferred(scope, deferred)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block
|
4
|
+
# and the values are arrays of elements in the collection that correspond to the key.
|
5
|
+
Puppet::Functions.create_function(:group_by) do
|
6
|
+
# @param collection A collection of things to group.
|
7
|
+
# @example Group array of strings by length, results in e.g. { 1 => [a, b], 2 => [ab] }
|
8
|
+
# [a, b, ab].group_by |$s| { $s.length }
|
9
|
+
# @example Group array of strings by length and index, results in e.g. {1 => ['a'], 2 => ['b', 'ab']}
|
10
|
+
# [a, b, ab].group_by |$i, $s| { $i%2 + $s.length }
|
11
|
+
# @example Group hash iterating by key-value pair, results in e.g. { 2 => [['a', [1, 2]]], 1 => [['b', [1]]] }
|
12
|
+
# { a => [1, 2], b => [1] }.group_by |$kv| { $kv[1].length }
|
13
|
+
# @example Group hash iterating by key and value, results in e.g. { 2 => [['a', [1, 2]]], 1 => [['b', [1]]] }
|
14
|
+
# { a => [1, 2], b => [1] }.group_by |$k, $v| { $v.length }
|
15
|
+
dispatch :group_by_1 do
|
16
|
+
required_param 'Collection', :collection
|
17
|
+
block_param 'Callable[1,1]', :block
|
18
|
+
return_type 'Hash'
|
19
|
+
end
|
20
|
+
|
21
|
+
dispatch :group_by_2a do
|
22
|
+
required_param 'Array', :array
|
23
|
+
block_param 'Callable[2,2]', :block
|
24
|
+
return_type 'Hash'
|
25
|
+
end
|
26
|
+
|
27
|
+
dispatch :group_by_2 do
|
28
|
+
required_param 'Collection', :collection
|
29
|
+
block_param 'Callable[2,2]', :block
|
30
|
+
return_type 'Hash'
|
31
|
+
end
|
32
|
+
|
33
|
+
def group_by_1(collection)
|
34
|
+
collection.group_by do |item|
|
35
|
+
yield(item)
|
36
|
+
end.freeze
|
37
|
+
end
|
38
|
+
|
39
|
+
def group_by_2a(array)
|
40
|
+
grouped = array.size.times.zip(array).group_by do |k, v|
|
41
|
+
yield(k, v)
|
42
|
+
end
|
43
|
+
|
44
|
+
grouped.each_with_object({}) do |(k, v), hsh|
|
45
|
+
hsh[k] = v.map { |item| item[1] }
|
46
|
+
end.freeze
|
47
|
+
end
|
48
|
+
|
49
|
+
def group_by_2(collection)
|
50
|
+
collection.group_by do |k, v|
|
51
|
+
yield(k, v)
|
52
|
+
end.freeze
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
# Returns the index (or key in a hash) to a first-found value in an `Iterable` value.
|
2
|
+
#
|
3
|
+
# When called with a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
|
4
|
+
# the lambda is called repeatedly using each value in a data structure until the lambda returns a "truthy" value which
|
5
|
+
# makes the function return the index or key, or if the end of the iteration is reached, undef is returned.
|
6
|
+
#
|
7
|
+
# This function can be called in two different ways; with a value to be searched for, or with
|
8
|
+
# a lambda that determines if an entry in the iterable matches.
|
9
|
+
#
|
10
|
+
# When called with a lambda the function takes two mandatory arguments, in this order:
|
11
|
+
#
|
12
|
+
# 1. An array, hash, string, or other iterable object that the function will iterate over.
|
13
|
+
# 2. A lambda, which the function calls for each element in the first argument. It can request one (value) or two (index/key, value) parameters.
|
14
|
+
#
|
15
|
+
# @example Using the `index` function
|
16
|
+
#
|
17
|
+
# `$data.index |$parameter| { <PUPPET CODE BLOCK> }`
|
18
|
+
#
|
19
|
+
# or
|
20
|
+
#
|
21
|
+
# `index($data) |$parameter| { <PUPPET CODE BLOCK> }`
|
22
|
+
#
|
23
|
+
# @example Using the `index` function with an Array and a one-parameter lambda
|
24
|
+
#
|
25
|
+
# ```puppet
|
26
|
+
# $data = ["routers", "servers", "workstations"]
|
27
|
+
# notice $data.index |$value| { $value == 'servers' } # notices 1
|
28
|
+
# notice $data.index |$value| { $value == 'hosts' } # notices undef
|
29
|
+
# ```
|
30
|
+
#
|
31
|
+
# @example Using the `index` function with a Hash and a one-parameter lambda
|
32
|
+
#
|
33
|
+
# ```puppet
|
34
|
+
# $data = {types => ["routers", "servers", "workstations"], colors => ['red', 'blue', 'green']}
|
35
|
+
# notice $data.index |$value| { 'servers' in $value } # notices 'types'
|
36
|
+
# notice $data.index |$value| { 'red' in $value } # notices 'colors'
|
37
|
+
# ```
|
38
|
+
# Note that the lambda gets the value and not an array with `[key, value]` as in other
|
39
|
+
# iterative functions.
|
40
|
+
#
|
41
|
+
# Using a lambda that accepts two values works the same way, it simply gets the index/key
|
42
|
+
# as the first parameter, and the value as the second.
|
43
|
+
#
|
44
|
+
# @example Using the `index` function with an Array and a two-parameter lambda
|
45
|
+
#
|
46
|
+
# ```puppet
|
47
|
+
# # Find the first even numbered index that has a non String value
|
48
|
+
# $data = [key1, 1, 3, 5]
|
49
|
+
# notice $data.index |$idx, $value| { $idx % 2 == 0 and $value !~ String } # notices 2
|
50
|
+
# ```
|
51
|
+
#
|
52
|
+
# When called on a `String`, the lambda is given each character as a value. What is typically wanted is to
|
53
|
+
# find a sequence of characters which is achieved by calling the function with a value to search for instead
|
54
|
+
# of giving a lambda.
|
55
|
+
#
|
56
|
+
#
|
57
|
+
# @example Using the `index` function with a String, search for first occurrence of a sequence of characters
|
58
|
+
#
|
59
|
+
# ```puppet
|
60
|
+
# # Find first occurrence of 'ah'
|
61
|
+
# $data = "blablahbleh"
|
62
|
+
# notice $data.index('ah') # notices 5
|
63
|
+
# ```
|
64
|
+
#
|
65
|
+
# @example Using the `index` function with a String, search for first occurrence of a regular expression
|
66
|
+
#
|
67
|
+
# ```puppet
|
68
|
+
# # Find first occurrence of 'la' or 'le'
|
69
|
+
# $data = "blablahbleh"
|
70
|
+
# notice $data.index(/l(a|e)/ # notices 1
|
71
|
+
# ```
|
72
|
+
#
|
73
|
+
# When searching in a `String` with a given value that is neither `String` nor `Regexp` the answer is always `undef`.
|
74
|
+
# When searching in any other iterable, the value is matched against each value in the iteration using strict
|
75
|
+
# Ruby `==` semantics. If Puppet Language semantics are wanted (where string compare is case insensitive) use a
|
76
|
+
# lambda and the `==` operator in Puppet.
|
77
|
+
#
|
78
|
+
# @example Using the `index` function to search for a given value in an Array
|
79
|
+
#
|
80
|
+
# ```puppet
|
81
|
+
# $data = ['routers', 'servers', 'WORKstations']
|
82
|
+
# notice $data.index('servers') # notices 1
|
83
|
+
# notice $data.index('workstations') # notices undef (not matching case)
|
84
|
+
# ```
|
85
|
+
#
|
86
|
+
# For an general examples that demonstrates iteration, see the Puppet
|
87
|
+
# [iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
|
88
|
+
# documentation.
|
89
|
+
#
|
90
|
+
# @since 6.3.0
|
91
|
+
#
|
92
|
+
Puppet::Functions.create_function(:index) do
|
93
|
+
dispatch :index_Hash_2 do
|
94
|
+
param 'Hash[Any, Any]', :hash
|
95
|
+
block_param 'Callable[2,2]', :block
|
96
|
+
end
|
97
|
+
|
98
|
+
dispatch :index_Hash_1 do
|
99
|
+
param 'Hash[Any, Any]', :hash
|
100
|
+
block_param 'Callable[1,1]', :block
|
101
|
+
end
|
102
|
+
|
103
|
+
dispatch :index_Enumerable_2 do
|
104
|
+
param 'Iterable', :enumerable
|
105
|
+
block_param 'Callable[2,2]', :block
|
106
|
+
end
|
107
|
+
|
108
|
+
dispatch :index_Enumerable_1 do
|
109
|
+
param 'Iterable', :enumerable
|
110
|
+
block_param 'Callable[1,1]', :block
|
111
|
+
end
|
112
|
+
|
113
|
+
dispatch :string_index do
|
114
|
+
param 'String', :str
|
115
|
+
param 'Variant[String,Regexp]', :match
|
116
|
+
end
|
117
|
+
|
118
|
+
dispatch :index_value do
|
119
|
+
param 'Iterable', :enumerable
|
120
|
+
param 'Any', :match
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
def index_Hash_1(hash)
|
125
|
+
hash.each_pair { |x, y| return x if yield(y) }
|
126
|
+
nil
|
127
|
+
end
|
128
|
+
|
129
|
+
def index_Hash_2(hash)
|
130
|
+
hash.each_pair.any? { |x, y| return x if yield(x, y) }
|
131
|
+
nil
|
132
|
+
end
|
133
|
+
|
134
|
+
def index_Enumerable_1(enumerable)
|
135
|
+
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, enumerable)
|
136
|
+
if enum.hash_style?
|
137
|
+
enum.each { |entry| return entry[0] if yield(entry[1]) }
|
138
|
+
else
|
139
|
+
enum.each_with_index { |e, i| return i if yield(e) }
|
140
|
+
end
|
141
|
+
nil
|
142
|
+
end
|
143
|
+
|
144
|
+
def index_Enumerable_2(enumerable)
|
145
|
+
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, enumerable)
|
146
|
+
if enum.hash_style?
|
147
|
+
enum.each { |entry| return entry[0] if yield(*entry) }
|
148
|
+
else
|
149
|
+
enum.each_with_index { |e, i| return i if yield(i, e) }
|
150
|
+
end
|
151
|
+
nil
|
152
|
+
end
|
153
|
+
|
154
|
+
def string_index(str, match)
|
155
|
+
str.index(match)
|
156
|
+
end
|
157
|
+
|
158
|
+
def index_value(enumerable, match)
|
159
|
+
enum = Puppet::Pops::Types::Iterable.asserted_iterable(self, enumerable)
|
160
|
+
if enum.hash_style?
|
161
|
+
enum.each { |entry| return entry[0] if entry[1] == match }
|
162
|
+
else
|
163
|
+
enum.each_with_index { |e, i| return i if e == match }
|
164
|
+
end
|
165
|
+
nil
|
166
|
+
end
|
167
|
+
end
|