Skip to content

Commit

Permalink
Fix update interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanrobert committed May 8, 2019
1 parent 4800c8f commit 11b0136
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 74 deletions.
4 changes: 2 additions & 2 deletions app/helpers/proxmox_container_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def parse_container_vm(args)
logger.debug("parse_container_config(): #{parsed_config}")
parsed_vm = parsed_vm.merge(parsed_config).merge(cpu).merge(memory).merge(ostemplate)
interfaces_to_add.each { |interface| parsed_vm = parsed_vm.merge(interface) }
parsed_vm = parsed_vm.merge(delete: ForemanFogProxmox::ProxmoxArray.to_s(interfaces_to_delete)) unless interfaces_to_delete.empty?
parsed_vm = parsed_vm.merge(delete: interfaces_to_delete.join(',')) unless interfaces_to_delete.empty?
volumes.each { |volume| parsed_vm = parsed_vm.merge(volume) }
logger.debug("parse_container_vm(): #{parsed_vm}")
parsed_vm
Expand Down Expand Up @@ -133,7 +133,7 @@ def add_container_interface(interface_attributes, interfaces_to_delete, interfac
nic = {}
id = interface_attributes['id']
logger.debug("parse_container_interface(): id=#{id}")
delete = interface_attributes['_destroy'].to_i == 1
delete = interface_attributes['_delete'].to_i == 1
if delete
interfaces_to_delete.push(id.to_s)
else
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/proxmox_server_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def parse_server_vm(args)
logger.debug("parse_server_config(): #{parsed_config}")
parsed_vm = parsed_vm.merge(parsed_config).merge(cpu).merge(memory).merge(cdrom)
interfaces_to_add.each { |interface| parsed_vm = parsed_vm.merge(interface) }
parsed_vm = parsed_vm.merge(delete: ForemanFogProxmox::ProxmoxArray.to_s(interfaces_to_delete)) unless interfaces_to_delete.empty?
parsed_vm = parsed_vm.merge(delete: interfaces_to_delete.join(',')) unless interfaces_to_delete.empty?
volumes.each { |volume| parsed_vm = parsed_vm.merge(volume) }
logger.debug("parse_server_vm(): #{parsed_vm}")
parsed_vm
Expand Down Expand Up @@ -139,7 +139,7 @@ def add_server_interface(interface_attributes, interfaces_to_delete, interfaces_
nic = {}
id = interface_attributes['id']
logger.debug("add_server_interface(): id=#{id}")
delete = interface_attributes['_destroy'].to_i == 1
delete = interface_attributes['_delete'].to_i == 1
if delete
interfaces_to_delete.push(id.to_s)
else
Expand Down
37 changes: 37 additions & 0 deletions app/models/concerns/host_ext/proxmox/interfaces.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

# Copyright 2018 Tristan Robert

# This file is part of ForemanFogProxmox.

# ForemanFogProxmox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# ForemanFogProxmox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with ForemanFogProxmox. If not, see <http://www.gnu.org/licenses/>.

module HostExt::Proxmox::Interfaces
extend ActiveSupport::Concern
def update(attributes = {})
add_interfaces_to_compute_attributes(attributes)
super(attributes)
end
def add_interfaces_to_compute_attributes(attributes)
attributes['compute_attributes']['interfaces_attributes'] = {}
attributes['interfaces_attributes'].each { |index,interface_attributes| add_interface_to_compute_attributes(index,interface_attributes,attributes['compute_attributes']['interfaces_attributes']) }
end
def add_interface_to_compute_attributes(index,interface_attributes,compute_attributes)
compute_attributes[index] = {}
compute_attributes[index].store('id',interface_attributes['identifier'])
compute_attributes[index].store('_delete',interface_attributes['_destroy'])
compute_attributes[index].store('macaddr',interface_attributes['mac'])
compute_attributes[index].merge!(interface_attributes['compute_attributes'].reject { |k,_v| k == 'id' })
end
end
11 changes: 2 additions & 9 deletions app/models/foreman_fog_proxmox/proxmox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ def template(vmid)

def host_compute_attrs(host)
super.tap do |attrs|
host.compute_attributes['interfaces_attributes'] = attrs['interfaces_attributes']
ostype = host.compute_attributes['config_attributes']['ostype']
type = host.compute_attributes['type']
case type
Expand All @@ -146,6 +145,8 @@ def host_interfaces_attrs(host)
mac = nic.mac
mac = nic.attributes['mac'] unless mac
nic_compute_attributes.store(:macaddr, mac) if (mac && !mac.empty?)
interface_compute_attributes = host.compute_attributes['interfaces_attributes'].select { |_k,v| v['id'] == nic.identifier }
nic_compute_attributes.store(:_delete, interface_compute_attributes[interface_compute_attributes.keys[0]]['_delete']) unless interface_compute_attributes.empty?
nic_compute_attributes.store(:ip, nic.ip) if (nic.ip && !nic.ip.empty?)
nic_compute_attributes.store(:ip6, nic.ip6) if (nic.ip6 && !nic.ip6.empty?)
hash.merge(index.to_s => nic_compute_attributes)
Expand Down Expand Up @@ -359,13 +360,6 @@ def save_volumes(vm, volumes_attributes)
end
end

def update_interfaces(vm, attrs)
interfaces = nested_attributes_for :interfaces, attrs
interfaces.each do |interface|
logger.debug("update_interfaces interface=#{interface}")
end
end

def save_vm(uuid, new_attributes)
vm = find_vm_by_uuid(uuid)
templated = new_attributes['templated']
Expand All @@ -379,7 +373,6 @@ def save_vm(uuid, new_attributes)
config_attributes = config_attributes.reject { |_key,value| ForemanFogProxmox::Value.empty?(value) }
cdrom_attributes = parsed_attr.select { |_key,value| Fog::Proxmox::DiskHelper.cdrom?(value.to_s) }
config_attributes = config_attributes.reject { |key,_value| Fog::Proxmox::DiskHelper.disk?(key) }
update_interfaces(vm, parsed_attr.select { |key,_value| Fog::Proxmox::NicHelper.nic?(key) })
vm.update(config_attributes.merge(cdrom_attributes))
end
vm = find_vm_by_uuid(uuid)
Expand Down
1 change: 1 addition & 0 deletions lib/foreman_fog_proxmox/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class Engine < ::Rails::Engine
::ComputeResourcesController.send :include, ForemanFogProxmox::Controller::Parameters::ComputeResource
Fog::Proxmox::Compute::Node.send :include, FogExtensions::Proxmox::Node
::Host::Managed.send :include, Orchestration::Proxmox::Compute
::Host::Managed.send :include, HostExt::Proxmox::Interfaces
end

end
Expand Down
26 changes: 0 additions & 26 deletions lib/foreman_fog_proxmox/proxmox_array.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ProxmoxContainerHelperTest < ActiveSupport::TestCase
'type' => 'lxc',
'node_id' => 'pve',
'volumes_attributes' => { '0' => { '_delete' => '1', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824', 'mp' => '/opt/path' }},
'interfaces_attributes' => { '0' => { '_destroy' => '1', 'id' => 'net0', 'name' => 'eth0' } }
'interfaces_attributes' => { '0' => { '_delete' => '1', 'id' => 'net0', 'name' => 'eth0' } }
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ProxmoxServerHelperTest < ActiveSupport::TestCase
'cdrom' => 'image',
'cdrom_iso' => 'local-lvm:iso/debian-netinst.iso',
'volumes_attributes' => { '0' => { '_delete' => '1', 'controller' => 'scsi', 'device' => '0', 'storage' => 'local-lvm', 'size' => '1073741824' }},
'interfaces_attributes' => { '0' => { '_destroy' => '1', 'id' => 'net0', 'model' => 'virtio' } }
'interfaces_attributes' => { '0' => { '_delete' => '1', 'id' => 'net0', 'model' => 'virtio' } }
}
end

Expand Down
30 changes: 0 additions & 30 deletions test/unit/foreman_fog_proxmox/proxmox_array_test.rb

This file was deleted.

6 changes: 3 additions & 3 deletions test/unit/foreman_fog_proxmox/proxmox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ProxmoxTest < ActiveSupport::TestCase
ip = IPAddr.new(1, Socket::AF_INET).to_s
ip6 = Array.new(4) { '%x' % rand(16**4) }.join(':') + '::1'
physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :ip => ip, :ip6 => ip6)
host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :compute_attributes => {'type' => 'qemu'})
host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :compute_attributes => {'type' => 'qemu', 'interfaces_attributes' => { '0' => physical_nic }})
nic_attributes = @cr.host_interfaces_attrs(host).values.select(&:present?)
nic_attr = nic_attributes.first
assert_equal 'net0', nic_attr[:id]
Expand All @@ -110,7 +110,7 @@ class ProxmoxTest < ActiveSupport::TestCase
it "raises Foreman::Exception when server ostype does not match os family" do
operatingsystem = FactoryBot.build(:solaris)
physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :primary => true)
host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :operatingsystem => operatingsystem, :compute_attributes => { 'type' => 'qemu', 'config_attributes' => { 'ostype' => 'l26' } })
host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :operatingsystem => operatingsystem, :compute_attributes => { 'type' => 'qemu', 'config_attributes' => { 'ostype' => 'l26' }, 'interfaces_attributes' => { '0' => physical_nic } })
err = assert_raises Foreman::Exception do
@cr.host_compute_attrs(host)
end
Expand All @@ -119,7 +119,7 @@ class ProxmoxTest < ActiveSupport::TestCase

it "sets container hostname with host name" do
physical_nic = FactoryBot.build(:nic_base_empty, :identifier => 'net0', :primary => true)
host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :compute_attributes => { 'type' => 'lxc', 'config_attributes' => { 'hostname' => '' } })
host = FactoryBot.build(:host_empty, :interfaces => [physical_nic], :compute_attributes => { 'type' => 'lxc', 'config_attributes' => { 'hostname' => '' }, 'interfaces_attributes' => { '0' => {} } })
@cr.host_compute_attrs(host)
assert_equal host.name, host.compute_attributes['config_attributes']['hostname']
end
Expand Down

0 comments on commit 11b0136

Please sign in to comment.