Puppet Function: swap_file_size_from_csv

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

Overview

swap_file_size_from_csv()Any

Given a csv of swap files and sizes, split by pipe (||), we can determine the size in bytes of the swapfile Will return false if the swapfile is not found in the csv Examples:

get_swap_file_size_from_csv('/mnt/swap.1','/mnt/swap.1||1019900,/mnt/swap.1||1019900')

Would return: 1019900

get_swap_file_size_from_csv('/mnt/swap.2','/mnt/swap.1||1019900,/mnt/swap.1||1019900')

Would return: false

Returns:

  • (Any)


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
# File 'lib/puppet/parser/functions/swap_file_size_from_csv.rb', line 7

newfunction(:swap_file_size_from_csv, type: :rvalue, doc: <<~EOS
  Given a csv of swap files and sizes, split by pipe (||), we can determine the size in bytes of the swapfile
  Will return false if the swapfile is not found in the csv
  *Examples:*
      get_swap_file_size_from_csv('/mnt/swap.1','/mnt/swap.1||1019900,/mnt/swap.1||1019900')
  Would return: 1019900
      get_swap_file_size_from_csv('/mnt/swap.2','/mnt/swap.1||1019900,/mnt/swap.1||1019900')
  Would return: false
EOS
) do |arguments|
  if arguments.size < 2
    raise(Puppet::ParseError, 'swap_file_size_from_csv(): Wrong number of arguments ' \
                              "given (#{arguments.size} for 2)")
  end
  raise(Puppet::ParseError, "swap_file_size_from_csv(): swapfile name but be a string (Got #{arguments[0].class}") unless arguments[0].is_a? String
  raise(Puppet::ParseError, "swap_file_size_from_csv(): Requires string to work with (Got #{arguments[1].class}") unless arguments[1].is_a? String

  lines = arguments[1].strip.split(',')

  swapfile_found = false

  lines.each do |swapfile_csv|
    swapfile_csv_array = swapfile_csv.split(',') # rubocop:todo Lint/UselessAssignment
    swapfile_name = swapfile_csv.split('||')[0]
    swapfile_size = swapfile_csv.split('||')[1]
    swapfile_found = swapfile_size if arguments[0] == swapfile_name
  end
  swapfile_found
end