lib/App/ElasticSearch/Utilities/Connection.pm

filter: Statementstmt 34.9% 52 / 149 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 46 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 51.8% 14 / 27 Podpod 100% 4 / 4 totaltotal 22.4% 70 / 312 CC 47 SCAR 50.1
CC
47
Cov
21%
request
420.0
_build_version
132.0
__ANON__
90.0
CRAP 149.6
« lib/App/ElasticSearch/Utilities.pm ↑ Summary lib/App/ElasticSearch/Utilities/Metrics.pm »
1 package App::ElasticSearch::Utilities::Connection;
2 # ABSTRACT: Abstract the connection element
3  
4  
5 13 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 our $VERSION = '9.1'; # VERSION
9  
10 13 ▶use App::ElasticSearch::Utilities::HTTPRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use App::ElasticSearch::Utilities::VersionHacks qw(_fix_version_request);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use CLI::Helpers qw(:output);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Ref::Util qw(is_ref is_arrayref is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Types::Standard qw( Enum HashRef InstanceOf Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 has 'host' => (
25 is => 'ro',
26 isa => Str,
27 0 ▶ default => sub { 'localhost' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 13 );
29  
30 has 'port' => (
31 is => 'ro',
32 isa => Int,
33 0 ▶ default => sub { 9200 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 13 );
35  
36  
37 has 'proto' => (
38 is => 'rw',
39 isa => Enum[qw(http https)],
40 0 ▶ default => sub { 'http' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 13 );
42  
43  
44 has 'timeout' => (
45 is => 'ro',
46 isa => Int,
47 0 ▶ default => sub { 10 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 13 );
49  
50  
51 has 'username' => (
52 is => 'ro',
53 isa => Str,
54 0 ▶ default => sub { $ENV{USER} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 13 );
56  
57  
58 13 has 'password' => (
59 is => 'ro',
60 );
61  
62  
63 has 'ssl_opts' => (
64 is => 'ro',
65 isa => HashRef,
66 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 13 );
68  
69  
70 13 has 'version' => (
71 is => 'lazy',
72 isa => Str,
73 init_arg => undef,
74 );
75  
76 0 ▶sub _build_version {
Subroutine _build_version: not called CC=11 SCAR=48.8
77 0 my $self = shift;
78  
79 # Retry with TLS and/or Auth
80 0 my $resp = $self->request('/',{skip_version_check => 1});
81 0 my $err;
82 0 ▶ if( $resp->is_success ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
elsif ($resp->code == 500 and $resp->message eq 'Server closed connection without sending any data back') { } 0 0 0
elsif ($resp->code == 401) { } 0 0 0
83 0 my $ver;
84 0 eval {
85 0 $ver = $resp->content->{version};
86 };
87 0 ▶ if( $ver ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver) { } 0 0 0
88 0 ▶ if( $ver->{distribution} and $ver->{distribution} eq 'opensearch' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver->{'distribution'} and $ver->{'distribution'} eq 'opensearch') { } 0 0 0
Condition0%0/3
$ver->{"distribution"} and $ver->{"distribution"} eq "opensearch"
!ll&&!rl&&r
000
MC/DC0%0/2
$ver->{"distribution"} and $ver->{"distribution"} eq "opensearch"
$ver->{"distribution"} $ver->{"distribution"} eq "opensearch"
Truth table0%0/3
A and B
A= $ver->{"distribution"}B= $ver->{"distribution"} eq "opensearch"
ABresult
0X0
100
111
89 0 return sprintf "%0.1f", version->parse($ver->{minimum_wire_compatibility_version});
90 }
91 else {
92 0 return sprintf "%0.1f", version->parse($ver->{number});
93 }
94 } else {
95 0 $err = "Parsing version failed";
96 }
97 }
98 ▶ elsif( $resp->code == 500 && $resp->message eq "Server closed connection without sending any data back" ) {
Condition0%0/3
$resp->code == 500 and $resp->message eq "Server closed connection without sending any data back"
!ll&&!rl&&r
000
MC/DC0%0/2
$resp->code == 500 and $resp->message eq "Server closed connection without sending any data back"
$resp->code == 500 $resp->message eq "Server closed connection without sending any data back"
Truth table0%0/3
A and B
A= $resp->code == 500B= $resp->message eq "Server closed connection without sending any data back"
ABresult
0X0
100
111
99 0 $err = "Attempting promotion to HTTPS, try setting 'proto: https' in ~/.es-utils.yaml";
100 }
101 elsif( $resp->code == 401 ) {
102 0 ▶ $err = $self->password ? sprintf("Authorization failed for user '%s'", $self->username)
Branch: 1 branch
BranchTrueFalseTotal
$self->password ? : 0 0 0
103 : "Authorization required, try setting 'password-exec: /home/user/bin/get-password.sh` in ~/.es-utils.yaml'";
104 }
105 else {
106 0 $err = "Failed getting version";
107 }
108 0 output({color=>'red',stderr=>1}, sprintf "FAIL [%d] Unable to determine Elasticsearch version: %s", $resp->code, $err);
109 0 ▶ output({color=>'red',stderr=>1}, ref $resp->content ? YAML::XS::Dump($resp->content) : $resp->content) if $resp->content;
Branch: 2 branches
BranchTrueFalseTotal
if $resp->content 0 0 0
ref $resp->content ? : 0 0 0
110 0 exit 1;
111 }
112  
113  
114 13 has 'ua' => (
115 is => 'lazy',
116 isa => InstanceOf["LWP::UserAgent"],
117 );
118  
119 0 ▶sub _build_ua {
Subroutine _build_ua: not called CC=5 SCAR=34.0
120 0 my ($self) = @_;
121  
122 # Construct the UA Object
123 ## no critic
124 0 ▶ my $local_version = eval '$VERSION' || '999.9';
Condition0%0/2
eval '$VERSION' || '999.9'
l!l
00
MC/DC0%0/1
eval '$VERSION' || '999.9'
eval '$VERSION'
Truth table0%0/2
A
A= eval '$VERSION'
Aresult
11
00
125 ## use critic
126 0 my $ua = LWP::UserAgent->new(
127 keep_alive => 3,
128 agent => sprintf("%s/%s (Perl %s)", __PACKAGE__, $local_version, $^V),
129 protocols_allowed => [qw(http https)],
130 timeout => $self->timeout,
131 ssl_opts => $self->ssl_opts,
132 );
133 0 ▶ debug({color=>'cyan'}, sprintf "Initialized a UA: %s%s", $ua->agent, $self->password ? ' (password provided)' : '');
Branch: 1 branch
BranchTrueFalseTotal
$self->password ? : 0 0 0
134  
135 # Decode the JSON Automatically
136 0 ▶ $ua->add_handler( response_done => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
137 0 my ($response,$lwp_ua,$headers) = @_;
138 0 debug( {color=>'magenta'}, "respone_done handler, got:");
139 0 debug($response->as_string);
140  
141 0 ▶ my $ctype = $response->content_type() || 'invalid';
Condition0%0/2
$response->content_type || 'invalid'
l!l
00
MC/DC0%0/1
$response->content_type || 'invalid'
$response->content_type
Truth table0%0/2
A
A= $response->content_type
Aresult
11
00
142 # JSON Transform
143 0 ▶ if( $ctype =~ m{^application/json\b} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ctype =~ m[^application/json\b]u) { } 0 0 0
elsif ($response->is_success and $ctype =~ m[^text/plain]u) { } 0 0 0
144 0 debug({color=>'yellow',indent=>1},"JSON Decoding Response Content");
145 0 eval {
146 0 my $decoded = decode_json( $response->content );
147 0 $response->content($decoded);
148 };
149 }
150 ▶ elsif ( $response->is_success && $ctype =~ m{^text/plain} ) {
Condition0%0/3
$response->is_success and $ctype =~ m[^text/plain]u
!ll&&!rl&&r
000
MC/DC0%0/2
$response->is_success and $ctype =~ m[^text/plain]u
$response->is_success $ctype =~ m[^text/plain]u
Truth table0%0/3
A and B
A= $response->is_successB= $ctype =~ m[^text/plain]u
ABresult
0X0
100
111
151 # Plain text transform for the _cat API
152 0 debug({color=>'yellow',indent=>1},"Plain Text Transform Response Content");
153 my $decoded = [
154 0 ▶ grep { defined && length && !/^\s+$/ }
Condition0%0/6
defined $_ && length $_ && !/^\s+$/u
!ll&&!rl&&r
000
defined $_ and length $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ && length $_ && !/^\s+$/u
defined $_ length $_ !/^\s+$/u
Truth table0%0/4
A and B && not(C)
A= defined $_B= length $_C= !/^\s+$/u
ABCresult
0XX0
10X0
1100
1111
155 split /\r?\n/, $response->content
156 ];
157 0 debug_var($decoded);
158 0 $response->content($decoded);
159 }
160 0 ▶ if( my $content = $response->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $content = $response->content) 0 0 0
161 0 debug({color=>'yellow'}, "After translation:");
162 0 ▶ if( is_ref($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_ref($content)) { } 0 0 0
163 0 debug_var( $content );
164 }
165 else{
166 0 debug( $content );
167 }
168 }
169 0 $_[0] = $response;
170 0 });
171  
172 # Warn About Basic Auth without TLS
173 0 ▶ warn "HTTP Basic Authorization configured and not using TLS, this is not supported"
Branch: 1 branch
BranchTrueFalseTotal
if length $self->password and $self->proto ne "https" 0 0 0
Condition0%0/3
length $self->password and $self->proto ne "https"
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->password and $self->proto ne "https"
length $self->password $self->proto ne "https"
Truth table0%0/3
A and B
A= length $self->passwordB= $self->proto ne "https"
ABresult
0X0
100
111
174 if length $self->password && $self->proto ne 'https';
175  
176 0 return $ua;
177 }
178  
179  
180 0 ▶sub request {
Subroutine request: not called CC=20 SCAR=60.4
Pod documented
181 0 my ($self,$url,$options,$body) = @_;
182  
183 # Skip Version Check
184 0 my $skip_version_check = delete $options->{skip_version_check};
185  
186 # Build the Path
187 0 ▶ $options->{command} ||= $url;
Condition0%0/3
$options->{"command"} ||= $url
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"command"} ||= $url
$options->{"command"} $url
Truth table0%0/3
A ||= B
A= $options->{"command"}B= $url
ABresult
1X1
011
000
188 0 ▶ my @path = grep { defined and length } @{ $options }{qw(index command)};
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
189 0 my $path = join('/', @path);
190 0 debug(sprintf "calling %s->request(%s)", ref $self, $path);
191  
192 # Build a URI
193 0 my $uri = URI->new( sprintf "%s://%s:%d",
194 $self->proto,
195 $self->host,
196 $self->port,
197 );
198 0 $uri->path($path);
199  
200 # Query String
201 0 ▶ if( exists $options->{uri_param} and is_hashref($options->{uri_param}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"uri_param"} and Ref::Util::XS::is_hashref($options->{"uri_param"})) 0 0 0
Condition0%0/3
exists $options->{"uri_param"} and Ref::Util::XS::is_hashref($options->{"uri_param"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"uri_param"} and Ref::Util::XS::is_hashref($options->{"uri_param"})
exists $options->{"uri_param"} Ref::Util::XS::is_hashref($options->{"uri_param"})
Truth table0%0/3
A and B
A= exists $options->{"uri_param"}B= Ref::Util::XS::is_hashref($options->{"uri_param"})
ABresult
0X0
100
111
202 0 foreach my $k ( keys %{ $options->{uri_param} } ) {
203 0 $uri->query_param( $k => $options->{uri_param}{$k} );
204 }
205 }
206 # Body Translations
207 0 ▶ if(!defined $body && exists $options->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $body and exists $options->{"body"}) 0 0 0
Condition0%0/3
not defined $body and exists $options->{"body"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $body and exists $options->{"body"}
not defined $body exists $options->{"body"}
Truth table0%0/3
not(A) and B
A= not defined $bodyB= exists $options->{"body"}
ABresult
0X0
100
111
208 0 ▶ $body ||= delete $options->{body};
Condition0%0/3
$body ||= delete $options->{"body"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$body ||= delete $options->{"body"}
$body delete $options->{"body"}
Truth table0%0/3
A ||= B
A= $bodyB= delete $options->{"body"}
ABresult
1X1
011
000
209 }
210  
211 # Determine request method
212 0 ▶ my $method = exists $options->{method} ? uc $options->{method} : 'GET';
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'method'} ? : 0 0 0
213  
214 # Special Case for Index Creation
215 0 ▶ if( $method eq 'PUT' && $options->{index} && $options->{command} eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "PUT" and $options->{"index"} and $options->{"command"} eq "/") 0 0 0
Condition0%0/6
$method eq "PUT" and $options->{"index"} and $options->{"command"} eq "/"
!ll&&!rl&&r
000
$method eq "PUT" and $options->{"index"}
!ll&&!rl&&r
000
MC/DC0%0/3
$method eq "PUT" and $options->{"index"} and $options->{"command"} eq "/"
$method eq "PUT" $options->{"index"} $options->{"command"} eq "/"
Truth table0%0/4
A and B and C
A= $method eq "PUT"B= $options->{"index"}C= $options->{"command"} eq "/"
ABCresult
0XX0
10X0
1100
1111
216 0 $uri->path($options->{index});
217 }
218  
219 # Apply VersionHacks
220 0 ▶ if ( !$skip_version_check ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($skip_version_check) 0 0 0
221 0 ($url,$options,$body) = _fix_version_request($self->version,$url,$options,$body);
222 }
223  
224 0 ▶ debug({color=>'magenta'}, sprintf "Issuing %s with URI of '%s' as '%s:%s'", $method, $uri->as_string, $self->username, length $self->password ? 'hunter2' : '');
Branch: 1 branch
BranchTrueFalseTotal
length $self->password ? : 0 0 0
225 0 ▶ if( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 0 0 0
226 0 ▶ if( is_ref($body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_ref($body)) { } 0 0 0
227 0 debug_var({indent=>1}, $body);
228 }
229 else {
230 0 debug({indent=>1}, split /\r?\n/, $body);
231 }
232 }
233  
234 # Make the request
235 0 my $req = App::ElasticSearch::Utilities::HTTPRequest->new( $method => $uri->as_string );
236  
237 # Authentication
238 0 ▶ $req->authorization_basic( $self->username, $self->password )
Branch: 1 branch
BranchTrueFalseTotal
if length $self->password and $self->proto eq "https" 0 0 0
Condition0%0/3
length $self->password and $self->proto eq "https"
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->password and $self->proto eq "https"
length $self->password $self->proto eq "https"
Truth table0%0/3
A and B
A= length $self->passwordB= $self->proto eq "https"
ABresult
0X0
100
111
239 if length $self->password and $self->proto eq 'https';
240  
241 0 ▶ $req->content($body) if defined $body;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
242  
243 0 return $self->ua->request( $req );
244 }
245  
246  
247  
248 0 ▶sub exists {
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
249 0 my ($self,%options) = @_;
250  
251 0 ▶ return unless exists $options{index};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"index"} 0 0 0
252 my %params = (
253 method => 'HEAD',
254 index => $options{index},
255 0 );
256  
257 0 return $self->request('', \%params,)->is_success;
258 }
259  
260  
261 0 ▶sub put {
Subroutine put: not called CC=3 SCAR=24.8
Pod documented
262 0 my ($self,%options) = @_;
263  
264 0 ▶ return unless exists $options{body};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"body"} 0 0 0
265 0 my %params = ( method => 'PUT' );
266 0 ▶ $params{index} = $options{index} if exists $options{index};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"index"} 0 0 0
267  
268 0 my $resp = $self->request('', \%params, $options{body});
269 0 return ( $resp->code, $resp->content );
270 }
271  
272  
273 0 ▶sub bulk {
Subroutine bulk: not called CC=3 SCAR=24.8
Pod documented
274 0 my ($self,%options) = @_;
275  
276 0 ▶ return unless exists $options{body};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options{"body"} 0 0 0
277 0 my %params = ( method => 'POST' );
278 0 ▶ $params{index} = $options{index} if exists $options{index};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"index"} 0 0 0
279  
280 0 my $resp = $self->request( '_bulk', \%params, $options{body} );
281 0 return ( $resp->code, $resp->content );
282 }
283  
284 13 __PACKAGE__->meta->make_immutable;
285  
286 __END__
« lib/App/ElasticSearch/Utilities.pm ↑ Summary lib/App/ElasticSearch/Utilities/Metrics.pm »