257 lines
8.2 KiB
PHP
257 lines
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\rest;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Services\vCenter;
|
|
use App\Repositories\Module\Datacenter\DatacenterRepositoryInterface;
|
|
use App\Repositories\Module\Datastore\DatastoreRepositoryInterface;
|
|
use App\Repositories\Module\Host\HostRepositoryInterface;
|
|
use App\Repositories\Module\IpAddress\IpAddressRepositoryInterface;
|
|
use App\Repositories\Module\Network\NetworkRepositoryInterface;
|
|
use App\Repositories\OsGroup\OsGroupRepositoryInterface;
|
|
use App\Repositories\vCenter\vCenterRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ModuleController extends Controller
|
|
{
|
|
public function __construct(
|
|
public vCenterRepositoryInterface $vCenterRepository,
|
|
public DatacenterRepositoryInterface $datacenterRepository,
|
|
public HostRepositoryInterface $hostRepository,
|
|
public DatastoreRepositoryInterface $datastoreRepository,
|
|
public NetworkRepositoryInterface $networkRepository,
|
|
public IpAddressRepositoryInterface $ipAddressRepository,
|
|
public vCenter $vCenter,
|
|
public OsGroupRepositoryInterface $osGroupRepository
|
|
)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function getVCenter()
|
|
{
|
|
return response()->json(
|
|
[
|
|
'error' => false,
|
|
'data' => $this->vCenterRepository->filter(['status' => 1])
|
|
]
|
|
);
|
|
}
|
|
|
|
public function getDatacenter(Request $request)
|
|
{
|
|
$vCenter_id = $request->get('vcenter');
|
|
if (!$vCenter_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose vCenter']);
|
|
}
|
|
$vcenter = $this->vCenterRepository->find($vCenter_id);
|
|
|
|
if (!$vcenter) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find vCenter']);
|
|
}
|
|
|
|
$datacenter = $this->datacenterRepository->filter(
|
|
[
|
|
'vcenter_id' => $vCenter_id
|
|
],
|
|
[],
|
|
[],
|
|
['id', 'name']
|
|
);
|
|
|
|
if (count($datacenter) > 0) {
|
|
return response()->json(['error' => false, 'data' => $datacenter]);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Can\'t find Datacenter by vCenter ID #' . $vCenter_id
|
|
]);
|
|
}
|
|
|
|
public function getHost(Request $request)
|
|
{
|
|
$dataCenter_id = $request->get('datacenter');
|
|
if (!$dataCenter_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose Datacenter']);
|
|
}
|
|
$datacenter = $this->datacenterRepository->find($dataCenter_id);
|
|
if (!$datacenter) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Datacenter']);
|
|
}
|
|
|
|
$host = $this->hostRepository->filter(
|
|
[
|
|
'module_datacenter_id' => $datacenter->id
|
|
],
|
|
[],
|
|
[],
|
|
['id', 'name']
|
|
);
|
|
|
|
if (count($host) > 0) {
|
|
return response()->json(['error' => false, 'data' => $host]);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Can\'t find Host by Datacenter ID #' . $dataCenter_id
|
|
]);
|
|
}
|
|
|
|
public function getDatastore(Request $request)
|
|
{
|
|
$host_id = $request->get('host');
|
|
if (!$host_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose Host']);
|
|
}
|
|
$host = $this->hostRepository->find($host_id);
|
|
|
|
if (!$host) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Datacenter']);
|
|
}
|
|
|
|
$datastores = $this->datastoreRepository->filter(
|
|
[
|
|
'module_host_id' => $host_id
|
|
],
|
|
[],
|
|
[],
|
|
['id', 'name']
|
|
);
|
|
if (count($datastores) > 0) {
|
|
return response()->json(['error' => false, 'data' => $datastores]);
|
|
}
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Can\'t find Datastore by Host ID #' . $host_id
|
|
]);
|
|
}
|
|
|
|
public function getNetwork(Request $request)
|
|
{
|
|
$host_id = $request->get('host');
|
|
if (!$host_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose Host']);
|
|
}
|
|
$host = $this->hostRepository->find($host_id);
|
|
|
|
if (!$host) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Datacenter']);
|
|
}
|
|
|
|
|
|
$networks = $this->networkRepository->filter(
|
|
[
|
|
'module_host_id' => $host_id
|
|
],
|
|
[],
|
|
[],
|
|
['id', 'name']
|
|
);
|
|
if (count($networks) > 0) {
|
|
return response()->json(['error' => false, 'data' => $networks]);
|
|
}
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Network by Host ID #' . $host_id]);
|
|
}
|
|
|
|
public function getIpAddress(Request $request)
|
|
{
|
|
$network_id = $request->get('network');
|
|
if (!$network_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose Network']);
|
|
}
|
|
|
|
$network = $this->networkRepository->find($network_id);
|
|
|
|
if (!$network) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Network']);
|
|
}
|
|
|
|
$ipAddress = $this->ipAddressRepository->filter(
|
|
[
|
|
'module_network_id' => $network_id
|
|
],
|
|
[],
|
|
[],
|
|
['id', 'ip']
|
|
);
|
|
if (count($ipAddress) > 0) {
|
|
return response()->json(
|
|
[
|
|
'error' => false,
|
|
'data' => $ipAddress->map(function ($item) {
|
|
$item['name'] = $item['ip'];
|
|
return $item;
|
|
})
|
|
]
|
|
);
|
|
}
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find IP by Network ID #' . $network_id]);
|
|
}
|
|
|
|
public function getOsServerVersion(Request $request)
|
|
{
|
|
$host_id = $request->get('host');
|
|
if (!$host_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose Host']);
|
|
}
|
|
|
|
$host = $this->hostRepository->findWithRelationships($host_id, ['datacenter.vCenter']);
|
|
if (!$host) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Datastore']);
|
|
}
|
|
$this->vCenter->token = $host->Datacenter->vCenter->token ?? '';
|
|
$this->vCenter->apiUri = $host->Datacenter->vCenter->host ?? '';
|
|
|
|
$listVersion = $this->vCenter->getAllVersion($host->template);
|
|
|
|
if ($listVersion === "Login_Again") {
|
|
$token = $this->vCenter->loginServer($host->datacenter->vCenter);
|
|
if ($token) {
|
|
$this->vCenterRepository->update($host->datacenter->vcenter_id, ['token' => $token]);
|
|
$this->vCenter->token = $token;
|
|
}
|
|
}
|
|
$listVersion = $this->vCenter->getAllVersion($host->template);
|
|
if ($listVersion) {
|
|
return response()->json(['error' => false, 'data' => $listVersion]);
|
|
}
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Can\'t find Version OS by Host ID #' . $host_id
|
|
]);
|
|
}
|
|
|
|
public function getOsGroup(Request $request)
|
|
{
|
|
$host_id = $request->get('host');
|
|
|
|
if (!$host_id) {
|
|
return response()->json(['error' => true, 'message' => 'Please choose Host']);
|
|
}
|
|
|
|
$host = $this->hostRepository->find($host_id);
|
|
|
|
if (!$host) {
|
|
return response()->json(['error' => true, 'message' => 'Can\'t find Host']);
|
|
}
|
|
$groups = $this->osGroupRepository->filter(
|
|
[
|
|
'module_host_id' => $host_id
|
|
],
|
|
[],
|
|
[],
|
|
['id', 'name']
|
|
);
|
|
if (count($groups) > 0) {
|
|
return response()->json(['error' => false, 'data' => $groups]);
|
|
}
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Can\'t find Os Group by Host ID #' . $host_id
|
|
]);
|
|
}
|
|
}
|