GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#50)
by Ha
02:25
created

Server::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php declare (strict_types=1);
2
3
namespace OpenStack\Compute\v2\Models;
4
5
use OpenCloud\Common\Resource\HasWaiterTrait;
6
use OpenCloud\Common\Resource\Creatable;
7
use OpenCloud\Common\Resource\Deletable;
8
use OpenCloud\Common\Resource\Listable;
9
use OpenCloud\Common\Resource\Retrievable;
10
use OpenCloud\Common\Resource\Updateable;
11
use OpenCloud\Common\Resource\OperatorResource;
12
use OpenCloud\Common\Transport\Utils;
13
use OpenStack\BlockStorage\v2\Models\Volume;
14
use OpenStack\BlockStorage\v2\Models\VolumeAttachment;
15
use OpenStack\Compute\v2\Enum;
16
use OpenStack\Networking\v2\Extensions\SecurityGroups\Models\SecurityGroup;
17
use Psr\Http\Message\ResponseInterface;
18
use Symfony\Component\VarDumper\VarDumper;
19
20
/**
21
 * @property \OpenStack\Compute\v2\Api $api
22
 */
23
class Server extends OperatorResource implements
0 ignored issues
show
Bug introduced by
There is one abstract method enumerate in this class; you could implement it, or declare this class as abstract.
Loading history...
24
    Creatable,
25
    Updateable,
26
    Deletable,
27
    Retrievable,
28
    Listable
