File Coverage

blib/lib/OpenStack/MetaAPI/Routes.pm
Criterion Covered Total %
statement 36 37 97.3
branch 5 8 62.5
condition 1 3 33.3
subroutine 10 11 90.9
pod 0 3 0.0
total 52 62 83.8


line stmt bran cond sub pod time code
1             package OpenStack::MetaAPI::Routes;
2              
3 6     6   42 use strict;
  6         10  
  6         163  
4 6     6   29 use warnings;
  6         12  
  6         132  
5              
6 6     6   3208 use Moo;
  6         62860  
  6         27  
7              
8 6     6   9996 use OpenStack::MetaAPI::API ();
  6         17  
  6         113  
9 6     6   2209 use YAML::XS;
  6         14948  
  6         296  
10              
11 6     6   2633 use OpenStack::MetaAPI::Helpers::DataAsYaml;
  6         16  
  6         2269  
12              
13             has 'auth' => (is => 'ro');
14             has 'api' => (is => 'ro');
15              
16             our $ROUTES;
17              
18             sub init_once {
19 6   33 6 0 45 $ROUTES //= OpenStack::MetaAPI::Helpers::DataAsYaml::LoadData();
20             }
21              
22             # cannot read from data block at compile time
23             #INIT { init_once() }
24              
25             sub list_all {
26 6     6 0 85 init_once();
27 6         101 return sort keys %$ROUTES;
28             }
29              
30       0     sub DESTROY {
31             }
32              
33             our $AUTOLOAD;
34              
35             sub AUTOLOAD {
36 39     39   33366 my (@args) = @_;
37 39         77 my $call_for = $AUTOLOAD;
38              
39 39         166 $call_for =~ s/.*:://;
40              
41 39 50       169 if (my $route = $ROUTES->{$call_for}) {
42 39 50       109 die "$call_for is a method call" unless ref $args[0] eq __PACKAGE__;
43 39         64 my $self = shift @args;
44              
45 39         130 my $service = $self->service($route->{service});
46              
47             # not easy to overwrite can if Moo/XS::Accessor
48 39         137 my $controller = $service->can_method($call_for);
49              
50 39 50       85 die "Invalid route '$call_for' for service '" . ref($service) . "'"
51             unless defined $controller;
52              
53 39         120 return $controller->($service, @args);
54             }
55              
56 0         0 die "Unknown function $call_for from AUTOLOAD";
57             }
58              
59             sub service {
60 39     39 0 82 my ($self, $name) = @_;
61              
62             # cache the service once
63 39         78 my $k = '_service_' . $name;
64 39 100       94 if (!$self->{$k}) {
65             $self->{$k} = OpenStack::MetaAPI::API::get_service(
66             name => $name, auth => $self->auth,
67 8         73 region => $ENV{'OS_REGION_NAME'},
68              
69             # backreference
70             api => $self->api,
71             );
72             }
73              
74 39         93 return $self->{$k};
75             }
76              
77             1;
78              
79             ## this data block describes the routes
80             # this could be moved to a file...
81              
82             =pod
83              
84             =encoding UTF-8
85              
86             =head1 NAME
87              
88             OpenStack::MetaAPI::Routes
89              
90             =head1 VERSION
91              
92             version 0.002
93              
94             =head1 AUTHOR
95              
96             Nicolas R
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is copyright (c) 2019 by cPanel, Inc.
101              
102             This is free software; you can redistribute it and/or modify it under
103             the same terms as the Perl 5 programming language system itself.
104              
105             =cut
106              
107             __DATA__