http://modulesgarden.com * CONTACT -> contact@modulesgarden.com * * * This software is furnished under a license and may be used and copied * only in accordance with the terms of such license and with the * inclusion of the above copyright notice. This software or any other * copies thereof may not be provided or otherwise made available to any * other person. No title to and ownership of the software is hereby * transferred. * * * ******************************************************************** */ namespace MGProvision\Proxmox\v2; use MGProvision\Proxmox\v2 as proxmox; /** * Description of Factory * * @author Pawel Kopec * @version 1.0.0 */ class Factory { /** * * @param array $params WHMCS params * @return \MGProvision\Proxmox\v2\Api */ static function api($params) { if(is_numeric($params['serverport']) && $params['serverip'] && !preg_match("/\:/",$params['serverip'])){ $params['serverip'] .=":".$params['serverport']; } if(is_numeric($params['serverport']) && $params['serverhostname'] && !preg_match("/\:/",$params['serverhostname'])){ $params['serverhostname'] .=":".$params['serverport']; } $host = $params['serverip'] ? $params['serverip'] : $params['serverhostname']; return new Api($host, $params['serverusername'], $params['serveraccesshash'], $params['serverpassword']); } /** * * @return \MGProvision\Proxmox\v2\Api */ static function apiFromServer(\ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server $server) { $host = $server->ipaddress ? $server->ipaddress : $server->hostname; return new Api($host, $server->username, $server->accesshash, decrypt($server->password)); } static function vm($vServer) { if ($vServer instanceof \MGProvision\ProxmoxCloud\models\VServer) { $vmid = $vServer->getVmid(); $node = $vServer->getNode(); $virtualization = $vServer->getVirtualization(); } else { $vmid = $vServer->vmid; $node = $vServer->node; $virtualization = $vServer->virtualization; } switch (strtolower($virtualization)) { case 'kvm': case 'qemu': return new proxmox\models\Kvm($node, $vmid); break; case 'lxc': return new proxmox\models\Lxc($node, $vmid); break; default: throw new \Exception(sprintf("Invalid virtualization type '%s'", $virtualization)); } } static function vmVps($product, $params) { if (is_string($product)) { $virtualization = $product; } else if (is_object($product) && method_exists($product, 'getConfig')) { $virtualization = $product->getConfig('virtualization_type'); }else if(is_object($product) && method_exists($product, 'getVirtualization')){ $virtualization = $product->getVirtualization(); } switch (strtolower($virtualization)) { case 'kvm': case 'qemu': $vm = new proxmox\models\Kvm($params['customfields']['node'], $params['customfields']['vmid']); break; case 'lxc': $vm = new proxmox\models\Lxc($params['customfields']['node'], $params['customfields']['vmid']); break; default: throw new \Exception(sprintf("Invalid virtualization type '%s'", $virtualization)); } $vm->setName(!empty($params['domain']) ? $params['domain'] : $params['customfields']['proxmoxHostname']); return $vm; } /** * * @param array $params * @return \MGProvision\Proxmox\v2\models\User */ static function user(array $params) { if (empty($params['customfields']['Authentication'])) { throw new \Exception("Custom field ('Authentication') is empty."); } if (empty($params['username'])) { throw new \Exception("Servuce field ('Username') is empty."); } $userid = $params['username'] . "@" . $params['customfields']['Authentication']; return new proxmox\models\User($userid); } }