Module: Fog::Proxmox::NicHelper

Defined in:
lib/fog/proxmox/helpers/nic_helper.rb

Overview

module NicHelper mixins

Constant Summary collapse

NICS_REGEXP =
/^(net)(\d+)/

Class Method Summary collapse

Class Method Details

.collect_nics(attributes) ⇒ Object



110
111
112
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 110

def self.collect_nics(attributes)
  attributes.select { |key| nic?(key.to_s) }
end

.extract_ip(nic_value) ⇒ Object



118
119
120
121
122
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 118

def self.extract_ip(nic_value)
  ip_regexp.match(nic_value) do |ip|
    ip[2]
  end
end

.extract_mac_address(nic_value) ⇒ Object



28
29
30
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
# 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) ⇒ Object

Convert nic attributes hash into API Proxmox parameters string



100
101
102
103
104
105
106
107
108
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 100

def self.flatten(nic_hash)
  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, :macaddr, :hwaddr].include? key.to_sym
  end
  nic_value += ',' + Fog::Proxmox::Hash.stringify(options) unless options.empty?
  { "#{nic_hash[:id]}": nic_value }
end

.has_ip?(nic_value) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# 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) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# 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) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 52

def self.has_name?(nic_value)
  nic_value.match?(name_regexp)
end

.ip_regexpObject



40
41
42
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 40

def self.ip_regexp
  %r{^(.+),{1}ip=([\d./]+),?(.+)?$}
end

.model_regexpObject



32
33
34
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 32

def self.model_regexp
  /^model=(\w+),.+/
end

.name_regexpObject



36
37
38
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 36

def self.name_regexp
  /^name=(\w+),.+/
end

.nic?(id) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 114

def self.nic?(id)
  NICS_REGEXP.match(id) ? true : false
end

.nic_name(nic) ⇒ Object



76
77
78
79
80
81
82
83
84
# 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_regexpObject



44
45
46
# 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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



70
71
72
73
74
# 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