Skip to content

Commit 144a671

Browse files
Raphaël Drozdrzraf
Raphaël Droz
authored andcommitted
Ability to override catalog URL, fix #341
1 parent 38a6860 commit 144a671

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Identity/v3/Models/Catalog.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,44 @@ public function populateFromArray(array $data): self
3131
return $this;
3232
}
3333

34+
/**
35+
* Retrieve a base URL for a service, according to its catalog name, type, region.
36+
*
37+
* @param string $name the name of the service as it appears in the catalog
38+
* @param string $type the type of the service as it appears in the catalog
39+
* @param string $region the region of the service as it appears in the catalog
40+
* @param string $interface the interface of the service as it appears in the catalog
41+
*
42+
* @return null|string NULL if no URL found
43+
*/
44+
public function getServiceUrlOverride(
45+
string $name,
46+
string $type,
47+
string $region,
48+
string $interface,
49+
array $overrides
50+
): ?string {
51+
foreach ($overrides as $override) {
52+
if (
53+
(empty($override['name']) || $name == $override['name'])
54+
&& (empty($override['type']) || $type == $override['type'])
55+
&& (empty($override['region']) || $region == $override['region'])
56+
&& (empty($override['interface']) || $interface == $override['interface'])
57+
) {
58+
if (empty($override['name']) && empty($override['type'])) {
59+
throw new \RuntimeException(sprintf("Endpoint override must at least specify an \"url\" and either \"name\" or a \"type\"."));
60+
}
61+
if (empty($override['url'])) {
62+
throw new \RuntimeException(sprintf("Endpoint override must specify an \"url\".\nName: %s\nType: %s\nRegion: %s\nInterface: %s", $override['name'] ?? '', $override['type'] ?? '', $override['region'] ?? '', $override['interface'] ?? ''));
63+
}
64+
65+
return $override['url'];
66+
}
67+
}
68+
69+
return null;
70+
}
71+
3472
/**
3573
* Retrieve a base URL for a service, according to its catalog name, type, region.
3674
*

src/Identity/v3/Service.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public function authenticate(array $options): array
4747
$region = $options['region'];
4848
$interface = $options['interface'] ?? Enum::INTERFACE_PUBLIC;
4949

50+
if (!empty($options['catalog_overrides'])) {
51+
$baseUrl = $token->catalog->getServiceUrlOverride(
52+
$name,
53+
$type,
54+
$region,
55+
$interface,
56+
$options['catalog_overrides']
57+
);
58+
if ($baseUrl) {
59+
return [$token, $baseUrl];
60+
}
61+
}
5062
if ($baseUrl = $token->catalog->getServiceUrl($name, $type, $region, $interface)) {
5163
return [$token, $baseUrl];
5264
}

0 commit comments

Comments
 (0)