IpAddressProvider.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\Utility;
  30. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
  31. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  32. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  33. use Illuminate\Database\Capsule\Manager as DB;
  34. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  35. class IpAddressProvider extends BaseDataProvider implements ClientArea
  36. {
  37. use ApiService;
  38. use ProductService;
  39. private $networkService;
  40. /**
  41. * @var IpSetIpFilterService
  42. */
  43. private $ipSetIpFilterService;
  44. /**
  45. * IpAddressProvider constructor.
  46. */
  47. public function __construct()
  48. {
  49. parent::__construct();
  50. $this->networkService = new NetworkService();
  51. $this->ipSetIpFilterService = new IpSetIpFilterService();
  52. }
  53. public function read()
  54. {
  55. if ($this->actionElementId && $this->actionElementId != "diskDataTable")
  56. {
  57. $this->data['id'] = $this->actionElementId;
  58. $this->data['ip'] = VmIpAddress::where("id", $this->actionElementId)->value("ip");
  59. }
  60. }
  61. public function create()
  62. {
  63. $ip = new VmIpAddress();
  64. $ip->fill($this->formData);
  65. $ip->hosting_id = $this->getWhmcsParamByKey("serviceid");
  66. $ip->server_id = $this->getWhmcsParamByKey("serverid");
  67. $this->networkService->create([$ip]);
  68. return (new HtmlDataJsonResponse())
  69. ->setStatusSuccess()
  70. ->setMessageAndTranslate('The IP Address has been added successfully');
  71. }
  72. public function update()
  73. {
  74. }
  75. public function delete()
  76. {
  77. try {
  78. $this->api();
  79. DB::beginTransaction();
  80. Api::beginTransaction();
  81. //delete ip
  82. /**
  83. * @var VmIpAddress $ip
  84. */
  85. $ip = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'))
  86. ->where("id", $this->formData['id'])
  87. ->firstOrFail();
  88. $ip->delete();
  89. //unlock in proxmox addon
  90. IpAddress::where('ip', $ip->ip)
  91. ->where('hosting_id', $ip->hosting_id)
  92. ->update(["hosting_id" => "0", 'last_check' => Utility::timeStamp()]);
  93. //delete network device
  94. if($ip->vm_id && $ip->net){
  95. $vm = (new VmFactory)->fromVmModel($ip->vmModel);
  96. sl('Vm')->setVm($vm);
  97. if($vm->config()[$ip->net]){
  98. $vm->deleteConfig($ip->net);
  99. }
  100. }
  101. //delete virtual interface
  102. VirtualInterface::ofHostingId($ip->hosting_id)
  103. ->ofIp($ip->ip)
  104. ->delete();
  105. //delete in hosting
  106. $ip->hosting->ipDelete($ip->ip);
  107. if ($this->configuration()->isIpsetIpFilter() && $vm)
  108. {
  109. $this->ipSetIpFilterService->create();
  110. }
  111. DB::commit();
  112. Api::commit();
  113. return (new HtmlDataJsonResponse())
  114. ->setStatusSuccess()
  115. ->setMessageAndTranslate('The IP Address has been deleted successfully');
  116. }catch (\Exception $ex){
  117. DB::rollBack();
  118. Api::commit();
  119. throw $ex;
  120. }
  121. }
  122. }