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\models; /** * Description of HardDisk * * @author Pawel Kopec * @version 1.0.0 */ class HardDisk extends AbstractObject{ protected $id; protected $location; protected $storage; protected $cache; protected $discard; protected $media; protected $replicate; protected $format; protected $backup; protected $mbps_rd; protected $mbps_rd_max; protected $mbps_wr; protected $mbps_wr_max; protected $iops_rd; protected $iops_rd_max; protected $iops_wr; protected $iops_wr_max; protected $size; protected $iothread; protected $ssd; protected $path; private $master; public function __construct($id, $config=null) { $this->id = $id; if ($config) { $config = self::asArray($config); $config['location'] = key($config); $ex = explode(":", key($config)); $config['storage'] = $ex[0]; $this->setAttributes($config); } } public function getId(){ return $this->id; } /** * @param mixed $id */ public function setId($id) { $this->id = $id; return $this; } public function setMaster($master){ $this->master = $master; return $this; } public function isMaster(){ if(is_bool($this->master)){ return $this->master; } return stripos($this->getLocation(), 'disk-0') !== false; } public function getName(){ $matches = array(); preg_match('/disk-[0-9]*/', $this->getLocation(), $matches); if ($matches[0]){ return $matches[0]; } } public function getLocation() { return $this->location; } public function getStorage(){ return $this->storage; } public function getFormat() { if(!$this->format && preg_match("/\./", $this->location)){ $ex = explode(".", $this->location); return end($ex); } return $this->format; } public function getBackup() { return $this->backup; } public function isBackup() { return is_null($this->backup); } public function getCache() { return $this->cache; } public function getMbps_rd() { return $this->mbps_rd; } public function getMbps_rd_max() { return $this->mbps_rd_max; } public function getMbps_wr() { return $this->mbps_wr; } public function getMbps_wr_max() { return $this->mbps_wr_max; } public function getIops_rd() { return $this->iops_rd; } public function getIops_rd_max() { return $this->iops_rd_max; } public function getIops_wr() { return $this->iops_wr; } public function getIops_wr_max() { return $this->iops_wr_max; } public function getSize() { return $this->size; } public function setLocation($location) { $this->location = $location; return $this; } /** * @param mixed $storage */ public function setStorage($storage) { $this->storage = $storage; return $this; } public function setFormat($format) { $this->format = $format; return $this; } public function setBackup($backup) { $this->backup = $backup; return $this; } public function setCache($cache) { $this->cache = $cache; return $this; } public function setMbps_rd($mbps_rd) { $this->mbps_rd = $mbps_rd; return $this; } public function setMbps_rd_max($mbps_rd_max) { $this->mbps_rd_max = $mbps_rd_max; return $this; } public function setMbps_wr($mbps_wr) { $this->mbps_wr = $mbps_wr; return $this; } public function setMbps_wr_max($mbps_wr_max) { $this->mbps_wr_max = $mbps_wr_max; return $this; } public function setIops_rd($iops_rd) { $this->iops_rd = $iops_rd; return $this; } public function setIops_rd_max($iops_rd_max) { $this->iops_rd_max = $iops_rd_max; return $this; } public function setIops_wr($iops_wr) { $this->iops_wr = $iops_wr; return $this; } public function setIops_wr_max($iops_wr_max) { $this->iops_wr_max = $iops_wr_max; return $this; } public function setSize($size) { $this->size = $size; return $this; } public function getDiscard() { return $this->discard; } public function getMedia() { return $this->media; } public function getReplicate() { return $this->replicate; } public function setDiscard($discard) { $this->discard = $discard; return $this; } public function setMedia($media) { $this->media = $media; return $this; } public function setReplicate($replicate) { $this->replicate = $replicate; return $this; } public function getBytes(){ if(preg_match('/K/', $this->getSize())){//KB => Bytes $size = (int) $this->getSize(); return $size * 1024 ; } elseif(preg_match('/M/', $this->getSize())){//MB => Bytes $size = (int) $this->getSize(); return $size * pow(1024,2); } else if(preg_match('/T/', $this->getSize())){//T => Bytes $size = (int) $this->getSize(); return $size * pow(1024,4); } else{//GB => Bytes $size = (int) $this->getSize(); return $size * pow(1024,3); } } public function getGb(){ if(preg_match('/M/', $this->getSize())){//MB => GB $size = (int) $this->getSize(); return $size / 1024; } else if(preg_match('/T/', $this->getSize())){//T => GB $size = (int) $this->getSize(); return $size * 1024; } else{//GB return (int) $this->getSize(); } } public function formatBytes(){//Bytes => GB return $this->getBytes() / pow(1024,3); } public function asConfig(){ $fields=['cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max', 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max','size', 'iothread','ssd']; $config = []; if($this->getLocation()){ $config[]= $this->getLocation(); }else{ $config[]= "{$this->getStorage()}:{$this->getSize()}"; unset($fields[array_search("size", $fields)]); } $this->toConfig($fields, $config); return implode(",", $config); } public function getPath() { return $this->path; } public function setPath($path) { $this->path = $path; return $this; } public function delete(){ return $this->api()->put($this->getPath(), ["delete"=> $this->getId()]); } public function create(){ $config = []; $config[] = $this->getStorage().":".$this->getSize(); $fields=['cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max', 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max', 'iothread']; $this->toConfig($fields, $config); $prameters = implode(",", $config); return $this->api()->put($this->getPath(), [$this->getId()=> $prameters]); } public function getType(){ return preg_replace('/[0-9]+/', '', $this->getId()); } public function getIothread() { return $this->iothread; } public function setIothread($iothread) { $this->iothread = $iothread; return $this; } /** * @return mixed */ public function getSsd() { return $this->ssd; } /** * @param mixed $ssd * @return HardDisk */ public function setSsd($ssd) { $this->ssd = $ssd; return $this; } public function getAttributes(){ $fields=['cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max', 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max','size', 'iothread', 'ssd']; $config=[]; foreach($fields as $field) { $methodName = 'get'.ucfirst($field); if(method_exists($this,$methodName ) && isset( $this->{$field})){ $config[$field]=$this->{$methodName}(); } } return $config; } public function update(){ $config = []; $config[]= $this->getLocation(); $fields=[ 'cache','discard','media','replicate','format','backup','mbps_rd','mbps_rd_max', 'mbps_wr','mbps_wr_max','iops_rd','iops_rd_max','iops_wr','iops_wr_max', 'iothread','ssd']; $this->toConfig($fields, $config); $prameters = implode(",", $config); return $this->api()->put($this->getPath(), [$this->getId() => $prameters]); } public function resize($size) { $setting = [ "disk" => $this->getId(), "size" => $size, ]; $path = str_replace('config', 'resize', $this->getPath()); return $this->api()->put($path, $setting); } public function getBus(){ return preg_replace("/[0-9]+/", "", $this->getId()); } public static function isConfigValid($id, $config){ $bus = [ "ide",'sata', 'virtio', 'scsi']; $invalid = ['hotplug' ,'agent']; if (in_array($id, $invalid)) { return false; } if (preg_match('/efidisk/', $config)) { return false; } if (preg_match('/unused/', $id)) { return false; } if (!preg_match('/disk/', $config)) { foreach ($bus as $b){ if(preg_match("/{$b}\d/", $id) && !preg_match('/cdrom/', $config) ){ return true; } } return false; } return true; } }