Files
baodev0 5d3ed57b32 feat: Enhance vCenter integration with resource pool and folder support
- Added methods to ResourceService for fetching resource pools and updated existing methods to include datacenter filtering.
- Modified CreateVirtualMachine to accept folder and resource pool parameters from product data.
- Updated various jobs to synchronize resource pools and include them in the provisioning process.
- Introduced new ModuleResourcePool model and migration for resource pool management.
- Enhanced existing models (ModuleDatastore, ModuleNetwork, Os) to include relationships with vCenter and resource pools.
- Added netmask field to IpPool and updated related logic for IP reservation.
- Improved error handling and logging in VM creation and IP reservation processes.
- Updated API routes to include sync functionality for vCenter.
2026-07-21 21:37:28 +07:00

203 lines
5.8 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,
])->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 = Http::withoutVerifying()
->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 = 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 = '', $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 = 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 = '', $datacenter_id = '')
{
return $this->getAllNetwork($host_id, $datacenter_id);
}
public function getNetwork($host = '', $datacenter = ''): array|bool|string
{
return $this->getAllNetwork($host, $datacenter);
}
public function GetFolderByHost($datacenter = '')
{
return \App\Models\ModuleFolder::where('module_datacenter_id', $datacenter)->first();
}
}