ApiService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\ProxmoxAddon\App\Services;
  20. use MGProvision\Proxmox\v2\Api;
  21. use MGProvision\Proxmox\v2\models\Kvm;
  22. use MGProvision\Proxmox\v2\models\Lxc;
  23. use ModulesGarden\ProxmoxAddon\App\Enum\Vps;
  24. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud;
  25. use ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings;
  26. /**
  27. * Trait ApiService
  28. * @package ModulesGarden\ProxmoxAddon\App\Services
  29. */
  30. trait ApiService
  31. {
  32. /**
  33. * @return Api
  34. */
  35. public function api()
  36. {
  37. if (!empty($this->api))
  38. {
  39. return $this->api;
  40. }
  41. $host = $this->getWhmcsParamByKey('serverip') ? $this->getWhmcsParamByKey('serverip') : $this->getWhmcsParamByKey('serverhostname');
  42. if(is_numeric($this->getWhmcsParamByKey('serverport'))){
  43. $host .=":".$this->getWhmcsParamByKey('serverport');
  44. }
  45. $username = $this->getWhmcsParamByKey('serverusername');
  46. $realm = $this->getWhmcsParamByKey('serveraccesshash');
  47. $password = $this->getWhmcsParamByKey('serverpassword');
  48. $this->api = new Api($host, $username, $realm, $password);
  49. $this->api->setInstance();
  50. $this->api->debug(ModuleSettings::isDebug());
  51. return $this->api;
  52. }
  53. /**
  54. * @return Kvm|Lxc
  55. * @throws \Exception
  56. */
  57. public function vm()
  58. {
  59. if (!empty($this->vm))
  60. {
  61. return $this->vm;
  62. }
  63. $virtualization = $this->configuration()->getVirtualization();
  64. $vmid = $this->getWhmcsCustomField(Vps\CustomField::VMID);
  65. $node = $this->getWhmcsCustomField(Vps\CustomField::NODE);
  66. switch (strtolower($virtualization))
  67. {
  68. case 'kvm':
  69. case 'qemu':
  70. $this->vm = new Kvm($node, $vmid);
  71. break;
  72. case 'lxc':
  73. $this->vm = new Lxc($node, $vmid);
  74. break;
  75. default:
  76. throw new \Exception(sprintf("Invalid virtualization type '%s'", $virtualization));
  77. }
  78. $hostname = $this->getWhmcsParamByKey('domain') ? $this->getWhmcsParamByKey('domain') : $this->getWhmcsCustomField(Vps\CustomField::HOSTNAME);
  79. $this->vm->setName($hostname);
  80. $this->vm->setApi($this->api());
  81. $this->vm->setApiParser();
  82. return $this->vm;
  83. }
  84. public function hasVm()
  85. {
  86. return $this->getWhmcsCustomField(Vps\CustomField::VMID) && $this->getWhmcsCustomField(Vps\CustomField::NODE);
  87. }
  88. public function setVm($vm)
  89. {
  90. $this->vm = $vm;
  91. return $this;
  92. }
  93. }