Volume::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 5
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('&region=%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&region=%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
     * @param string $snapshotId      The unique identifier for the volume snapshot from which to create the volume. Should not be specified with a region_id.
103
     *
104
     * @return VolumeEntity
105
     */
106
    public function create($name, $description, $sizeInGigabytes, $regionSlug, $snapshotId = null)
107
    {
108
        $data = [
109
            'size_gigabytes' => $sizeInGigabytes,
110
            'name' => $name,
111
            'description' => $description,
112
            'region' => $regionSlug,
113
        ];
114
115
        if($snapshotId !== null) {
116
            $data['snapshot_id'] = $snapshotId;
117
        }
118
119
        $volume = $this->adapter->post(sprintf('%s/volumes', $this->endpoint), $data);
120
121
        $volume = json_decode($volume);
122
123
        return new VolumeEntity($volume->volume);
124
    }
125
126
    /**
127
     * @param string $id
128
     *
129
     * @throws HttpException
130
     */
131
    public function delete($id)
132
    {
133
        $this->adapter->delete(sprintf('%s/volumes/%s', $this->endpoint, $id));
134
    }
135
136
    /**
137
     * @param string $driveName  restricts the search to volumes with the specified name
138
     * @param string $regionSlug restricts the search to volumes available in a specific region
139
     *
140
     * @throws HttpException
141
     */
142
    public function deleteWithNameAndRegion($driveName, $regionSlug)
143
    {
144
        $this->adapter->delete(sprintf('%s/volumes?name=%s&region=%s', $this->endpoint, $driveName, $regionSlug));
145
    }
146
147
    /**
148
     * @param string $id         the id of the volume
149
     * @param int    $dropletId  the unique identifier for the Droplet the volume will be attached to
150
     * @param string $regionSlug the slug identifier for the region the volume is located in
151
     *
152
     * @return ActionEntity
153
     */
154
    public function attach($id, $dropletId, $regionSlug)
155
    {
156
        $data = [
157
            'type' => 'attach',
158
            'droplet_id' => $dropletId,
159
            'region' => $regionSlug,
160
        ];
161
162
        $action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data);
163
164
        $action = json_decode($action);
165
166
        return new ActionEntity($action->action);
167
    }
168
169
    /**
170
     * @param string $id         the id of the volume
171
     * @param int    $dropletId  the unique identifier for the Droplet the volume will detach from
172
     * @param string $regionSlug the slug identifier for the region the volume is located in
173
     *
174
     * @return ActionEntity
175
     */
176
    public function detach($id, $dropletId, $regionSlug)
177
    {
178
        $data = [
179
            'type' => 'detach',
180
            'droplet_id' => $dropletId,
181
            'region' => $regionSlug,
182
        ];
183
184
        $action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data);
185
186
        $action = json_decode($action);
187
188
        return new ActionEntity($action->action);
189
    }
190
191
    /**
192
     * @param string $id         the id of the volume
193
     * @param int    $newSize    the new size of the Block Storage volume in GiB
194
     * @param string $regionSlug the slug identifier for the region the volume is located in
195
     *
196
     * @return ActionEntity
197
     */
198
    public function resize($id, $newSize, $regionSlug)
199
    {
200
        $data = [
201
            'type' => 'resize',
202
            'size_gigabytes' => $newSize,
203
            'region' => $regionSlug,
204
        ];
205
206
        $action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data);
207
208
        $action = json_decode($action);
209
210
        return new ActionEntity($action->action);
211
    }
212
213
    /**
214
     * Create a new snapshot of the volume.
215
     *
216
     * @param string $id   the id of the volume
217
     * @param string $name a human-readable name for the volume snapshot
218
     *
219
     * @throws HttpException
220
     *
221
     * @return SnapshotEntity
222
     */
223
    public function snapshot($id, $name)
224
    {
225
        $data = [
226
            'name' => $name,
227
        ];
228
229
        $snapshot = $this->adapter->post(sprintf('%s/volumes/%s/snapshots', $this->endpoint, $id), $data);
230
231
        $snapshot = json_decode($snapshot);
232
233
        return new SnapshotEntity($snapshot->snapshot);
234
    }
235
236
    /**
237
     * @param string $id
238
     * @param int    $actionId
239
     *
240
     * @return ActionEntity
241
     */
242
    public function getActionById($id, $actionId)
243
    {
244
        $action = $this->adapter->get(sprintf('%s/volumes/%s/actions/%d', $this->endpoint, $id, $actionId));
245
246
        $action = json_decode($action);
247
248
        return new ActionEntity($action->action);
249
    }
250
251
    /**
252
     * @param string $id
253
     *
254
     * @return ActionEntity[]
255
     */
256
    public function getActions($id)
257
    {
258
        $actions = $this->adapter->get(sprintf('%s/volumes/%s/actions?per_page=%d', $this->endpoint, $id, 200));
259
260
        $actions = json_decode($actions);
261
262
        $this->meta = $this->extractMeta($actions);
263
264
        return array_map(function ($action) {
265
            return new ActionEntity($action);
266
        }, $actions->actions);
267
    }
268
}
269