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



100
101
102
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 100

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

.extract_ip(nic_value) ⇒ Object



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

def self.extract_ip(nic_value)
  if ip = self.ip_regexp.match(nic_value)
    ip[2]
  end
end

.extract_mac_address(nic_value) ⇒ Object



29
30
31
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 29

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



61
62
63
64
65
66
67
68
69
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 61

def self.extract_nic_id(nic_value)
  if self.has_model?(nic_value)
    nic_value.scan(self.model_regexp).first.first
  elsif self.has_name?(nic_value)
    nic_value.scan(self.name_regexp).first.first
  else
    nic_value.scan(self.nic_update_regexp).first.first
  end
end

.flatten(nic_hash) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 87

def self.flatten(nic_hash)
  nic_id = nic_hash[self.nic_name(nic_hash).to_sym]
  if nic_hash.has_key?(:macaddr)
    nic_value = nic_id + "=" + nic_hash[:macaddr]
  else
    nic_value = self.nic_name(nic_hash) + "=" + nic_id
  end
  options = nic_hash.reject { |key, _value| [self.nic_name(nic_hash).to_sym,:id,:macaddr].include? key.to_sym }
  nic_value += ',' unless options.empty?
  nic_value += Fog::Proxmox::Hash.stringify(options) unless options.empty?
  { "#{nic_hash[:id]}": nic_value }
end

.has_ip?(nic_value) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 57

def self.has_ip?(nic_value)
  nic_value.match?(self.ip_regexp)
end

.has_model?(nic_value) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 49

def self.has_model?(nic_value)
  nic_value.match?(self.model_regexp)
end

.has_name?(nic_value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.ip_regexpObject



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

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

.model_regexpObject



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

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

.name_regexpObject



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

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

.nic?(id) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 104

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

.nic_name(nic) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 77

def self.nic_name(nic)
  if nic.has_key?(:model)
    "model"
  elsif nic.has_key?(:name)
    "name"
  else
    ""
  end
end

.nic_update_regexpObject



45
46
47
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 45

def self.nic_update_regexp
  /^(\w+)[=]{1}([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2}).+/
end

.to_mac_adresses_array(nics) ⇒ Object



71
72
73
74
75
# File 'lib/fog/proxmox/helpers/nic_helper.rb', line 71

def self.to_mac_adresses_array(nics)
  addresses = []
  nics.each { |nic| addresses.push(nic.macaddr) }
  addresses
end