Update source into github
This commit is contained in:
@@ -0,0 +1,569 @@
|
||||
<script>
|
||||
const productId = {{ $product->id }};
|
||||
const vmId = '{{ $product->vmware_id }}';
|
||||
|
||||
// Cấu hình toast nếu có thể
|
||||
if (typeof FuiToast !== 'undefined' && FuiToast.config) {
|
||||
FuiToast.config({
|
||||
position: 'top-center'
|
||||
});
|
||||
}
|
||||
|
||||
function showNotification(message, type = 'success') {
|
||||
if (typeof FuiToast !== 'undefined') {
|
||||
if (type === 'success') {
|
||||
FuiToast.success(message);
|
||||
} else {
|
||||
FuiToast.error(message);
|
||||
}
|
||||
} else {
|
||||
alert(message);
|
||||
}
|
||||
}
|
||||
|
||||
async function startVirtualMachine() {
|
||||
const startBtn = document.getElementById('startVMButton');
|
||||
if (startBtn.disabled) return;
|
||||
|
||||
const accessToken = '{{ request()->cookie('accessToken') }}';
|
||||
if (!accessToken) {
|
||||
showNotification('Chưa đăng nhập, vui lòng đăng nhập lại', 'error');
|
||||
setTimeout(() => {
|
||||
window.location.href = '/login';
|
||||
}, 2000);
|
||||
return;
|
||||
}
|
||||
|
||||
const requestBody = {
|
||||
vm_id: vmId
|
||||
};
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}',
|
||||
'Authorization': 'Bearer ' + accessToken
|
||||
};
|
||||
|
||||
const startPromise = fetch('/api/v1/start-virtual-machine', {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: JSON.stringify(requestBody),
|
||||
credentials: 'same-origin'
|
||||
}).then(async (response) => {
|
||||
if (response.redirected || response.status === 401) {
|
||||
throw new Error('Phiên đăng nhập đã hết hạn, vui lòng đăng nhập lại');
|
||||
}
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (parseError) {
|
||||
throw new Error('Lỗi định dạng dữ liệu từ máy chủ');
|
||||
}
|
||||
|
||||
if (!data.error) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return data.message;
|
||||
} else {
|
||||
if (data.type === 'com.vmware.vapi.std.errors.already_in_desired_state' ||
|
||||
(data.message && data.message.error_type === 'ALREADY_IN_DESIRED_STATE')) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return 'Máy chủ đã được bật';
|
||||
} else {
|
||||
let errorMessage = 'Có lỗi xảy ra';
|
||||
if (data.message) {
|
||||
if (typeof data.message === 'object') {
|
||||
if (data.message.messages && data.message.messages.length > 0) {
|
||||
errorMessage = data.message.messages[0].default_message || data.message
|
||||
.messages[0]
|
||||
.message || 'Có lỗi xảy ra';
|
||||
} else if (data.message.error_type) {
|
||||
errorMessage = `Lỗi: ${data.message.error_type}`;
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.message;
|
||||
}
|
||||
} else if (data.value && data.value.messages && data.value.messages.length > 0) {
|
||||
errorMessage = data.value.messages[0].default_message;
|
||||
} else if (typeof data === 'string') {
|
||||
errorMessage = data;
|
||||
} else if (data.error) {
|
||||
if (typeof data.error === 'object') {
|
||||
if (data.error.message) {
|
||||
errorMessage = data.error.message;
|
||||
} else if (data.error.toString && data.error.toString() !==
|
||||
'[object Object]') {
|
||||
errorMessage = data.error.toString();
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.error;
|
||||
}
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof FuiToast !== 'undefined' && FuiToast.promise) {
|
||||
FuiToast.promise(
|
||||
startPromise, {
|
||||
loading: 'Đang khởi động máy ảo...',
|
||||
success: (data) => {
|
||||
console.log(data);
|
||||
return data;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(error);
|
||||
return error.message || 'Có lỗi xảy ra khi khởi động máy ảo';
|
||||
}
|
||||
}, {
|
||||
isClose: true
|
||||
}
|
||||
);
|
||||
} else {
|
||||
showNotification('Đang khởi động máy ảo...', 'success');
|
||||
startPromise.then(data => {
|
||||
showNotification(data, 'success');
|
||||
}).catch(error => {
|
||||
showNotification(error.message || 'Có lỗi xảy ra khi khởi động máy ảo', 'error');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function stopVirtualMachine() {
|
||||
const stopBtn = document.getElementById('stopVMButton');
|
||||
if (stopBtn.disabled) return;
|
||||
|
||||
const accessToken = '{{ request()->cookie('accessToken') }}';
|
||||
if (!accessToken) {
|
||||
showNotification('Chưa đăng nhập, vui lòng đăng nhập lại', 'error');
|
||||
setTimeout(() => {
|
||||
window.location.href = '/login';
|
||||
}, 2000);
|
||||
return;
|
||||
}
|
||||
|
||||
const requestBody = {
|
||||
vm_id: vmId
|
||||
};
|
||||
|
||||
const headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}',
|
||||
'Authorization': 'Bearer ' + accessToken
|
||||
};
|
||||
|
||||
const stopPromise = fetch('/api/v1/stop-virtual-machine', {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: JSON.stringify(requestBody),
|
||||
credentials: 'same-origin'
|
||||
}).then(async (response) => {
|
||||
if (response.redirected || response.status === 401) {
|
||||
throw new Error('Phiên đăng nhập đã hết hạn, vui lòng đăng nhập lại');
|
||||
}
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (parseError) {
|
||||
throw new Error('Lỗi định dạng dữ liệu từ máy chủ');
|
||||
}
|
||||
|
||||
if (!data.error) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return data.message;
|
||||
} else {
|
||||
if (data.type === 'com.vmware.vapi.std.errors.already_in_desired_state' ||
|
||||
(data.message && data.message.error_type === 'ALREADY_IN_DESIRED_STATE')) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return 'Máy chủ đã được tắt';
|
||||
} else {
|
||||
let errorMessage = 'Có lỗi xảy ra';
|
||||
if (data.message) {
|
||||
if (typeof data.message === 'object') {
|
||||
if (data.message.messages && data.message.messages.length > 0) {
|
||||
errorMessage = data.message.messages[0].default_message || data.message
|
||||
.messages[0]
|
||||
.message || 'Có lỗi xảy ra';
|
||||
} else if (data.message.error_type) {
|
||||
errorMessage = `Lỗi: ${data.message.error_type}`;
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.message;
|
||||
}
|
||||
} else if (data.value && data.value.messages && data.value.messages.length > 0) {
|
||||
errorMessage = data.value.messages[0].default_message;
|
||||
} else if (typeof data === 'string') {
|
||||
errorMessage = data;
|
||||
} else if (data.error) {
|
||||
if (typeof data.error === 'object') {
|
||||
if (data.error.message) {
|
||||
errorMessage = data.error.message;
|
||||
} else if (data.error.toString && data.error.toString() !==
|
||||
'[object Object]') {
|
||||
errorMessage = data.error.toString();
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.error;
|
||||
}
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof FuiToast !== 'undefined' && FuiToast.promise) {
|
||||
FuiToast.promise(
|
||||
stopPromise, {
|
||||
loading: 'Đang dừng máy ảo...',
|
||||
success: (data) => {
|
||||
console.log(data);
|
||||
return data;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(error);
|
||||
return error.message || 'Có lỗi xảy ra khi dừng máy ảo';
|
||||
}
|
||||
}, {
|
||||
isClose: true
|
||||
}
|
||||
);
|
||||
} else {
|
||||
showNotification('Đang dừng máy ảo...', 'success');
|
||||
stopPromise.then(data => {
|
||||
showNotification(data, 'success');
|
||||
}).catch(error => {
|
||||
showNotification(error.message || 'Có lỗi xảy ra khi dừng máy ảo', 'error');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function restartVirtualMachine() {
|
||||
const restartBtn = document.getElementById('restartVMButton');
|
||||
if (restartBtn.disabled) return;
|
||||
|
||||
const restartPromise = fetch(`/api/v1/restart-virtual-machine`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
vm_id: vmId
|
||||
})
|
||||
}).then(async (response) => {
|
||||
let data;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (parseError) {
|
||||
throw new Error('Lỗi định dạng dữ liệu từ máy chủ');
|
||||
}
|
||||
|
||||
if (data.success) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return data.message;
|
||||
} else {
|
||||
let errorMessage = 'Có lỗi xảy ra';
|
||||
if (data.message) {
|
||||
if (typeof data.message === 'object') {
|
||||
if (data.message.messages && data.message.messages.length > 0) {
|
||||
errorMessage = data.message.messages[0].default_message || data.message
|
||||
.messages[0]
|
||||
.message || 'Có lỗi xảy ra';
|
||||
} else if (data.message.error_type) {
|
||||
errorMessage = `Lỗi: ${data.message.error_type}`;
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.message;
|
||||
}
|
||||
} else if (data.error) {
|
||||
if (typeof data.error === 'object') {
|
||||
if (data.error.message) {
|
||||
errorMessage = data.error.message;
|
||||
} else if (data.error.toString && data.error.toString() !== '[object Object]') {
|
||||
errorMessage = data.error.toString();
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.error;
|
||||
}
|
||||
} else if (typeof data === 'string') {
|
||||
errorMessage = data;
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof FuiToast !== 'undefined' && FuiToast.promise) {
|
||||
FuiToast.promise(
|
||||
restartPromise, {
|
||||
loading: 'Đang khởi động lại máy ảo...',
|
||||
success: (data) => {
|
||||
console.log(data);
|
||||
return data;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(error);
|
||||
return error.message || 'Có lỗi xảy ra khi khởi động lại máy ảo';
|
||||
}
|
||||
}, {
|
||||
isClose: true
|
||||
}
|
||||
);
|
||||
} else {
|
||||
showNotification('Đang khởi động lại máy ảo...', 'success');
|
||||
restartPromise.then(data => {
|
||||
showNotification(data, 'success');
|
||||
}).catch(error => {
|
||||
showNotification(error.message || 'Có lỗi xảy ra khi khởi động lại máy ảo', 'error');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function suspendVirtualMachine() {
|
||||
const suspendBtn = document.getElementById('suspendVMButton');
|
||||
if (suspendBtn.disabled) return;
|
||||
|
||||
const suspendPromise = fetch(`/api/v1/suspend-virtual-machine`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
vm_id: vmId
|
||||
})
|
||||
}).then(async (response) => {
|
||||
let data;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (parseError) {
|
||||
throw new Error('Lỗi định dạng dữ liệu từ máy chủ');
|
||||
}
|
||||
|
||||
if (data.success) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return data.message;
|
||||
} else {
|
||||
let errorMessage = 'Có lỗi xảy ra';
|
||||
if (data.message) {
|
||||
if (typeof data.message === 'object') {
|
||||
if (data.message.messages && data.message.messages.length > 0) {
|
||||
errorMessage = data.message.messages[0].default_message || data.message
|
||||
.messages[0]
|
||||
.message || 'Có lỗi xảy ra';
|
||||
} else if (data.message.error_type) {
|
||||
errorMessage = `Lỗi: ${data.message.error_type}`;
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.message;
|
||||
}
|
||||
} else if (data.error) {
|
||||
if (typeof data.error === 'object') {
|
||||
if (data.error.message) {
|
||||
errorMessage = data.error.message;
|
||||
} else if (data.error.toString && data.error.toString() !== '[object Object]') {
|
||||
errorMessage = data.error.toString();
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.error;
|
||||
}
|
||||
} else if (typeof data === 'string') {
|
||||
errorMessage = data;
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof FuiToast !== 'undefined' && FuiToast.promise) {
|
||||
FuiToast.promise(
|
||||
suspendPromise, {
|
||||
loading: 'Đang tạm dừng máy ảo...',
|
||||
success: (data) => {
|
||||
console.log(data);
|
||||
return data;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(error);
|
||||
return error.message || 'Có lỗi xảy ra khi tạm dừng máy ảo';
|
||||
}
|
||||
}, {
|
||||
isClose: true
|
||||
}
|
||||
);
|
||||
} else {
|
||||
showNotification('Đang tạm dừng máy ảo...', 'success');
|
||||
suspendPromise.then(data => {
|
||||
showNotification(data, 'success');
|
||||
}).catch(error => {
|
||||
showNotification(error.message || 'Có lỗi xảy ra khi tạm dừng máy ảo', 'error');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function shutdownVirtualMachine() {
|
||||
const shutdownBtn = document.getElementById('shutdownVMButton');
|
||||
if (shutdownBtn.disabled) return;
|
||||
|
||||
const shutdownPromise = fetch(`/api/v1/shutdown-virtual-machine`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
vm_id: vmId
|
||||
})
|
||||
}).then(async (response) => {
|
||||
let data;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (parseError) {
|
||||
throw new Error('Lỗi định dạng dữ liệu từ máy chủ');
|
||||
}
|
||||
|
||||
if (data.success) {
|
||||
setTimeout(() => {
|
||||
location.reload();
|
||||
}, 2000);
|
||||
return data.message;
|
||||
} else {
|
||||
let errorMessage = 'Có lỗi xảy ra';
|
||||
if (data.message) {
|
||||
if (typeof data.message === 'object') {
|
||||
if (data.message.messages && data.message.messages.length > 0) {
|
||||
errorMessage = data.message.messages[0].default_message || data.message
|
||||
.messages[0]
|
||||
.message || 'Có lỗi xảy ra';
|
||||
} else if (data.message.error_type) {
|
||||
errorMessage = `Lỗi: ${data.message.error_type}`;
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.message;
|
||||
}
|
||||
} else if (data.error) {
|
||||
if (typeof data.error === 'object') {
|
||||
if (data.error.message) {
|
||||
errorMessage = data.error.message;
|
||||
} else if (data.error.toString && data.error.toString() !== '[object Object]') {
|
||||
errorMessage = data.error.toString();
|
||||
} else {
|
||||
errorMessage = 'Có lỗi xảy ra từ hệ thống';
|
||||
}
|
||||
} else {
|
||||
errorMessage = data.error;
|
||||
}
|
||||
} else if (typeof data === 'string') {
|
||||
errorMessage = data;
|
||||
}
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof FuiToast !== 'undefined' && FuiToast.promise) {
|
||||
FuiToast.promise(
|
||||
shutdownPromise, {
|
||||
loading: 'Đang shutdown máy ảo...',
|
||||
success: (data) => {
|
||||
console.log(data);
|
||||
return data;
|
||||
},
|
||||
error: (error) => {
|
||||
console.error(error);
|
||||
return error.message || 'Có lỗi xảy ra khi shutdown máy ảo';
|
||||
}
|
||||
}, {
|
||||
isClose: true
|
||||
}
|
||||
);
|
||||
} else {
|
||||
showNotification('Đang shutdown máy ảo...', 'success');
|
||||
shutdownPromise.then(data => {
|
||||
showNotification(data, 'success');
|
||||
}).catch(error => {
|
||||
showNotification(error.message || 'Có lỗi xảy ra khi shutdown máy ảo', 'error');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// DOM ready
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const startBtn = document.getElementById('startVMButton');
|
||||
const stopBtn = document.getElementById('stopVMButton');
|
||||
const restartBtn = document.getElementById('restartVMButton');
|
||||
const suspendBtn = document.getElementById('suspendVMButton');
|
||||
const shutdownBtn = document.getElementById('shutdownVMButton');
|
||||
|
||||
if (startBtn) {
|
||||
startBtn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
startVirtualMachine();
|
||||
});
|
||||
}
|
||||
|
||||
if (stopBtn) {
|
||||
stopBtn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
stopVirtualMachine();
|
||||
});
|
||||
}
|
||||
|
||||
if (restartBtn) {
|
||||
restartBtn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
restartVirtualMachine();
|
||||
});
|
||||
}
|
||||
|
||||
if (suspendBtn) {
|
||||
suspendBtn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
suspendVirtualMachine();
|
||||
});
|
||||
}
|
||||
|
||||
if (shutdownBtn) {
|
||||
shutdownBtn.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
shutdownVirtualMachine();
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user