Module: ForemanFogProxmox::ProxmoxInterfaces
- Included in:
- Proxmox
- Defined in:
- app/models/foreman_fog_proxmox/proxmox_interfaces.rb
Instance Method Summary collapse
- #check_cidr(nic_compute_attributes, v6, ip) ⇒ Object
- #cidr_prefix(nic_compute_attributes, v6 = false) ⇒ Object
- #cidr_prefix_method(v6) ⇒ Object
- #container?(host) ⇒ Boolean
- #container_nic_name_valid?(nic) ⇒ Boolean
- #dhcp?(nic_compute_attributes, v6 = false) ⇒ Boolean
- #editable_network_interfaces? ⇒ Boolean
- #host_interfaces_attrs(host) ⇒ Object
- #ip_s(v6) ⇒ Object
- #set_container_interface_name(_host, nic, index) ⇒ Object
- #set_ip(host, nic, nic_compute_attributes, v6 = false) ⇒ Object
- #set_mac(nic_compute_attributes, mac, type) ⇒ Object
- #set_nic_identifier(nic, index) ⇒ Object
- #to_boolean(value) ⇒ Object
- #to_cidr_method(v6) ⇒ Object
- #v6_s(v6) ⇒ Object
- #vm_type(host) ⇒ Object
Instance Method Details
#check_cidr(nic_compute_attributes, v6, ip) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 70 def check_cidr(nic_compute_attributes, v6, ip) valid = Fog::Proxmox::IpHelper.send(cidr_prefix_method(v6), cidr_prefix(nic_compute_attributes, v6)) ipv = "IPv#{v6 ? '6' : '4'}" max = v6 ? 128 : 32 checked = valid || ForemanFogProxmox::Value.empty?(ip) = format( 'Invalid Interface Proxmox CIDR %<ip>s. If %<ip>s is not empty, Proxmox CIDR prefix must be an integer between 0 and %<max>i.', ip: ipv, max: max ) raise ::Foreman::Exception, _() unless checked end |
#cidr_prefix(nic_compute_attributes, v6 = false) ⇒ Object
61 62 63 64 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 61 def cidr_prefix(nic_compute_attributes, v6 = false) attr_name = "cidr#{v6_s(v6)}" nic_compute_attributes[attr_name] if nic_compute_attributes.key?(attr_name) end |
#cidr_prefix_method(v6) ⇒ Object
66 67 68 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 66 def cidr_prefix_method(v6) "cidr#{v6_s(v6)}_prefix?".to_sym end |
#container?(host) ⇒ Boolean
44 45 46 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 44 def container?(host) vm_type(host) == 'lxc' end |
#container_nic_name_valid?(nic) ⇒ Boolean
48 49 50 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 48 def container_nic_name_valid?(nic) /^(eth)(\d+)$/.match?(nic.compute_attributes['name']) end |
#dhcp?(nic_compute_attributes, v6 = false) ⇒ Boolean
113 114 115 116 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 113 def dhcp?(nic_compute_attributes, v6 = false) attr_name = "dhcp#{v6_s(v6)}" nic_compute_attributes.key?(attr_name) ? to_boolean(nic_compute_attributes[attr_name]) : false end |
#editable_network_interfaces? ⇒ Boolean
25 26 27 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 25 def editable_network_interfaces? true end |
#host_interfaces_attrs(host) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 124 def host_interfaces_attrs(host) host.interfaces.select(&:physical?).each.with_index.reduce({}) do |hash, (nic, index)| set_nic_identifier(nic, index) set_container_interface_name(host, nic, index) if container?(host) ForemanFogProxmox::HashCollection.remove_empty_values(nic.compute_attributes) mac = nic.mac mac ||= nic.attributes['mac'] set_mac(nic.compute_attributes, mac, vm_type(host)) if mac.present? interface_compute_attributes = if host.compute_attributes['interfaces_attributes'] host.compute_attributes['interfaces_attributes'].select do |_k, v| v['id'] == nic.compute_attributes[:id] end else {} end unless interface_compute_attributes.empty? nic.compute_attributes.store(:_delete, interface_compute_attributes[interface_compute_attributes.keys[0]]['_delete']) end set_ip(host, nic, nic.compute_attributes) set_ip(host, nic, nic.compute_attributes, true) ForemanFogProxmox::HashCollection.remove_keys(nic.compute_attributes, ['dhcp', 'dhcp6', 'cidr', 'cidr6']) hash.merge(index.to_s => nic.compute_attributes) end end |
#ip_s(v6) ⇒ Object
85 86 87 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 85 def ip_s(v6) "ip#{v6_s(v6)}" end |
#set_container_interface_name(_host, nic, index) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 52 def set_container_interface_name(_host, nic, index) nic.compute_attributes['name'] = format('eth%<index>s', index: index) if nic.compute_attributes['name'].empty? unless container_nic_name_valid?(nic) raise ::Foreman::Exception, _(format('Invalid name interface[%<index>s]. Must be eth[n] with n integer >= 0', index: index)) end end |
#set_ip(host, nic, nic_compute_attributes, v6 = false) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 93 def set_ip(host, nic, nic_compute_attributes, v6 = false) ip = nic.send(ip_s(v6).to_sym) if container?(host) if dhcp?(nic_compute_attributes, v6) ip = 'dhcp' elsif !ForemanFogProxmox::Value.empty?(cidr_prefix(nic_compute_attributes, v6)) check_cidr(nic_compute_attributes, v6, ip) if ip ip = Fog::Proxmox::IpHelper.send(to_cidr_method(v6), nic.send(ip_s(v6).to_sym), cidr_prefix(nic_compute_attributes, v6)) end end end nic_compute_attributes[ip_s(v6).to_sym] = ip end |
#set_mac(nic_compute_attributes, mac, type) ⇒ Object
118 119 120 121 122 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 118 def set_mac(nic_compute_attributes, mac, type) mac_attr_name = { 'qemu' => :macaddr, 'lxc' => :hwaddr } mac_key = mac_attr_name[type] || 'mac' nic_compute_attributes[mac_key] = Net::Validations.normalize_mac(mac).upcase end |
#set_nic_identifier(nic, index) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 29 def set_nic_identifier(nic, index) nic.compute_attributes[:id] = format('net%<index>s', index: index) if nic.compute_attributes[:id].empty? unless Fog::Proxmox::NicHelper.nic?(nic.compute_attributes[:id]) raise ::Foreman::Exception, _(format('Invalid proxmox NIC id on interface[%<index>s]. Must be net[n] with n integer >= 0', index: index)) end end |
#to_boolean(value) ⇒ Object
109 110 111 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 109 def to_boolean(value) [1, true, '1', 'true'].include?(value) end |
#to_cidr_method(v6) ⇒ Object
89 90 91 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 89 def to_cidr_method(v6) "to_cidr#{v6_s(v6)}".to_sym end |
#v6_s(v6) ⇒ Object
81 82 83 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 81 def v6_s(v6) v6 ? '6' : '' end |
#vm_type(host) ⇒ Object
38 39 40 41 42 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 38 def vm_type(host) type = host.compute_attributes['type'] type ||= host.compute_attributes[:config_attributes].key?(:arch) ? 'lxc' : 'qemu' type end |