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