95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Create;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class CheckIPAddress implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
protected string $vmware_id;
|
|
protected $serviceVCenter;
|
|
protected $data;
|
|
protected array $info;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($vmware_id, $data,$virtual_info,$service)
|
|
{
|
|
$this->vmware_id = $vmware_id;
|
|
$this->serviceVCenter = $service;
|
|
$this->data = $data;
|
|
$this->info = $virtual_info;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$DetailVirtualMachine = $this->serviceVCenter->GetIPAddressVirtualMachine($this->vmware_id);
|
|
|
|
if ($DetailVirtualMachine['error']) {
|
|
throw new \Exception(json_encode($DetailVirtualMachine['message']));
|
|
}
|
|
|
|
// Get expected IP address with null safety
|
|
$expectedIP = null;
|
|
|
|
// Try to get IP from old relationship first
|
|
if ($this->data && isset($this->data->IpAddress) && $this->data->IpAddress) {
|
|
$expectedIP = $this->data->IpAddress->ip;
|
|
}
|
|
|
|
// If no IP from old relationship, try to get from ModuleVmware
|
|
if (!$expectedIP && $this->data) {
|
|
// Try to find ModuleVmware by vmware_id
|
|
$moduleVmware = \App\Models\ModuleVmware::with('ipAddress')
|
|
->where('vmware_id', $this->vmware_id)
|
|
->first();
|
|
|
|
if ($moduleVmware && $moduleVmware->ipAddress) {
|
|
$expectedIP = $moduleVmware->ipAddress->ip;
|
|
}
|
|
}
|
|
|
|
// If still no expected IP, log warning but don't fail
|
|
if (!$expectedIP) {
|
|
\Illuminate\Support\Facades\Log::warning('⚠️ Không tìm thấy expected IP cho VM', [
|
|
'vmware_id' => $this->vmware_id,
|
|
'data_class' => $this->data ? get_class($this->data) : 'null',
|
|
'has_ip_address_relation' => isset($this->data->IpAddress) && $this->data->IpAddress ? 'yes' : 'no'
|
|
]);
|
|
|
|
// Skip IP check and continue to next job
|
|
$jobs_configDB = new ConfigDatabase($this->data, $this->info);
|
|
dispatch($jobs_configDB)->delay(now()->addSeconds(5));
|
|
return;
|
|
}
|
|
|
|
// Compare actual vs expected IP
|
|
if ($DetailVirtualMachine['data']['ip_address'] !== $expectedIP) {
|
|
\Illuminate\Support\Facades\Log::warning('⚠️ IP address mismatch', [
|
|
'vmware_id' => $this->vmware_id,
|
|
'actual_ip' => $DetailVirtualMachine['data']['ip_address'],
|
|
'expected_ip' => $expectedIP
|
|
]);
|
|
throw new \Exception('IP address mismatch: actual=' . $DetailVirtualMachine['data']['ip_address'] . ', expected=' . $expectedIP);
|
|
}
|
|
$jobs_configDB = new ConfigDatabase($this->data,$this->info);
|
|
dispatch($jobs_configDB)->delay(now()->addSeconds(5));
|
|
|
|
}
|
|
}
|