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