- Introduced a new method `resolveVCenterFromProduct` in `SnapshotService` to streamline vCenter resolution from product data. - Updated `CreateSnapshot`, `RemoveSnapshot`, and `RevertSnapshot` jobs to utilize the new vCenter resolution method. - Enhanced the `VmController` to support UUID-based service identification for VM operations. - Added new actions for changing VM IP and RDP port, encapsulating the logic for these operations. - Improved error handling and logging for VM operations, including detailed responses for API consumers. - Removed unnecessary relationships in model queries to optimize performance.
66 lines
2.0 KiB
PHP
66 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\VM;
|
|
|
|
use App\Http\Services\vCenter;
|
|
use App\Models\ModuleVmware;
|
|
|
|
class ChangeIpVMAction
|
|
{
|
|
public function __construct(private vCenter $vcenter) {}
|
|
|
|
/**
|
|
* Change IP address of a VM.
|
|
*
|
|
* Linux → changeIPLinux.ps1 (-VMName, -IP, -Gateway, -DNS, -GuestUser, -GuestPassword)
|
|
* Windows → changeWindows1line.ps1 (legacy inline variable pattern)
|
|
*
|
|
* @param ModuleVmware $vm
|
|
* @param string $ipAddress
|
|
* @param string $gateway Empty string if not provided
|
|
* @param string $osType 'windows' | 'linux'
|
|
* @param string $dns DNS server, default 8.8.8.8
|
|
* @return string
|
|
*/
|
|
public function execute(
|
|
ModuleVmware $vm,
|
|
string $ipAddress,
|
|
string $gateway,
|
|
string $osType,
|
|
string $dns = '8.8.8.8',
|
|
string $guestUser = '',
|
|
string $guestPassword = ''
|
|
): string {
|
|
try {
|
|
$vcenter = $vm->vCenter
|
|
?? $vm->ipAddress?->host?->datacenter?->vCenter;
|
|
|
|
if (!$vcenter) {
|
|
return 'vCenter_Not_Found';
|
|
}
|
|
|
|
if (!$this->vcenter->loginServer($vcenter)) {
|
|
return 'Login_Again';
|
|
}
|
|
|
|
// Use provided guest credentials, fallback to VM stored credentials
|
|
$user = $guestUser ?: (string) $vm->user;
|
|
$pass = $guestPassword ?: (string) $vm->getRawOriginal('password');
|
|
|
|
return $this->vcenter->changeIpVM(
|
|
(string) $vm->name, // -VMName (Get-VM -Name)
|
|
(string) $vm->vmware_id, // VMware ID (Windows -Id pattern)
|
|
$ipAddress,
|
|
$gateway,
|
|
$osType,
|
|
$user, // -GuestUser
|
|
$pass, // -GuestPassword (plaintext)
|
|
$dns,
|
|
(string) $vm->hostname
|
|
);
|
|
} catch (\Throwable $e) {
|
|
return 'Error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|