Project

General

Profile

« Previous | Next » 

Revision 2309e9e1

Added by Adam, Till - D0249540 over 7 years ago

fixes #18216 - volume size editable for existing VMs one

View differences:

app/models/compute_resource.rb
end
def update_required?(old_attrs, new_attrs)
old_attrs.merge(new_attrs) do |k,old_v,new_v|
update_required?(old_v, new_v) if old_v.is_a?(Hash)
return true unless old_v == new_v
old_attrs.deep_symbolize_keys.merge(new_attrs.deep_symbolize_keys) do |k, old_v, new_v|
if old_v.is_a?(Hash) && new_v.is_a?(Hash)
return true if update_required?(old_v, new_v)
elsif old_v.to_s != new_v.to_s
Rails.logger.debug "Scheduling compute instance update because #{k} changed it's value from '#{old_v}' (#{old_v.class}) to '#{new_v}' (#{new_v.class})"
return true
end
new_v
end
false
......
vm_attrs = vm.attributes rescue {}
vm_attrs = vm_attrs.reject{|k,v| k == :id }
if vm.respond_to?(:volumes)
volumes = vm.volumes || []
vm_attrs[:volumes_attributes] = Hash[volumes.each_with_index.map { |volume, idx| [idx.to_s, volume.attributes] }]
end
vm_attrs = set_vm_volumes_attributes(vm, vm_attrs)
vm_attrs
rescue ActiveRecord::RecordNotFound
logger.warn("VM with UUID '#{uuid}' not found on #{self}")
......
private
def set_vm_volumes_attributes(vm, vm_attrs)
if vm.respond_to?(:volumes)
volumes = vm.volumes || []
vm_attrs[:volumes_attributes] = Hash[volumes.each_with_index.map { |volume, idx| [idx.to_s, volume.attributes] }]
end
vm_attrs
end
def set_attributes_hash
self.attrs ||= {}
end
app/models/compute_resources/foreman/model/vmware.rb
client.servers.new opts
end
def save_vm(uuid, attr)
vm = find_vm_by_uuid(uuid)
vm.attributes.merge!(attr.deep_symbolize_keys)
#volumes are not part of vm.attributes so we have to set them seperately if needed
if attr.has_key?(:volumes_attributes)
vm.volumes.each do |vm_volume|
volume_attrs = attr[:volumes_attributes].values.detect {|vol| vol[:id] == vm_volume.id}
vm_volume.size_gb = volume_attrs[:size_gb]
end
end
vm.save
end
def destroy_vm(uuid)
find_vm_by_uuid(uuid).destroy :force => true
rescue ActiveRecord::RecordNotFound
......
true
end
def update_required?(old_attrs, new_attrs)
super(old_attrs.deep_merge(old_attrs) {|_,_,v| v.to_s}, new_attrs)
end
# === Power on
#
# Foreman will try and start this vm after clone in a seperate request.
......
return 'efi' if firmware_type == :uefi
'bios'
end
def set_vm_volumes_attributes(vm, vm_attrs)
volumes = vm.volumes || []
vm_attrs[:volumes_attributes] = Hash[volumes.each_with_index.map { |volume, idx| [idx.to_s, volume.attributes.merge(:size_gb => volume.size_gb)] }]
vm_attrs
end
end
end
app/models/concerns/orchestration/compute.rb
def compute_update_required?
return false unless compute_resource.supports_update? && !compute_attributes.nil?
old.compute_attributes = compute_resource.find_vm_by_uuid(uuid).attributes
compute_resource.update_required?(old.compute_attributes, compute_attributes.symbolize_keys)
old.compute_attributes = compute_resource.vm_compute_attributes_for(uuid)
compute_resource.update_required?(old.compute_attributes, compute_attributes)
end
def find_image
app/views/compute_resources_vms/form/vmware/_volume.html.erb
<%= text_f f, :name, :class => "col-md-2", :label => _("Name"), :label_size => "col-md-2", :disabled => !new_host %>
<%= text_f f, :size_gb,
:class => "col-md-2",
:label => _("Size (GB)"), :label_size => "col-md-2", :disabled => !new_host %>
:label => _("Size (GB)"), :label_size => "col-md-2" %>
<%= checkbox_f f, :thin, {
:label => _("Thin provision"), :label_size => "col-md-2", :disabled => !new_host},
"true",
bundler.d/vmware.rb
group :vmware do
gem 'fog-vsphere', '>= 1.6.0'
gem 'fog-vsphere', '>= 1.7.0'
end
test/models/compute_resource_test.rb
@vm = mock()
@vm.stubs(:attributes).returns(plain_attrs)
@cr = compute_resources(:vmware)
@cr = compute_resources(:ovirt)
@cr.stubs(:find_vm_by_uuid).returns(@vm)
vol1 = mock()
......
assert_equal '1', nic_attributes.first[:id]
end
end
context '#update_required?' do
let(:compute_resource) { compute_resources(:mycompute) }
test 'should not require an update if hashes are equal' do
old_attrs = {:a => 'b', 'c' => {:a => '1', :d => 3}}
new_attrs = {:a => 'b', :c => {'a' => 1}}
assert_equal false, compute_resource.update_required?(old_attrs, new_attrs)
end
test 'should require an update if hashes are different' do
old_attrs = {:a => 'b', 'c' => {:a => '1', :d => 3}}
new_attrs = {:a => 'b', :c => {'a' => 2}}
assert_equal true, compute_resource.update_required?(old_attrs, new_attrs)
end
end
end
test/models/compute_resources/vmware_test.rb
vm = mock('vm', :interfaces => [iface1, iface2])
assert_equal host, as_admin { cr.associated_host(vm) }
end
describe "vm_compute_attributes_for" do
before do
plain_attrs = {
:id => 'abc',
:cpus => 5
}
@vm = mock('vm')
@vm.stubs(:attributes).returns(plain_attrs)
@cr = compute_resources(:vmware)
@cr.stubs(:find_vm_by_uuid).returns(@vm)
vol1 = mock('vol1')
vol1.stubs(:attributes).returns({:vol => 1})
vol1.stubs(:size_gb).returns(4)
vol2 = mock('vol2')
vol2.stubs(:attributes).returns({:vol => 2})
vol2.stubs(:size_gb).returns(4)
@volumes = [
vol1,
vol2
]
end
test "returns vm attributes without id" do
@vm.stubs(:volumes).returns(@volumes)
expected_attrs = {
:cpus => 5,
:volumes_attributes => {
"0" => { :vol => 1, :size_gb => 4 },
"1" => { :vol => 2, :size_gb => 4 }
}
}
attrs = @cr.vm_compute_attributes_for('abc')
assert_equal expected_attrs, attrs
end
end
end

Also available in: Unified diff