refactor: implement centralized cache key management and add utility traits for vCenter SSH operations.
This commit is contained in:
@@ -10,6 +10,7 @@ use App\Models\Type;
|
||||
use App\Repositories\Group\GroupRepositoryInterface;
|
||||
use App\Repositories\Log\LogRepositoryInterface;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Support\Cache\CacheKey;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -154,23 +155,19 @@ class GroupController extends Controller
|
||||
/**
|
||||
* Clear all group and product-related caches
|
||||
*/
|
||||
private function clearGroupCaches($typeId)
|
||||
private function clearGroupCaches($typeId): void
|
||||
{
|
||||
// Clear general shop caches
|
||||
Cache::forget('shop_types_with_groups');
|
||||
|
||||
Cache::forget(CacheKey::build('group', 'shop-types'));
|
||||
|
||||
if ($typeId) {
|
||||
// Clear type-specific group cache
|
||||
Cache::forget("groups_type_{$typeId}");
|
||||
|
||||
// Clear all product caches for this type
|
||||
Cache::forget(CacheKey::build('group', 'by-type', ['type_id' => $typeId]));
|
||||
|
||||
$groups = $this->groupRepository->filter(['type_id' => $typeId]);
|
||||
foreach ($groups as $group) {
|
||||
Cache::forget("products_type_{$typeId}_group_{$group->id}");
|
||||
Cache::forget(CacheKey::build('group', 'products', ['type_id' => $typeId, 'group_id' => $group->id]));
|
||||
if ($group->slug) {
|
||||
// Clear paginated product caches for multiple pages
|
||||
for ($page = 1; $page <= 10; $page++) {
|
||||
Cache::forget("group_products_{$group->slug}_page_{$page}");
|
||||
Cache::forget(CacheKey::build('group', 'slug-products', ['page' => $page, 'slug' => $group->slug]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -560,6 +560,105 @@ class VCenterManagerController extends Controller
|
||||
return response()->json(['error' => true, 'message' => $errorData['value'] ?? $errorData['error_type'] ?? 'Failed to power off VM'], 400);
|
||||
}
|
||||
|
||||
public function restart(Request $request)
|
||||
{
|
||||
$vmId = (int) $request->input('vm_id');
|
||||
|
||||
$vm = VirtualMachines::find($vmId);
|
||||
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'VM not found'], 404);
|
||||
}
|
||||
|
||||
$vmwareId = $vm->vm_id;
|
||||
|
||||
if (!$vmwareId) {
|
||||
return response()->json(['error' => true, 'message' => 'VM VMware ID not found'], 400);
|
||||
}
|
||||
|
||||
$vcenter = $vm->datacenter?->vCenter ?? $vm->host?->datacenter?->vCenter;
|
||||
|
||||
if (!$vcenter) {
|
||||
return response()->json(['error' => true, 'message' => 'vCenter not found'], 400);
|
||||
}
|
||||
|
||||
$vCenterService = new \App\Http\Services\vCenter();
|
||||
|
||||
if (!$vCenterService->loginServer($vcenter)) {
|
||||
return response()->json(['error' => true, 'message' => 'Cannot login to vCenter'], 400);
|
||||
}
|
||||
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders(['vmware-api-session-id' => $vCenterService->token])
|
||||
->post("https://{$vCenterService->apiUri}/rest/vcenter/vm/$vmwareId/power/reset");
|
||||
|
||||
if ($response->status() === 200) {
|
||||
return response()->json(['error' => false, 'message' => 'Restart command sent successfully']);
|
||||
}
|
||||
|
||||
$errorData = $response->json();
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $errorData['value'] ?? $errorData['error_type'] ?? 'Failed to restart VM',
|
||||
], 400);
|
||||
}
|
||||
|
||||
public function snapshot(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'vm_id' => 'required|integer',
|
||||
'name' => 'required|string|max:255',
|
||||
'description' => 'nullable|string|max:1000',
|
||||
]);
|
||||
|
||||
$vmId = (int) $request->input('vm_id');
|
||||
|
||||
$vm = VirtualMachines::find($vmId);
|
||||
|
||||
if (!$vm) {
|
||||
return response()->json(['error' => true, 'message' => 'VM not found'], 404);
|
||||
}
|
||||
|
||||
$vmwareId = $vm->vm_id;
|
||||
|
||||
if (!$vmwareId) {
|
||||
return response()->json(['error' => true, 'message' => 'VM VMware ID not found'], 400);
|
||||
}
|
||||
|
||||
$vcenter = $vm->datacenter?->vCenter ?? $vm->host?->datacenter?->vCenter;
|
||||
|
||||
if (!$vcenter) {
|
||||
return response()->json(['error' => true, 'message' => 'vCenter not found'], 400);
|
||||
}
|
||||
|
||||
$vCenterService = new \App\Http\Services\vCenter();
|
||||
|
||||
if (!$vCenterService->loginServer($vcenter)) {
|
||||
return response()->json(['error' => true, 'message' => 'Cannot login to vCenter'], 400);
|
||||
}
|
||||
|
||||
$response = Http::withoutVerifying()
|
||||
->withHeaders(['vmware-api-session-id' => $vCenterService->token])
|
||||
->post("https://{$vCenterService->apiUri}/rest/vcenter/vm/$vmwareId/snapshot", [
|
||||
'spec' => [
|
||||
'name' => $request->input('name'),
|
||||
'description' => $request->input('description', ''),
|
||||
'memory' => false,
|
||||
'quiesce' => false,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($response->status() === 200) {
|
||||
return response()->json(['error' => false, 'message' => 'Snapshot created successfully']);
|
||||
}
|
||||
|
||||
$errorData = $response->json();
|
||||
return response()->json([
|
||||
'error' => true,
|
||||
'message' => $errorData['value'] ?? $errorData['error_type'] ?? 'Failed to create snapshot',
|
||||
], 400);
|
||||
}
|
||||
|
||||
public function getPoweredOffVms()
|
||||
{
|
||||
$vms = VirtualMachines::whereNotNull('vm_id')
|
||||
|
||||
Reference in New Issue
Block a user