From 144a671272354d6d47e2df8cc7a9edb01f6d6561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= Date: Thu, 13 Jan 2022 14:44:05 -0300 Subject: [PATCH] Ability to override catalog URL, fix #341 --- src/Identity/v3/Models/Catalog.php | 38 ++++++++++++++++++++++++++++++ src/Identity/v3/Service.php | 12 ++++++++++ 2 files changed, 50 insertions(+) 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]; }