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.
This commit is contained in:
@@ -102,6 +102,10 @@ class vCenter
|
||||
{
|
||||
return $this->resource->getAllFolder($datacenter);
|
||||
}
|
||||
public function getAllResourcePool($host = '', $datacenter = '')
|
||||
{
|
||||
return $this->resource->getAllResourcePool($host, $datacenter);
|
||||
}
|
||||
public function getAllDataStore($host = '')
|
||||
{
|
||||
return $this->resource->getAllDataStore($host);
|
||||
@@ -110,17 +114,17 @@ class vCenter
|
||||
{
|
||||
return $this->resource->loadDatastore($host_id);
|
||||
}
|
||||
public function loadNetWork($data = [], $host_id = '')
|
||||
public function loadNetWork($data = [], $host_id = '', $datacenter_id = '')
|
||||
{
|
||||
return $this->resource->loadNetWork($host_id);
|
||||
return $this->resource->loadNetWork($host_id, $datacenter_id);
|
||||
}
|
||||
public function getAllNetwork($host = '')
|
||||
public function getAllNetwork($host = '', $datacenter = '')
|
||||
{
|
||||
return $this->resource->getNetwork($host);
|
||||
return $this->resource->getNetwork($host, $datacenter);
|
||||
}
|
||||
public function getNetwork($host = ''): array|bool|string
|
||||
public function getNetwork($host = '', $datacenter = ''): array|bool|string
|
||||
{
|
||||
return $this->resource->getNetwork($host);
|
||||
return $this->resource->getNetwork($host, $datacenter);
|
||||
}
|
||||
|
||||
// ─── Templates ───────────────────────────────────────────────────────────
|
||||
|
||||
@@ -59,10 +59,8 @@ class ResourceService
|
||||
])
|
||||
->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'];
|
||||
}
|
||||
@@ -71,11 +69,37 @@ class ResourceService
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
public function getAllDataStore($host = '')
|
||||
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
|
||||
'host_filter' => $host,
|
||||
'datacenter_filter' => $datacenter
|
||||
]);
|
||||
|
||||
if (!$this->client->checkApiExits()) {
|
||||
@@ -87,6 +111,9 @@ class ResourceService
|
||||
if (!empty($host)) {
|
||||
$params['filter.hosts'] = $host;
|
||||
}
|
||||
if (!empty($datacenter)) {
|
||||
$params['filter.datacenters'] = $datacenter;
|
||||
}
|
||||
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
@@ -107,13 +134,14 @@ class ResourceService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function loadDatastore($host_id = '')
|
||||
public function loadDatastore($host_id = '', $datacenter_id = '')
|
||||
{
|
||||
Log::info('[vCenter] loadDatastore()', [
|
||||
'esxi_host' => $host_id
|
||||
'esxi_host' => $host_id,
|
||||
'datacenter' => $datacenter_id
|
||||
]);
|
||||
|
||||
$allDatastore = $this->getAllDataStore($host_id);
|
||||
$allDatastore = $this->getAllDataStore($host_id, $datacenter_id);
|
||||
|
||||
if ($allDatastore === "Login_Again") {
|
||||
Log::warning('[vCenter] Token expired when getAllDataStore');
|
||||
@@ -132,13 +160,16 @@ class ResourceService
|
||||
return $allDatastore;
|
||||
}
|
||||
|
||||
public function getAllNetwork($host = '')
|
||||
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([
|
||||
@@ -154,14 +185,14 @@ class ResourceService
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
public function loadNetWork($host_id = '')
|
||||
public function loadNetWork($host_id = '', $datacenter_id = '')
|
||||
{
|
||||
return $this->getAllNetwork($host_id);
|
||||
return $this->getAllNetwork($host_id, $datacenter_id);
|
||||
}
|
||||
|
||||
public function getNetwork($host = ''): array|bool|string
|
||||
public function getNetwork($host = '', $datacenter = ''): array|bool|string
|
||||
{
|
||||
return $this->getAllNetwork($host);
|
||||
return $this->getAllNetwork($host, $datacenter);
|
||||
}
|
||||
|
||||
public function GetFolderByHost($datacenter = '')
|
||||
|
||||
@@ -95,9 +95,9 @@ class VirtualMachineService
|
||||
'source' => $template,
|
||||
'placement' => [
|
||||
'datacenter' => $datacenter->datacenter_id,
|
||||
'folder' => $providerSettings->defaultFolder(),
|
||||
'folder' => $product_data['folder'] ?? $providerSettings->defaultFolder(),
|
||||
'host' => $host->host_id,
|
||||
'resource_pool' => $providerSettings->defaultResourcePool(),
|
||||
'resource_pool' => $product_data['resource_pool'] ?? $providerSettings->defaultResourcePool(),
|
||||
'datastore' => $datastoreId,
|
||||
],
|
||||
'power_on' => false,
|
||||
@@ -159,6 +159,12 @@ class VirtualMachineService
|
||||
return ['error' => false, 'data' => $vmId];
|
||||
}
|
||||
|
||||
Log::error('VM clone failed on vCenter', [
|
||||
'status' => $response->status(),
|
||||
'request' => $body,
|
||||
'response' => $responseData ?? $response->body(),
|
||||
]);
|
||||
|
||||
return ['error' => true, 'data' => $responseData['value'] ?? $response->body()];
|
||||
} catch (Exception $e) {
|
||||
Log::error('Exception when creating VM', ['message' => $e->getMessage()]);
|
||||
|
||||
Reference in New Issue
Block a user