module Fog::Proxmox::NicHelper

module NicHelper mixins

Constants

NICS_REGEXP

Public Class Methods

collect_nics(attributes) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 117
def self.collect_nics(attributes)
  attributes.select { |key| nic?(key.to_s) }
end
extract_ip(nic_value) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 125
def self.extract_ip(nic_value)
  ip_regexp.match(nic_value) do |ip|
    ip[2]
  end
end
extract_mac_address(nic_value) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 28
def self.extract_mac_address(nic_value)
  nic_value[/([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})/]
end
extract_nic_id(nic_value) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 60
def self.extract_nic_id(nic_value)
  if has_model?(nic_value)
    nic_value.scan(model_regexp).first.first
  elsif has_name?(nic_value)
    nic_value.scan(name_regexp).first.first
  else
    nic_value.scan(nic_update_regexp).first.first
  end
end
flatten(nic_hash) click to toggle source

Convert nic attributes hash into API Proxmox parameters string

# File lib/fog/proxmox/helpers/nic_helper.rb, line 100
def self.flatten(nic_hash)
  if nic_hash[:hwaddr].nil?
    nic_id = nic_hash[nic_name(nic_hash).to_sym]
    nic_value = set_mac(nic_id, nic_hash)
    options = nic_hash.reject do |key, _value|
      [nic_name(nic_hash).to_sym, :id, :hwaddr, :macaddr].include? key.to_sym
    end
    nic_value += ',' + Fog::Proxmox::Hash.stringify(options) unless options.empty?
  else
    options = nic_hash.reject do |key, _value|
      %i[id model macaddr].include? key.to_sym
    end
    nic_value = Fog::Proxmox::Hash.stringify(options) unless options.empty?
  end
  { "#{nic_hash[:id]}": nic_value }
end
has_ip?(nic_value) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 56
def self.has_ip?(nic_value)
  nic_value.match?(ip_regexp)
end
has_model?(nic_value) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 48
def self.has_model?(nic_value)
  nic_value.match?(model_regexp)
end
has_name?(nic_value) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 52
def self.has_name?(nic_value)
  nic_value.match?(name_regexp)
end
ip_regexp() click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 40
def self.ip_regexp
  %r{^(.+),{1}ip=([\d./]+),?(.+)?$}
end
model_regexp() click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 32
def self.model_regexp
  /^model=(\w+),.+/
end
name_regexp() click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 36
def self.name_regexp
  /^name=(\w+),.+/
end
nic?(id) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 121
def self.nic?(id)
  NICS_REGEXP.match(id) ? true : false
end
nic_name(nic) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 76
def self.nic_name(nic)
  if nic.has_key?(:model)
    'model'
  elsif nic.has_key?(:name)
    'name'
  else
    ''
  end
end
nic_update_regexp() click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 44
def self.nic_update_regexp
  /^(\w+)={1}([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}).+/
end
set_mac(nic_id, nic_hash) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 86
def self.set_mac(nic_id, nic_hash)
  mac_keys = %i[macaddr hwaddr]
  nic_value = ''
  if (nic_hash.keys & mac_keys).empty?
    nic_value = nic_name(nic_hash) + '=' + nic_id
  else
    mac_value = nic_hash[mac_keys[0]] if nic_hash.key?(mac_keys[0])
    mac_value ||= nic_hash[mac_keys[1]] if nic_hash.key?(mac_keys[1])
    nic_value = nic_id + '=' + mac_value
  end
  nic_value
end
to_mac_adresses_array(nics) click to toggle source
# File lib/fog/proxmox/helpers/nic_helper.rb, line 70
def self.to_mac_adresses_array(nics)
  addresses = []
  nics.each { |nic| addresses.push(nic.macaddr) }
  addresses
end