1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Identity\v3; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use OpenStack\Common\Auth\IdentityService; |
7
|
|
|
use OpenStack\Common\Error\BadResponseError; |
8
|
|
|
use OpenStack\Common\Service\AbstractService; |
9
|
|
|
use OpenStack\Identity\v3\Models; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents the Keystone v3 service. |
13
|
|
|
* |
14
|
|
|
* @property \OpenStack\Identity\v3\Api $api |
15
|
|
|
*/ |
16
|
|
|
class Service extends AbstractService implements IdentityService |
17
|
|
|
{ |
18
|
5 |
|
public static function factory(ClientInterface $client): self |
19
|
|
|
{ |
20
|
5 |
|
return new static($client, new Api()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Authenticates credentials, giving back a token and a base URL for the service. |
25
|
|
|
* |
26
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postTokens} |
27
|
|
|
* |
28
|
|
|
* @return array Returns a {@see Models\Token} as the first element, a string base URL as the second |
29
|
|
|
*/ |
30
|
6 |
|
public function authenticate(array $options): array |
31
|
|
|
{ |
32
|
6 |
|
$authOptions = array_intersect_key($options, $this->api->postTokens()['params']); |
33
|
|
|
|
34
|
6 |
|
$token = $this->generateToken($authOptions); |
35
|
|
|
|
36
|
2 |
|
$name = $options['catalogName']; |
37
|
2 |
|
$type = $options['catalogType']; |
38
|
2 |
|
$region = $options['region']; |
39
|
2 |
|
$interface = isset($options['interface']) ? $options['interface'] : Enum::INTERFACE_PUBLIC; |
40
|
|
|
|
41
|
2 |
|
if ($baseUrl = $token->catalog->getServiceUrl($name, $type, $region, $interface)) { |
42
|
1 |
|
return [$token, $baseUrl]; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
throw new \RuntimeException(sprintf("No service found with type [%s] name [%s] region [%s] interface [%s]", |
46
|
1 |
|
$type, $name, $region, $interface)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Generates a new authentication token |
51
|
|
|
* |
52
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postTokens} |
53
|
|
|
* |
54
|
|
|
* @return Models\Token |
55
|
|
|
*/ |
56
|
8 |
|
public function generateToken(array $options): Models\Token |
57
|
|
|
{ |
58
|
8 |
|
return $this->model(Models\Token::class)->create($options); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieves a token object and populates its unique identifier object. This operation will not perform a GET or |
63
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
64
|
|
|
* |
65
|
|
|
* @param string $id The unique ID of the token to retrieve |
66
|
|
|
* |
67
|
|
|
* @return Models\Token |
68
|
|
|
*/ |
69
|
1 |
|
public function getToken(string $id): Models\Token |
70
|
|
|
{ |
71
|
1 |
|
return $this->model(Models\Token::class, ['id' => $id]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Validates a token, identified by its ID, and returns TRUE if its valid, FALSE if not. |
76
|
|
|
* |
77
|
|
|
* @param string $id The unique ID of the token |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
2 |
|
public function validateToken(string $id): bool |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
2 |
|
$this->execute($this->api->headTokens(), ['tokenId' => $id]); |
85
|
1 |
|
return true; |
86
|
1 |
|
} catch (BadResponseError $e) { |
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Revokes a token, identified by its ID. After this operation completes, users will not be able to use this token |
93
|
|
|
* again for authentication. |
94
|
|
|
* |
95
|
|
|
* @param string $id The unique ID of the token |
96
|
|
|
*/ |
97
|
|
|
public function revokeToken(string $id) |
98
|
|
|
{ |
99
|
1 |
|
$this->execute($this->api->deleteTokens(), ['tokenId' => $id]); |
100
|
1 |
|
} |
101
|
1 |
|
|
102
|
1 |
|
/** |
103
|
|
|
* Creates a new service according to the provided options. |
104
|
|
|
* |
105
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postServices} |
106
|
|
|
* |
107
|
|
|
* @return Models\Service |
108
|
|
|
*/ |
109
|
|
|
public function createService(array $options): Models\Service |
110
|
|
|
{ |
111
|
1 |
|
return $this->model(Models\Service::class)->create($options); |
|
|
|
|
112
|
|
|
} |
113
|
1 |
|
|
114
|
|
|
/** |
115
|
|
|
* Returns a generator which will yield a collection of service objects. The elements which generators yield can be |
116
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
117
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
118
|
|
|
* |
119
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getServices} |
120
|
|
|
* |
121
|
|
|
* @return \Generator |
122
|
|
|
*/ |
123
|
|
|
public function listServices(array $options = []): \Generator |
124
|
|
|
{ |
125
|
1 |
|
return $this->model(Models\Service::class)->enumerate($this->api->getServices(), $options); |
|
|
|
|
126
|
|
|
} |
127
|
1 |
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieves a service object and populates its unique identifier object. This operation will not perform a GET or |
130
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
131
|
|
|
* |
132
|
|
|
* @param string $id The unique ID of the service |
133
|
|
|
* |
134
|
|
|
* @return Models\Service |
135
|
|
|
*/ |
136
|
|
|
public function getService(string $id): Models\Service |
137
|
|
|
{ |
138
|
1 |
|
return $this->model(Models\Service::class, ['id' => $id]); |
139
|
|
|
} |
140
|
1 |
|
|
141
|
|
|
/** |
142
|
|
|
* Creates a new endpoint according to the provided options. |
143
|
|
|
* |
144
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postEndpoints} |
145
|
|
|
* |
146
|
|
|
* @return Models\Endpoint |
147
|
|
|
*/ |
148
|
|
|
public function createEndpoint(array $options): Models\Endpoint |
149
|
|
|
{ |
150
|
2 |
|
return $this->model(Models\Endpoint::class)->create($options); |
|
|
|
|
151
|
|
|
} |
152
|
2 |
|
|
153
|
|
|
/** |
154
|
|
|
* Retrieves an endpoint object and populates its unique identifier object. This operation will not perform a GET or |
155
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
156
|
|
|
* |
157
|
|
|
* @param string $id The unique ID of the service |
158
|
|
|
* |
159
|
|
|
* @return Models\Endpoint |
160
|
|
|
*/ |
161
|
|
|
public function getEndpoint(string $id): Models\Endpoint |
162
|
|
|
{ |
163
|
1 |
|
return $this->model(Models\Endpoint::class, ['id' => $id]); |
164
|
|
|
} |
165
|
1 |
|
|
166
|
|
|
/** |
167
|
|
|
* Returns a generator which will yield a collection of endpoint objects. The elements which generators yield can be |
168
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
169
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
170
|
|
|
* |
171
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getEndpoints} |
172
|
|
|
* |
173
|
|
|
* @return \Generator |
174
|
|
|
*/ |
175
|
|
|
public function listEndpoints(array $options = []): \Generator |
176
|
|
|
{ |
177
|
1 |
|
return $this->model(Models\Endpoint::class)->enumerate($this->api->getEndpoints(), $options); |
|
|
|
|
178
|
|
|
} |
179
|
1 |
|
|
180
|
|
|
/** |
181
|
|
|
* Creates a new domain according to the provided options. |
182
|
|
|
* |
183
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postDomains} |
184
|
|
|
* |
185
|
|
|
* @return Models\Domain |
186
|
|
|
*/ |
187
|
|
|
public function createDomain(array $options): Models\Domain |
188
|
|
|
{ |
189
|
1 |
|
return $this->model(Models\Domain::class)->create($options); |
|
|
|
|
190
|
|
|
} |
191
|
1 |
|
|
192
|
|
|
/** |
193
|
|
|
* Returns a generator which will yield a collection of domain objects. The elements which generators yield can be |
194
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
195
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
196
|
|
|
* |
197
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getDomains} |
198
|
|
|
* |
199
|
|
|
* @return \Generator |
200
|
|
|
*/ |
201
|
|
|
public function listDomains(array $options = []): \Generator |
202
|
|
|
{ |
203
|
1 |
|
return $this->model(Models\Domain::class)->enumerate($this->api->getDomains(), $options); |
|
|
|
|
204
|
|
|
} |
205
|
1 |
|
|
206
|
|
|
/** |
207
|
|
|
* Retrieves a domain object and populates its unique identifier object. This operation will not perform a GET or |
208
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
209
|
|
|
* |
210
|
|
|
* @param string $id The unique ID of the domain |
211
|
|
|
* |
212
|
|
|
* @return Models\Domain |
213
|
|
|
*/ |
214
|
|
|
public function getDomain(string $id): Models\Domain |
215
|
|
|
{ |
216
|
1 |
|
return $this->model(Models\Domain::class, ['id' => $id]); |
217
|
|
|
} |
218
|
1 |
|
|
219
|
|
|
/** |
220
|
|
|
* Creates a new project according to the provided options. |
221
|
|
|
* |
222
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postProjects} |
223
|
|
|
* |
224
|
|
|
* @return Models\Project |
225
|
|
|
*/ |
226
|
|
|
public function createProject(array $options): Models\Project |
227
|
|
|
{ |
228
|
1 |
|
return $this->model(Models\Project::class)->create($options); |
|
|
|
|
229
|
|
|
} |
230
|
1 |
|
|
231
|
|
|
/** |
232
|
|
|
* Returns a generator which will yield a collection of project objects. The elements which generators yield can be |
233
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
234
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
235
|
|
|
* |
236
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getProjects} |
237
|
|
|
* |
238
|
|
|
* @return \Generator |
239
|
|
|
*/ |
240
|
|
|
public function listProjects(array $options = []): \Generator |
241
|
|
|
{ |
242
|
1 |
|
return $this->model(Models\Project::class)->enumerate($this->api->getProjects(), $options); |
|
|
|
|
243
|
|
|
} |
244
|
1 |
|
|
245
|
|
|
/** |
246
|
|
|
* Retrieves a project object and populates its unique identifier object. This operation will not perform a GET or |
247
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
248
|
|
|
* |
249
|
|
|
* @param string $id The unique ID of the project |
250
|
|
|
* |
251
|
|
|
* @return Models\Project |
252
|
|
|
*/ |
253
|
|
|
public function getProject(string $id): Models\Project |
254
|
|
|
{ |
255
|
1 |
|
return $this->model(Models\Project::class, ['id' => $id]); |
256
|
|
|
} |
257
|
1 |
|
|
258
|
|
|
/** |
259
|
|
|
* Creates a new user according to the provided options. |
260
|
|
|
* |
261
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postUsers} |
262
|
|
|
* |
263
|
|
|
* @return Models\User |
264
|
|
|
*/ |
265
|
|
|
public function createUser(array $options): Models\User |
266
|
|
|
{ |
267
|
1 |
|
return $this->model(Models\User::class)->create($options); |
|
|
|
|
268
|
|
|
} |
269
|
1 |
|
|
270
|
|
|
/** |
271
|
|
|
* Returns a generator which will yield a collection of user objects. The elements which generators yield can be |
272
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
273
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
274
|
|
|
* |
275
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getUsers} |
276
|
|
|
* |
277
|
|
|
* @return \Generator |
278
|
|
|
*/ |
279
|
|
|
public function listUsers(array $options = []): \Generator |
280
|
|
|
{ |
281
|
1 |
|
return $this->model(Models\User::class)->enumerate($this->api->getUsers(), $options); |
|
|
|
|
282
|
|
|
} |
283
|
1 |
|
|
284
|
|
|
/** |
285
|
|
|
* Retrieves a user object and populates its unique identifier object. This operation will not perform a GET or |
286
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
287
|
|
|
* |
288
|
|
|
* @param string $id The unique ID of the user |
289
|
|
|
* |
290
|
|
|
* @return Models\User |
291
|
|
|
*/ |
292
|
|
|
public function getUser(string $id): Models\User |
293
|
|
|
{ |
294
|
1 |
|
return $this->model(Models\User::class, ['id' => $id]); |
295
|
|
|
} |
296
|
1 |
|
|
297
|
|
|
/** |
298
|
|
|
* Creates a new group according to the provided options. |
299
|
|
|
* |
300
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postGroups} |
301
|
|
|
* |
302
|
|
|
* @return Models\Group |
303
|
|
|
*/ |
304
|
|
|
public function createGroup(array $options): Models\Group |
305
|
|
|
{ |
306
|
1 |
|
return $this->model(Models\Group::class)->create($options); |
|
|
|
|
307
|
|
|
} |
308
|
1 |
|
|
309
|
|
|
/** |
310
|
|
|
* Returns a generator which will yield a collection of group objects. The elements which generators yield can be |
311
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
312
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
313
|
|
|
* |
314
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getGroups} |
315
|
|
|
* |
316
|
|
|
* @return \Generator |
317
|
|
|
*/ |
318
|
|
|
public function listGroups(array $options = []): \Generator |
319
|
|
|
{ |
320
|
1 |
|
return $this->model(Models\Group::class)->enumerate($this->api->getGroups(), $options); |
|
|
|
|
321
|
|
|
} |
322
|
1 |
|
|
323
|
|
|
/** |
324
|
|
|
* Retrieves a group object and populates its unique identifier object. This operation will not perform a GET or |
325
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
326
|
|
|
* |
327
|
|
|
* @param string $id The unique ID of the group |
328
|
|
|
* |
329
|
|
|
* @return Models\Group |
330
|
|
|
*/ |
331
|
|
|
public function getGroup($id): Models\Group |
332
|
|
|
{ |
333
|
1 |
|
return $this->model(Models\Group::class, ['id' => $id]); |
334
|
|
|
} |
335
|
1 |
|
|
336
|
|
|
/** |
337
|
|
|
* Creates a new credential according to the provided options. |
338
|
|
|
* |
339
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postCredentials} |
340
|
|
|
* |
341
|
|
|
* @return Models\Credential |
342
|
|
|
*/ |
343
|
|
|
public function createCredential(array $options): Models\Credential |
344
|
|
|
{ |
345
|
1 |
|
return $this->model(Models\Credential::class)->create($options); |
|
|
|
|
346
|
|
|
} |
347
|
1 |
|
|
348
|
|
|
/** |
349
|
|
|
* Returns a generator which will yield a collection of credential objects. The elements which generators yield can |
350
|
|
|
* be accessed using a foreach loop. Often the API will not return the full state of the resource in collections; |
351
|
|
|
* you will need to use retrieve() to pull in the full state of the remote resource from the API. |
352
|
|
|
* |
353
|
|
|
* @return \Generator |
354
|
|
|
*/ |
355
|
|
|
public function listCredentials(): \Generator |
356
|
|
|
{ |
357
|
1 |
|
return $this->model(Models\Credential::class)->enumerate($this->api->getCredentials()); |
|
|
|
|
358
|
|
|
} |
359
|
1 |
|
|
360
|
|
|
/** |
361
|
|
|
* Retrieves a credential object and populates its unique identifier object. This operation will not perform a GET |
362
|
|
|
* or HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
363
|
|
|
* |
364
|
|
|
* @param string $id The unique ID of the credential |
365
|
|
|
* |
366
|
|
|
* @return Models\Credential |
367
|
|
|
*/ |
368
|
|
|
public function getCredential(string $id): Models\Credential |
369
|
|
|
{ |
370
|
1 |
|
return $this->model(Models\Credential::class, ['id' => $id]); |
371
|
|
|
} |
372
|
1 |
|
|
373
|
|
|
/** |
374
|
|
|
* Creates a new role according to the provided options. |
375
|
|
|
* |
376
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postRoles} |
377
|
|
|
* |
378
|
|
|
* @return Models\Role |
379
|
|
|
*/ |
380
|
|
|
public function createRole(array $options): Models\Role |
381
|
|
|
{ |
382
|
1 |
|
return $this->model(Models\Role::class)->create($options); |
|
|
|
|
383
|
|
|
} |
384
|
1 |
|
|
385
|
|
|
/** |
386
|
|
|
* Returns a generator which will yield a collection of role objects. The elements which generators yield can be |
387
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
388
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
389
|
|
|
* |
390
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getRoles} |
391
|
|
|
* |
392
|
|
|
* @return \Generator |
393
|
|
|
*/ |
394
|
|
|
public function listRoles(array $options = []): \Generator |
395
|
|
|
{ |
396
|
1 |
|
return $this->model(Models\Role::class)->enumerate($this->api->getRoles(), $options); |
|
|
|
|
397
|
|
|
} |
398
|
1 |
|
|
399
|
|
|
/** |
400
|
|
|
* Returns a generator which will yield a collection of role assignment objects. The elements which generators |
401
|
|
|
* yield can be accessed using a foreach loop. Often the API will not return the full state of the resource in |
402
|
|
|
* collections; you will need to use retrieve() to pull in the full state of the remote resource from the API. |
403
|
|
|
* |
404
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getRoleAssignments} |
405
|
|
|
* |
406
|
|
|
* @return \Generator |
407
|
|
|
*/ |
408
|
|
|
public function listRoleAssignments(array $options = []): \Generator |
409
|
|
|
{ |
410
|
1 |
|
return $this->model(Models\Assignment::class)->enumerate($this->api->getRoleAssignments(), $options); |
|
|
|
|
411
|
|
|
} |
412
|
1 |
|
|
413
|
|
|
/** |
414
|
|
|
* Creates a new policy according to the provided options. |
415
|
|
|
* |
416
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postPolicies} |
417
|
|
|
* |
418
|
|
|
* @return Models\Policy |
419
|
|
|
*/ |
420
|
|
|
public function createPolicy(array $options): Models\Policy |
421
|
|
|
{ |
422
|
1 |
|
return $this->model(Models\Policy::class)->create($options); |
|
|
|
|
423
|
|
|
} |
424
|
1 |
|
|
425
|
|
|
/** |
426
|
|
|
* Updates a policy object according to the provided options. |
427
|
|
|
* |
428
|
|
|
* @param string $id |
429
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::patchPolicy} |
430
|
|
|
* |
431
|
|
|
* @return Models\Policy |
432
|
|
|
*/ |
433
|
|
|
public function updatePolicy(string $id, array $options): Models\Policy |
434
|
|
|
{ |
435
|
|
|
$options['id'] = $id; |
436
|
1 |
|
|
437
|
|
|
return $this->model(Models\Policy::class)->update($options); |
|
|
|
|
438
|
1 |
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Returns a generator which will yield a collection of policy objects. The elements which generators yield can be |
442
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
443
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
444
|
|
|
* |
445
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getPolicies} |
446
|
|
|
* |
447
|
|
|
* @return \Generator |
448
|
|
|
*/ |
449
|
1 |
|
public function listPolicies(array $options = []): \Generator |
450
|
|
|
{ |
451
|
1 |
|
return $this->model(Models\Policy::class)->enumerate($this->api->getPolicies(), $options); |
|
|
|
|
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Retrieves a policy object and populates its unique identifier object. This operation will not perform a GET or |
456
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
457
|
|
|
* |
458
|
|
|
* @param string $id The unique ID of the policy |
459
|
|
|
* |
460
|
|
|
* @return Models\Policy |
461
|
|
|
*/ |
462
|
|
|
public function getPolicy(string $id): Models\Policy |
463
|
|
|
{ |
464
|
|
|
return $this->model(Models\Policy::class, ['id' => $id]); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
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: