MountPoint.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Apr 12, 2018)
  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\models;
  20. /**
  21. * Description of MounPoint
  22. *
  23. * @author Pawel Kopec <pawelk@modulesgardne.com>
  24. */
  25. class MountPoint extends AbstractObject
  26. {
  27. protected $id;
  28. protected $path;
  29. protected $mp;
  30. protected $size;
  31. protected $acl;
  32. protected $backup;
  33. protected $quota;
  34. protected $replicate;
  35. protected $ro;
  36. protected $location;
  37. protected $name;
  38. protected $gb;
  39. public function __construct($id, $config = null)
  40. {
  41. $this->id = $id;
  42. if ($config)
  43. {
  44. $config = self::asArray($config);
  45. $config['location'] = key($config);
  46. $matches['0'] = str_replace(['-'], [" "], $matches['0']);
  47. $config['name'] = ucfirst($matches['0']);
  48. $this->setAttributes($config);
  49. if ($id == 'rootfs')
  50. {
  51. $this->setBackup(1);
  52. }
  53. }
  54. }
  55. public function getId()
  56. {
  57. return $this->id;
  58. }
  59. public function getPath()
  60. {
  61. return $this->path;
  62. }
  63. public function getLocation()
  64. {
  65. return $this->location;
  66. }
  67. public function getName()
  68. {
  69. if($this->getId()=="rootfs"){
  70. return $this->name = "Root Disk";
  71. }
  72. preg_match('/disk-[0-9]/', $this->getLocation(), $matches);
  73. $matches['0'] = str_replace(['-'], [" "], $matches['0']);
  74. return $this->name = ucfirst($matches['0']);
  75. }
  76. public function setLocation($location)
  77. {
  78. $this->location = $location;
  79. return $this;
  80. }
  81. public function setName($name)
  82. {
  83. $this->name = $name;
  84. return $this;
  85. }
  86. public function setPath($path)
  87. {
  88. $this->path = $path;
  89. return $this;
  90. }
  91. public function getMp()
  92. {
  93. return $this->mp;
  94. }
  95. public function getSize()
  96. {
  97. return $this->size;
  98. }
  99. public function getAcl()
  100. {
  101. return $this->acl;
  102. }
  103. public function getBackup()
  104. {
  105. return $this->backup;
  106. }
  107. public function isBackup()
  108. {
  109. return $this->backup==1;
  110. }
  111. public function getQuota()
  112. {
  113. return $this->quota;
  114. }
  115. public function getReplicate()
  116. {
  117. return $this->replicate;
  118. }
  119. public function getRo()
  120. {
  121. return $this->ro;
  122. }
  123. public function setMp($mp)
  124. {
  125. $this->mp = $mp;
  126. return $this;
  127. }
  128. public function setSize($size)
  129. {
  130. $this->size = $size;
  131. return $this;
  132. }
  133. public function setAcl($acl)
  134. {
  135. $this->acl = $acl;
  136. return $this;
  137. }
  138. public function setBackup($backup)
  139. {
  140. $this->backup = $backup;
  141. return $this;
  142. }
  143. public function setQuota($quota)
  144. {
  145. $this->quota = $quota;
  146. return $this;
  147. }
  148. public function setReplicate($replicate)
  149. {
  150. $this->replicate = $replicate;
  151. return $this;
  152. }
  153. public function setRo($ro)
  154. {
  155. $this->ro = $ro;
  156. return $this;
  157. }
  158. public function getBytes()
  159. {
  160. if(preg_match('/K/', $this->getSize())){//KB => Bytes
  161. $size = (int) $this->getSize();
  162. return $size * 1024 ;
  163. }
  164. elseif(preg_match('/M/', $this->getSize())){//MB => Bytes
  165. $size = (int) $this->getSize();
  166. return $size * pow(1024,2);
  167. }
  168. else if(preg_match('/T/', $this->getSize())){//T => Bytes
  169. $size = (int) $this->getSize();
  170. return $size * pow(1024,4);
  171. }
  172. else{//GB => Bytes
  173. $size = (int) $this->getSize();
  174. return $size * pow(1024,3);
  175. }
  176. }
  177. public function getGb()
  178. {
  179. if(preg_match('/M/', $this->getSize())){//MB => GB
  180. $size = (int) $this->getSize();
  181. return $size / 1024;
  182. }
  183. else if(preg_match('/T/', $this->getSize())){//T => GB
  184. $size = (int) $this->getSize();
  185. return $size * 1024;
  186. }
  187. else{//GB
  188. return (int) $this->getSize();
  189. }
  190. }
  191. /**
  192. * @param mixed $id
  193. */
  194. public function setId($id)
  195. {
  196. $this->id = $id;
  197. return $this;
  198. }
  199. public function resize($size)
  200. {
  201. $path = str_replace('config', 'resize', $this->path);
  202. return $this->api()->put($path, ['disk' => $this->id, 'size' => $size]);
  203. }
  204. public function asConfig()
  205. {
  206. $config = [$this->location];
  207. foreach (['mp', 'acl', 'backup', 'quota', 'replicate', 'ro'] as $k)
  208. {
  209. if (!is_null($this->{$k}))
  210. {
  211. $config[] = "{$k}=" . $this->{$k};
  212. }
  213. }
  214. return implode(",", $config);
  215. }
  216. public function update()
  217. {
  218. return $this->api()->put($this->path, [$this->id => $this->asConfig()]);
  219. }
  220. public function create()
  221. {
  222. return $this->api()->put($this->path, [$this->id => $this->asConfig()]);
  223. }
  224. public function delete()
  225. {
  226. return $this->api()->put($this->path, ["delete" => $this->id]);
  227. }
  228. public function isMaster(){
  229. return $this->getId() == "rootfs";
  230. }
  231. }