refactor: implement centralized cache key management and add utility traits for vCenter SSH operations.
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
|
||||
namespace App\Http\Services\vCenter;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Support\Cache\CacheKey;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use App\Http\Facade\SignRSA;
|
||||
|
||||
class SnapshotService
|
||||
@@ -68,10 +69,10 @@ class SnapshotService
|
||||
} catch (\Exception $e) {
|
||||
return ['error' => true, 'message' => 'Lỗi setup credentials: ' . $e->getMessage()];
|
||||
}
|
||||
|
||||
|
||||
$vmware_name = (string) $product->name;
|
||||
// In original code, it checks checkApiExits(), if true proceed.
|
||||
|
||||
|
||||
if ($this->client->checkApiExits()) {
|
||||
try {
|
||||
$Snapshot = $vmware_name . "_" . date('d_m_Y_H_i_s');
|
||||
@@ -102,24 +103,23 @@ class SnapshotService
|
||||
|
||||
private function clearSnapshotCache($product): void
|
||||
{
|
||||
$vmware_name = (string) $product->name;
|
||||
$cacheKey = "snapshots_{$product->id}_{$vmware_name}";
|
||||
$cacheKey = CacheKey::build('snapshot', 'list', ['vm_id' => $product->id]);
|
||||
Cache::forget($cacheKey);
|
||||
Log::channel('Custom')->info("🗑️ Cleared snapshot cache for VM: {$vmware_name}");
|
||||
Log::channel('Custom')->info("🗑️ Cleared snapshot cache for VM: {$product->name}");
|
||||
}
|
||||
|
||||
public function getSnapShot($product): string|array
|
||||
{
|
||||
// ... (Similar logic, using SOAP)
|
||||
// Since SOAP logic is quite complex and self-contained in vCenter.php,
|
||||
// Since SOAP logic is quite complex and self-contained in vCenter.php,
|
||||
// I'll copy it here but need to adapt $this->callSoap etc.
|
||||
// Actually, callSoap uses curl with manual implementation.
|
||||
// I should probably simplify or copy that helper too.
|
||||
|
||||
|
||||
// This method heavily relies on internal SOAP helpers.
|
||||
// For simplicity in this 'splitting' task, I will implement the public method
|
||||
// For simplicity in this 'splitting' task, I will implement the public method
|
||||
// and add the private helpers it depends on.
|
||||
|
||||
|
||||
try {
|
||||
$vcenterModel = $this->resolveVCenterFromProduct($product);
|
||||
|
||||
@@ -132,10 +132,8 @@ class SnapshotService
|
||||
return ['error' => true, 'message' => 'Lỗi setup credentials: ' . $e->getMessage()];
|
||||
}
|
||||
|
||||
$vmware_name = (string) $product->name;
|
||||
$vmId = $product->vmware_id;
|
||||
|
||||
$cacheKey = "snapshots_{$product->id}_{$vmware_name}";
|
||||
$vmId = $product->vmware_id;
|
||||
$cacheKey = CacheKey::build('snapshot', 'list', ['vm_id' => $product->id]);
|
||||
$cachedResult = Cache::get($cacheKey);
|
||||
|
||||
if ($cachedResult !== null) {
|
||||
@@ -234,7 +232,7 @@ class SnapshotService
|
||||
try {
|
||||
$xml = simplexml_load_string($result['body']);
|
||||
if ($xml === false) return [];
|
||||
|
||||
|
||||
$xml->registerXPathNamespace('u', 'urn:vim25');
|
||||
$json = json_encode($xml);
|
||||
$array = json_decode($json, true);
|
||||
@@ -271,14 +269,14 @@ class SnapshotService
|
||||
|
||||
if (isset($node['childSnapshotList'])) {
|
||||
$children = $node['childSnapshotList'];
|
||||
|
||||
|
||||
// Check if 'snapshot' key exists directly (single child wrapped)
|
||||
if (isset($children['snapshot'])) {
|
||||
// If it's an array of snapshots or single object?
|
||||
// XML conversion: if strictly one child, might be direct. if multiple, array.
|
||||
// But simplexml usually produces array if multiple same tags.
|
||||
// However, structure is usually childSnapshotList -> snapshot (array or single)
|
||||
|
||||
|
||||
$snaps = $children['snapshot'];
|
||||
// If $snaps has numeric keys, it's array. Else single.
|
||||
if (array_keys($snaps) !== range(0, count($snaps) - 1)) {
|
||||
@@ -288,7 +286,7 @@ class SnapshotService
|
||||
foreach ($snaps as $child) {
|
||||
$list = array_merge($list, $this->flattenSnapshotTree($child));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
@@ -296,7 +294,7 @@ class SnapshotService
|
||||
private function callSoap($soapBody, $cookie = null)
|
||||
{
|
||||
$curl = curl_init();
|
||||
$host = $this->client->apiUri;
|
||||
$host = $this->client->apiUri;
|
||||
|
||||
$headers = [
|
||||
'Content-Type: text/xml',
|
||||
@@ -327,11 +325,11 @@ class SnapshotService
|
||||
));
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
|
||||
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
|
||||
$header = substr($response, 0, $header_size);
|
||||
$body = substr($response, $header_size);
|
||||
|
||||
|
||||
curl_close($curl);
|
||||
|
||||
// Parse cookie
|
||||
@@ -344,7 +342,7 @@ class SnapshotService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return ['body' => $body, 'cookie' => $cookieValue];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user