| 1 | package Consul::Simple; | ||||||||||||||||||||||||||||
| 2 | 10 | $Consul::Simple::VERSION = '1.142430'; | |||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict;use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use HTTP::Request::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use MIME::Base64; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 13 | 6 | my $self = {}; | |||||||||||||||||||||||||||
| 14 | 6 | bless ($self,$class); | |||||||||||||||||||||||||||
| 15 | 6 | my %args; | |||||||||||||||||||||||||||
| 16 | 6 | { my @args = @_; | |||||||||||||||||||||||||||
| 17 | 6 | ▶ | die 'Consul::Simple::new: even number of arguments required' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | if scalar @args % 2; | ||||||||||||||||||||||||||||
| 19 | 6 | %args = @args; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | 6 | ▶ | $self->{consul_server} = $args{consul_server} || 'localhost'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'consul_server'} || 'localhost'
MC/DC100%1/1
$args{'consul_server'} || 'localhost'
$args{'consul_server'}
Truth table100%2/2
A
A= $args{'consul_server'}
| |||||||||||||||||||||||||||||
| 22 | 6 | ▶ | $args{kv_prefix} = $args{kvPrefix} if $args{kvPrefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 6 | ▶ | $self->{kv_prefix} = $args{kv_prefix} || '/'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'kv_prefix'} || '/'
MC/DC100%1/1
$args{'kv_prefix'} || '/'
$args{'kv_prefix'}
Truth table100%2/2
A
A= $args{'kv_prefix'}
| |||||||||||||||||||||||||||||
| 24 | 6 | $self->{kv_prefix} =~ s/\/\//\//g; | |||||||||||||||||||||||||||
| 25 | 6 | $self->{kv_prefix} =~ s/\/\//\//g; | |||||||||||||||||||||||||||
| 26 | 6 | $self->{kv_prefix} =~ s/\/\//\//g; | |||||||||||||||||||||||||||
| 27 | 6 | ▶ | if($self->{kv_prefix} !~ /^\//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 4 | $self->{kv_prefix} = '/' . $self->{kv_prefix}; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 6 | ▶ | if($self->{kv_prefix} !~ /\/$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 4 | $self->{kv_prefix} = $self->{kv_prefix} . '/'; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 6 | { my %ua_args = (); | |||||||||||||||||||||||||||
| 34 | 6 | ▶ | $ua_args{ssl_opts} = $args{ssl_opts} if $args{ssl_opts}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 6 | $self->{ua} = LWP::UserAgent->new(%ua_args); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 6 | ▶ | $self->{proto} = $args{proto} || 'http'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'proto'} || 'http'
MC/DC0%0/1
$args{'proto'} || 'http'
$args{'proto'}
Truth table50%1/2
A
A= $args{'proto'}
| |||||||||||||||||||||||||||||
| 39 | 6 | ▶ | $self->{consul_port} = $args{port} || 8500; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'port'} || 8500
MC/DC0%0/1
$args{'port'} || 8500
$args{'port'}
Truth table50%1/2
A
A= $args{'port'}
| |||||||||||||||||||||||||||||
| 40 | 6 | $self->{constructor_args} = \%args; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | $self->{warning_handler} = $args{warning_handler} || sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my $warnstr = shift; | |||||||||||||||||||||||||||
| 43 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 44 | 0 | print STDERR "[WARN]: $warnstr\n"; | |||||||||||||||||||||||||||
| 45 | 6 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'warning_handler'} || sub {
my $warnstr = shift();
my(%args) = @_;
print STDERR "[WARN]: $warnstr\n";
}
MC/DC0%0/2
$args{'warning_handler'} || sub {
my $warnstr = shift();
my(%args) = @_;
print STDERR "[WARN]: $warnstr\n";
}
$args{'warning_handler'}
sub {
my $warnstr = shift();
my(%args) = @_;
print STDERR "[WARN]: $warnstr\n";
}
Truth table33%1/3
A || B
A= $args{'warning_handler'}B= sub {
my $warnstr = shift();
my(%args) = @_;
print STDERR "[WARN]: $warnstr\n";
}
| |||||||||||||||||||||||||||||
| 46 | 6 | return $self; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _warn { | ||||||||||||||||||||||||||
|
Subroutine
_warn: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 0 | $self->{warning_handler}->(@_); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 16 | ▶ | sub _do_request { | ||||||||||||||||||||||||||
|
Subroutine
_do_request: called CC=8 SCAR=25.1
| |||||||||||||||||||||||||||||
| 55 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | 16 | my $req = shift; | |||||||||||||||||||||||||||
| 57 | 16 | my %req_args = @_; | |||||||||||||||||||||||||||
| 58 | 16 | my $ret; | |||||||||||||||||||||||||||
| 59 | 16 | ▶ | my $tries = $self->{constructor_args}->{retries} || 5; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'constructor_args'}{'retries'} || 5
MC/DC0%0/1
$self->{'constructor_args'}{'retries'} || 5
$self->{'constructor_args'}{'retries'}
Truth table50%1/2
A
A= $self->{'constructor_args'}{'retries'}
| |||||||||||||||||||||||||||||
| 60 | 16 | ▶ | my $timeout = $self->{constructor_args}->{timeout} || 2; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'constructor_args'}{'timeout'} || 2
MC/DC0%0/1
$self->{'constructor_args'}{'timeout'} || 2
$self->{'constructor_args'}{'timeout'}
Truth table50%1/2
A
A= $self->{'constructor_args'}{'timeout'}
| |||||||||||||||||||||||||||||
| 61 | 16 | ▶ | while($tries--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 16 | eval { | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | local $SIG{ALRM} = sub { die "timed out\n"; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 64 | 16 | alarm $timeout; | |||||||||||||||||||||||||||
| 65 | 16 | $ret = $self->{ua}->request($req, %req_args); | |||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | 16 | alarm 0; | |||||||||||||||||||||||||||
| 68 | 16 | ▶ | last if $ret->status_line =~ /^404 /; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 15 | ▶ | last if $ret and $ret->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ret and $ret->is_success
MC/DC0%0/2
$ret and $ret->is_success
$ret
$ret->is_success
Truth table33%1/3
A and B
A= $retB= $ret->is_success
| |||||||||||||||||||||||||||||
| 70 | 0 | my $err; | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | if($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $err = $@; | |||||||||||||||||||||||||||
| 73 | } else { | ||||||||||||||||||||||||||||
| 74 | 0 | $err = 'http request failed with ' . $ret->status_line; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | $self->_warn("request failed: $err", response => $ret); | |||||||||||||||||||||||||||
| 77 | 0 | sleep 1; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 16 | return $ret; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | sub KVGet { | ||||||||||||||||||||||||||
|
Subroutine
KVGet: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 84 | 6 | my $key = shift; | |||||||||||||||||||||||||||
| 85 | 6 | ▶ | die 'Consul::Simple::KVGet: key required as first argument' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 6 | my %args; | |||||||||||||||||||||||||||
| 87 | 6 | { my @args = @_; | |||||||||||||||||||||||||||
| 88 | 6 | ▶ | die 'Consul::Simple::KVGet: even number of arguments required' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | if scalar @args % 2; | ||||||||||||||||||||||||||||
| 90 | 6 | %args = @args; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 6 | my @entries = (); | |||||||||||||||||||||||||||
| 93 | 6 | eval { | |||||||||||||||||||||||||||
| 94 | 6 | my $url = $self->_mk_kv_url($key); | |||||||||||||||||||||||||||
| 95 | 6 | ▶ | $url .= '?recurse' if $args{recurse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 6 | my $res = $self->_do_request(GET $url); | |||||||||||||||||||||||||||
| 97 | 6 | my $content = $res->content; | |||||||||||||||||||||||||||
| 98 | 6 | my $values = JSON::decode_json($content); | |||||||||||||||||||||||||||
| 99 | 5 | @entries = @$values; | |||||||||||||||||||||||||||
| 100 | }; | ||||||||||||||||||||||||||||
| 101 | 6 | my @ret = (); | |||||||||||||||||||||||||||
| 102 | 6 | foreach my $entry (@entries) { | |||||||||||||||||||||||||||
| 103 | #The returned entry Value is always base64 encoded | ||||||||||||||||||||||||||||
| 104 | 6 | $entry->{Value} = MIME::Base64::decode_base64($entry->{Value}); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #the idea below is to try to JSON decode it. If that works, | ||||||||||||||||||||||||||||
| 107 | #return the JSON decoded value. Otherwise, return it un-decoded | ||||||||||||||||||||||||||||
| 108 | 6 | my $value; | |||||||||||||||||||||||||||
| 109 | 6 | eval { | |||||||||||||||||||||||||||
| 110 | 6 | $value = JSON::decode_json($entry->{Value}); | |||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||
| 112 | 6 | ▶ | $value = $entry->{Value} unless $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 6 | $entry->{Value} = $value; | |||||||||||||||||||||||||||
| 114 | 6 | push @ret, $entry; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 6 | return @ret; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 6 | ▶ | sub KVPut { | ||||||||||||||||||||||||||
|
Subroutine
KVPut: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | 6 | my $key = shift; | |||||||||||||||||||||||||||
| 124 | 6 | ▶ | die 'Consul::Simple::KVPut: key required as first argument' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 6 | ▶ | my $value = shift or die 'Consul::Simple::KVPut: value required as second argument'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 6 | ▶ | if(ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 3 | $value = JSON::encode_json($value); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 6 | my %args; | |||||||||||||||||||||||||||
| 130 | 6 | { my @args = @_; | |||||||||||||||||||||||||||
| 131 | 6 | ▶ | die 'Consul::Simple::KVPut: even number of arguments required' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | if scalar @args % 2; | ||||||||||||||||||||||||||||
| 133 | 6 | %args = @args; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 6 | my $res = $self->_do_request(PUT $self->_mk_kv_url($key), Content => $value); | |||||||||||||||||||||||||||
| 136 | 6 | return $res; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 16 | ▶ | sub _mk_kv_url { | ||||||||||||||||||||||||||
|
Subroutine
_mk_kv_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 140 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 16 | my $key = shift; | |||||||||||||||||||||||||||
| 142 | 16 | return $self->{proto} . '://' . $self->{consul_server} . ':' . $self->{consul_port} . '/v1/kv' . $self->{kv_prefix} . $key; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 4 | ▶ | sub KVDelete { | ||||||||||||||||||||||||||
|
Subroutine
KVDelete: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 147 | 4 | my $key = shift; | |||||||||||||||||||||||||||
| 148 | 4 | ▶ | die 'Consul::Simple::KVDelete: key required as first argument' unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 4 | my %args; | |||||||||||||||||||||||||||
| 150 | 4 | { my @args = @_; | |||||||||||||||||||||||||||
| 151 | 4 | ▶ | die 'Consul::Simple::KVPut: even number of arguments required' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | if scalar @args % 2; | ||||||||||||||||||||||||||||
| 153 | 4 | %args = @args; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 4 | my $url = $self->_mk_kv_url($key); | |||||||||||||||||||||||||||
| 156 | 4 | ▶ | $url .= '?recurse' if $args{recurse}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 4 | my $res = $self->_do_request( | |||||||||||||||||||||||||||
| 158 | HTTP::Request::Common::_simple_req( | ||||||||||||||||||||||||||||
| 159 | 'DELETE', | ||||||||||||||||||||||||||||
| 160 | $url | ||||||||||||||||||||||||||||
| 161 | ) | ||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||
| 163 | 4 | return $res; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 10 | 1; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||