1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the DigitalOceanV2 library. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DigitalOceanV2\Api; |
13
|
|
|
|
14
|
|
|
use DigitalOceanV2\Entity\Action as ActionEntity; |
15
|
|
|
use DigitalOceanV2\Entity\Snapshot as SnapshotEntity; |
16
|
|
|
use DigitalOceanV2\Entity\Volume as VolumeEntity; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Yassir Hannoun <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Volume extends AbstractApi |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param string $regionSlug restricts results to volumes available in a specific region |
25
|
|
|
* |
26
|
|
|
* @return VolumeEntity[] Lists all of the Block Storage volumes available |
27
|
|
|
*/ |
28
|
|
|
public function getAll($regionSlug = null) |
29
|
|
|
{ |
30
|
|
|
$regionQueryParameter = is_null($regionSlug) ? '' : sprintf('®ion=%s', $regionSlug); |
31
|
|
|
$volumes = $this->adapter->get(sprintf('%s/volumes?per_page=%d%s', $this->endpoint, 200, $regionQueryParameter)); |
32
|
|
|
|
33
|
|
|
$volumes = json_decode($volumes); |
34
|
|
|
|
35
|
|
|
$this->extractMeta($volumes); |
36
|
|
|
|
37
|
|
|
return array_map(function ($volume) { |
38
|
|
|
return new VolumeEntity($volume); |
39
|
|
|
}, $volumes->volumes); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $driveName restricts results to volumes with the specified name |
44
|
|
|
* @param string $regionSlug restricts results to volumes available in a specific region |
45
|
|
|
* |
46
|
|
|
* @return VolumeEntity[] Lists all of the Block Storage volumes available |
47
|
|
|
*/ |
48
|
|
|
public function getByNameAndRegion($driveName, $regionSlug) |
49
|
|
|
{ |
50
|
|
|
$volumes = $this->adapter->get(sprintf('%s/volumes?per_page=%d®ion=%s&name=%s', $this->endpoint, 200, $regionSlug, $driveName)); |
51
|
|
|
|
52
|
|
|
$volumes = json_decode($volumes); |
53
|
|
|
|
54
|
|
|
$this->extractMeta($volumes); |
55
|
|
|
|
56
|
|
|
return array_map(function ($volume) { |
57
|
|
|
return new VolumeEntity($volume); |
58
|
|
|
}, $volumes->volumes); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $id |
63
|
|
|
* |
64
|
|
|
* @return VolumeEntity the Block Storage volume with the specified id |
65
|
|
|
*/ |
66
|
|
|
public function getById($id) |
67
|
|
|
{ |
68
|
|
|
$volume = $this->adapter->get(sprintf('%s/volumes/%s?per_page=%d', $this->endpoint, $id, 200)); |
69
|
|
|
|
70
|
|
|
$volume = json_decode($volume); |
71
|
|
|
|
72
|
|
|
return new VolumeEntity($volume->volume); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get all volume snapshots. |
77
|
|
|
* |
78
|
|
|
* @param string $id |
79
|
|
|
* |
80
|
|
|
* @return ImageEntity[] |
81
|
|
|
*/ |
82
|
|
|
public function getSnapshots($id) |
83
|
|
|
{ |
84
|
|
|
$snapshots = $this->adapter->get(sprintf('%s/volumes/%s/snapshots?per_page=%d', $this->endpoint, $id, 200)); |
85
|
|
|
|
86
|
|
|
$snapshots = json_decode($snapshots); |
87
|
|
|
|
88
|
|
|
$this->meta = $this->extractMeta($snapshots); |
89
|
|
|
|
90
|
|
|
return array_map(function ($snapshot) { |
91
|
|
|
$snapshot = new SnapshotEntity($snapshot); |
92
|
|
|
|
93
|
|
|
return $snapshot; |
94
|
|
|
}, $snapshots->snapshots); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name A human-readable name for the Block Storage volume |
99
|
|
|
* @param string $description Free-form text field to describe a Block Storage volume |
100
|
|
|
* @param string $sizeInGigabytes The size of the Block Storage volume in GiB |
101
|
|
|
* @param string $regionSlug The region where the Block Storage volume will be created |
102
|
|
|
* |
103
|
|
|
* @throws HttpException |
104
|
|
|
* |
105
|
|
|
* @return VolumeEntity the Block Storage volume that was created |
106
|
|
|
*/ |
107
|
|
|
public function create($name, $description, $sizeInGigabytes, $regionSlug) |
108
|
|
|
{ |
109
|
|
|
$data = [ |
110
|
|
|
'size_gigabytes' => $sizeInGigabytes, |
111
|
|
|
'name' => $name, |
112
|
|
|
'description' => $description, |
113
|
|
|
'region' => $regionSlug, |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$volume = $this->adapter->post(sprintf('%s/volumes', $this->endpoint), $data); |
117
|
|
|
|
118
|
|
|
$volume = json_decode($volume); |
119
|
|
|
|
120
|
|
|
return new VolumeEntity($volume->volume); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $id |
125
|
|
|
* |
126
|
|
|
* @throws HttpException |
127
|
|
|
*/ |
128
|
|
|
public function delete($id) |
129
|
|
|
{ |
130
|
|
|
$this->adapter->delete(sprintf('%s/volumes/%s', $this->endpoint, $id)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $driveName restricts the search to volumes with the specified name |
135
|
|
|
* @param string $regionSlug restricts the search to volumes available in a specific region |
136
|
|
|
* |
137
|
|
|
* @throws HttpException |
138
|
|
|
*/ |
139
|
|
|
public function deleteWithNameAndRegion($driveName, $regionSlug) |
140
|
|
|
{ |
141
|
|
|
$this->adapter->delete(sprintf('%s/volumes?name=%s®ion=%s', $this->endpoint, $driveName, $regionSlug)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $id the id of the volume |
146
|
|
|
* @param int $dropletId the unique identifier for the Droplet the volume will be attached to |
147
|
|
|
* @param string $regionSlug the slug identifier for the region the volume is located in |
148
|
|
|
* |
149
|
|
|
* @return ActionEntity |
150
|
|
|
*/ |
151
|
|
|
public function attach($id, $dropletId, $regionSlug) |
152
|
|
|
{ |
153
|
|
|
$data = [ |
154
|
|
|
'type' => 'attach', |
155
|
|
|
'droplet_id' => $dropletId, |
156
|
|
|
'region' => $regionSlug, |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
$action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data); |
160
|
|
|
|
161
|
|
|
$action = json_decode($action); |
162
|
|
|
|
163
|
|
|
return new ActionEntity($action->action); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $id the id of the volume |
168
|
|
|
* @param int $dropletId the unique identifier for the Droplet the volume will detach from |
169
|
|
|
* @param string $regionSlug the slug identifier for the region the volume is located in |
170
|
|
|
* |
171
|
|
|
* @return ActionEntity |
172
|
|
|
*/ |
173
|
|
|
public function detach($id, $dropletId, $regionSlug) |
174
|
|
|
{ |
175
|
|
|
$data = [ |
176
|
|
|
'type' => 'detach', |
177
|
|
|
'droplet_id' => $dropletId, |
178
|
|
|
'region' => $regionSlug, |
179
|
|
|
]; |
180
|
|
|
|
181
|
|
|
$action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data); |
182
|
|
|
|
183
|
|
|
$action = json_decode($action); |
184
|
|
|
|
185
|
|
|
return new ActionEntity($action->action); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $id the id of the volume |
190
|
|
|
* @param int $newSize the new size of the Block Storage volume in GiB |
191
|
|
|
* @param string $regionSlug the slug identifier for the region the volume is located in |
192
|
|
|
* |
193
|
|
|
* @return ActionEntity |
194
|
|
|
*/ |
195
|
|
|
public function resize($id, $newSize, $regionSlug) |
196
|
|
|
{ |
197
|
|
|
$data = [ |
198
|
|
|
'type' => 'resize', |
199
|
|
|
'size_gigabytes' => $newSize, |
200
|
|
|
'region' => $regionSlug, |
201
|
|
|
]; |
202
|
|
|
|
203
|
|
|
$action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data); |
204
|
|
|
|
205
|
|
|
$action = json_decode($action); |
206
|
|
|
|
207
|
|
|
return new ActionEntity($action->action); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Create a new snapshot of the volume. |
212
|
|
|
* |
213
|
|
|
* @param string $id the id of the volume |
214
|
|
|
* @param string $name a human-readable name for the volume snapshot |
215
|
|
|
* |
216
|
|
|
* @throws HttpException |
217
|
|
|
* |
218
|
|
|
* @return SnapshotEntity |
219
|
|
|
*/ |
220
|
|
|
public function snapshot($id, $name) |
221
|
|
|
{ |
222
|
|
|
$data = [ |
223
|
|
|
'name' => $name, |
224
|
|
|
]; |
225
|
|
|
|
226
|
|
|
$snapshot = $this->adapter->post(sprintf('%s/volumes/%s/snapshots', $this->endpoint, $id), $data); |
227
|
|
|
|
228
|
|
|
$snapshot = json_decode($snapshot); |
229
|
|
|
|
230
|
|
|
return new SnapshotEntity($snapshot->snapshot); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $id |
235
|
|
|
* @param int $actionId |
236
|
|
|
* |
237
|
|
|
* @return ActionEntity |
238
|
|
|
*/ |
239
|
|
|
public function getActionById($id, $actionId) |
240
|
|
|
{ |
241
|
|
|
$action = $this->adapter->get(sprintf('%s/volumes/%s/actions/%d', $this->endpoint, $id, $actionId)); |
242
|
|
|
|
243
|
|
|
$action = json_decode($action); |
244
|
|
|
|
245
|
|
|
return new ActionEntity($action->action); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param string $id |
250
|
|
|
* |
251
|
|
|
* @return ActionEntity[] |
252
|
|
|
*/ |
253
|
|
|
public function getActions($id) |
254
|
|
|
{ |
255
|
|
|
$actions = $this->adapter->get(sprintf('%s/volumes/%s/actions?per_page=%d', $this->endpoint, $id, 200)); |
256
|
|
|
|
257
|
|
|
$actions = json_decode($actions); |
258
|
|
|
|
259
|
|
|
$this->meta = $this->extractMeta($actions); |
260
|
|
|
|
261
|
|
|
return array_map(function ($action) { |
262
|
|
|
return new ActionEntity($action); |
263
|
|
|
}, $actions->actions); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|