Puppet Function: ganglia_validate_clusters

Defined in:
lib/puppet/parser/functions/ganglia_validate_clusters.rb
Function type:
Ruby 3.x API

Overview

ganglia_validate_clusters()Any

The following values will pass:

  • { ‘name’ => ‘my cluster’, ‘address’ => ‘localhost’ }
  • { ‘name’ => ‘my cluster’, ‘address’ => ‘localhost’, ‘polling_interval’ => 10 }
  • [

    'name'    => 'foo',
    'address' => [
      'foo1.example.org',
      'foo2.example.org',
      'foo3.example.org',
    ],
    

    ,

    'name'             => 'bar',
    'address'          => [
      'bar1.example.org',
      'bar2.example.org',
      'bar3.example.org'
    ],
    'polling_interval' => 42,
    

    ,

    'name'    => 'baz',
    'address' => [
      'baz1.example.org',
      'baz2.example.org',
      'baz3.example.org',
    ],
    

    ,

]

The following values will fail, causing compilation to abort:

  • true

  • false

  • {}

  • ‘foo’

  • undef

  • ‘foo’, ‘bar’
  • {}, {}
  • { ‘address’ => ‘localhost’ }
  • { ‘name’ => [‘my cluster’], ‘address’ => ‘localhost’ }
  • { ‘name’ => ‘my cluster’ }
  • { ‘name’ => ‘my cluster’, ‘address’ => => 1 }
  • { ‘name’ => ‘my cluster’, ‘address’ => ‘localhost’, ‘polling_interval’ => [ 10

    }]

  • { ‘name’ => ‘my cluster’, ‘address’ => ‘localhost’, ‘polling_interval’ => 10, ‘foo’ => 1, ‘bar’ => 2 }

Returns:

  • (Any)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/parser/functions/ganglia_validate_clusters.rb', line 2

newfunction(:ganglia_validate_clusters, type: :rvalue, doc: <<-'ENDHEREDOC') do |args|
  The following values will pass:

  * [{ 'name' => 'my cluster', 'address' => 'localhost' }]
  * [{ 'name' => 'my cluster', 'address' => 'localhost', 'polling_interval' => 10 }]
  * [
    {
      'name'    => 'foo',
      'address' => [
        'foo1.example.org',
        'foo2.example.org',
        'foo3.example.org',
      ],
    },
    {
      'name'             => 'bar',
      'address'          => [
        'bar1.example.org',
        'bar2.example.org',
        'bar3.example.org'
      ],
      'polling_interval' => 42,
    },
    {
      'name'    => 'baz',
      'address' => [
        'baz1.example.org',
        'baz2.example.org',
        'baz3.example.org',
      ],
    },
  ]

  The following values will fail, causing compilation to abort:

  * true
  * false
  * {}
  * 'foo'
  * undef
  * []
  * ['foo', 'bar']
  * [{}, {}]
  * [{ 'address' => 'localhost' }]
  * [{ 'name' => ['my cluster'], 'address' => 'localhost' }]
  * [{ 'name' => 'my cluster' }]
  * [{ 'name' => 'my cluster', 'address' => {'a' => 1} }]
  * [{ 'name' => 'my cluster', 'address' => 'localhost', 'polling_interval' => [ 10 ] }]
  * [{ 'name' => 'my cluster', 'address' => 'localhost', 'polling_interval' => 10, 'foo' => 1, 'bar' => 2 }]

  ENDHEREDOC

  # we accept only one arg
  unless args.length == 1
    raise Puppet::ParseError, "ganglia_validate_clusters(): wrong number of arguments (#{args.length}; must be 1)"
  end

  # which must be an array
  # NOTE: This should likely be removed, as it's validated prior to getting here (param type validation)
  function_validate_array(args)

  # that is not empty
  clusters = args[0]
  if clusters.empty?
    raise Puppet::ParseError, 'ganglia_validate_clusters(): passed Array may not be empty'
  end

  # which must contain only Hashes
  clusters.each do |c|
    function_validate_hash([c])

    # that are not empty
    if c.empty?
      raise Puppet::ParseError, 'ganglia_validate_clusters(): nested Hash may not be empty'
    end

    # and must contain the name key
    unless c.key?('name')
      raise Puppet::ParseError, 'ganglia_validate_clusters(): nested Hash must contain a name key'
    end
    # which is a string
    unless c['name'].is_a?(String)
      raise Puppet::ParseError, 'ganglia_validate_clusters(): nested Hash name key must be a String'
    end

    # and must contain the address key
    unless c.key?('address')
      raise Puppet::ParseError, 'ganglia_validate_clusters(): nested Hash must contain an address key'
    end
    # which is a string or an array
    unless c['address'].is_a?(String) || c['address'].is_a?(Array)
      raise Puppet::ParseError, 'ganglia_validate_clusters(): nested Hash address key must be a String or Array'
    end

    # if the optional polling_interval key is set
    if c.key?('polling_interval')
      # it must be a string (int really)
      unless c['polling_interval'].is_a?(String) || c['polling_interval'].is_a?(Integer)
        raise Puppet::ParseError, 'ganglia_validate_clusters(): nested Hash polling_interval key must be a String or Integer'
      end
    end

    # any other keys should be rejected
    extras = c.keys - ['name', 'address', 'polling_interval']
    unless extras.empty?
      raise Puppet::ParseError, "ganglia_validate_clusters(): nested Hash contains unknown keys (#{extras.sort.join(' ')})"
    end
  end
end