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