|
import $ from 'jquery';
|
|
import { activateDatatables } from './foreman_tools';
|
|
import { notify } from './foreman_toast_notifications';
|
|
|
|
export default {
|
|
ec2: require('./compute_resource/ec2'),
|
|
libvirt: require('./compute_resource/libvirt'),
|
|
openstack: require('./compute_resource/openstack'),
|
|
ovirt: require('./compute_resource/ovirt'),
|
|
vmware: require('./compute_resource/vmware'),
|
|
capacityEdit,
|
|
providerSelected,
|
|
testConnection
|
|
};
|
|
|
|
// Common functions used by one or more Compute Resource
|
|
|
|
// AJAX load vm listing
|
|
$(() => {
|
|
$('#vms, #images_list, #key_pairs_list').filter('[data-url]').each(function () {
|
|
const url = $(this).attr('data-url');
|
|
|
|
$(this).load(`${url} table`, function (response, status, xhr) {
|
|
if (status === 'error') {
|
|
$(this).html(
|
|
// eslint-disable-next-line no-undef
|
|
Jed.sprintf(__('There was an error listing VMs: %(status)s %(statusText)s'), {
|
|
status: xhr.status,
|
|
statusText: xhr.statusText
|
|
})
|
|
);
|
|
} else {
|
|
activateDatatables();
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
// eslint-disable-next-line max-statements
|
|
export function providerSelected(item) {
|
|
const computeConnection = $('#compute_connection');
|
|
const provider = $(item).val();
|
|
|
|
if (provider === '') {
|
|
computeConnection.hide();
|
|
$('[type=submit]').attr('disabled', true);
|
|
return false;
|
|
}
|
|
$('[type=submit]').attr('disabled', false);
|
|
const url = $(item).attr('data-url');
|
|
const data = `provider=${provider}`;
|
|
|
|
computeConnection.show();
|
|
computeConnection.load(`${url} div#compute_connection`, data, () => {
|
|
// eslint-disable-next-line no-undef
|
|
password_caps_lock_hint();
|
|
});
|
|
|
|
return false;
|
|
}
|
|
|
|
export function testConnection(item) {
|
|
let crId = $('form').data('id');
|
|
|
|
if (crId === undefined || crId === null) {
|
|
crId = '';
|
|
}
|
|
|
|
let password = $('input#compute_resource_password').val();
|
|
|
|
$('.tab-error').removeClass('tab-error');
|
|
$('#test_connection_indicator').show();
|
|
$.ajax({
|
|
type: 'put',
|
|
url: $(item).attr('data-url'),
|
|
data: `${$('form').serialize()}&cr_id=${crId}`,
|
|
success(result) {
|
|
let res = $(`<div>${result}</div>`);
|
|
|
|
$('#compute_connection').html(res.find('#compute_connection'));
|
|
$('#compute_connection').prepend(res.find('.alert'));
|
|
if (!$('#compute_resource_provider').prop('disabled')) {
|
|
$('#compute_resource_password').prop('disabled', false);
|
|
}
|
|
if (
|
|
$('.alert-danger', result).length === 0 &&
|
|
$('#compute_connection .has-error', result).length === 0
|
|
) {
|
|
notify({ message: `<p>${__('Test connection was successful')}</p>`, type: 'success' });
|
|
}
|
|
},
|
|
error({ statusText }) {
|
|
notify({
|
|
message: `<p>${__('An error occurred while testing the connection: ')}${statusText}</p>`,
|
|
type: 'danger'
|
|
});
|
|
},
|
|
complete(result) {
|
|
// we need to restore the password field as it is not sent back from the server.
|
|
$('input#compute_resource_password').val(password);
|
|
$('#test_connection_indicator').hide();
|
|
// eslint-disable-next-line no-undef
|
|
reloadOnAjaxComplete('#test_connection_indicator');
|
|
}
|
|
});
|
|
}
|
|
|
|
export function capacityEdit(element) {
|
|
let buttons = $(element).closest('.fields').find('button[name=allocation_radio_btn].btn.active');
|
|
|
|
if (buttons.length > 0 && $(buttons[0]).text() === 'Full') {
|
|
let allocation = $(element).closest('.fields').find('[id$=allocation]')[0];
|
|
|
|
allocation.value = element.value;
|
|
}
|
|
return false;
|
|
}
|