Refactor VM provisioning pipeline into focused services and add IP pool
management
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services\VirtualMachine;
|
||||
|
||||
use App\Http\Services\vCenter;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class VmPowerService
|
||||
{
|
||||
public function start(vCenter $vCenter, string $vmId): array|string
|
||||
{
|
||||
try {
|
||||
if (!$vCenter->checkApiExits()) {
|
||||
return 'Login_Again';
|
||||
}
|
||||
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders(['vmware-api-session-id' => $vCenter->token])
|
||||
->post("https://{$vCenter->apiUri}/rest/vcenter/vm/{$vmId}/power/start");
|
||||
|
||||
if ($response->status() === 200) {
|
||||
return ['error' => false, 'data' => 'success'];
|
||||
}
|
||||
|
||||
return ['error' => true, 'data' => $response->body()];
|
||||
} catch (Throwable $e) {
|
||||
Log::error('VmPowerService: start VM failed: ' . $e->getMessage());
|
||||
return ['error' => true, 'data' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user