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

Add endpoint for listing all WebAPI routes #6070

Merged
merged 1 commit into from
Dec 3, 2024
Merged
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
2 changes: 2 additions & 0 deletions lib/OpenQA/WebAPI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ sub startup ($self) {
my $api_ro = $api_auth_operator->any('/')->to(namespace => 'OpenQA::WebAPI::Controller::API::V1');
my $api_ra = $api_auth_admin->any('/')->to(namespace => 'OpenQA::WebAPI::Controller::API::V1');
my $api_public_r = $api_public->any('/')->to(namespace => 'OpenQA::WebAPI::Controller::API::V1');

$api_public_r->get('/routes')->name('api_v1_list_routes')->to('routes#list');
push @api_routes, $api_ru, $api_ro, $api_ra, $api_public_r;
# this is fallback redirect if one does not use apache
$api_public_r->websocket(
Expand Down
54 changes: 54 additions & 0 deletions lib/OpenQA/WebAPI/Controller/API/V1/Routes.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later

package OpenQA::WebAPI::Controller::API::V1::Routes;
use Mojo::Base 'Mojolicious::Controller', -signatures;

=pod

=head1 NAME

OpenQA::WebAPI::Controller::API::V1::Routes

=head1 SYNOPSIS

use OpenQA::WebAPI::Controller::API::V1::Routes;

=head1 DESCRIPTION


=head1 METHODS

=over 4

=item list()

Lists all WebAPI routes

=back

=cut

sub list ($self) {
my $routes = $self->app->routes->children;
my %hash;
_walk($_, \%hash, '') for @$routes;
my @list;
for my $route (sort keys %hash) {
my $methods = $hash{$route};
push @list, {path => $route, methods => [sort keys %$methods]};
}
$self->render(json => {routes => \@list});
}

sub _walk ($route, $hash, $path) {
my $pattern = $route->pattern->unparsed || '';
my $methods = $route->can('methods') ? $route->methods : $route->via;
my $endpoint = "$path$pattern" || '/';
$hash->{$endpoint}->{$_} = 1 for @{$methods || []};
for my $child (@{$route->children || []}) {
_walk($child, $hash, "$path$pattern");
}
}

1;
3 changes: 3 additions & 0 deletions t/22-dashboard.t
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ subtest 'Changelog' => sub {

$t->get_ok('/health')->status_is(200)->content_is('ok', 'health check route just returns plain ok');

$t->get_ok('/api/v1/routes')->status_is(200)
->json_is('/routes/1/path', '/admin', 'simple check for listing WebAPI routes');

$t->get_ok('/dashboard_build_results')->status_is(200);
@h2 = $t->tx->res->dom->find('h2 a')->map('text')->each;
is_deeply(\@h2, ['opensuse', 'opensuse test'], 'empty parent group not shown');
Expand Down
Loading