Revision e52bc50f
Added by Adam Ruzicka about 8 years ago
app/controllers/api/v2/compute_resources_controller.rb | ||
---|---|---|
param :tenant, String, :desc => N_("for OpenStack only")
|
||
param :server, String, :desc => N_("for VMware")
|
||
param :set_console_password, :bool, :desc => N_("for Libvirt and VMware only")
|
||
param :display_type, %w(VNC SPICE), :desc => N_('for Libvirt only')
|
||
param_group :taxonomies, ::Api::V2::BaseController
|
||
end
|
||
end
|
app/models/compute_resource.rb | ||
---|---|---|
encrypts :password
|
||
|
||
attr_accessible :name, :provider, :description, :url, :set_console_password,
|
||
:user, :password
|
||
:user, :password, :display_type
|
||
|
||
validates_lengths_from_database
|
||
|
||
... | ... | |
self.attrs[:setpw] = nil
|
||
end
|
||
|
||
# this method is overwritten for Libvirt
|
||
def display_type=(_)
|
||
end
|
||
|
||
# this method is overwritten for Libvirt
|
||
def display_type
|
||
nil
|
||
end
|
||
|
||
def compute_profile_for(id)
|
||
compute_attributes.find_by_compute_profile_id(id)
|
||
end
|
app/models/compute_resources/foreman/model/libvirt.rb | ||
---|---|---|
class Libvirt < ComputeResource
|
||
include ComputeResourceConsoleCommon
|
||
|
||
ALLOWED_DISPLAY_TYPES = %w(vnc spice)
|
||
|
||
validates :url, :format => { :with => URI.regexp }
|
||
validates :display_type, :inclusion => { :in => ALLOWED_DISPLAY_TYPES }
|
||
|
||
attr_accessible :display_type, :uuid
|
||
|
||
... | ... | |
end
|
||
|
||
def display_type=(display)
|
||
self.attrs[:display] = display
|
||
self.attrs[:display] = display.downcase
|
||
end
|
||
|
||
def provided_attributes
|
||
... | ... | |
# Listen address cannot be updated while the guest is running
|
||
# When we update the display password, we pass the existing listen address
|
||
vm.update_display(:password => password, :listen => vm.display[:listen], :type => vm.display[:type])
|
||
WsProxy.start(:host => hypervisor.hostname, :host_port => vm.display[:port], :password => password).merge(:type => vm.display[:type].downcase, :name=> vm.name)
|
||
WsProxy.start(:host => hypervisor.hostname, :host_port => vm.display[:port], :password => password).merge(:type => vm.display[:type], :name=> vm.name)
|
||
rescue ::Libvirt::Error => e
|
||
if e.message =~ /cannot change listen address/
|
||
logger.warn e
|
||
... | ... | |
:memory => 768*Foreman::SIZE[:mega],
|
||
:nics => [new_nic],
|
||
:volumes => [new_volume],
|
||
:display => { :type => display_type.downcase,
|
||
:display => { :type => display_type,
|
||
:listen => Setting[:libvirt_default_console_address],
|
||
:password => random_password,
|
||
:port => '-1' }
|
db/migrate/20160404074723_downcase_display_types.rb | ||
---|---|---|
class DowncaseDisplayTypes < ActiveRecord::Migration
|
||
def up
|
||
Foreman::Model::Libvirt.all.each do |cr|
|
||
downcased = cr.display_type.downcase
|
||
if cr.display_type != downcased
|
||
cr.display_type = downcased
|
||
cr.save
|
||
end
|
||
end
|
||
end
|
||
end
|
test/functional/api/v2/compute_resources_controller_test.rb | ||
---|---|---|
test "should update boolean attribute set_console_password for Libvirt compute resource" do
|
||
cr = compute_resources(:one)
|
||
put :update, { :id => cr.id, :compute_resource => { :set_console_password => true } }
|
||
assert_response :success
|
||
cr.reload
|
||
assert_equal 1, cr.attrs[:setpw]
|
||
end
|
||
... | ... | |
test "should update boolean attribute set_console_password for VMware compute resource" do
|
||
cr = compute_resources(:vmware)
|
||
put :update, { :id => cr.id, :compute_resource => { :set_console_password => true } }
|
||
assert_response :success
|
||
cr.reload
|
||
assert_equal 1, cr.attrs[:setpw]
|
||
end
|
||
... | ... | |
test "should not update set_console_password to true for non-VMware or non-Libvirt compute resource" do
|
||
cr = compute_resources(:openstack)
|
||
put :update, { :id => cr.id, :compute_resource => { :set_console_password => true } }
|
||
assert_response :success
|
||
cr.reload
|
||
assert_nil cr.attrs[:setpw]
|
||
end
|
||
|
||
test "should not update display_type for non-Libvirt compute resource" do
|
||
cr = compute_resources(:openstack)
|
||
put :update, { :id => cr.id, :compute_resource => { :display_type => 'SPICE' } }
|
||
assert_response :success
|
||
cr.reload
|
||
assert_nil cr.attrs[:display]
|
||
end
|
||
end
|
test/unit/compute_resources/libvirt_test.rb | ||
---|---|---|
assert_equal nil, attrs[:memory]
|
||
end
|
||
end
|
||
|
||
describe '#display_type' do
|
||
let(:cr) { FactoryGirl.build(:libvirt_cr) }
|
||
|
||
test "default display type is 'vnc'" do
|
||
assert_nil cr.attrs[:display]
|
||
assert_equal 'vnc', cr.display_type
|
||
end
|
||
|
||
test "display type can be set" do
|
||
expected = 'spice'
|
||
cr.display_type = 'SPICE'
|
||
assert_equal expected, cr.attrs[:display]
|
||
assert_equal expected, cr.display_type
|
||
assert cr.valid?
|
||
end
|
||
|
||
test "don't allow wrong display type to be set" do
|
||
cr.display_type = 'teletype'
|
||
refute cr.valid?
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #14330 - Provide option in hammer to change display type for
libvirt compute resource