diff --git a/src/Identity/v3/Models/Catalog.php b/src/Identity/v3/Models/Catalog.php index d208d7b6..a4957243 100644 --- a/src/Identity/v3/Models/Catalog.php +++ b/src/Identity/v3/Models/Catalog.php @@ -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. * diff --git a/src/Identity/v3/Service.php b/src/Identity/v3/Service.php index 5760436f..407cd9cb 100644 --- a/src/Identity/v3/Service.php +++ b/src/Identity/v3/Service.php @@ -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]; }