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 |
|
* List flavors. |
74
|
|
|
* |
75
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getFlavors} |
76
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
77
|
|
|
* @param bool $detailed Set to true to fetch flavors' details. |
78
|
|
|
* |
79
|
|
|
* @return \Generator |
80
|
|
|
*/ |
81
|
|
|
public function listFlavors(array $options = [], callable $mapFn = null, bool $detailed = false): \Generator |
82
|
|
|
{ |
83
|
|
|
$def = $detailed === true ? $this->api->getFlavorsDetail() : $this->api->getFlavors(); |
84
|
|
|
return $this->model(Flavor::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
1 |
|
* Retrieve a flavor object without calling the remote API. Any values provided in the array will populate the |
89
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
90
|
1 |
|
* and have the response populate the object, call {@see Flavor::retrieve}. |
91
|
1 |
|
* |
92
|
1 |
|
* @param array $options An array of attributes that will be set on the {@see Flavor} object. The array keys need to |
93
|
|
|
* correspond to the class public properties. |
94
|
|
|
* |
95
|
|
|
* @return \OpenStack\Compute\v2\Models\Flavor |
96
|
|
|
*/ |
97
|
|
|
public function getFlavor(array $options = []): Flavor |
98
|
|
|
{ |
99
|
|
|
$flavor = $this->model(Flavor::class); |
100
|
|
|
$flavor->populateFromArray($options); |
101
|
|
|
return $flavor; |
102
|
|
|
} |
103
|
1 |
|
|
104
|
|
|
/** |
105
|
1 |
|
* Create a new flavor resource. |
106
|
|
|
* |
107
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::postFlavors} |
108
|
|
|
* |
109
|
|
|
* @return Flavor |
110
|
|
|
*/ |
111
|
|
|
public function createFlavor(array $options = []): Flavor |
112
|
|
|
{ |
113
|
|
|
return $this->model(Flavor::class)->create($options); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* List images. |
118
|
1 |
|
* |
119
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getImages} |
120
|
1 |
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
121
|
1 |
|
* |
122
|
1 |
|
* @return \Generator |
123
|
|
|
*/ |
124
|
|
|
public function listImages(array $options = [], callable $mapFn = null): \Generator |
125
|
|
|
{ |
126
|
|
|
return $this->model(Image::class)->enumerate($this->api->getImages(), $options, $mapFn); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Retrieve an image object without calling the remote API. Any values provided in the array will populate the |
131
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
132
|
|
|
* and have the response populate the object, call {@see Image::retrieve}. |
133
|
|
|
* |
134
|
|
|
* @param array $options An array of attributes that will be set on the {@see Image} object. The array keys need to |
135
|
|
|
* correspond to the class public properties. |
136
|
|
|
* |
137
|
|
|
* @return \OpenStack\Compute\v2\Models\Image |
138
|
|
|
*/ |
139
|
|
|
public function getImage(array $options = []): Image |
140
|
|
|
{ |
141
|
|
|
$image = $this->model(Image::class); |
142
|
|
|
$image->populateFromArray($options); |
143
|
|
|
return $image; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* List key pairs. |
148
|
|
|
* |
149
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getKeyPairs} |
150
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
151
|
|
|
* |
152
|
|
|
* @return \Generator |
153
|
|
|
*/ |
154
|
|
|
public function listKeypairs(array $options = [], callable $mapFn = null): \Generator |
155
|
|
|
{ |
156
|
|
|
return $this->model(Keypair::class)->enumerate($this->api->getKeypairs(), $options, $mapFn); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Create or import keypair |
161
|
|
|
* |
162
|
|
|
* @param array $options |
163
|
|
|
* |
164
|
|
|
* @return Keypair |
165
|
|
|
*/ |
166
|
|
|
public function createKeypair(array $options): Keypair |
167
|
|
|
{ |
168
|
|
|
return $this->model(Keypair::class)->create($options); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get keypair |
173
|
|
|
* |
174
|
|
|
* @param array $options |
175
|
|
|
* |
176
|
|
|
* @return Keypair |
177
|
|
|
*/ |
178
|
|
|
public function getKeypair(array $options = []): Keypair |
179
|
|
|
{ |
180
|
|
|
$keypair = $this->model(Keypair::class); |
181
|
|
|
$keypair->populateFromArray($options); |
182
|
|
|
return $keypair; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Shows rate and absolute limits for the tenant |
187
|
|
|
* |
188
|
|
|
* @return Limit |
189
|
|
|
*/ |
190
|
|
|
public function getLimits(): Limit |
191
|
|
|
{ |
192
|
|
|
$limits = $this->model(Limit::class); |
193
|
|
|
$limits->populateFromResponse($this->execute($this->api->getLimits(), [])); |
194
|
|
|
return $limits; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Shows summary statistics for all hypervisors over all compute nodes. |
199
|
|
|
* |
200
|
|
|
* @return HypervisorStatistic |
201
|
|
|
*/ |
202
|
|
|
public function getHypervisorStatistics(): HypervisorStatistic |
203
|
|
|
{ |
204
|
|
|
$statistics = $this->model(HypervisorStatistic::class); |
205
|
|
|
$statistics->populateFromResponse($this->execute($this->api->getHypervisorStatistics(), [])); |
206
|
|
|
return $statistics; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* List hypervisors. |
211
|
|
|
* |
212
|
|
|
* @param bool $detailed Determines whether detailed information will be returned. If FALSE is specified, only |
213
|
|
|
* the ID, name and links attributes are returned, saving bandwidth. |
214
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getHypervisors} |
215
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
216
|
|
|
* |
217
|
|
|
* @return \Generator |
218
|
|
|
*/ |
219
|
|
|
public function listHypervisors(bool $detailed = false, array $options = [], callable $mapFn = null): \Generator |
220
|
|
|
{ |
221
|
|
|
$def = ($detailed === true) ? $this->api->getHypervisorsDetail() : $this->api->getHypervisors(); |
222
|
|
|
return $this->model(Hypervisor::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Shows details for a given hypervisor. |
227
|
|
|
* |
228
|
|
|
* @param array $options |
229
|
|
|
* |
230
|
|
|
* @return Hypervisor |
231
|
|
|
*/ |
232
|
|
|
public function getHypervisor(array $options = []): Hypervisor |
233
|
|
|
{ |
234
|
|
|
$hypervisor = $this->model(Hypervisor::class); |
235
|
|
|
return $hypervisor->populateFromArray($options); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* List hosts. |
240
|
|
|
* |
241
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getHosts} |
242
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
243
|
|
|
* |
244
|
|
|
* @return \Generator |
245
|
|
|
*/ |
246
|
|
|
public function listHosts(array $options = [], callable $mapFn = null): \Generator |
247
|
|
|
{ |
248
|
|
|
return $this->model(Host::class)->enumerate($this->api->getHosts(), $options, $mapFn); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Retrieve a host object without calling the remote API. Any values provided in the array will populate the |
253
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
254
|
|
|
* and have the response populate the object, call {@see Host::retrieve}. For example: |
255
|
|
|
* |
256
|
|
|
* <code>$server = $service->getHost(['name' => '{name}']);</code> |
257
|
|
|
* |
258
|
|
|
* @param array $options An array of attributes that will be set on the {@see Host} object. The array keys need to |
259
|
|
|
* correspond to the class public properties. |
260
|
|
|
* |
261
|
|
|
* @return \OpenStack\Compute\v2\Models\Host |
262
|
|
|
*/ |
263
|
|
|
public function getHost(array $options = []): Host |
264
|
|
|
{ |
265
|
|
|
$host = $this->model(Host::class); |
266
|
|
|
$host->populateFromArray($options); |
267
|
|
|
return $host; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* List AZs |
272
|
|
|
* |
273
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getAvailabilityZones} |
274
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
275
|
|
|
* |
276
|
|
|
* @return \Generator |
277
|
|
|
*/ |
278
|
|
|
public function listAvailabilityZones(array $options = [], callable $mapFn = null): \Generator |
279
|
|
|
{ |
280
|
|
|
return $this->model(AvailabilityZone::class)->enumerate($this->api->getAvailabilityZones(), $options, $mapFn); |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Shows A Quota for a tenant |
285
|
|
|
* |
286
|
|
|
* @param string $tenantId |
287
|
|
|
* @param bool $detailed |
288
|
|
|
* |
289
|
|
|
* @return QuotaSet |
290
|
|
|
*/ |
291
|
|
|
public function getQuotaSet(string $tenantId, bool $detailed = false): QuotaSet |
292
|
|
|
{ |
293
|
|
|
$quotaSet = $this->model(QuotaSet::class); |
294
|
|
|
$quotaSet->populateFromResponse($this->execute($detailed ? $this->api->getQuotaSetDetail() : $this->api->getQuotaSet(), ['tenantId' => $tenantId])); |
295
|
|
|
|
296
|
|
|
return $quotaSet; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
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: