NodeSettingProvider.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  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\ProxmoxAddon\App\UI\Cluster\Providers;
  20. use MGProvision\Proxmox\v2\ProxmoxApiException;
  21. use MGProvision\Proxmox\v2\repository\StorageRepository;
  22. use ModulesGarden\ProxmoxAddon\App\Models\NodeSetting;
  23. use ModulesGarden\ProxmoxAddon\App\Services\BaseService;
  24. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  25. use ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  26. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
  27. use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
  28. /**
  29. *
  30. * Description of RangeVmProvider
  31. *
  32. * @author Pawel Kopec <pawelk@modulesgardne.com>
  33. */
  34. class NodeSettingProvider extends BaseModelDataProvider implements AdminArea
  35. {
  36. use BaseService;
  37. public function __construct()
  38. {
  39. parent::__construct(NodeSetting::class);
  40. }
  41. public function read()
  42. {
  43. if (!$this->actionElementId)
  44. {
  45. return false;
  46. }
  47. $this->data['node'] = $this->actionElementId;
  48. $this->data['server_id'] = $this->getRequestValue('id');
  49. $query = NodeSetting::ofServer($this->getRequestValue('id'))
  50. ->ofNode($this->data['node']);
  51. foreach ($query->get() as $nodeSetting)
  52. {
  53. $this->data[$nodeSetting->setting] = $nodeSetting->value;
  54. }
  55. if (isset($this->data['vmCreate']))
  56. {
  57. $vmCreate = $this->data['vmCreate'];
  58. $this->data['vmCreate'] = $vmCreate == 1 ? "on" : "off";
  59. }
  60. $this->loadDefaultStorages();
  61. sl('lang')->addReplacementConstant('node', $this->data['node']);
  62. }
  63. private function loadDefaultStorages(){
  64. //defaultStorage
  65. $this->availableValues['defaultStorage'][0] = sl("lang")->tr("None");
  66. if(!$this->data['node']){
  67. return;
  68. }
  69. try{
  70. $this->loadRequestObj()->setServerId($this->getRequestValue('id'))->getApi()->setInstance();
  71. $storageRepository = new StorageRepository();
  72. $storageRepository->findByNodes([$this->data['node']])
  73. ->findEnabed();
  74. foreach ($storageRepository->fetch() as $entity)
  75. {
  76. if (!in_array("images", $entity->getContentAsArray()))
  77. {
  78. continue;
  79. }
  80. $this->availableValues['defaultStorage'][$entity->getStorage()] = $entity->getStorage();
  81. }
  82. }catch (ProxmoxApiException $ex){
  83. }
  84. }
  85. public function update()
  86. {
  87. $fillable = ['vmCreate', 'vmsMax', 'cpuMax', 'diskMax', 'ramMax', 'defaultStorage'];
  88. $vmCreate = $this->formData['vmCreate'];
  89. $this->formData['vmCreate'] = $vmCreate == "on" ? 1 : 0;
  90. foreach ($this->formData as $key => $value)
  91. {
  92. if (!in_array($key, $fillable))
  93. {
  94. continue;
  95. }
  96. //Create & update
  97. if (isset($value))
  98. {
  99. $entity = NodeSetting::OfServer($this->formData['server_id'])->ofNode($this->formData['node'])->ofSetting($key)->first();
  100. if (!$entity)
  101. {
  102. $entity = new NodeSetting();
  103. }
  104. $entity->fill(['node' => $this->formData['node'], 'server_id' => $this->formData['server_id'], 'setting' => $key, 'value' => $value])
  105. ->save();
  106. //Delete
  107. }
  108. else
  109. {
  110. if (empty($value))
  111. {
  112. NodeSetting::OfServer($this->formData['server_id'])->ofNode($$this->formData['node'])->ofSetting($key)->delete();
  113. }
  114. }
  115. }
  116. sl('lang')->addReplacementConstant('node', $this->formData['node']);
  117. return (new HtmlDataJsonResponse())->setMessageAndTranslate('The settings for the node :node: has been edited successfully');
  118. }
  119. }