cfmgrv4 init...4

This commit is contained in:
최준흠 2024-10-25 13:01:54 +09:00
parent e71752c3f1
commit f08172e272
5 changed files with 22 additions and 272 deletions

View File

@ -23,55 +23,34 @@ class Cloudflare extends BaseController
parent::initController($request, $response, $logger);
// $this->_db = \Config\Database::connect();
}
private function auth_process(mixed $uid = false): array
{
$authModel = model(AuthModel::class);
if (is_numeric($uid) && $uid > 0) {
$authModel->where(AuthModel::PK, intval($uid));
} else {
$authModel->where('status', DEFAULTS["STATUS"]);
}
$entitys = $authModel->getEntitys();
log_message("debug", $authModel->getLastQuery());
return $entitys;
}
private function account_process(AuthEntity $auth_entity): array
{
$account = new AccountService();
return $account->reload($auth_entity);
}
private function zone_process(AccountEntity $account_entity): array
{
$zone = new ZoneService();
return $zone->reload($account_entity);
}
private function record_process(ZoneEntity $zone_entity): array
{
$record = new RecordService();
return $record->reload($zone_entity);
}
private function firewall_process(ZoneEntity $zone_entity): array
{
$firewall = new FirewallService();
return $firewall->reload($zone_entity);
}
public function reload(mixed $uid = false): void
{
//Transaction Start
// $this->_db->transStart();
try {
$auths = $this->auth_process($uid);
foreach ($auths as $auth) {
$accounts = $this->account_process($auth);
foreach (array_values($accounts) as $account) {
$zones = $this->zone_process($account);
$auth_model = model(AuthModel::class);
if (is_numeric($uid) && $uid > 0) {
$auth_model->where(AuthModel::PK, intval($uid));
} else {
$auth_model->where('status', DEFAULTS["STATUS"]);
}
$auth_entitys = $auth_model->getEntitys();
foreach ($auth_entitys as $auth_entity) {
$account = new AccountService();
$account_entitys = $account->reload($auth_entity);
foreach (array_values($account_entitys) as $account_entity) {
$zone = new ZoneService();
$record = new RecordService();
$firewall = new FirewallService();
$zone_entitys = $zone->reload($account_entity);
$cnt = 1;
$total = count($zones);
foreach (array_values($zones) as $zone) {
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone->getTitle()}의 Record/Firewall 처리 시작-----------");
$records = $this->record_process($zone);
$firewalls = $this->firewall_process($zone);
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone->getTitle()}의 Record/Firewall 처리 완료-----------");
$total = count($zone_entitys);
foreach (array_values($zone_entitys) as $zone_entity) {
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone_entity->getTitle()}의 Record/Firewall 처리 시작-----------");
$record->reload($zone_entity);
$firewall->reload($zone_entity);
log_message("notice", "\n-----------[{$cnt}/$total}] Zone {$zone_entity->getTitle()}의 Record/Firewall 처리 완료-----------");
$cnt++;
}
}

View File

@ -1,76 +0,0 @@
<?php
namespace App\Controllers\Cloudflare;
use App\Controllers\MVController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\Cloudflare\AccountModel;
use App\Models\Cloudflare\AuthModel;
use App\Models\Cloudflare\RecordModel;
use App\Models\Cloudflare\ZoneModel;
abstract class CloudflareController extends MVController
{
private $_authModel = null;
private $_accountModel = null;
private $_zoneModel = null;
private $_recordModel = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
final protected function getAuthModel(): AuthModel
{
if ($this->_authModel === null) {
$this->_authModel = new AuthModel();
}
return $this->_authModel;
}
final protected function getAccountModel(): AccountModel
{
if ($this->_accountModel === null) {
$this->_accountModel = new AccountModel();
}
return $this->_accountModel;
}
final protected function getZoneModel(): ZoneModel
{
if ($this->_zoneModel === null) {
$this->_zoneModel = new ZoneModel();
}
return $this->_zoneModel;
}
final protected function getRecordModel(): RecordModel
{
if ($this->_recordModel === null) {
$this->_recordModel = new RecordModel();
}
return $this->_recordModel;
}
//Trigger
protected function webhook_process(): void
{
log_message("debug", var_export($this->request->getVar(), true));
}
final protected function webhook_procedure(): ResponseInterface
{
//Transaction Start
$this->getService()->getModel()->transStart();
try {
// 여기에 필요한 로직을 구현하세요
log_message("debug", var_export($this->request->getVar(), true));
$this->webhook_process();
$this->message = "{$this->class_name}: Trigger 작업이 완료되었습니다.";
$this->getService()->getModel()->transCommit();
log_message("notice", __FUNCTION__ . $this->message);
return $this->response->setJSON(['status' => 200, $this->message], 200);
} catch (\Exception $e) {
//Transaction Rollback
$this->getService()->getModel()->transRollback();
log_message("error", $e->getMessage());
return $this->response->setJSON(['status' => 500, 'message' => $e->getMessage()], 500);
}
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace App\Controllers\Cloudflare;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\Cloudflare\FirewallModel;
use App\Services\Cloudflare\FirewallService;
class FirewallController extends CloudflareController
{
private $_zone_entity = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
final protected function getModel(): FirewallModel
{
if ($this->model === null) {
$this->model = new FirewallModel();
}
return $this->model;
}
protected function getService(): FirewallService
{
if ($this->service === null) {
$this->service = new FirewallService();
}
return $this->service;
}
//Trigger작업
protected function webhook_process(): void
{
parent::webhook_process();
// //자신정보정의
// $this->entity = $this->getModel()->getEntityByPK($uid);
// if ($this->entity === null) {
// throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
// }
// //부모데이터정의
// $this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent());
// //Socket처리
// $this->entity = $this->getMyLibrary()->sync($this->entity);
log_message("notice", message: "Firewall Trigger 작업완료");
}
public function webhook(): ResponseInterface
{
return $this->webhook_procedure();
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace App\Controllers\Cloudflare;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\Cloudflare\RecordModel;
use App\Services\Cloudflare\RecordService;
class RecordController extends CloudflareController
{
private $_zone_entity = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
final protected function getModel(): RecordModel
{
if ($this->model === null) {
$this->model = new RecordModel();
}
return $this->model;
}
protected function getService(): RecordService
{
if ($this->service === null) {
$this->service = new RecordService();
}
return $this->service;
}
//Trigger작업
protected function webhook_process(): void
{
parent::webhook_process();
// //자신정보정의
// $this->entity = $this->getModel()->getEntityByPK($uid);
// if ($this->entity === null) {
// throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
// }
// //부모데이터정의
// $this->_zone_entity = $this->getZoneModel()->getEntityByPK($this->entity->getParent());
// //Socket처리
// $this->entity = $this->getMyLibrary()->sync($this->entity);
log_message("notice", "Record Trigger 작업완료");
}
public function webhook(): ResponseInterface
{
return $this->webhook_procedure();
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace App\Controllers\Cloudflare;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Models\Cloudflare\ZoneModel;
use App\Services\Cloudflare\ZoneService;
class ZoneController extends CloudflareController
{
private $_account_entity = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
final protected function getModel(): ZoneModel
{
if ($this->model === null) {
$this->model = new ZoneModel();
}
return $this->model;
}
protected function getService(): ZoneService
{
if ($this->service === null) {
$this->service = new ZoneService();
}
return $this->service;
}
//Trigger작업
protected function webhook_process(): void
{
parent::webhook_process();
// //자신정보정의
// $this->entity = $this->getModel()->getEntityByPK($uid);
// if ($this->entity === null) {
// throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
// }
// //부모데이터정의
// $this->_account_entity = $this->getAccountModel()->getEntityByPK($this->entity->getParent());
// //Socket처리
// $this->entity = $this->getMyLibrary()->sync($this->entity);
log_message("notice", "Zone Trigger 작업완료");
}
public function webhook(): ResponseInterface
{
return $this->webhook_procedure();
}
}