|
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\Hypervisor; |
|
13
|
|
|
use OpenStack\Compute\v2\Models\QuotaSet; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Compute v2 service for OpenStack. |
|
17
|
|
|
* |
|
18
|
|
|
* @property \OpenStack\Compute\v2\Api $api |
|
19
|
|
|
*/ |
|
20
|
|
|
class Service extends AbstractService |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Create a new server resource. This operation will provision a new virtual machine on a host chosen by your |
|
24
|
|
|
* service API. |
|
25
|
1 |
|
* |
|
26
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::postServer} |
|
27
|
1 |
|
* |
|
28
|
|
|
* @return \OpenStack\Compute\v2\Models\Server |
|
29
|
|
|
*/ |
|
30
|
|
|
public function createServer(array $options): Server |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->model(Server::class)->create($options); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* List servers. |
|
37
|
|
|
* |
|
38
|
|
|
* @param bool $detailed Determines whether detailed information will be returned. If FALSE is specified, only |
|
39
|
|
|
* the ID, name and links attributes are returned, saving bandwidth. |
|
40
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getServers} |
|
41
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
|
42
|
1 |
|
* |
|
43
|
1 |
|
* @return \Generator |
|
44
|
|
|
*/ |
|
45
|
|
|
public function listServers(bool $detailed = false, array $options = [], callable $mapFn = null): \Generator |
|
46
|
|
|
{ |
|
47
|
|
|
$def = ($detailed === true) ? $this->api->getServersDetail() : $this->api->getServers(); |
|
48
|
|
|
return $this->model(Server::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Retrieve a server object without calling the remote API. Any values provided in the array will populate the |
|
53
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
|
54
|
|
|
* and have the response populate the object, call {@see Server::retrieve}. For example: |
|
55
|
|
|
* |
|
56
|
|
|
* <code>$server = $service->getServer(['id' => '{serverId}']);</code> |
|
57
|
|
|
* |
|
58
|
1 |
|
* @param array $options An array of attributes that will be set on the {@see Server} object. The array keys need to |
|
59
|
|
|
* correspond to the class public properties. |
|
60
|
1 |
|
* |
|
61
|
1 |
|
* @return \OpenStack\Compute\v2\Models\Server |
|
62
|
1 |
|
*/ |
|
63
|
|
|
public function getServer(array $options = []): Server |
|
64
|
|
|
{ |
|
65
|
|
|
$server = $this->model(Server::class); |
|
66
|
|
|
$server->populateFromArray($options); |
|
67
|
|
|
return $server; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* List flavors. |
|
72
|
|
|
* |
|
73
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getFlavors} |
|
74
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
|
75
|
1 |
|
* |
|
76
|
|
|
* @return \Generator |
|
77
|
|
|
*/ |
|
78
|
|
|
public function listFlavors(array $options = [], callable $mapFn = null): \Generator |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->model(Flavor::class)->enumerate($this->api->getFlavors(), $options, $mapFn); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Retrieve a flavor object without calling the remote API. Any values provided in the array will populate the |
|
85
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
|
86
|
|
|
* and have the response populate the object, call {@see Flavor::retrieve}. |
|
87
|
|
|
* |
|
88
|
1 |
|
* @param array $options An array of attributes that will be set on the {@see Flavor} object. The array keys need to |
|
89
|
|
|
* correspond to the class public properties. |
|
90
|
1 |
|
* |
|
91
|
1 |
|
* @return \OpenStack\Compute\v2\Models\Flavor |
|
92
|
1 |
|
*/ |
|
93
|
|
|
public function getFlavor(array $options = []): Flavor |
|
94
|
|
|
{ |
|
95
|
|
|
$flavor = $this->model(Flavor::class); |
|
96
|
|
|
$flavor->populateFromArray($options); |
|
97
|
|
|
return $flavor; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Create a new flavor resource. |
|
102
|
|
|
* |
|
103
|
1 |
|
* @param array $options {@see \OpenStack\Compute\v2\Api::postFlavors} |
|
104
|
|
|
* |
|
105
|
1 |
|
* @return Flavor |
|
106
|
|
|
*/ |
|
107
|
|
|
public function createFlavor(array $options = []): Flavor |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->model(Flavor::class)->create($options); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* List images. |
|
114
|
|
|
* |
|
115
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getImages} |
|
116
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
|
117
|
|
|
* |
|
118
|
1 |
|
* @return \Generator |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function listImages(array $options = [], callable $mapFn = null): \Generator |
|
121
|
1 |
|
{ |
|
122
|
1 |
|
return $this->model(Image::class)->enumerate($this->api->getImages(), $options, $mapFn); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Retrieve an image object without calling the remote API. Any values provided in the array will populate the |
|
127
|
|
|
* empty object, allowing you greater control without the expense of network transactions. To call the remote API |
|
128
|
|
|
* and have the response populate the object, call {@see Image::retrieve}. |
|
129
|
|
|
* |
|
130
|
|
|
* @param array $options An array of attributes that will be set on the {@see Image} object. The array keys need to |
|
131
|
|
|
* correspond to the class public properties. |
|
132
|
|
|
* |
|
133
|
|
|
* @return \OpenStack\Compute\v2\Models\Image |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getImage(array $options = []): Image |
|
136
|
|
|
{ |
|
137
|
|
|
$image = $this->model(Image::class); |
|
138
|
|
|
$image->populateFromArray($options); |
|
139
|
|
|
return $image; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* List key pairs. |
|
144
|
|
|
* |
|
145
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getKeyPairs} |
|
146
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
|
147
|
|
|
* |
|
148
|
|
|
* @return \Generator |
|
149
|
|
|
*/ |
|
150
|
|
|
public function listKeypairs(array $options = [], callable $mapFn = null): \Generator |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->model(Keypair::class)->enumerate($this->api->getKeypairs(), $options, $mapFn); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Create or import keypair |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $options |
|
159
|
|
|
* |
|
160
|
|
|
* @return Keypair |
|
161
|
|
|
*/ |
|
162
|
|
|
public function createKeypair(array $options): Keypair |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->model(Keypair::class)->create($options); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get keypair |
|
169
|
|
|
* |
|
170
|
|
|
* @param array $options |
|
171
|
|
|
* |
|
172
|
|
|
* @return Keypair |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getKeypair(array $options = []): Keypair |
|
175
|
|
|
{ |
|
176
|
|
|
$keypair = $this->model(Keypair::class); |
|
177
|
|
|
$keypair->populateFromArray($options); |
|
178
|
|
|
return $keypair; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Shows rate and absolute limits for the tenant |
|
183
|
|
|
* |
|
184
|
|
|
* @return Limit |
|
185
|
|
|
*/ |
|
186
|
|
|
public function getLimits(): Limit |
|
187
|
|
|
{ |
|
188
|
|
|
$limits = $this->model(Limit::class); |
|
189
|
|
|
$limits->populateFromResponse($this->execute($this->api->getLimits(), [])); |
|
190
|
|
|
return $limits; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Shows summary statistics for all hypervisors over all compute nodes. |
|
195
|
|
|
* |
|
196
|
|
|
* @return HypervisorStatistic |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getHypervisorStatistics(): HypervisorStatistic |
|
199
|
|
|
{ |
|
200
|
|
|
$statistics = $this->model(HypervisorStatistic::class); |
|
201
|
|
|
$statistics->populateFromResponse($this->execute($this->api->getHypervisorStatistics(), [])); |
|
202
|
|
|
return $statistics; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* List hypervisors. |
|
207
|
|
|
* |
|
208
|
|
|
* @param bool $detailed Determines whether detailed information will be returned. If FALSE is specified, only |
|
209
|
|
|
* the ID, name and links attributes are returned, saving bandwidth. |
|
210
|
|
|
* @param array $options {@see \OpenStack\Compute\v2\Api::getHypervisors} |
|
211
|
|
|
* @param callable $mapFn A callable function that will be invoked on every iteration of the list. |
|
212
|
|
|
* |
|
213
|
|
|
* @return \Generator |
|
214
|
|
|
*/ |
|
215
|
|
|
public function listHypervisors(bool $detailed = false, array $options = [], callable $mapFn = null): \Generator |
|
216
|
|
|
{ |
|
217
|
|
|
$def = ($detailed === true) ? $this->api->getHypervisorsDetail() : $this->api->getHypervisors(); |
|
218
|
|
|
return $this->model(Hypervisor::class)->enumerate($def, $options, $mapFn); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Shows details for a given hypervisor. |
|
223
|
|
|
* |
|
224
|
|
|
* @param array $options |
|
225
|
|
|
* |
|
226
|
|
|
* @return Hypervisor |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getHypervisor(array $options = []): Hypervisor |
|
229
|
|
|
{ |
|
230
|
|
|
$hypervisor = $this->model(Hypervisor::class); |
|
231
|
|
|
return $hypervisor->populateFromArray($options); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Shows A Quota for a tenant |
|
236
|
|
|
* |
|
237
|
|
|
* @param string $tenantId |
|
238
|
|
|
* @param bool $detailed |
|
239
|
|
|
* |
|
240
|
|
|
* @return QuotaSet |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getQuotaSet(string $tenantId, bool $detailed = false): QuotaSet |
|
243
|
|
|
{ |
|
244
|
|
|
$quotaSet = $this->model(QuotaSet::class); |
|
245
|
|
|
$quotaSet->populateFromResponse($this->execute($detailed ? $this->api->getQuotaSetDetail() : $this->api->getQuotaSet(), ['tenantId' => $tenantId])); |
|
246
|
|
|
|
|
247
|
|
|
return $quotaSet; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
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: