Files
portal-api/app/Http/Services/vCenter/ResourceService.php
T

166 lines
4.6 KiB
PHP

<?php
namespace App\Http\Services\vCenter;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class ResourceService
{
protected Client $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function getAllDatacenter()
{
if ($this->client->checkApiExits()) {
$response = Http::withoutVerifying()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->get("https://{$this->client->apiUri}/rest/vcenter/datacenter")->json();
if (!isset($response['type'])) {
return $response['value'];
}
return false;
}
return "Login_Again";
}
public function getAllHost($datacenter = '')
{
if ($this->client->checkApiExits()) {
$response = Http::withoutVerifying()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->get("https://{$this->client->apiUri}/rest/vcenter/host", [
'filter.datacenters.1' => $datacenter
])->json();
if (!isset($response['type'])) {
return $response['value'];
}
return false;
}
return "Login_Again";
}
public function getAllFolder($datacenter = '')
{
if ($this->client->checkApiExits()) {
$response = Http::withoutVerifying()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->get("https://{$this->client->apiUri}/rest/vcenter/folder", [
'filter.datacenters.1' => $datacenter,
'filter.type' => "VIRTUAL_MACHINE",
'filter.names' => "vm"
])->json();
if (!isset($response['type'])) {
return $response['value'];
}
return false;
}
return "Login_Again";
}
public function getAllDataStore($host = '')
{
Log::info('[vCenter] getAllDataStore call', [
'api' => $this->client->apiUri,
'host_filter' => $host
]);
if (!$this->client->checkApiExits()) {
Log::warning('[vCenter] checkApiExits = false');
return "Login_Again";
}
$params = [];
if (!empty($host)) {
$params['filter.hosts'] = $host;
}
$response = Http::withoutVerifying()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->get("https://{$this->client->apiUri}/rest/vcenter/datastore", $params);
$json = $response->json();
if (!isset($json['type'])) {
return $json['value'];
}
Log::warning('[vCenter] API returned error', [
'response' => $json
]);
return false;
}
public function loadDatastore($host_id = '')
{
Log::info('[vCenter] loadDatastore()', [
'esxi_host' => $host_id
]);
$allDatastore = $this->getAllDataStore($host_id);
if ($allDatastore === "Login_Again") {
Log::warning('[vCenter] Token expired when getAllDataStore');
return "Login_Again";
}
if (!$allDatastore) {
Log::warning('[vCenter] getAllDataStore return false');
return false;
}
Log::info('[vCenter] Datastore count from API', [
'count' => count($allDatastore)
]);
return $allDatastore;
}
public function getAllNetwork($host = '')
{
if ($this->client->checkApiExits()) {
$params = [];
if (!empty($host)) {
$params['filter.hosts'] = $host;
}
$response = Http::withoutVerifying()
->withHeaders([
'vmware-api-session-id' => $this->client->token
])
->get("https://{$this->client->apiUri}/rest/vcenter/network", $params)->json();
if (!isset($response['type'])) {
return $response['value'];
}
return false;
}
return "Login_Again";
}
public function loadNetWork($host_id = '')
{
return $this->getAllNetwork($host_id);
}
public function GetFolderByHost($datacenter = '')
{
return \App\Models\ModuleFolder::where('module_datacenter_id', $datacenter)->first();
}
}