Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.1 oVirt cherry-picks #7820

Merged
merged 3 commits into from Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions app/controllers/api/v2/compute_resources_controller.rb
Expand Up @@ -46,7 +46,7 @@ def show
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 and oVirt only')
param :keyboard, ComputeResource::ALLOWED_KEYBOARD_LAYOUTS, :desc => N_('for oVirt only')
param :keyboard_layout, ComputeResource::ALLOWED_KEYBOARD_LAYOUTS, :desc => N_('for oVirt only')
param :caching_enabled, :bool, :desc => N_('enable caching, for VMware only')
param :project, String, :desc => N_("Project id for GCE only")
param :email, String, :desc => N_("Email for GCE only")
Expand All @@ -57,7 +57,7 @@ def show
end

def change_datacenter_to_uuid(datacenter)
if @compute_resource.respond_to?(:get_datacenter_uuid) && datacenter.present? && !Foreman.is_uuid?(datacenter)
if @compute_resource.respond_to?(:get_datacenter_uuid) && datacenter.present?
@compute_resource.test_connection
@compute_resource.get_datacenter_uuid(datacenter)
end
Expand All @@ -68,12 +68,11 @@ def change_datacenter_to_uuid(datacenter)

def create
begin
@compute_resource = ComputeResource.new_provider(compute_resource_params)
@compute_resource = ComputeResource.new_provider(compute_resource_params.except(:datacenter))
rescue Foreman::Exception => e
render_exception(e, :status => :unprocessable_entity)
return
end

datacenter = change_datacenter_to_uuid(compute_resource_params[:datacenter])
@compute_resource.datacenter = datacenter if datacenter.present?

Expand Down
18 changes: 12 additions & 6 deletions app/models/compute_resources/foreman/model/ovirt.rb
Expand Up @@ -11,6 +11,7 @@ class Ovirt < ComputeResource
validates :keyboard_layout, :inclusion => { :in => ALLOWED_KEYBOARD_LAYOUTS }
validates :user, :password, :presence => true
after_validation :connect, :update_available_operating_systems unless Rails.env.test?
before_save :validate_quota

alias_attribute :datacenter, :uuid

Expand Down Expand Up @@ -198,10 +199,16 @@ def datacenters(options = {})
client.datacenters(options).map { |dc| [dc[:name], dc[:id]] }
end

def get_datacenter_uuid(name)
datacenter_uuid = datacenters.select { |dc| dc[0] == name }
raise ::Foreman::Exception.new(N_('Datacenter was not found')) if datacenter_uuid.empty?
datacenter_uuid.first[1]
def get_datacenter_uuid(datacenter)
return @datacenter_uuid if @datacenter_uuid
if Foreman.is_uuid?(datacenter)
@datacenter_uuid = datacenter
else
@datacenter_uuid = datacenters.select { |dc| dc[0] == datacenter }
raise ::Foreman::Exception.new(N_('Datacenter was not found')) if @datacenter_uuid.empty?
@datacenter_uuid = @datacenter_uuid.first[1]
end
@datacenter_uuid
end

def editable_network_interfaces?
Expand Down Expand Up @@ -481,7 +488,7 @@ def nictypes
]
end

def validate_quota(client)
def validate_quota
if attrs[:ovirt_quota_id].nil?
attrs[:ovirt_quota_id] = client.quotas.first.id
else
Expand Down Expand Up @@ -512,7 +519,6 @@ def client
:api_version => use_v4? ? 'v4' : 'v3'
)
client.datacenters
validate_quota(client)
@client = client
rescue => e
if e.message =~ /SSL_connect.*certificate verify failed/ ||
Expand Down
4 changes: 4 additions & 0 deletions test/controllers/hosts_controller_test.rb
Expand Up @@ -1344,8 +1344,12 @@ class Host::Valid < Host::Managed; end
end

test "#host update shouldn't diassociate from VM" do
require 'fog/ovirt/models/compute/quota'
hostgroup = FactoryBot.create(:hostgroup, :with_environment, :with_subnet, :with_domain, :with_os)
compute_resource = compute_resources(:ovirt)
quota = Fog::Ovirt::Compute::Quota.new({ :id => '1', :name => "Default" })
client_mock = mock.tap { |m| m.stubs(datacenters: [], quotas: [quota]) }
compute_resource.stubs(:client).returns(client_mock)
compute_resource.update(:locations => hostgroup.locations, :organizations => hostgroup.organizations)
host = FactoryBot.create(:host, :hostgroup => hostgroup, :compute_resource => compute_resource)
host_attributes = host.attributes
Expand Down
12 changes: 8 additions & 4 deletions test/models/compute_resources/ovirt_test.rb
Expand Up @@ -62,6 +62,7 @@ class Foreman::Model:: OvirtTest < ActiveSupport::TestCase
end
@compute_resource = FactoryBot.build(:ovirt_cr)
@host = FactoryBot.build(:host, :mac => 'ca:d0:e6:32:16:97')
@quota = Fog::Ovirt::Compute::Quota.new({ :id => '1', :name => "Default" })
end

it 'maps operating system to ovirt operating systems' do
Expand Down Expand Up @@ -90,13 +91,15 @@ class Foreman::Model:: OvirtTest < ActiveSupport::TestCase
it 'caches the operating systems in the compute resource' do
client_mock = mock.tap { |m| m.stubs(:operating_systems).returns(@ovirt_oses) }
@compute_resource.stubs(:client).returns(client_mock)
client_mock.stubs(:quotas).returns([@quota])
assert @compute_resource.supports_operating_systems?
assert_equal @os_hashes, @compute_resource.available_operating_systems
end

it 'handles a case when the operating systems endpoint is missing' do
client_mock = mock.tap { |m| m.stubs(:operating_systems).raises(Fog::Ovirt::Errors::OvirtEngineError, StandardError.new('404')) }
@compute_resource.stubs(:client).returns(client_mock)
client_mock.stubs(:quotas).returns([@quota])
refute @compute_resource.supports_operating_systems?
end
end
Expand Down Expand Up @@ -145,26 +148,27 @@ class Foreman::Model:: OvirtTest < ActiveSupport::TestCase
@compute_resource = FactoryBot.build(:ovirt_cr)
@quota = Fog::Ovirt::Compute::Quota.new({ :id => '1', :name => 'Default' })
@client_mock = mock.tap { |m| m.stubs(datacenters: [], quotas: [@quota]) }
@compute_resource.stubs(:client).returns(@client_mock)
end

test 'quota validation - id entered' do
@compute_resource.ovirt_quota = '1'
assert_equal('1', @compute_resource.validate_quota(@client_mock))
assert_equal('1', @compute_resource.validate_quota)
end

test 'quota validation - name entered' do
@compute_resource.ovirt_quota = 'Default'
assert_equal('1', @compute_resource.validate_quota(@client_mock))
assert_equal('1', @compute_resource.validate_quota)
end

test 'quota validation - nothing entered' do
assert_equal('1', @compute_resource.validate_quota(@client_mock))
assert_equal('1', @compute_resource.validate_quota)
end

test 'quota validation - name entered' do
@compute_resource.ovirt_quota = 'Default2'
assert_raise Foreman::Exception do
@compute_resource.validate_quota(@client_mock)
@compute_resource.validate_quota
end
end
end
Expand Down