|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenStack\Identity\v2\Models; |
|
4
|
|
|
|
|
5
|
|
|
use OpenStack\Common\Resource\Alias; |
|
6
|
|
|
use OpenStack\Common\Resource\OperatorResource; |
|
7
|
|
|
use OpenStack\Common\Transport\Utils; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Represents an Identity v2 service catalog. |
|
12
|
|
|
* |
|
13
|
|
|
* @package OpenStack\Identity\v2\Models |
|
14
|
|
|
*/ |
|
15
|
|
|
class Catalog extends OperatorResource implements \OpenStack\Common\Auth\Catalog |
|
16
|
|
|
{ |
|
17
|
|
|
const DEFAULT_URL_TYPE = 'publicURL'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The catalog entries |
|
21
|
|
|
* |
|
22
|
|
|
* @var []Entry |
|
23
|
|
|
*/ |
|
24
|
|
|
public $entries = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
1 |
|
*/ |
|
29
|
|
|
protected function getAliases(): array |
|
30
|
1 |
|
{ |
|
31
|
|
|
return parent::getAliases() + [ |
|
32
|
1 |
|
'entries' => new Alias('entries', Entry::class, true) |
|
33
|
1 |
|
]; |
|
34
|
1 |
|
} |
|
35
|
1 |
|
|
|
36
|
|
|
/** |
|
37
|
2 |
|
* {@inheritDoc} |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function populateFromResponse(ResponseInterface $response): self |
|
40
|
1 |
|
{ |
|
41
|
1 |
|
$entries = Utils::jsonDecode($response)['access']['serviceCatalog']; |
|
42
|
|
|
|
|
43
|
1 |
|
foreach ($entries as $entry) { |
|
44
|
|
|
$this->entries[] = $this->model(Entry::class, $entry); |
|
45
|
1 |
|
} |
|
46
|
1 |
|
|
|
47
|
1 |
|
return $this; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getServiceUrl( |
|
51
|
|
|
string $serviceName, |
|
52
|
|
|
string $serviceType, |
|
53
|
|
|
string $region, |
|
54
|
|
|
string $urlType = self::DEFAULT_URL_TYPE |
|
55
|
|
|
): string { |
|
56
|
|
|
foreach ($this->entries as $entry) { |
|
57
|
|
|
if ($entry->matches($serviceName, $serviceType) && ($url = $entry->getEndpointUrl($region, $urlType))) { |
|
58
|
|
|
return $url; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
throw new \RuntimeException(sprintf( |
|
63
|
|
|
"Endpoint URL could not be found in the catalog for this service.\nName: %s\nType: %s\nRegion: %s\nURL type: %s", |
|
64
|
|
|
$serviceName, $serviceType, $region, $urlType |
|
65
|
|
|
)); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|