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

Fixes #37519 - Host form - Libvirt improvements #10190

Merged
merged 1 commit into from
Aug 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/compute_resources/foreman/model/libvirt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def networks

def template(id)
template = client.volumes.get(id)
raise Foreman::Exception.new(N_("Unable to find template %s"), id) unless template.persisted?
raise Foreman::Exception.new(N_("Unable to find template %s"), id) unless template&.persisted?
template
end

Expand Down
2 changes: 0 additions & 2 deletions app/views/compute_resources_vms/form/libvirt/_base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
{ :disabled => images.empty? || (params[:host] && params[:host][:provision_method] == 'build'),
:'data-url' => template_selected_compute_resource_path(compute_resource),
:onchange => 'tfm.computeResource.libvirt.imageSelected(this);',
:help_inline => :indicator,
:help_block => _("Image to use"),
:label => _('Image'), :label_size => "col-md-2"} %>
</div>

Expand Down
44 changes: 32 additions & 12 deletions webpack/assets/javascripts/compute_resource/libvirt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
/* eslint-disable jquery/no-val */
/* eslint-disable jquery/no-find */
/* eslint-disable jquery/no-parent */
/* eslint-disable jquery/no-text */
/* eslint-disable jquery/no-class */

import $ from 'jquery';
import { showSpinner } from '../foreman_tools';
import { translate as __ } from '../react_app/common/I18n';

export function networkSelected(item) {
const selected = $(item).val();
Expand Down Expand Up @@ -54,26 +57,43 @@ export function imageSelected(item) {

if (template) {
const url = $(item).attr('data-url');
// For some reason there are two help blocks
// so we need to select the correct one
const help = $('#image_selection .form-group > div > .help-block');
MariaAga marked this conversation as resolved.
Show resolved Hide resolved

showSpinner();

$.ajax({
type: 'post',
url,
data: `template_id=${template}`,
success(result) {
const capacity = $('#storage_volumes')
.children('.fields')
.find('[id$=capacity]')[0];

if (
parseInt(capacity.value.slice(0, -1), 10) <
parseInt(result.capacity, 10)
) {
capacity.value = `${result.capacity}G`;
help.empty();

const capacity = $(
'#host_compute_attributes_volumes_attributes_0_capacity'
);

const capacityInForm = parseInt(
capacity.attr('value').slice(0, -1),
10
);
const capacityFromImage = parseInt(result.capacity, capacityInForm);

if (capacityInForm < capacityFromImage) {
capacity.attr('value', `${capacityFromImage}G`);
}
$('#storage_volumes')
.children('.fields')
.find('[id$=format_type]')[0].value = 'qcow2';

$('#storage_volumes .fields')
.find('#host_compute_attributes_volumes_attributes_0_format_type')
.select2('val', 'qcow2');
},
error() {
help.html(
$('<span />')
.addClass('text-danger')
.text(__('Image not found'))
);
},
complete() {
// eslint-disable-next-line no-undef
Expand Down
Loading