198 lines
5.7 KiB
PHP
198 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services\vCenter;
|
|
|
|
use App\Support\TlsHttpClientFactory;
|
|
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 = TlsHttpClientFactory::vCenter()
|
|
->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 = TlsHttpClientFactory::vCenter()
|
|
->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 = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/folder", [
|
|
'filter.datacenters.1' => $datacenter,
|
|
])->json();
|
|
|
|
if (!isset($response['type'])) {
|
|
return $response['value'];
|
|
}
|
|
return false;
|
|
}
|
|
return "Login_Again";
|
|
}
|
|
|
|
public function getAllResourcePool($host = '', $datacenter = '')
|
|
{
|
|
if ($this->client->checkApiExits()) {
|
|
$params = [];
|
|
if (!empty($host)) {
|
|
$params['filter.hosts'] = $host;
|
|
}
|
|
if (!empty($datacenter)) {
|
|
$params['filter.datacenters'] = $datacenter;
|
|
}
|
|
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->withHeaders([
|
|
'vmware-api-session-id' => $this->client->token
|
|
])
|
|
->get("https://{$this->client->apiUri}/rest/vcenter/resource-pool", $params)->json();
|
|
|
|
if (!isset($response['type'])) {
|
|
return $response['value'];
|
|
}
|
|
return false;
|
|
}
|
|
return "Login_Again";
|
|
}
|
|
|
|
public function getAllDataStore($host = '', $datacenter = '')
|
|
{
|
|
Log::info('[vCenter] getAllDataStore call', [
|
|
'api' => $this->client->apiUri,
|
|
'host_filter' => $host,
|
|
'datacenter_filter' => $datacenter
|
|
]);
|
|
|
|
if (!$this->client->checkApiExits()) {
|
|
Log::warning('[vCenter] checkApiExits = false');
|
|
return "Login_Again";
|
|
}
|
|
|
|
$params = [];
|
|
if (!empty($host)) {
|
|
$params['filter.hosts'] = $host;
|
|
}
|
|
if (!empty($datacenter)) {
|
|
$params['filter.datacenters'] = $datacenter;
|
|
}
|
|
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->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 = '', $datacenter_id = '')
|
|
{
|
|
Log::info('[vCenter] loadDatastore()', [
|
|
'esxi_host' => $host_id,
|
|
'datacenter' => $datacenter_id
|
|
]);
|
|
|
|
$allDatastore = $this->getAllDataStore($host_id, $datacenter_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 = '', $datacenter = '')
|
|
{
|
|
if ($this->client->checkApiExits()) {
|
|
$params = [];
|
|
if (!empty($host)) {
|
|
$params['filter.hosts'] = $host;
|
|
}
|
|
if (!empty($datacenter)) {
|
|
$params['filter.datacenters'] = $datacenter;
|
|
}
|
|
|
|
$response = TlsHttpClientFactory::vCenter()
|
|
->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 = '', $datacenter_id = '')
|
|
{
|
|
return $this->getAllNetwork($host_id, $datacenter_id);
|
|
}
|
|
|
|
public function getNetwork($host = '', $datacenter = ''): array|bool|string
|
|
{
|
|
return $this->getAllNetwork($host, $datacenter);
|
|
}
|
|
}
|