Module: Puppet::Icinga2::Utils

Defined in:
lib/puppet_x/icinga2/utils.rb

Overview

Module with methods to parse Icinga 2 DSL config

Class Method Summary collapse

Class Method Details

.attribute_types(attr) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/puppet_x/icinga2/utils.rb', line 175

def self.attribute_types(attr)
  if %r{^[a-zA-Z0-9_]+$}.match?(attr)
    attr
  else
    "\"#{attr}\""
  end
end

.attributes(attrs, globals, consts, indent = 2) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/puppet_x/icinga2/utils.rb', line 301

def self.attributes(attrs, globals, consts, indent = 2)
  # globals (params.pp) and all keys of attrs hash itselfs must not quoted
  @constants = globals.concat(consts.keys) << 'name'

  # select all attributes and constants if there value is a hash
  @hash_attrs = attrs.merge(consts).select { |_x, y| y.is_a?(Hash) }.keys

  # initialize returned configuration
  config = ''

  attrs.each do |attr, value_frozen|
    value = value_frozen.dup
    if value.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
      value = value.unwrap
      value = '-:"' + value + '"' if value.is_a?(String) && !@constants.include?(value)
    end

    if %r{^(assign|ignore) where$}.match?(attr)
      value.each do |x|
        config += "%{ind}%{att} %{expr}\n" % { ind: ' ' * indent, att: attr, expr: parse(x) } if x
      end
    elsif attr == 'vars'
      if value.is_a?(Hash)
        # delete pair of key '+' because a merge at this point is not allowed
        value.delete('+')
        config += process_hash(value, indent + 2, 1, '%{ind}%{att}.' % { ind: ' ' * indent, att: attr })
      elsif value.is_a?(Array)
        value.each do |item_frozen|
          item = item_frozen.dup
          if item.is_a?(String)
            config += "%{ind}%{att} += %{lst}\n" % { ind: ' ' * indent, att: attr, lst: item.sub(%r{^[\+,-]\s+}, '') }
          else
            item.delete('+')
            config += if item.empty?
                        "%{ind}%{att} += {}\n" % { ind: ' ' * indent, att: attr }
                      else
                        process_hash(item, indent + 2, 1, '%{ind}%{att}.' % { ind: ' ' * indent, att: attr })
                      end
          end
        end
      else
        op = '+' if %r{^\+\s+}.match?(value)
        config += "%{ind}%{att} #{op}= %{val}\n" % { ind: ' ' * indent, att: attr, val: parse(value.sub(%r{^\+\s+}, '')) }
      end
    else
      config += if value.is_a?(Hash)
                  op = '+' if value.delete('+')
                  if !value.empty?
                    "%{ind}%{att} #{op}= {\n%{lst}%{blnk}}\n" % { ind: ' ' * indent, att: attr, lst: process_hash(value, indent + 2), blnk: ' ' * indent }
                  else
                    "%{ind}%{att} #{op}= {}\n" % { ind: ' ' * indent, att: attr }
                  end
                elsif value.is_a?(Array)
                  op = value.delete_at(0) if value[0] == '+' || value[0] == '-'
                  "%{ind}%{att} #{op}= [ %{lst}]\n" % { ind: ' ' * indent, att: attr, lst: process_array(value) }
                elsif value.to_s =~ %r{^([\+,-])\s+}
                  # String: attr = '+ config' -> attr += config
                  "%{ind}%{att} #{Regexp.last_match(1)}= %{expr}\n" % { ind: ' ' * indent, att: attr, expr: parse(value.sub(%r{^[\+,-]\s+}, '')) }
                else
                  "%{ind}%{att} = %{expr}\n" % { ind: ' ' * indent, att: attr, expr: parse(value) }
                end
    end
  end
  config
end

.parse(row) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/puppet_x/icinga2/utils.rb', line 183

