module Fog::Proxmox::RngHelper

Convert RNG devices to and from Proxmox configuration strings

Constants

RNG_SOURCES

Public Class Methods

extract(rng) click to toggle source
# File lib/fog/proxmox/helpers/rng_helper.rb, line 51
def self.extract(rng)
  options = rng.split(',').each_with_object({}) do |option, values|
    key, value = option.split('=', 2)
    values[key.to_sym] = value&.sub(/\A"(.*)"\z/, '\1')
  end

  options.slice(:max_bytes, :period, :source)
end
flatten(rng) click to toggle source
# File lib/fog/proxmox/helpers/rng_helper.rb, line 26
def self.flatten(rng)
  return {} if rng.nil?

  options = rng.select do |key, value|
    %i[max_bytes period source].include?(key) && !value.nil? && value != ''
  end

  validate(options)

  { rng0: Fog::Proxmox::Hash.stringify(options) }
end
validate(rng) click to toggle source
# File lib/fog/proxmox/helpers/rng_helper.rb, line 38
def self.validate(rng)
  raise ArgumentError, 'RNG source is required and must be /dev/urandom, /dev/random, or /dev/hwrng' unless RNG_SOURCES.include?(rng[:source].to_s)

  %i[max_bytes period].each do |key|
    next if rng[key].nil? || rng[key] == ''
    next if rng[key].to_s.match?(/\A[0-9]+\z/)

    raise ArgumentError, "RNG #{key} must be a non-negative integer"
  end

  true
end