CdRomRepository.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (May 28, 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\repository;
  20. use MGProvision\Proxmox\v2\models\CdRom;
  21. use MGProvision\Proxmox\v2\models\CloudInitDrive;
  22. use \MGProvision\Proxmox\v2\ProxmoxApiException;
  23. use \MGProvision\Proxmox\v2\models\IpConfig;
  24. /**
  25. * Description of IpConfigRepository
  26. *
  27. * @author Pawel Kopec <pawelk@modulesgardne.com>
  28. */
  29. class CdRomRepository extends AbstractRepository
  30. {
  31. protected $path;
  32. public function findByPath($path)
  33. {
  34. if (!preg_match('/config/', $path))
  35. {
  36. throw new ProxmoxApiException(sprintf("CdRom path ('%s') is not valid", $path));
  37. }
  38. $this->path = $path;
  39. return $this;
  40. }
  41. /**
  42. *
  43. * @return CdRom[]
  44. * @throws ProxmoxApiException
  45. */
  46. public function fetch()
  47. {
  48. if ($this->fetch)
  49. {
  50. return $this->fetch;
  51. }
  52. if (empty($this->path))
  53. {
  54. throw new ProxmoxApiException("CdRom path is empty");
  55. }
  56. $this->fetch = [];
  57. $get = $this->api()->get($this->path);
  58. foreach ($get as $id => $config)
  59. {
  60. if (preg_match('/media\=cdrom/', $config) && !preg_match('/cloudinit/', $config))
  61. {
  62. $entity = new CdRom($id, $config);
  63. $entity->setPath($this->path);
  64. $entity->setApi($this->api);
  65. $this->fetch[] = $entity;
  66. }
  67. }
  68. return $this->fetch;
  69. }
  70. /**
  71. * @return CdRom
  72. * @throws ProxmoxApiException
  73. */
  74. public function first(){
  75. return $this->fetch()[0];
  76. }
  77. public function hasCloudInit()
  78. {
  79. if (empty($this->path))
  80. {
  81. throw new ProxmoxApiException("CdRom path is empty");
  82. }
  83. if (!$this->fetch)
  84. {
  85. $this->fetch = $this->api()->get($this->path);
  86. }
  87. foreach ($this->fetch as $id => $config)
  88. {
  89. if (preg_match('/cloudinit/', $config))
  90. {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. /**
  97. * @return CloudInitDrive
  98. * @throws ProxmoxApiException
  99. */
  100. public function getCloudInitDrive(){
  101. if (empty($this->path))
  102. {
  103. throw new ProxmoxApiException("CdRom path is empty");
  104. }
  105. if (!$this->fetch)
  106. {
  107. $this->fetch = $this->api()->get($this->path);
  108. }
  109. foreach ($this->fetch as $id => $config)
  110. {
  111. if (preg_match('/cloudinit/', $config))
  112. {
  113. $entity = new CloudInitDrive($id, $config);
  114. $entity->setPath($this->path);
  115. $entity->setApi($this->api);
  116. return $entity;
  117. }
  118. }
  119. throw new ProxmoxApiException("Cloud Init Drive not found");
  120. }
  121. }