def self.parse(row)
  row = row.to_s
  result = ''

  # parser is disabled
  if row =~ %r{^-:(.*)$}m
    return Regexp.last_match(1)
  end

  case row
  when %r{^\{{2}(.+)\}{2}$}m
    # scan function
    result += '{{%{expr}}}' % { expr: Regexp.last_match(1) }
  when %r{^(.+)\s([\+-]|\*|\/|==|!=|&&|\|{2}|in)\s\{{2}(.+)\}{2}$}m
    # scan expression + function (function should contain expressions, but we donno parse it)
    result += '%{expr} %{op} {{%{fct}}}' % { expr: parse(Regexp.last_match(1)), op: Regexp.last_match(2), fct: Regexp.last_match(3) }
  when %r{^(.+)\s([\+-]|\*|\/|==|!=|&&|\|{2}|in)\s(.+)$}
    # scan expression
    result += '%{expr1} %{op} %{expr2}' % { expr1: parse(Regexp.last_match(1)), op: Regexp.last_match(2), expr2: parse(Regexp.last_match(3)) }
  when %r{^(.+)\((.*)$}
    result += '%{fct}(%{param}' % { fct: Regexp.last_match(1), param: Regexp.last_match(2).split(',').map { |x| parse(x.lstrip) }.join(', ') }
  when %r{^(.*)\)(.+)?$}
    # closing bracket ) with optional access of an attribute e.g. '.arguments'
    result += '%{param})%{expr}' % { param: Regexp.last_match(1).split(',').map { |x| parse(x.lstrip) }.join(', '), expr: Regexp.last_match(2) }
  when %r{^\((.*)$}
    result += '(%{expr}' % { expr: parse(Regexp.last_match(1)) }
  when %r{^\s*\[\s*(.*)\s*\]\s?(.+)?$}
    # parse array
    result += '[ %{lst}]' % { lst: process_array(Regexp.last_match(1).split(',')) }
    result += ' %{expr}' % { expr: parse(Regexp.last_match(2)) } if Regexp.last_match(2)
  when %r{^\s*\{\s*(.*)\s*\}\s?(.+)?$}
    # parse hash
    result += "{\n%{expr}}" % { expr: process_hash(Hash[Regexp.last_match(1).gsub(%r{\s*=>\s*|\s*,\s*}, ',').split(',').each_slice(2).to_a]) }
    result += ' %{expr}' % { expr: parse(Regexp.last_match(2)) } if Regexp.last_match(2)
  else
    result += value_types(row.to_s.strip)
  end
  result.gsub(%r{" in "}, ' in ')
end

.process_array(items, indent = 2) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/puppet_x/icinga2/utils.rb', line 223

def self.process_array(items, indent = 2)
  result = ''
  items.each do |value_frozen|
    value = value_frozen.dup
    if value.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
      value = value.unwrap
      value = '-:"' + value + '"' if value.is_a?(String)
    end
    if value.is_a?(Hash)
      result += "\n%{ind1}{\n%{expr}%{ind2}}, " % { ind1: ' ' * indent, expr: process_hash(value, indent + 2), ind2: ' ' * indent }
    elsif value.is_a?(Array)
      result += '[ %{lst}], ' % { lst: process_array(value, indent + 2) }
    elsif value
      result += '%{expr}, ' % { expr: parse(value) }
    end
  end
  result
end

.process_hash(attrs, indent = 2, level = 3, prefix = ' ' * indent) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/puppet_x/icinga2/utils.rb', line 242

def self.process_hash(attrs, indent = 2, level = 3, prefix = ' ' * indent)
  result = ''
  attrs.each do |attr, value_frozen|
    value = value_frozen.dup
    if value.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
      value = value.unwrap
      value = '-:"' + value + '"' if value.is_a?(String)
    end
    result += if value.is_a?(Hash)
                op = '+' if value.delete('+')
                if value.empty?
                  case level
                  when 1 then
                    "%{pre}%{att} #{op}= {}\n" % { pre: prefix, att: attribute_types(attr) }
                  when 2 then
                    "%{pre}[\"%{att}\"] #{op}= {}\n" % { pre: prefix, att: attr }
                  else
                    "%{pre}%{att} #{op}= {}\n" % { pre: prefix, att: attribute_types(attr) }
                  end
                else
                  case level
                  when 1 then
                    process_hash(value, indent, 2, '%{pre}%{att}' % { pre: prefix, att: attr })
                  when 2 then
                    "%{pre}[\"%{att}\"] #{op}= {\n%{lst}%{ind}}\n" % { pre: prefix, att: attr, lst: process_hash(value, indent), ind: ' ' * (indent - 2) }
                  else
                    "%{pre}%{att} #{op}= {\n%{lst}%{ind}}\n" % { pre: prefix, att: attribute_types(attr), lst: process_hash(value, indent + 2), ind: ' ' * indent }
                  end
                end
              elsif value.is_a?(Array)
                op = value.delete_at(0) if value[0] == '+' || value[0] == '-'
                case level
                when 2 then
                  "%{pre}[\"%{att}\"] #{op}= [ %{lst}]\n" % { pre: prefix, att: attribute_types(attr), lst: process_array(value) }
                else
                  "%{pre}%{att} #{op}= [ %{lst}]\n" % { pre: prefix, att: attribute_types(attr), lst: process_array(value) }
                end
              else
                # String: attr = '+ value' -> attr += 'value'
                if value.to_s =~ %r{^([\+,-])\s+}
                  operator = "#{Regexp.last_match(1)}="
                  value = value.sub(%r{^[\+,-]\s+}, '')
                else
                  operator = '='
                end
                if value != :nil
                  if level == 3
                    "%{pre}%{att} #{operator} %{val}\n" % { pre: prefix, att: attribute_types(attr), val: parse(value) }
                  elsif level > 1
                    "%{pre}[\"%{att}\"] #{operator} %{val}\n" % { pre: prefix, att: attr, val: parse(value) }
                  else
                    "%{pre}%{att} #{operator} %{val}\n" % { pre: prefix, att: attr, val: parse(value) }
                  end
                end
              end
  end
  result
end

.value_types(value) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/puppet_x/icinga2/utils.rb', line 166

def self.value_types(value)
  if value.match?(%r{^(-?\d+\.?\d*[dhms]?|true|false|null|\{{2}.*\}{2})$|^!?(host|service|user)\.}) ||
     @constants.index { |x| @hash_attrs.include?(x) ? value =~ %r{^!?(#{x})(\..+$|$)} : value =~ %r{^!?#{x}$} }
    value
  else
    value.dump
  end
end