| 1 | package OpenStack::MetaAPI::API::Service; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 17 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 17 | ▶ | use OpenStack::MetaAPI::API::Specs::Default (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 17 | has 'auth' => (is => 'ro', required => 1); | |||||||||||||||||||||||||||
| 10 | 17 | has 'name' => (is => 'ro', required => 1); | |||||||||||||||||||||||||||
| 11 | 17 | has 'region' => (is => 'ro', required => 1); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 17 | has 'interface' => (is => 'ro', default => 'public'); # admin internal or public | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has 'client' => ( | ||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||
| 17 | lazy => 1, | ||||||||||||||||||||||||||||
| 18 | 34 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 34 | return $self->auth->service( | |||||||||||||||||||||||||||
| 22 | $self->name, | ||||||||||||||||||||||||||||
| 23 | region => $self->region, | ||||||||||||||||||||||||||||
| 24 | interface => $self->interface); | ||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||
| 26 | 17 | handles => [qw/endpoint get put post delete/] | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 17 | has 'api' => (is => 'ro', required => 1) | |||||||||||||||||||||||||||
| 31 | ; # this is backreference to the mainapi so we can call other services | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 17 | has 'api_specs' => (is => 'ro', lazy => 1, default => \&BUILD_api_specs); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | ## FIXME: this needs a refactor... | ||||||||||||||||||||||||||||
| 36 | # idea always strip the version from endpoint so we can add it to the uri later.. | ||||||||||||||||||||||||||||
| 37 | # this would make uri consistent... and improve root_uri | ||||||||||||||||||||||||||||
| 38 | 17 | has 'version' => ('is' => 'ro', lazy => 1, default => \&BUILD_version); | |||||||||||||||||||||||||||
| 39 | 17 | has 'version_prefix' => ('is' => 'ro'); # added to very routes [optional] | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 43 | ▶ | has 'methods' => (is => 'ro', default => sub { return {} }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 34 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 34 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 34 | $self->api_specs->setup_api_methods_for_service($self); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 34 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 26 | ▶ | sub BUILD_version { | ||||||||||||||||||||||||||
|
Subroutine
BUILD_version: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 26 | my ($self) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 26 | my $url = $self->client->endpoint; | |||||||||||||||||||||||||||
| 55 | 26 | ▶ | if ($url =~ m{/(v[0-9\.]+)}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 21 | return $1; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 5 | return 'default'; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 34 | ▶ | sub BUILD_api_specs { # load specs | ||||||||||||||||||||||||||
|
Subroutine
BUILD_api_specs: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 62 | 34 | my ($self) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 34 | my $v = $self->version; | |||||||||||||||||||||||||||
| 65 | 34 | $v =~ s{\.}{_}; | |||||||||||||||||||||||||||
| 66 | 34 | my $pkg = | |||||||||||||||||||||||||||
| 67 | 'OpenStack::MetaAPI::API::Specs::' . ucfirst($self->name) . '::' . $v; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 34 | (my $file = $pkg) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 70 | 34 | $file .= '.pm'; | |||||||||||||||||||||||||||
| 71 | 34 | my $load = eval { require $file; 1 }; | |||||||||||||||||||||||||||
| 72 | 34 | ▶ | if ($load) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 28 | return $pkg->new(); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # default void specs [ undefined ] | ||||||||||||||||||||||||||||
| 77 | # we do not have to define all specs for now | ||||||||||||||||||||||||||||
| 78 | 6 | return OpenStack::MetaAPI::API::Specs::Default->new(); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 106 | ▶ | sub root_uri { | ||||||||||||||||||||||||||
|
Subroutine
root_uri: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | 106 | my ($self, $uri) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 106 | ▶ | return unless defined $uri; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 104 | ▶ | return $uri if $uri =~ m{^v}; # already contains a version | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # endpoint already contains a version | ||||||||||||||||||||||||||||
| 89 | 102 | ▶ | return $uri if $self->endpoint && $self->endpoint =~ m{/v\d}a; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->endpoint and $self->endpoint =~ m[/v\d]a
MC/DC50%1/2
$self->endpoint and $self->endpoint =~ m[/v\d]a
$self->endpoint
$self->endpoint =~ m[/v\d]a
Truth table66%2/3
A and B
A= $self->endpointB= $self->endpoint =~ m[/v\d]a
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # append our prefix to the endpoint | ||||||||||||||||||||||||||||
| 92 | 31 | ▶ | if ($self->version_prefix) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 30 | my $base = $self->version_prefix; | |||||||||||||||||||||||||||
| 94 | 30 | ▶ | $base .= '/' unless $uri =~ m{^/}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 30 | $base .= $uri; | |||||||||||||||||||||||||||
| 96 | 30 | return $base; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | return $uri; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 121 | ▶ | sub setup_method { | ||||||||||||||||||||||||||
|
Subroutine
setup_method: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 121 | my ($self, $name, $sub) = @_; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 121 | ▶ | die "setup_method must be called as an instance method" unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 121 | ▶ | die "setup_method: method name is required" unless $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 121 | ▶ | die "setup_method: second argument must be a CODE ref" unless ref $sub eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 121 | my $methods = $self->methods(); | |||||||||||||||||||||||||||
| 110 | 121 | ▶ | die "Method '$name' already exists" if defined $methods->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 120 | ▶ | die "Function '$name' already exists for " . ref($self) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | if $self->can($name); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 120 | $methods->{$name} = $sub; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 120 | return 1; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ### not sure how to overwrite 'can' method to let Moo/Class::XSAccessor see the methods | ||||||||||||||||||||||||||||
| 120 | 172 | ▶ | sub can_method { | ||||||||||||||||||||||||||
|
Subroutine
can_method: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 172 | my ($self, $method) = @_; # Not shift, using goto. | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #my $sub = UNIVERSAL::can($self, $method); | ||||||||||||||||||||||||||||
| 124 | 172 | my $sub = $self->can($method); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 172 | ▶ | if (not defined $sub && ref $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sub and ref $self
MC/DC50%1/2
defined $sub and ref $self
defined $sub
ref $self
Truth table66%2/3
A and B
A= defined $subB= ref $self
| |||||||||||||||||||||||||||||
| 127 | 110 | $sub = $self->methods()->{$method}; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 172 | return $sub; # May be undefined. | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 17 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||