Skip to content

Commit

Permalink
Ability to override catalog URL, fix #341
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Droz authored and drzraf committed Mar 27, 2024
1 parent 38a6860 commit 144a671
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Identity/v3/Models/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ public function populateFromArray(array $data): self
return $this;
}

/**
* Retrieve a base URL for a service, according to its catalog name, type, region.
*
* @param string $name the name of the service as it appears in the catalog
* @param string $type the type of the service as it appears in the catalog
* @param string $region the region of the service as it appears in the catalog
* @param string $interface the interface of the service as it appears in the catalog
*
* @return null|string NULL if no URL found
*/
public function getServiceUrlOverride(
string $name,
string $type,
string $region,
string $interface,
array $overrides
): ?string {
foreach ($overrides as $override) {
if (
(empty($override['name']) || $name == $override['name'])
&& (empty($override['type']) || $type == $override['type'])
&& (empty($override['region']) || $region == $override['region'])
&& (empty($override['interface']) || $interface == $override['interface'])
) {
if (empty($override['name']) && empty($override['type'])) {
throw new \RuntimeException(sprintf("Endpoint override must at least specify an \"url\" and either \"name\" or a \"type\"."));
}
if (empty($override['url'])) {
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'] ?? ''));
}

return $override['url'];
}
}

return null;
}

/**
* Retrieve a base URL for a service, according to its catalog name, type, region.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Identity/v3/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public function authenticate(array $options): array
$region = $options['region'];
$interface = $options['interface'] ?? Enum::INTERFACE_PUBLIC;

if (!empty($options['catalog_overrides'])) {
$baseUrl = $token->catalog->getServiceUrlOverride(
$name,
$type,
$region,
$interface,
$options['catalog_overrides']
);
if ($baseUrl) {
return [$token, $baseUrl];
}
}
if ($baseUrl = $token->catalog->getServiceUrl($name, $type, $region, $interface)) {
return [$token, $baseUrl];
}
Expand Down

0 comments on commit 144a671

Please sign in to comment.