Files

36 lines
1013 B
PHP

<?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()];
}
}
}