Factory.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /* * ********************************************************************
  3. * proxmoxCloud product developed. (2016-12-30)
  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 MGProvision\Proxmox\v2;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. /**
  22. * Description of Factory
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgarden.com>
  25. * @version 1.0.0
  26. */
  27. class Factory
  28. {
  29. /**
  30. *
  31. * @param array $params WHMCS params
  32. * @return \MGProvision\Proxmox\v2\Api
  33. */
  34. static function api($params)
  35. {
  36. if(is_numeric($params['serverport']) && $params['serverip'] && !preg_match("/\:/",$params['serverip'])){
  37. $params['serverip'] .=":".$params['serverport'];
  38. }
  39. if(is_numeric($params['serverport']) && $params['serverhostname'] && !preg_match("/\:/",$params['serverhostname'])){
  40. $params['serverhostname'] .=":".$params['serverport'];
  41. }
  42. $host = $params['serverip'] ? $params['serverip'] : $params['serverhostname'];
  43. return new Api($host, $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']);
  44. }
  45. /**
  46. *
  47. * @return \MGProvision\Proxmox\v2\Api
  48. */
  49. static function apiFromServer(\ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server $server)
  50. {
  51. $host = $server->ipaddress ? $server->ipaddress : $server->hostname;
  52. return new Api($host, $server->username, $server->accesshash, decrypt($server->password));
  53. }
  54. static function vm($vServer)
  55. {
  56. if ($vServer instanceof \MGProvision\ProxmoxCloud\models\VServer)
  57. {
  58. $vmid = $vServer->getVmid();
  59. $node = $vServer->getNode();
  60. $virtualization = $vServer->getVirtualization();
  61. }
  62. else
  63. {
  64. $vmid = $vServer->vmid;
  65. $node = $vServer->node;
  66. $virtualization = $vServer->virtualization;
  67. }
  68. switch (strtolower($virtualization))
  69. {
  70. case 'kvm':
  71. case 'qemu':
  72. return new proxmox\models\Kvm($node, $vmid);
  73. break;
  74. case 'lxc':
  75. return new proxmox\models\Lxc($node, $vmid);
  76. break;
  77. default:
  78. throw new \Exception(sprintf("Invalid virtualization type '%s'", $virtualization));
  79. }
  80. }
  81. static function vmVps($product, $params)
  82. {
  83. if (is_string($product))
  84. {
  85. $virtualization = $product;
  86. }
  87. else if (is_object($product) && method_exists($product, 'getConfig'))
  88. {
  89. $virtualization = $product->getConfig('virtualization_type');
  90. }else if(is_object($product) && method_exists($product, 'getVirtualization')){
  91. $virtualization = $product->getVirtualization();
  92. }
  93. switch (strtolower($virtualization))
  94. {
  95. case 'kvm':
  96. case 'qemu':
  97. $vm = new proxmox\models\Kvm($params['customfields']['node'], $params['customfields']['vmid']);
  98. break;
  99. case 'lxc':
  100. $vm = new proxmox\models\Lxc($params['customfields']['node'], $params['customfields']['vmid']);
  101. break;
  102. default:
  103. throw new \Exception(sprintf("Invalid virtualization type '%s'", $virtualization));
  104. }
  105. $vm->setName(!empty($params['domain']) ? $params['domain'] : $params['customfields']['proxmoxHostname']);
  106. return $vm;
  107. }
  108. /**
  109. *
  110. * @param array $params
  111. * @return \MGProvision\Proxmox\v2\models\User
  112. */
  113. static function user(array $params)
  114. {
  115. if (empty($params['customfields']['Authentication']))
  116. {
  117. throw new \Exception("Custom field ('Authentication') is empty.");
  118. }
  119. if (empty($params['username']))
  120. {
  121. throw new \Exception("Servuce field ('Username') is empty.");
  122. }
  123. $userid = $params['username'] . "@" . $params['customfields']['Authentication'];
  124. return new proxmox\models\User($userid);
  125. }
  126. }