| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- /* * ********************************************************************
- * ProxmoxVPS product developed. (2017-07-18)
- * *
- *
- * CREATED BY MODULESGARDEN -> 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 <pawelk@modulesgarden.com>
- * @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;
- }
- }
|