Puppet Function: ganglia_validate_rras

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

Overview

ganglia_validate_rras()Any

The following values will pass:

  • { cf => ‘AVERAGE’, xff => 0.5, steps => 1, rows => 5856 }
  • [

    {
      cf => 'AVERAGE',
      xff => 0.5,
      steps => 1,
      rows => 5856
     },
     {
      cf => 'MAX',
      xff => 0.5,
      steps => 1,
      rows => 5856
     },
    

    ]

  • cf (consolidation function) must be AVERAGE | MIN | MAX | LAST

  • xff (xfiles factor) must be a float between 0 and 1

  • steps must be an integer

  • rows must be an integer greater than zero

If all hashes are not defined then compilation will fail

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppet/parser/functions/ganglia_validate_rras.rb', line 2

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

  * [{ cf => 'AVERAGE', xff => 0.5, steps => 1, rows => 5856 }]
  * [
      {
        cf => 'AVERAGE',
        xff => 0.5,
        steps => 1,
        rows => 5856
       },
       {
        cf => 'MAX',
        xff => 0.5,
        steps => 1,
        rows => 5856
       },
    ]

  * cf (consolidation function) must be AVERAGE | MIN | MAX | LAST
  * xff (xfiles factor) must be a float between 0 and 1
  * steps must be an integer
  * rows must be an integer greater than zero

  If all hashes are not defined then compilation will fail

  ENDHEREDOC

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

  # which must be an array
  function_validate_array(args)

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

  # which must contain only Hashes
  rras.each do |r|
    function_validate_hash([r])

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

    # and must contain the cf key
    unless r.key?('cf')
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash must contain a cf key'
    end
    # which is a string
    unless r['cf'].is_a?(String)
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash cf key must be a String of AVERAGE | MIN | MAX | LAST'
    end
    # and must be either AVERAGE, MIN, MAX or LAST
    unless r['cf'] == 'AVERAGE' || r['cf'] == 'MIN' || r['cf'] == 'MAX' || r['cf'] == 'LAST'
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash cf key must be a String of AVERAGE | MIN | MAX | LAST'
    end

    # validate values

    # and must contain the xff key
    unless r.key?('xff')
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash must contain an xff key'
    end
    r['xff'] = Float(r['xff'])
    # must be between 0 and 1
    unless (r['xff'] >= 0.0) && (r['xff'] <= 1.0)
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash xff key must be a Float or Integer between 0 and 1'
    end

    # and must contain the steps key
    unless r.key?('steps')
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash must contain a steps key'
    end

    # convert steps to integer for checking
    r['steps'] = begin
                   Integer(r['steps'])
                 rescue
                   nil
                 end

    # steps must be an integer
    unless r['steps'].is_a?(Integer)
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash steps key must be an Integer greater than 0'
    end

    # and greater than zero
    unless r['steps'] > 0
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash steps key must be an Integer greater than 0'
    end

    # and must contain the rows key
    unless r.key?('rows')
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash must contain a rows key'
    end

    # convert rows to integer for checking
    r['rows'] = begin
                  Integer(r['rows'])
                rescue
                  nil
                end

    # rows must be an integer
    unless r['rows'].is_a?(Integer)
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash rows key must be an Integer greater than 0'
    end

    # and greater than zero
    unless r['rows'] > 0
      raise Puppet::ParseError, 'ganglia_validate_rras(): nested Hash rows key must be an Integer greater than 0'
    end

    # any other keys should be rejected
    extras = r.keys - ['cf', 'xff', 'steps', 'rows']
    unless extras.empty?
      raise Puppet::ParseError, "ganglia_validate_rras(): nested Hash contains unknown keys (#{extras.sort.join(' ')})"
    end
  end
end