29
{
30
    use HasWaiterTrait;
31
32
    /** @var string */
33
    public $id;
34
35
    /** @var string */
36
    public $ipv4;
37
38
    /** @var string */
39
    public $ipv6;
40
41
    /** @var array */
42
    public $addresses;
43
44
    /** @var \DateTimeImmutable */
45
    public $created;
46
47
    /** @var \DateTimeImmutable */
48
    public $updated;
49
50
    /** @var Flavor */
51
    public $flavor;
52
53
    /** @var string */
54
    public $hostId;
55
56
    /** @var Image */
57
    public $image;
58
59
    /** @var array */
60
    public $links;
61
62
    /** @var array */
63
    public $metadata;
64
65
    /** @var string */
66
    public $name;
67
68
    /** @var string */
69
    public $progress;
70
71
    /** @var string */
72
    public $status;
73
74
    /** @var string */
75
    public $tenantId;
76
77
    /** @var string */
78
    public $userId;
79
80
    /** @var string */
81
    public $adminPass;
82
83
    /** @var string */
84
    public $taskState;
85
86
    protected $resourceKey = 'server';
87
    protected $resourcesKey = 'servers';
88
    protected $markerKey = 'id';
89
90
    protected $aliases = [
91
        'block_device_mapping_v2' => 'blockDeviceMapping',
92
        'accessIPv4'              => 'ipv4',
93
        'accessIPv6'              => 'ipv6',
94
        'tenant_id'               => 'tenantId',
95
        'user_id'                 => 'userId',
96
        'security_groups'         => 'securityGroups',
97
        'OS-EXT-STS:task_state'   => 'taskState',
98
    ];
99
100
    /**
101 2
     * {@inheritDoc}
102
     *
103 2
     * @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer}
104 2
     */
105
    public function create(array $userOptions): Creatable
106
    {
107
        $response = $this->execute($this->api->postServer(), $userOptions);
108
        return $this->populateFromResponse($response);
109
    }
110 1
111
    /**
112 1
     * {@inheritDoc}
113
     */
114 1
    public function update()
115
    {
116
        $response = $this->execute($this->api->putServer(), $this->getAttrs(['id', 'name', 'ipv4', 'ipv6']));
117
        $this->populateFromResponse($response);
118
    }
119
120 1
    /**
121
     * {@inheritDoc}
122 1
     */
123 1
    public function delete()
124
    {
125
        $this->execute($this->api->deleteServer(), $this->getAttrs(['id']));
126
    }
127
128 1
    /**
129
     * {@inheritDoc}
130 1
     */
131
    public function retrieve()
132 1
    {
133
        $response = $this->execute($this->api->getServer(), $this->getAttrs(['id']));
134
        $this->populateFromResponse($response);
135
    }
136
137
    /**
138
     * Changes the root password for a server.
139
     *
140 1
     * @param string $newPassword The new root password
141
     */
142 1
    public function changePassword(string $newPassword)
143 1
    {
144
        $this->execute($this->api->changeServerPassword(), [
145 1
            'id'       => $this->id,
146 1
            'password' => $newPassword,
147
        ]);
148
    }
149
150
    /**
151
     * Reboots the server.
152
     *
153 2
     * @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported.
154
     */
155 2
    public function reboot(string $type = Enum::REBOOT_SOFT)
156 1
    {
157
        if (!in_array($type, ['SOFT', 'HARD'])) {
158
            throw new \RuntimeException('Reboot type must either be SOFT or HARD');
159 1
        }
160 1
161 1
        $this->execute($this->api->rebootServer(), [
162 1
            'id'   => $this->id,
163 1
            'type' => $type,
164
        ]);
165
    }
166
167
    /**
168
     * Rebuilds the server.
169
     *
170 1
     * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer}
171
     */
172 1
    public function rebuild(array $options)
173 1
    {
174
        $options['id'] = $this->id;
175 1
        $response = $this->execute($this->api->rebuildServer(), $options);
176 1
177
        $this->populateFromResponse($response);
178
    }
179
180
    /**
181
     * Resizes the server to a new flavor. Once this operation is complete and server has transitioned
182
     * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}.
183
     *
184 1
     * @param string $flavorId The UUID of the new flavor your server will be based on.
185
     */
186 1
    public function resize(string $flavorId)
187 1
    {
188 1
        $response = $this->execute($this->api->resizeServer(), [
189 1
            'id'       => $this->id,
190
            'flavorId' => $flavorId,
191 1
        ]);
192 1
193
        $this->populateFromResponse($response);
194
    }
195
196
    /**
197 1
     * Confirms a previous resize operation.
198
     */
199 1
    public function confirmResize()
200 1
    {
201
        $this->execute($this->api->confirmServerResize(), ['confirmResize' => null, 'id' => $this->id]);
202
    }
203
204
    /**
205 1
     * Reverts a previous resize operation.
206
     */
207 1
    public function revertResize()
208 1
    {
209
        $this->execute($this->api->revertServerResize(), ['revertResize' => null, 'id' => $this->id]);
210
    }
211
212
    /**
213
     * Creates an image for the current server.
214
     *
215 1
     * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage}
216
     */
217 1
    public function createImage(array $options)
218 1
    {
219 1
        $options['id'] = $this->id;
220
        $this->execute($this->api->createServerImage(), $options);
221
    }
222
223
    /**
224
     * Iterates over all the IP addresses for this server.
225
     *
226
     * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork}
227
     *
228 2
     * @return array An array containing to two keys: "public" and "private"
229
     */
230 2
    public function listAddresses(array $options = []): array
231
    {
232 2
        $options['id'] = $this->id;
233 2
234 2
        $data = (isset($options['networkLabel'])) ? $this->api->getAddressesByNetwork() : $this->api->getAddresses();
235
        $response = $this->execute($data, $options);
236
        return Utils::jsonDecode($response)['addresses'];
237
    }
238
239
    /**
240
     * Retrieves metadata from the API.
241
     *
242 1
     * @return array
243
     */
244 1
    public function getMetadata(): array
245 1
    {
246
        $response = $this->execute($this->api->getServerMetadata(), ['id' => $this->id]);
247
        return $this->parseMetadata($response);
248
    }
249
250
    /**
251
     * Resets all the metadata for this server with the values provided. All existing metadata keys
252
     * will either be replaced or removed.
253
     *
254
     * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata}
255
     */
256 1
    public function resetMetadata(array $metadata)
257
    {
258 1
        $response = $this->execute($this->api->putServerMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
259 1
        $this->metadata = $this->parseMetadata($response);
260
    }
261
262
    /**
263
     * Merges the existing metadata for the server with the values provided. Any existing keys
264
     * referenced in the user options will be replaced with the user's new values. All other
265
     * existing keys will remain unaffected.
266
     *
267
     * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata}
268
     *
269
     * @return array
270
     */
271 1
    public function mergeMetadata(array $metadata)
272
    {
273 1
        $response = $this->execute($this->api->postServerMetadata(), ['id' => $this->id, 'metadata' => $metadata]);
274 1
        $this->metadata = $this->parseMetadata($response);
275
    }
276
277
    /**
278
     * Retrieve the value for a specific metadata key.
279
     *
280
     * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey}
281
     *
282
     * @return mixed
283
     */
284 1 View Code Duplication
    public function getMetadataItem(string $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
285
    {
286 1
        $response = $this->execute($this->api->getServerMetadataKey(), ['id' => $this->id, 'key' => $key]);
287 1
        $value = $this->parseMetadata($response)[$key];
288
        $this->metadata[$key] = $value;
289
        return $value;
290
    }
291
292
    /**
293
     * Remove a specific metadata key.
294
     *
295 1
     * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey}
296
     */
297 1 View Code Duplication
    public function deleteMetadataItem(string $key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298 1
    {
299
        if (isset($this->metadata[$key])) {
300
            unset($this->metadata[$key]);
301
        }
302
303
        $this->execute($this->api->deleteServerMetadataKey(), ['id' => $this->id, 'key' => $key]);
304
    }
305
306
307
    /**
308
     * Add security group to a server (addSecurityGroup action)
309
     *
310
     * @param array $options {@see \OpenStack\Compute\v2\Api::addSecurityGroup}
311
     */
312
    public function addSecurityGroup(array $options)
313
    {
314
        $options['id'] = $this->id;
315
        $this->execute($this->api->addSecurityGroup(), $options);
316
    }
317
318
    /**
319
     * Add security group to a server (addSecurityGroup action)
320
     *
321
     * @param array $options {@see \OpenStack\Compute\v2\Api::removeSecurityGroup}
322
     */
323
    public function removeSecurityGroup(array $options)
324
    {
325
        $options['id'] = $this->id;
326
        $this->execute($this->api->removeSecurityGroup(), $options);
327
    }
328
329
    public function parseMetadata(ResponseInterface $response): array
330
    {
331
        return Utils::jsonDecode($response)['metadata'];
332
    }
333
334
    /**
335
     * Returns Generator for SecurityGroups
336
     *
337
     * @return \Generator
338
     */
339
    public function listSecurityGroups()
340
    {
341
        return $this->model(SecurityGroup::class)->enumerate($this->api->listSecurityGroupByServer(), ['id' => $this->id]);
342
    }
343
344
345
    /**
346
     * Returns Generator for SecurityGroups
347
     *
348
     * @return \Generator
349
     */
350
    public function listVolumeAttachments()
351
    {
352
        return $this->model(VolumeAttachment::class)->enumerate($this->api->listVolumeAttachments(),['id' => $this->id]);
353
    }
354
355
    /**
356
     * @param $volumeId
357
     *
358
     * @return VolumeAttachment
359
     */
360
    public function attachVolume($volumeId)
361
    {
362
        $response =  $this->execute($this->api->attachVolume(), ['id' => $this->id, 'volumeId' => $volumeId]);
363
364
        return $this->model(VolumeAttachment::class)->populateFromResponse($response);
365
    }
366
367
    public function detachVolume($attachmentId)
368
    {
369
        return $this->execute($this->api->detachVolume(), ['id' => $this->id, 'attachmentId' => $attachmentId]);
370
    }
371
}
372