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
69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 69 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
60 61 62 63 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 60 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
65 66 67 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 65 def cidr_prefix_method(v6) "cidr#{v6_s(v6)}_prefix?".to_sym end |
#container?(host) ⇒ Boolean
43 44 45 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 43 def container?(host) vm_type(host) == 'lxc' end |
#container_nic_name_valid?(nic) ⇒ Boolean
47 48 49 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 47 def container_nic_name_valid?(nic) /^(eth)(\d+)$/.match?(nic.compute_attributes['name']) end |
#dhcp?(nic_compute_attributes, v6 = false) ⇒ Boolean
112 113 114 115 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 112 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
24 25 26 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 24 def editable_network_interfaces? true end |
#host_interfaces_attrs(host) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 123 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
84 85 86 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 84 def ip_s(v6) "ip#{v6_s(v6)}" end |
#set_container_interface_name(_host, nic, index) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 51 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
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 92 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
117 118 119 120 121 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 117 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
28 29 30 31 32 33 34 35 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 28 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
108 109 110 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 108 def to_boolean(value) [1, true, '1', 'true'].include?(value) end |
#to_cidr_method(v6) ⇒ Object
88 89 90 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 88 def to_cidr_method(v6) "to_cidr#{v6_s(v6)}".to_sym end |
#v6_s(v6) ⇒ Object
80 81 82 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 80 def v6_s(v6) v6 ? '6' : '' end |
#vm_type(host) ⇒ Object
37 38 39 40 41 |
# File 'app/models/foreman_fog_proxmox/proxmox_interfaces.rb', line 37 def vm_type(host) type = host.compute_attributes['type'] type ||= host.compute_attributes[:config_attributes].key?(:arch) ? 'lxc' : 'qemu' type end |