Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to override catalog URL, fix #341 #342

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

/**
* Override a given service's predetermined endpoint URL.
*
* @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