Kubernetes/Role/APIAccess.pm

filter: Statementstmt 93.3% 56 / 60 Branchbran 77.7% 14 / 18 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 87.8% 87 / 99 CC 12 SCAR 17.1
CC
12
Cov
89%
__ANON__
9.8
create_request
4.0
BEGIN
1.0
CRAP 5.5
« Kubernetes.pm ↑ Summary Kubernetes/Resource/Secret.pm »
1 package Net::Kubernetes::Role::APIAccess;
2 # ABSTRACT: Role allowing direct access to the REST api
3 4 $Net::Kubernetes::Role::APIAccess::VERSION = '0.19';
4 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 require LWP::UserAgent;
6 4 require HTTP::Request;
7 4 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 require Cpanel::JSON::XS;
9 4 require URI;
10 4 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 4 has url => (
14 is => 'ro',
15 isa => 'Str',
16 required => 1,
17 default => 'http://localhost:8080',
18 );
19  
20 4 has api_version => (
21 is => 'ro',
22 isa => 'Str',
23 required => 1,
24 );
25  
26 4 has base_path => (
27 is => 'ro',
28 isa => 'Str',
29 required => 1,
30 lazy => 1,
31 builder => '_create_default_base_path'
32 );
33  
34 4 has password => (
35 is => 'ro',
36 isa => 'Str',
37 required => 0,
38 );
39  
40 4 has username => (
41 is => 'ro',
42 isa => 'Str',
43 required => 0,
44 );
45  
46 4 has ua => (
47 is => 'ro',
48 isa => 'LWP::UserAgent',
49 required => 1,
50 builder => '_build_lwp_agent',
51 );
52  
53 4 has token => (
54 is => 'ro',
55 isa => 'Str',
56 required => 0
57 );
58  
59 4 has 'json' => (
60 is => 'ro',
61 isa => JSON::MaybeXS::JSON,
62 required => 1,
63 lazy => 1,
64 builder => '_build_json',
65 );
66  
67 around BUILDARGS => sub {
68 133 ▶ my $orig = shift;
Subroutine __ANON__: called CC=9 SCAR=22.8
69 133 my $class = shift;
70 133 my(%input) = @_;
71 133 ▶ if(ref($input{token})){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $input{'token'}) { } 1 132 133
elsif (exists $input{'token'} and -f $input{'token'}) { } 1 261 262
72 1 ▶ if($input{token}->can('getlines')){
Branch: 2 branches
BranchTrueFalseTotal
if ($input{'token'}->can('getlines')) { } 1 0 1
elsif (ref $input{'token'} eq 'GLOB') { } 0 0 0
73 1 $input{token} = join('', $input{token}->getlines);
74 }
75 elsif (ref($input{token}) eq 'GLOB') {
76 0 my $fh = $input{token};
77 0 $input{token} = do{ local $/; <$fh>};
78 }
79 ▶ }elsif (exists $input{token} && -f $input{token}) {
Condition100%3/3
exists $input{"token"} and -f $input{"token"}
!ll&&!rl&&r
13011
MC/DC100%2/2
exists $input{"token"} and -f $input{"token"}
exists $input{"token"} -f $input{"token"}
Truth table100%3/3
A and B
A= exists $input{"token"}B= -f $input{"token"}
ABresult
0X0
100
111
80 1 open(my $fh, '<', $input{token});
81 1 $input{token} = do{ local $/; <$fh>};
82 1 close($fh);
83 }
84 133 ▶ if(! exists $input{api_version}){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $input{"api_version"}) 96 37 133
85 96 ▶ if(exists $input{base_path}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $input{'base_path'}) { } 43 53 96
86 43 ▶ if($input{base_path} =~ m{/api/(v[^/]+)}){
Branch: 1 branch
BranchTrueFalseTotal
if ($input{"base_path"} =~ m[/api/(v[^/]+)]) 43 0 43
87 43 $input{api_version} = $1;
88 }
89 }
90 else {
91 53 $input{api_version}='v1';
92 }
93 }
94 133 return $class->$orig(%input);
95 4 };
96  
97 sub _create_default_base_path {
98 41 ▶ my($self) = @_;
Subroutine _create_default_base_path: called CC=1 SCAR=0.0
99 41 return '/api/'.$self->api_version;
100 }
101  
102 sub path {
103 95 ▶ my($self) = @_;
Subroutine path: called CC=1 SCAR=0.0
Pod undocumented
104 95 return $self->url.$self->base_path;
105 }
106  
107 sub _build_lwp_agent {
108 123 ▶ my $self = shift;
Subroutine _build_lwp_agent: called CC=1 SCAR=0.0
109 123 my $ua = LWP::UserAgent->new(agent=>'net-kubernetes-perl/0.01');
110 123 $ua->ssl_opts(verify_hostname=>0);
111 123 return $ua;
112 }
113  
114 sub _build_json {
115 67 ▶ return JSON::MaybeXS->new->allow_blessed(1)->convert_blessed(1);
Subroutine _build_json: called CC=1 SCAR=0.0
116 }
117  
118 sub create_request {
119 102 ▶ my($self, @options) = @_;
Subroutine create_request: called CC=4 SCAR=13.9
Pod undocumented
120 102 my $req = HTTP::Request->new(@options);
121 102 ▶ if ($self->username && $self->password) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->username and $self->password) { } 1 101 102
elsif ($self->token) { } 3 98 101
Condition66%2/3
$self->username and $self->password
!ll&&!rl&&r
10101
MC/DC50%1/2
$self->username and $self->password
$self->username $self->password
Truth table66%2/3
A and B
A= $self->usernameB= $self->password
ABresult
0X0
100
111
122 1 $req->header(Authorization=>"Basic ".encode_base64($self->username.':'.$self->password));
123 }
124 elsif($self->token){
125 3 $req->header(Authorization=>"Bearer ".$self->token);
126 }
127 102 return $req;
128 }
129  
130  
131 4 return 42;
132  
133 __END__
134  
135 =pod
136  
137 =encoding UTF-8
138  
139 =head1 NAME
140  
141 Net::Kubernetes::Role::APIAccess - Role allowing direct access to the REST api
142  
143 =head1 VERSION
144  
145 version 0.19
146  
147 =head1 AUTHOR
148  
149 Dave Mueller <dave@perljedi.com>
150  
151 =head1 COPYRIGHT AND LICENSE
152  
153 This software is Copyright (c) 2015 by Dave Mueller.
154  
155 This is free software, licensed under:
156  
157 The MIT (X11) License
158  
159 =head1 SEE ALSO
160  
161 Please see those modules/websites for more information related to this module.
162  
163 =over 4
164  
165 =item *
166  
167 L<Net::Kubernetes|Net::Kubernetes>
168  
169 =back
170  
171 =cut
« Kubernetes.pm ↑ Summary Kubernetes/Resource/Secret.pm »