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
Push — master ( 4ba218...ab686e )
by Jamie
04:16
created

Server::revertResize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

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