feat: VM metrics via portal-monitor integration
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
0b08689ea4
commit
9ed77a85fd
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use RuntimeException;
|
||||
|
||||
class PortalMonitorClient
|
||||
{
|
||||
public function vmMetrics(string $vmwareId): array
|
||||
{
|
||||
return $this->get($vmwareId, 'metrics', 'VM metrics are unavailable');
|
||||
}
|
||||
|
||||
public function vmStorage(string $vmwareId): array
|
||||
{
|
||||
return $this->get($vmwareId, 'storage', 'VM storage is unavailable');
|
||||
}
|
||||
|
||||
private function get(string $vmwareId, string $resource, string $notFoundMessage): array
|
||||
{
|
||||
$url = rtrim((string) config('portal-monitor.url'), '/');
|
||||
$apiKey = (string) config('portal-monitor.api_key');
|
||||
|
||||
if ($url === '' || $apiKey === '') {
|
||||
throw new RuntimeException('Portal Monitor is not configured');
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::acceptJson()
|
||||
->withHeaders(['X-API-Key' => $apiKey])
|
||||
->timeout((int) config('portal-monitor.timeout', 5))
|
||||
->get($url . '/api/v1/vsphere/vms/' . rawurlencode($vmwareId) . '/' . $resource);
|
||||
} catch (ConnectionException) {
|
||||
throw new RuntimeException('Portal Monitor is unavailable');
|
||||
}
|
||||
|
||||
if ($response->status() === 404) {
|
||||
throw new RuntimeException($notFoundMessage, 404);
|
||||
}
|
||||
|
||||
if (!$response->successful()) {
|
||||
throw new RuntimeException('Portal Monitor request failed');
|
||||
}
|
||||
|
||||
$data = $response->json('data');
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException('Portal Monitor returned an invalid response');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user