106 lines
3.5 KiB
PHP
106 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\VirtualMachine\Sync;
|
|
|
|
use App\Http\Services\vCenter as ServiceVCenter;
|
|
use App\Models\ModuleDatacenter;
|
|
use App\Models\ModuleHost;
|
|
use App\Models\ModuleNetwork;
|
|
use App\Models\vCenter;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
class SyncNetwork implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $vCenter = null;
|
|
protected $data = null;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->data = vCenter::select('id','host','user','password','token')->get();
|
|
$this->vCenter = new ServiceVCenter;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
foreach ($this->data as $item) {
|
|
$this->vCenter->apiUri = $item->host;
|
|
|
|
if (!$item->token) {
|
|
$token = $this->vCenter->loginServer($item);
|
|
if ($token) {
|
|
vCenter::where('id', $item->id)->update(['token' => $token]);
|
|
$this->vCenter->token = $token;
|
|
}
|
|
}
|
|
$this->vCenter->token = $item->token;
|
|
|
|
if (! $this->vCenter->token) {
|
|
continue;
|
|
}
|
|
|
|
$dataCenter = ModuleDatacenter::where('vcenter_id', $item->id)->select('id', 'datacenter_id')->get();
|
|
foreach ($dataCenter as $_item) {
|
|
$allHost = ModuleHost::where('module_datacenter_id',$_item->id)->select('id','name')->get();
|
|
if ($allHost)
|
|
{
|
|
foreach ($allHost as $hostItem)
|
|
{
|
|
|
|
$network = $this->vCenter->loadNetWork($item,$hostItem->name);
|
|
if ($network === "Login_Again") {
|
|
$token = $this->vCenter->loginServer($item);
|
|
if ($token)
|
|
{
|
|
vCenter::where('id',$item->id)->update(['token' => $token]);
|
|
$this->vCenter->token = $token;
|
|
$network = $this->vCenter->loadNetWork($item,$hostItem->name);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if ($network)
|
|
{
|
|
foreach ($network as $_subItem)
|
|
{
|
|
if (! ModuleNetwork::where('network',$_subItem['network'])->first())
|
|
{
|
|
ModuleNetwork::create([
|
|
'name' => $_subItem['name'],
|
|
'network' => $_subItem['network'],
|
|
'type' => $_subItem['type'],
|
|
'ModuleHost_id' => $hostItem->id
|
|
]);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Notify success via Telegram
|
|
(new \App\Http\Services\Telegram())->SendJobRunInfo('SyncNetwork', 'Success', [
|
|
'message' => 'Sync network completed successfully',
|
|
'synced_vcenters' => $this->data->count(),
|
|
]);
|
|
}
|
|
}
|