NetworkProvider.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\ProxmoxVps\App\UI\Network\Providers;
  20. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  21. use ModulesGarden\ProxmoxAddon\App\Services\Vps\IpSetIpFilterService;
  22. use ModulesGarden\ProxmoxAddon\App\Services\Vps\NetworkService;
  23. use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
  24. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  26. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  27. class NetworkProvider extends BaseDataProvider implements ClientArea
  28. {
  29. use ProductService;
  30. use ApiService;
  31. /**
  32. * @var NetworkService
  33. */
  34. private $networkService;
  35. public function read()
  36. {
  37. if ($this->actionElementId)
  38. {
  39. $this->data['id'] = $this->actionElementId;
  40. }
  41. $this->data['bridge'] = $this->configuration()->getPrivateBridge();
  42. }
  43. public function create()
  44. {
  45. if ($this->vm()->getNetworkDevices($this->configuration()->getPrivateBridge()))
  46. {
  47. return (new HtmlDataJsonResponse())
  48. ->setStatusError()
  49. ->setMessageAndTranslate('Private network limit exceeded');
  50. }
  51. $this->networkService = new NetworkService();
  52. if (!$this->networkService->hasPrivateIpAddress())
  53. {
  54. throw new \Exception("Private IP Addresses are not available.");
  55. }
  56. if ($this->configuration()->isLxc())
  57. {
  58. $this->lxc();
  59. }
  60. else
  61. {
  62. if ($this->configuration()->isQemu())
  63. {
  64. $this->qemu();
  65. }
  66. }
  67. if($this->configuration()->isIpsetIpFilter()){
  68. $ipSetIpFilterService = new IpSetIpFilterService();
  69. $ipSetIpFilterService->create();
  70. }
  71. return (new HtmlDataJsonResponse())
  72. ->setStatusSuccess()
  73. ->setMessageAndTranslate('The Network Device has been created successfully')
  74. ->addData('createButtonStatus', false)
  75. ->setCallBackFunction('pmToggleButton');
  76. }
  77. private function lxc()
  78. {
  79. //Get Private IP Addresses
  80. $ip = $this->networkService->getPrivateIpAddress();
  81. //Create network device
  82. $this->networkService->addPrivate([$ip]);
  83. }
  84. private function qemu()
  85. {
  86. //Get Private IP Addresses
  87. $ip = $this->networkService->getPrivateIpAddress();
  88. //Create network device
  89. $this->networkService->addPrivate([$ip]);
  90. }
  91. public function update()
  92. {
  93. }
  94. public function delete()
  95. {
  96. if (!$this->vm()->getNetworkDevices($this->configuration()->getPrivateBridge()))
  97. {
  98. return (new HtmlDataJsonResponse())
  99. ->setStatusError()
  100. ->setMessageAndTranslate('Private network device not found');
  101. }
  102. $this->networkService = new NetworkService();
  103. $this->networkService->deleteByNetworkId([$this->formData['id']]);
  104. if($this->configuration()->isIpsetIpFilter()){
  105. $ipSetIpFilterService = new IpSetIpFilterService();
  106. $ipSetIpFilterService->create();
  107. }
  108. return (new HtmlDataJsonResponse())
  109. ->setStatusSuccess()
  110. ->setMessageAndTranslate('The Network Device has been deleted successfully')
  111. ->addData('createButtonStatus', true)
  112. ->setCallBackFunction('pmToggleButton');
  113. }
  114. }