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