IpAddressProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxVPS Product developed. (27.03.19)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\IpAddress\Providers;
  20. use MGProvision\Proxmox\v2\Api;
  21. use MGProvision\Proxmox\v2\VmFactory;
  22. use ModulesGarden\ProxmoxAddon\App\Models\IpAddress;
  23. use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
  24. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  25. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  26. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\IpSetIpFilterService;
  27. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\NetworkService;
  28. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  29. use ModulesGarden\ProxmoxAddon\App\Services\IpLogService;
  30. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  31. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  32. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  33. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  34. use Illuminate\Database\Capsule\Manager as DB;
  35. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  36. class IpAddressProvider extends BaseDataProvider implements ClientArea
  37. {
  38. use ApiService;
  39. use ProductService;
  40. private $networkService;
  41. /**
  42. * @var IpSetIpFilterService
  43. */
  44. private $ipSetIpFilterService;
  45. /**
  46. * IpAddressProvider constructor.
  47. */
  48. public function __construct()
  49. {
  50. parent::__construct();
  51. $this->networkService = new NetworkService();
  52. $this->ipSetIpFilterService = new IpSetIpFilterService();
  53. }
  54. public function read()
  55. {
  56. if ($this->actionElementId && $this->actionElementId != "diskDataTable")
  57. {
  58. $this->data['id'] = $this->actionElementId;
  59. $this->data['ip'] = VmIpAddress::where("id", $this->actionElementId)->value("ip");
  60. }
  61. }
  62. public function create()
  63. {
  64. $ip = new VmIpAddress();
  65. $ip->fill($this->formData);
  66. $ip->hosting_id = $this->getWhmcsParamByKey("serviceid");
  67. $ip->server_id = $this->getWhmcsParamByKey("serverid");
  68. $this->networkService->create([$ip]);
  69. return (new HtmlDataJsonResponse())
  70. ->setStatusSuccess()
  71. ->setMessageAndTranslate('The IP Address has been added successfully');
  72. }
  73. public function update()
  74. {
  75. }
  76. public function delete()
  77. {
  78. try {
  79. $this->api();
  80. $ipunassigned = [];
  81. DB::beginTransaction();
  82. Api::beginTransaction();
  83. //delete ip
  84. /**
  85. * @var VmIpAddress $ip
  86. */
  87. $ip = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  88. ->where("id", $this->formData['id'])
  89. ->firstOrFail();
  90. $ip->delete();
  91. //unlock in proxmox addon
  92. IpAddress::where('ip', $ip->ip)
  93. ->where('hosting_id', $ip->hosting_id)
  94. ->update(["hosting_id" => "0", 'last_check' => Utility::timeStamp()]);
  95. //delete network device
  96. if($ip->vm_id && $ip->net){
  97. $vm = (new VmFactory)->fromVmModel($ip->vmModel);
  98. sl('Vm')->setVm($vm);
  99. if($vm->config()[$ip->net]){
  100. $vm->deleteConfig($ip->net);
  101. }
  102. }
  103. //delete virtual interface
  104. VirtualInterface::ofHostingId($ip->hosting_id)
  105. ->ofIp($ip->ip)
  106. ->delete();
  107. //delete in hosting
  108. $ip->hosting->ipDelete($ip->ip);
  109. $ipunassigned[] = $ip->ip;
  110. if ($this->configuration()->isIpsetIpFilter() && $vm)
  111. {
  112. $this->ipSetIpFilterService->create();
  113. }
  114. $ip->hosting->save();
  115. if(!empty( $ipunassigned)){
  116. (new IpLogService())->unassigned($ipunassigned);
  117. }
  118. DB::commit();
  119. Api::commit();
  120. return (new HtmlDataJsonResponse())
  121. ->setStatusSuccess()
  122. ->setMessageAndTranslate('The IP Address has been deleted successfully');
  123. }catch (\Exception $ex){
  124. DB::rollBack();
  125. Api::commit();
  126. throw $ex;
  127. }
  128. }
  129. }