# Tóm tắt thay đổi - Xử lý Addon sau khi Clone VM ## Tổng quan Tài liệu này mô tả các thay đổi đã thực hiện để cải thiện hệ thống quản lý vCenter virtual machines trong Laravel application. ## Các thay đổi chính ### 1. Cải thiện vCenter Service (app/Http/Services/vCenter.php) #### 1.1 Thêm các method cập nhật phần cứng VM - **updateVMHardware()**: Method chính để cập nhật cấu hình phần cứng VM - **updateVMCPU()**: Cập nhật số lượng CPU cores - **updateVMMemory()**: Cập nhật RAM (convert GB sang MiB) - **updateVMDisk()**: Cập nhật dung lượng disk (mở rộng disk) #### 1.2 Cải thiện CreateVirtualMachine() - Thêm logic để áp dụng addon sau khi clone VM thành công - Convert RAM từ GB sang MiB trước khi gọi API - Thêm logging chi tiết cho quá trình tạo VM và cập nhật addon - Gửi thông báo Discord khi tạo VM thành công #### 1.3 Thêm method test - **testAddonUpdate()**: Method để test việc cập nhật addon ### 2. Cập nhật CreateVirtualMachineJob (app/Jobs/VirtualMachine/Create/CreateVirtualMachineJob.php) #### 2.1 Cải thiện xử lý addon - Parse đúng số lượng addon từ order items - Truyền thông tin addon vào vCenter service - Thêm logging chi tiết cho quá trình xử lý #### 2.2 Lưu vm_id vào invoice - Lưu VM ID vào bảng `invoice` sau khi tạo thành công - Cập nhật trạng thái order và gửi email thông báo ### 3. Tạo ServiceController (app/Http/Controllers/frontend/ServiceController.php) #### 3.1 Chức năng chính - **show()**: Hiển thị chi tiết service dựa trên invoice_id - **toggleVM()**: Điều khiển VM (start/stop/restart) - placeholder - **getVMInfo()**: Lấy thông tin VM từ vCenter - placeholder #### 3.2 Tích hợp vCenter API - **getVMInfoFromVCenter()**: Lấy thông tin VM thực từ vCenter API - **getDefaultVMInfo()**: Thông tin mặc định khi không thể kết nối vCenter - Tự động login vào vCenter và lấy thông tin chi tiết VM #### 3.3 Thông tin VM được lấy - Trạng thái power (POWERED_ON, POWERED_OFF, SUSPENDED) - IP address từ guest info - Thông tin phần cứng (CPU, RAM, Disk) - Thông tin chi tiết (folder, resource pool, datastore, network, etc.) ### 4. Tạo Service Detail View (resources/views/frontend/service/show.blade.php) #### 4.1 Giao diện hiển thị - Thông tin cơ bản của service (invoice, customer, billing) - Thông tin chi tiết VM từ vCenter API - Danh sách addon đã mua - Nút điều khiển VM (placeholder) #### 4.2 Thông tin VM hiển thị - VM ID, tên VM, trạng thái - IP address, hostname, MAC address - CPU, RAM, Disk, OS - Thông tin chi tiết: folder, resource pool, datastore, network, version ### 5. Cập nhật Routes (routes/web.php) #### 5.1 Thêm routes cho service ```php Route::get('/service/{invoice_id}', [ServiceController::class, 'show'])->name('service.show'); Route::post('/service/{invoice_id}/toggle-vm', [ServiceController::class, 'toggleVM'])->name('service.toggle-vm'); Route::get('/service/{invoice_id}/vm-info', [ServiceController::class, 'getVMInfo'])->name('service.vm-info'); ``` ### 6. Cập nhật My Service View (resources/views/frontend/my-service.blade.php) #### 6.1 Thêm link chi tiết - Cập nhật link "Xem chi tiết" để trỏ đến route mới - Hiển thị VM ID nếu có ### 7. Cập nhật Admin Invoice Views #### 7.1 Hiển thị VM ID - **resources/views/admin/invoice/show.blade.php**: Thêm cột VM ID - **resources/views/admin/invoice/index.blade.php**: Thêm cột VM ID ### 8. Tạo Test Commands #### 8.1 TestVMInfo Command (app/Console/Commands/TestVMInfo.php) - Test việc lấy thông tin VM từ vCenter API - Kiểm tra login, VM details, power status, IP info - Hiển thị thông tin chi tiết để debug #### 8.2 TestSaveVMId Command (app/Console/Commands/TestSaveVMId.php) - Test việc lưu VM ID vào invoice - Simulate quá trình tạo VM và lưu thông tin #### 8.3 TestCreateVMWithAddon Command (app/Console/Commands/TestCreateVMWithAddon.php) - Test toàn bộ quá trình tạo VM với addon - Từ order đến tạo VM và cập nhật addon ## Các vấn đề đã giải quyết ### 1. Vấn đề addon không áp dụng - **Nguyên nhân**: Logic cũ không cập nhật phần cứng sau khi clone VM - **Giải pháp**: Thêm logic cập nhật phần cứng sau khi clone thành công ### 2. Vấn đề convert RAM - **Nguyên nhân**: RAM được truyền dưới dạng GB nhưng API yêu cầu MiB - **Giải pháp**: Convert GB sang MiB (1 GB = 1024 MiB) trước khi gọi API ### 3. Vấn đề hiển thị thông tin VM - **Nguyên nhân**: Không có cách để xem thông tin VM thực từ vCenter - **Giải pháp**: Tạo service detail page tích hợp với vCenter API ### 4. Vấn đề lưu VM ID - **Nguyên nhân**: VM ID không được lưu vào database - **Giải pháp**: Lưu VM ID vào bảng `invoice` sau khi tạo thành công ## Cách sử dụng ### 1. Test lấy thông tin VM ```bash php artisan test:vm-info {invoice_id} ``` ### 2. Test lưu VM ID ```bash php artisan test:save-vm-id {invoice_id} ``` ### 3. Test tạo VM với addon ```bash php artisan test:create-vm-with-addon {order_id} ``` ### 4. Truy cập service detail - URL: `/service/{invoice_id}` - Yêu cầu đăng nhập và quyền sở hữu invoice ## Lưu ý quan trọng ### 1. Bảo mật - Kiểm tra quyền truy cập: chỉ customer sở hữu mới được xem service detail - Validate input và handle exception cẩn thận ### 2. Performance - Cache thông tin VM nếu cần thiết - Timeout cho vCenter API calls - Fallback khi không thể kết nối vCenter ### 3. Monitoring - Log chi tiết tất cả operations - Gửi thông báo Discord cho các events quan trọng - Track performance của vCenter API calls ## Các bước tiếp theo ### 1. Implement VM Control Actions - Start/Stop/Restart VM thực sự - Console access - Snapshot management ### 2. Cải thiện UI/UX - Real-time status updates - Progress indicators - Better error handling ### 3. Monitoring & Alerting - VM health monitoring - Resource usage tracking - Automated alerts ### 4. Backup & Recovery - Automated backup scheduling - Disaster recovery procedures - Backup verification ## Database Changes ### 1. Invoice Table - Thêm cột `vm_id` để lưu VM ID từ vCenter - Cột này được populate sau khi tạo VM thành công ### 2. Không có migration cần thiết - Cột `vm_id` đã tồn tại trong database ## API Endpoints ### 1. vCenter API Integration - `GET /rest/vcenter/vm/{vm_id}` - Lấy thông tin VM - `GET /rest/vcenter/vm/{vm_id}/guest/power` - Lấy trạng thái power - `GET /rest/vcenter/vm/{vm_id}/guest/identity` - Lấy thông tin guest - `PATCH /rest/vcenter/vm/{vm_id}/hardware/cpu` - Cập nhật CPU - `PATCH /rest/vcenter/vm/{vm_id}/hardware/memory` - Cập nhật RAM ### 2. Application API - `GET /service/{invoice_id}` - Service detail page - `POST /service/{invoice_id}/toggle-vm` - Điều khiển VM - `GET /service/{invoice_id}/vm-info` - Lấy thông tin VM ## Testing ### 1. Unit Tests - Test vCenter service methods - Test VM creation workflow - Test addon application logic ### 2. Integration Tests - Test vCenter API integration - Test complete VM creation flow - Test service detail page ### 3. Manual Testing - Test với VM thực trên vCenter - Test các trường hợp lỗi - Test performance với nhiều VM ## Troubleshooting ### 1. VM Creation Issues - Kiểm tra vCenter connectivity - Verify template availability - Check resource availability ### 2. Addon Application Issues - Verify addon quantities - Check API responses - Review logs for errors ### 3. Service Detail Page Issues - Check vCenter login - Verify VM ID exists - Review API responses ## Conclusion Các thay đổi này đã cải thiện đáng kể hệ thống quản lý VM: - Tự động áp dụng addon sau khi tạo VM - Hiển thị thông tin VM thực từ vCenter - Giao diện quản lý service chi tiết - Monitoring và logging tốt hơn Hệ thống hiện tại đã sẵn sàng cho production với các tính năng cơ bản hoạt động ổn định.