Completed
Push — master ( 8de90a...8aecb2 )
by Yassir
9s
created

Volume::resize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
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\Volume as VolumeEntity;
15
use DigitalOceanV2\Entity\Action as ActionEntity;
16
17
/**
18
 * @author Yassir Hannoun <[email protected]>
19
 */
20
class Volume extends AbstractApi
21
{
22
    /**
23
     * @param string $regionSlug restricts results to volumes available in a specific region.
24
     *
25
     * @return VolumeEntity[] Lists all of the Block Storage volumes available.
26
     */
27
    public function getAll($regionSlug = null)
28
    {
29
        $regionQueryParameter = is_null($regionSlug) ? '' : sprintf('&region=%s', $regionSlug);
30
        $volumes = $this->adapter->get(sprintf('%s/volumes?per_page=%d%s', $this->endpoint, 200, $regionQueryParameter));
31
32
        $volumes = json_decode($volumes);
33
34
        $this->extractMeta($volumes);
35
36
        return array_map(function ($volume) {
37
            return new VolumeEntity($volume);
38
        }, $volumes->volumes);
39
    }
40
41
    /**
42
     * @param string $driveName  restricts results to volumes with the specified name.
43
     * @param string $regionSlug restricts results to volumes available in a specific region.
44
     *
45
     * @return VolumeEntity[] Lists all of the Block Storage volumes available.
46
     */
47
    public function getByNameAndRegion($driveName, $regionSlug)
48
    {
49
        $volumes = $this->adapter->get(sprintf('%s/volumes?per_page=%d&region=%s&name=%s', $this->endpoint, 200, $regionSlug, $driveName));
50
51
        $volumes = json_decode($volumes);
52
53
        $this->extractMeta($volumes);
54
55
        return array_map(function ($volume) {
56
            return new VolumeEntity($volume);
57
        }, $volumes->volumes);
58
    }
59
60
    /**
61
     * @param string $id
62
     *
63
     * @return VolumeEntity the Block Storage volume with the specified id.
64
     */
65
    public function getById($id)
66
    {
67
        $volume = $this->adapter->get(sprintf('%s/volumes/%s?per_page=%d', $this->endpoint, $id, 200));
68
69
        $volume = json_decode($volume);
70
71
        return new VolumeEntity($volume->volume);
72
    }
73
74
    /**
75
     * @param string $name            A human-readable name for the Block Storage volume.
76
     * @param string $description     Free-form text field to describe a Block Storage volume.
77
     * @param string $sizeInGigabytes The size of the Block Storage volume in GiB.
78
     * @param string $regionSlug      The region where the Block Storage volume will be created.
79
     *
80
     * @throws HttpException
81
     * 
82
     * @return VolumeEntity the Block Storage volume that was created.
83
     */
84
    public function create($name, $description, $sizeInGigabytes, $regionSlug)
85
    {
86
        $data = [
87
            'size_gigabytes' => $sizeInGigabytes,
88
            'name' => $name,
89
            'description' => $description,
90
            'region' => $regionSlug,
91
        ];
92
93
        $volume = $this->adapter->post(sprintf('%s/volumes', $this->endpoint), $data);
94
95
        $volume = json_decode($volume);
96
97
        return new VolumeEntity($volume->volume);
98
    }
99
100
    /**
101
     * @param string $id
102
     *
103
     * @throws HttpException
104
     */
105
    public function delete($id)
106
    {
107
        $this->adapter->delete(sprintf('%s/volumes/%s', $this->endpoint, $id));
108
    }
109
110
    /**
111
     * @param string $driveName  restricts the search to volumes with the specified name.
112
     * @param string $regionSlug restricts the search to volumes available in a specific region.
113
     *
114
     * @throws HttpException
115
     */
116
    public function deleteWithNameAndRegion($driveName, $regionSlug)
117
    {
118
        $this->adapter->delete(sprintf('%s/volumes?name=%s&region=%s', $this->endpoint, $driveName, $regionSlug));
119
    }
120
121
    /**
122
     * @param string $id         the id of the volume
123
     * @param int    $dropletId  the unique identifier for the Droplet the volume will be attached to.
124
     * @param string $regionSlug the slug identifier for the region the volume is located in.
125
     *
126
     * @return ActionEntity
127
     */
128
    public function attach($id, $dropletId, $regionSlug)
129
    {
130
        $data = [
131
            'type' => 'attach',
132
            'droplet_id' => $dropletId,
133
            'region' => $regionSlug,
134
        ];
135
136
        $action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data);
137
138
        $action = json_decode($action);
139
140
        return new ActionEntity($action->action);
141
    }
142
143
    /**
144
     * @param string $id         the id of the volume
145
     * @param int    $dropletId  the unique identifier for the Droplet the volume will detach from.
146
     * @param string $regionSlug the slug identifier for the region the volume is located in.
147
     *
148
     * @return ActionEntity
149
     */
150
    public function detach($id, $dropletId, $regionSlug)
151
    {
152
        $data = [
153
            'type' => 'detach',
154
            'droplet_id' => $dropletId,
155
            'region' => $regionSlug,
156
        ];
157
158
        $action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data);
159
160
        $action = json_decode($action);
161
162
        return new ActionEntity($action->action);
163
    }
164
165
    /**
166
     * @param string $id         the id of the volume
167
     * @param int    $newSize    the new size of the Block Storage volume in GiB.
168
     * @param string $regionSlug the slug identifier for the region the volume is located in.
169
     *
170
     * @return ActionEntity
171
     */
172
    public function resize($id, $newSize, $regionSlug)
173
    {
174
        $data = [
175
            'type' => 'resize',
176
            'size_gigabytes' => $newSize,
177
            'region' => $regionSlug,
178
        ];
179
180
        $action = $this->adapter->post(sprintf('%s/volumes/%s/actions', $this->endpoint, $id), $data);
181
182
        $action = json_decode($action);
183
184
        return new ActionEntity($action->action);
185
    }
186
187
    /**
188
     * @param string $id
189
     * @param int    $actionId
190
     *
191
     * @return ActionEntity
192
     */
193
    public function getActionById($id, $actionId)
194
    {
195
        $action = $this->adapter->get(sprintf('%s/volumes/%s/actions/%d', $this->endpoint, $id, $actionId));
196
197
        $action = json_decode($action);
198
199
        return new ActionEntity($action->action);
200
    }
201
202
    /**
203
     * @param string $id
204
     *
205
     * @return ActionEntity[]
206
     */
207
    public function getActions($id)
208
    {
209
        $actions = $this->adapter->get(sprintf('%s/volumes/%s/actions?per_page=%d', $this->endpoint, $id, 200));
210
211
        $actions = json_decode($actions);
212
213
        $this->meta = $this->extractMeta($actions);
214
215
        return array_map(function ($action) {
216
            return new ActionEntity($action);
217
        }, $actions->actions);
218
    }
219
}
220