feat: Implement comprehensive vCenter integration with new services for VM configuration, power management, templates, snapshots, and resource management, alongside infrastructure management and synchronization job updates.
This commit is contained in:
+83
-113
@@ -16,9 +16,9 @@ use phpseclib3\Net\SSH2;
|
||||
class vCenter
|
||||
{
|
||||
public ?string $token = null;
|
||||
public string $apiUri = '10.10.10.6';
|
||||
private string $username = 'administrator@vsphere.local';
|
||||
private string $password = 'Tgs@1238989$%^';
|
||||
public string $apiUri;
|
||||
private string $username;
|
||||
private string $password;
|
||||
private int $diskCapacity = 1073741824;
|
||||
private int $memory = 1024;
|
||||
|
||||
@@ -35,7 +35,7 @@ class vCenter
|
||||
|
||||
$response = Http::withoutVerifying()
|
||||
->withBasicAuth($this->username, $this->password)
|
||||
->post("https://{$this->apiUri}/rest/com/vmware/cis/session");
|
||||
->post("https://{$this->apiUri}/rest/com/vmware/cis/session", []);
|
||||
|
||||
// Log::info('Response login: ' . $response->body());
|
||||
|
||||
@@ -246,69 +246,38 @@ class vCenter
|
||||
|
||||
public function loadNetWork($data = [], $host_id = '')
|
||||
{
|
||||
|
||||
$this->username = $data->user;
|
||||
|
||||
$this->password = SignRSA::DecryptFunc($data->password);
|
||||
|
||||
$this->apiUri = $data->host;
|
||||
|
||||
$information = $this->loadInformation($host_id);
|
||||
// Pass the host_id directly to the API call
|
||||
$allNetwork = $this->getAllNetwork($host_id);
|
||||
|
||||
if ($information) {
|
||||
|
||||
if (is_array($information->Network)) {
|
||||
$information = $information->Network;
|
||||
} else {
|
||||
$information = array($information->Network);
|
||||
}
|
||||
|
||||
$netWork = [];
|
||||
$allNetwork = $this->getAllNetwork();
|
||||
if ($allNetwork && $allNetwork !== "Login_Again") {
|
||||
foreach ($allNetwork as $_subItem) {
|
||||
if (in_array($_subItem['network'], $information)) {
|
||||
$netWork[] = $_subItem;
|
||||
}
|
||||
}
|
||||
return $netWork;
|
||||
}
|
||||
return $allNetwork;
|
||||
}
|
||||
return $information;
|
||||
return $allNetwork;
|
||||
}
|
||||
|
||||
protected function loadInformation($host_id = '')
|
||||
private function getAllNetwork($host = '')
|
||||
{
|
||||
try {
|
||||
Log::info('host_id', ['data' => $host_id]);
|
||||
$cmd = "\$vmhost='$host_id'; cd /; C:\CLISave\getvmhostinfo.ps1;";
|
||||
$results = $this->Command($cmd);
|
||||
$check = $this->checkApiExits();
|
||||
if ($check) {
|
||||
|
||||
if (!$results) {
|
||||
return null;
|
||||
$params = [];
|
||||
if (!empty($host)) {
|
||||
$params['filter.hosts'] = $host;
|
||||
}
|
||||
|
||||
$startPos = strpos($results, '{');
|
||||
$endPos = strrpos($results, '}');
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
'vmware-api-session-id' => $this->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/network", $params)->json();
|
||||
|
||||
if ($startPos === false || $endPos === false) {
|
||||
return null;
|
||||
if (!isset($response['type'])) {
|
||||
return $response['value'];
|
||||
}
|
||||
$jsonData = substr($results, $startPos, $endPos - $startPos + 1);
|
||||
|
||||
$jsonData = trim($jsonData);
|
||||
|
||||
try {
|
||||
$decoded = json_decode($jsonData, false, 512, JSON_THROW_ON_ERROR);
|
||||
return $decoded;
|
||||
} catch (\JsonException $e) {
|
||||
return null;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Error in loadInformation: ' . $e->getMessage());
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
protected function Command($cmd)
|
||||
@@ -597,76 +566,77 @@ PS;
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function getAllNetwork()
|
||||
{
|
||||
$check = $this->checkApiExits();
|
||||
if ($check) {
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
'vmware-api-session-id' => $this->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/network")->json();
|
||||
if (!isset($response['type'])) {
|
||||
return $response['value'];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return "Login_Again";
|
||||
}
|
||||
|
||||
public function loadDatastore($data = [], $host_id = '')
|
||||
{
|
||||
Log::info('[vCenter] loadDatastore()', [
|
||||
'host' => $data->host,
|
||||
'esxi_host' => $host_id
|
||||
]);
|
||||
|
||||
$this->username = $data->user;
|
||||
$allDatastore = $this->getAllDataStore($host_id);
|
||||
|
||||
$this->password = SignRSA::DecryptFunc($data->password);
|
||||
|
||||
$this->apiUri = $data->host;
|
||||
|
||||
$information = $this->loadInformation($host_id);
|
||||
|
||||
if ($information) {
|
||||
if (is_array($information->Datastore)) {
|
||||
$information = $information->Datastore;
|
||||
} else {
|
||||
$information = array($information->Datastore);
|
||||
}
|
||||
|
||||
$dataStore = [];
|
||||
$allDatastore = $this->getAllDataStore();
|
||||
|
||||
if ($allDatastore && $allDatastore !== "Login_Again") {
|
||||
foreach ($allDatastore as $_item) {
|
||||
if (in_array($_item['datastore'], $information)) {
|
||||
$dataStore[] = $_item;
|
||||
}
|
||||
}
|
||||
return $dataStore;
|
||||
}
|
||||
return $allDatastore;
|
||||
if ($allDatastore === "Login_Again") {
|
||||
Log::warning('[vCenter] Token expired when getAllDataStore');
|
||||
return "Login_Again";
|
||||
}
|
||||
return $information;
|
||||
}
|
||||
|
||||
public function getAllDataStore()
|
||||
{
|
||||
$check = $this->checkApiExits();
|
||||
if ($check) {
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders([
|
||||
'vmware-api-session-id' => $this->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/datastore")->json();
|
||||
if (!isset($response['type'])) {
|
||||
return $response['value'];
|
||||
}
|
||||
if (!$allDatastore) {
|
||||
Log::warning('[vCenter] getAllDataStore return false');
|
||||
return false;
|
||||
}
|
||||
return "Login_Again";
|
||||
|
||||
Log::info('[vCenter] Datastore count from API', [
|
||||
'count' => count($allDatastore)
|
||||
]);
|
||||
|
||||
return $allDatastore;
|
||||
}
|
||||
|
||||
|
||||
public function getAllDataStore($host = '')
|
||||
{
|
||||
Log::info('[vCenter] getAllDataStore call', [
|
||||
'api' => $this->apiUri,
|
||||
'host_filter' => $host
|
||||
]);
|
||||
|
||||
$check = $this->checkApiExits();
|
||||
|
||||
if (!$check) {
|
||||
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->token
|
||||
])
|
||||
->get("https://$this->apiUri/rest/vcenter/datastore", $params);
|
||||
|
||||
Log::info('[vCenter] datastore raw response', [
|
||||
'status' => $response->status(),
|
||||
// 'body' => $response->body() // Comment out to reduce log noise if large
|
||||
]);
|
||||
|
||||
$json = $response->json();
|
||||
|
||||
if (!isset($json['type'])) {
|
||||
return $json['value'];
|
||||
}
|
||||
|
||||
Log::warning('[vCenter] API returned error', [
|
||||
'response' => $json
|
||||
]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function CreateVirtualMachine($product_data): array|string
|
||||
{
|
||||
$vcenterModel = $product_data['vcenter'];
|
||||
|
||||
Reference in New Issue
Block a user