1<?php
2
3/**
4 *
5 * bareos-webui - Bareos Web-Frontend
6 *
7 * @link      https://github.com/bareos/bareos for the canonical source repository
8 * @copyright Copyright (C) 2013-2019 Bareos GmbH & Co. KG (http://www.bareos.org/)
9 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26namespace Storage\Model;
27
28class StorageModel
29{
30
31   /**
32    * Get all Storages
33    *
34    * @param $bsock
35    *
36    * @return array
37    */
38   public function getStorages(&$bsock=null)
39   {
40      if(isset($bsock)) {
41         $cmd = 'list storages';
42         $result = $bsock->send_command($cmd, 2);
43         $storages = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
44         return $storages['result']['storages'];
45      }
46      else {
47         throw new \Exception('Missing argument.');
48      }
49   }
50
51   /**
52    * Get Status Storage Slots
53    *
54    * @param $bsock
55    * @param $storage
56    *
57    * @return array
58    */
59   public function getStatusStorageSlots(&$bsock=null, $storage=null)
60   {
61      if(isset($bsock, $storage)) {
62         $cmd = 'status storage="' . $storage . '" slots';
63         $result = $bsock->send_command($cmd, 2);
64         $slots = \Zend\Json\Json::decode($result, \Zend\Json\Json::TYPE_ARRAY);
65         return $slots['result']['contents'];
66      }
67      else {
68         throw new \Exception('Missing argument.');
69      }
70   }
71
72   /**
73    * Import Slots
74    *
75    * @param $bsock
76    * @param $storage
77    * @param $srcslots
78    * @param $dstslots
79    *
80    * @return string
81    */
82   public function importSlots(&$bsock=null, $storage=null, $srcslots=null, $dstslots=null)
83   {
84      if(isset($bsock, $storage)) {
85         if($srcslots == null) {
86            if($dstslots == null) {
87               $cmd = 'import storage="' . $storage;
88            }
89            else {
90               $cmd = 'import storage="' . $storage . '" dstslots=' . $dstslots;
91            }
92         }
93         else {
94            if($dstslots == null) {
95               $cmd = 'import storage="' . $storage . '" srcslots=' . $srcslots;
96            }
97            else {
98               $cmd = 'import storage="' . $storage . '" srcslots=' . $srcslots . ' dstslots=' . $dstslots;
99            }
100         }
101         $result = $bsock->send_command($cmd, 0);
102         return $result;
103      }
104      else {
105         throw new \Exception('Missing argument.');
106      }
107   }
108
109   /**
110    * Export Slots
111    *
112    * @param $bsock
113    * @param $storage
114    * @param $slots
115    *
116    * @return string
117    */
118   public function exportSlots(&$bsock=null, $storage=null, $slots=null)
119   {
120      if(isset($bsock, $storage, $slots)) {
121         $cmd = 'export storage="' . $storage . '" srcslots=' . $slots;
122         $result = $bsock->send_command($cmd, 0);
123         return $result;
124      }
125      else {
126         throw new \Exception('Missing argument.');
127      }
128   }
129
130   /**
131    * Mound Slots
132    *
133    * @param $bsock
134    * @param $storage
135    * @param $slot
136    * @param $drive
137    *
138    * @return string
139    */
140   public function mountSlot(&$bsock=null, $storage=null, $slot=null, $drive=null)
141   {
142      if(isset($bsock, $storage, $slot, $drive)) {
143         $cmd = 'mount storage="' . $storage . '" slot=' . $slot . ' drive=' . $drive;
144         $result = $bsock->send_command($cmd, 0);
145         return $result;
146      }
147      else {
148         throw new \Exception('Missing argument.');
149      }
150   }
151
152   /**
153    * Unmount Slots
154    *
155    * @param $bsock
156    * @param $storage
157    * @param $drive
158    *
159    * @return string
160    */
161   public function unmountSlot(&$bsock=null, $storage=null, $drive=null)
162   {
163      if(isset($bsock, $storage, $drive)) {
164         $cmd = 'unmount storage="' . $storage . '" drive=' . $drive;
165         $result = $bsock->send_command($cmd, 0);
166         return $result;
167      }
168      else {
169         throw new \Exception('Missing argument.');
170      }
171   }
172
173   /**
174    * Release Slot
175    *
176    * @param $bsock
177    * @param $storage
178    * @param $drive
179    *
180    * @return string
181    */
182   public function releaseSlot(&$bsock=null, $storage=null, $drive=null)
183   {
184      if(isset($bsock, $storage, $drive)) {
185         $cmd = 'release storage="' . $storage . '" drive=' . $drive;
186         $result = $bsock->send_command($cmd, 0);
187         return $result;
188      }
189      else {
190         throw new \Exception('Missing argument.');
191      }
192   }
193
194   /**
195    * Update Slots
196    *
197    * @param $bsock
198    * @param $storage
199    *
200    * @return string
201    */
202   public function updateSlots(&$bsock=null, $storage=null)
203   {
204      if(isset($bsock, $storage)) {
205         $cmd = 'update slots storage="' . $storage . '"';
206         $result = $bsock->send_command($cmd, 0);
207         return $result;
208      }
209      else {
210         throw new \Exception('Missing argument.');
211      }
212   }
213
214   /**
215    * Move Slots
216    *
217    * @param $bsock
218    * @param $storage
219    * @param $srcslots
220    * @param $dstslots
221    *
222    * @return json
223    */
224   public function moveSlots(&$bsock=null, $storage=null, $srcslots=null, $dstslots=null)
225   {
226      if(isset($bsock, $storage, $srcslots, $dstslots)) {
227         $cmd = 'move storage="' . $storage . '" srcslots=' . $srcslots . ' dstslots=' . $dstslots;
228         $result = $bsock->send_command($cmd, 2);
229         return $result;
230      }
231      else {
232         throw new \Exception('Missing argument.');
233      }
234   }
235
236   /**
237    * Label
238    *
239    * @param $bsock
240    * @param $storage
241    * @param $pool
242    * @param $drive
243    * @param $slots
244    *
245    * @return string
246    */
247   public function label(&$bsock=null, $storage=null, $pool=null, $drive=null, $slots=null)
248   {
249      if(isset($bsock, $storage, $pool, $drive)) {
250         $cmd = 'label storage="' . $storage . '" pool="'.$pool.'" drive="'.$drive.'" barcodes yes';
251         $result = $bsock->send_command($cmd, 0);
252         return $result;
253      }
254      else {
255         throw new \Exception('Missing argument.');
256      }
257   }
258
259   /**
260    * Get Slots
261    *
262    * @param $bsock
263    * @param $storage
264    *
265    * @return array
266    */
267   public function getSlots(&$bsock=null, $storage=null)
268   {
269      if(isset($bsock, $storage)) {
270         $cmd = 'status storage="'.$storage.'" slots';
271         $slots = $bsock->send_command($cmd, 2);
272         $result = \Zend\Json\Json::decode($slots, \Zend\Json\Json::TYPE_ARRAY);
273         return $result['result']['contents'];
274      }
275      else {
276         throw new \Exception('Missing argument.');
277      }
278   }
279
280   /**
281    * Status Storage
282    *
283    * @param $bsock
284    * @param $storage
285    *
286    * @return string
287    */
288   public function statusStorage(&$bsock=null, $storage=null)
289   {
290      if(isset($bsock, $storage)) {
291         $cmd = 'status storage="'.$storage;
292         $result = $bsock->send_command($cmd, 0);
293         return $result;
294      }
295      else {
296         throw new \Exception('Missing argument.');
297      }
298   }
299}
300