Skip to content

Commit db12e22

Browse files
author
Raphaël Droz
committed
Ability to override catalog URL, fix #341
1 parent 42e2d09 commit db12e22

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
@@ -34,6 +34,44 @@ public function populateFromArray(array $data): self
3434
return $this;
3535
}
3636

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

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 = isset($options['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)