1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Compute\v2; |
4
|
|
|
|
5
|
|
|
use OpenStack\Common\Service\AbstractService; |
6
|
|
|
use OpenStack\Compute\v2\Models\Flavor; |
7
|
|
|
use OpenStack\Compute\v2\Models\HypervisorStatistic; |
8
|
|
|
use OpenStack\Compute\v2\Models\Image; |
9
|
|
|
use OpenStack\Compute\v2\Models\Keypair; |
10
|
|
|
use OpenStack\Compute\v2\Models\Limit; |
11
|
|
|
use OpenStack\Compute\v2\Models\Server; |
12
|
|
|
use OpenStack\Compute\v2\Models\Host; |
13
|
|
|
use OpenStack\Compute\v2\Models\Hypervisor; |
14
|
|
|
use OpenStack\Compute\v2\Models\AvailabilityZone; |
15
|
|
|
use OpenStack\Compute\v2\Models\QuotaSet; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Compute v2 service for OpenStack. |
19
|
|
|
* |
20
|
|
|
* @property \OpenStack\Compute\v2\Api $api |
21
|
|
|
*/ |
22
|
|
|
class Service extends AbstractService |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
1 |
|
* Create a new server resource. This operation will provision a new virtual machine on a host chosen by your |
26
|
|
|
* service API. |
27
|
1 |
|
* |
28
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::postServer} |
29
|
|
|
* |
30
|
|
|
* @return \OpenStack\Compute\v2\Models\Server |
31
|
|
|
*/ |
32
|
|
|
public function createServer(array $options): Server |
33
|
|
|
{ |
34
|
|
|
return $this->model(Server::class)->create($options); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* List servers. |
39
|
|
|
* |
40
|
1 |
|
* @param bool $detailed Determines whether detailed information will be returned. If FALSE is specified, only |
41
|
|
|
* the ID, name and links attributes are returned, saving bandwidth. |
42
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getServers} |
43
|
1 |
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
44
|
|
|
* |
45
|
|
|
* @return \Generator |
46
|
|
|
*/ |
47
|
|
|
public function listServers(bool $detailed = false, array $options = [], callable $mapFn = null): \Generator |
48
|
|
|
{ |
49
|
|
|
$def = ($detailed === true) ? $this->api->getServersDetail() : $this->api->getServers(); |
50
|
|
|
return $this->model(Server::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Retrieve a server object without calling the remote API. Any values provided in the array will populate the |
55
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
56
|
|
|
* and have the response populate the object, call {@see Server::retrieve}. For example: |
57
|
|
|
* |
58
|
1 |
|
* <code>$server = $service->getServer(['id' => '{serverId}']);</code> |
59
|
|
|
* |
60
|
1 |
|
* @param array $options An array of attributes that will be set on the {@see Server} object. The array keys need to |
61
|
1 |
|
* correspond to the class public properties. |
62
|
1 |
|
* |
63
|
|
|
* @return \OpenStack\Compute\v2\Models\Server |
64
|
|
|
*/ |
65
|
|
|
public function getServer(array $options = []): Server |
66
|
|
|
{ |
67
|
|
|
$server = $this->model(Server::class); |
68
|
|
|
$server->populateFromArray($options); |
69
|
|
|
return $server; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
1 |
|
* Get server diagnostics |
74
|
|
|
* |
75
|
1 |
|
* @param array $options An array of attributes that will be set on the {@see Server} object. The array keys need to |
76
|
|
|
* correspond to the class public properties. |
77
|
|
|
* |
78
|
|
|
* @return \OpenStack\Compute\v2\Models\Server |
79
|
|
|
*/ |
80
|
|
|
public function getServerDiagnostics(array $options = []): Server |
81
|
|
|
{ |
82
|
|
|
$server = $this->model(Server::class); |
83
|
|
|
$server->populateFromArray($options); |
84
|
|
|
return $server; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
1 |
|
* List flavors. |
89
|
|
|
* |
90
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getFlavors} |
91
|
1 |
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
92
|
1 |
|
* @param bool $detailed Set to true to fetch flavors' details. |
93
|
|
|
* |
94
|
|
|
* @return \Generator |
95
|
|
|
*/ |
96
|
|
|
public function listFlavors(array $options = [], callable $mapFn = null, bool $detailed = false): \Generator |
97
|
|
|
{ |
98
|
|
|
$def = $detailed === true ? $this->api->getFlavorsDetail() : $this->api->getFlavors(); |
99
|
|
|
return $this->model(Flavor::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
1 |
|
* Retrieve a flavor object without calling the remote API. Any values provided in the array will populate the |
104
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
105
|
1 |
|
* and have the response populate the object, call {@see Flavor::retrieve}. |
106
|
|
|
* |
107
|
|
|
* @param array $options An array of attributes that will be set on the {@see Flavor} object. The array keys need to |
108
|
|
|
* correspond to the class public properties. |
109
|
|
|
* |
110
|
|
|
* @return \OpenStack\Compute\v2\Models\Flavor |
111
|
|
|
*/ |
112
|
|
|
public function getFlavor(array $options = []): Flavor |
113
|
|
|
{ |
114
|
|
|
$flavor = $this->model(Flavor::class); |
115
|
|
|
$flavor->populateFromArray($options); |
116
|
|
|
return $flavor; |
117
|
|
|
} |
118
|
1 |
|
|
119
|
|
|
/** |
120
|
1 |
|
* Create a new flavor resource. |
121
|
1 |
|
* |
122
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::postFlavors} |
123
|
|
|
* |
124
|
|
|
* @return Flavor |
125
|
|
|
*/ |
126
|
|
|
public function createFlavor(array $options = []): Flavor |
127
|
|
|
{ |
128
|
|
|
return $this->model(Flavor::class)->create($options); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* List images. |
133
|
|
|
* |
134
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getImages} |
135
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
136
|
|
|
* |
137
|
|
|
* @return \Generator |
138
|
|
|
*/ |
139
|
|
|
public function listImages(array $options = [], callable $mapFn = null): \Generator |
140
|
|
|
{ |
141
|
|
|
return $this->model(Image::class)->enumerate($this->api->getImages(), $options, $mapFn); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Retrieve an image object without calling the remote API. Any values provided in the array will populate the |
146
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
147
|
|
|
* and have the response populate the object, call {@see Image::retrieve}. |
148
|
|
|
* |
149
|
|
|
* @param array $options An array of attributes that will be set on the {@see Image} object. The array keys need to |
150
|
|
|
* correspond to the class public properties. |
151
|
|
|
* |
152
|
|
|
* @return \OpenStack\Compute\v2\Models\Image |
153
|
|
|
*/ |
154
|
|
|
public function getImage(array $options = []): Image |
155
|
|
|
{ |
156
|
|
|
$image = $this->model(Image::class); |
157
|
|
|
$image->populateFromArray($options); |
158
|
|
|
return $image; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* List key pairs. |
163
|
|
|
* |
164
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getKeyPairs} |
165
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
166
|
|
|
* |
167
|
|
|
* @return \Generator |
168
|
|
|
*/ |
169
|
|
|
public function listKeypairs(array $options = [], callable $mapFn = null): \Generator |
170
|
|
|
{ |
171
|
|
|
return $this->model(Keypair::class)->enumerate($this->api->getKeypairs(), $options, $mapFn); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Create or import keypair |
176
|
|
|
* |
177
|
|
|
* @param array $options |
178
|
|
|
* |
179
|
|
|
* @return Keypair |
180
|
|
|
*/ |
181
|
|
|
public function createKeypair(array $options): Keypair |
182
|
|
|
{ |
183
|
|
|
return $this->model(Keypair::class)->create($options); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get keypair |
188
|
|
|
* |
189
|
|
|
* @param array $options |
190
|
|
|
* |
191
|
|
|
* @return Keypair |
192
|
|
|
*/ |
193
|
|
|
public function getKeypair(array $options = []): Keypair |
194
|
|
|
{ |
195
|
|
|
$keypair = $this->model(Keypair::class); |
196
|
|
|
$keypair->populateFromArray($options); |
197
|
|
|
return $keypair; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Shows rate and absolute limits for the tenant |
202
|
|
|
* |
203
|
|
|
* @return Limit |
204
|
|
|
*/ |
205
|
|
|
public function getLimits(): Limit |
206
|
|
|
{ |
207
|
|
|
$limits = $this->model(Limit::class); |
208
|
|
|
$limits->populateFromResponse($this->execute($this->api->getLimits(), [])); |
209
|
|
|
return $limits; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Shows summary statistics for all hypervisors over all compute nodes. |
214
|
|
|
* |
215
|
|
|
* @return HypervisorStatistic |
216
|
|
|
*/ |
217
|
|
|
public function getHypervisorStatistics(): HypervisorStatistic |
218
|
|
|
{ |
219
|
|
|
$statistics = $this->model(HypervisorStatistic::class); |
220
|
|
|
$statistics->populateFromResponse($this->execute($this->api->getHypervisorStatistics(), [])); |
221
|
|
|
return $statistics; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* List hypervisors. |
226
|
|
|
* |
227
|
|
|
* @param bool $detailed Determines whether detailed information will be returned. If FALSE is specified, only |
228
|
|
|
* the ID, name and links attributes are returned, saving bandwidth. |
229
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getHypervisors} |
230
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
231
|
|
|
* |
232
|
|
|
* @return \Generator |
233
|
|
|
*/ |
234
|
|
|
public function listHypervisors(bool $detailed = false, array $options = [], callable $mapFn = null): \Generator |
235
|
|
|
{ |
236
|
|
|
$def = ($detailed === true) ? $this->api->getHypervisorsDetail() : $this->api->getHypervisors(); |
237
|
|
|
return $this->model(Hypervisor::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Shows details for a given hypervisor. |
242
|
|
|
* |
243
|
|
|
* @param array $options |
244
|
|
|
* |
245
|
|
|
* @return Hypervisor |
246
|
|
|
*/ |
247
|
|
|
public function getHypervisor(array $options = []): Hypervisor |
248
|
|
|
{ |
249
|
|
|
$hypervisor = $this->model(Hypervisor::class); |
250
|
|
|
return $hypervisor->populateFromArray($options); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* List hosts. |
255
|
|
|
* |
256
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getHosts} |
257
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
258
|
|
|
* |
259
|
|
|
* @return \Generator |
260
|
|
|
*/ |
261
|
|
|
public function listHosts(array $options = [], callable $mapFn = null): \Generator |
262
|
|
|
{ |
263
|
|
|
return $this->model(Host::class)->enumerate($this->api->getHosts(), $options, $mapFn); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Retrieve a host object without calling the remote API. Any values provided in the array will populate the |
268
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
269
|
|
|
* and have the response populate the object, call {@see Host::retrieve}. For example: |
270
|
|
|
* |
271
|
|
|
* <code>$server = $service->getHost(['name' => '{name}']);</code> |
272
|
|
|
* |
273
|
|
|
* @param array $options An array of attributes that will be set on the {@see Host} object. The array keys need to |
274
|
|
|
* correspond to the class public properties. |
275
|
|
|
* |
276
|
|
|
* @return \OpenStack\Compute\v2\Models\Host |
277
|
|
|
*/ |
278
|
|
|
public function getHost(array $options = []): Host |
279
|
|
|
{ |
280
|
|
|
$host = $this->model(Host::class); |
281
|
|
|
$host->populateFromArray($options); |
282
|
|
|
return $host; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* List AZs |
287
|
|
|
* |
288
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getAvailabiltyZones} |
289
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
290
|
|
|
* |
291
|
|
|
* @return \Generator |
292
|
|
|
*/ |
293
|
|
|
public function listAvailabilityZones(array $options = [], callable $mapFn = null): \Generator |
294
|
|
|
{ |
295
|
|
|
return $this->model(AvailabilityZone::class)->enumerate($this->api->getAvailabilityZones(), $options, $mapFn); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Shows A Quota for a tenant |
300
|
|
|
* |
301
|
|
|
* @param string $tenantId |
302
|
|
|
* @param bool $detailed |
303
|
|
|
* |
304
|
|
|
* @return QuotaSet |
305
|
|
|
*/ |
306
|
|
|
public function getQuotaSet(string $tenantId, bool $detailed = false): QuotaSet |
307
|
|
|
{ |
308
|
|
|
$quotaSet = $this->model(QuotaSet::class); |
309
|
|
|
$quotaSet->populateFromResponse($this->execute($detailed ? $this->api->getQuotaSetDetail() : $this->api->getQuotaSet(), ['tenantId' => $tenantId])); |
310
|
|
|
|
311
|
|
|
return $quotaSet; |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: