lib/WWW/Hetzner/Robot/CLI/Cmd/Failover.pm

filter: Statementstmt 91.6% 44 / 48 Branchbran 75.0% 12 / 16 Conditioncond 57.8% 11 / 19 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 74.0% 74 / 100 CC 19 SCAR 24.1
CC
19
Cov
81%
execute
14.8
_list
9.2
BEGIN
1.0
CRAP 11.1
« lib/WWW/Hetzner/CLI/Cmd/StorageBox/Cmd/AddLabel.pm ↑ Summary lib/WWW/Hetzner/Robot/API/Traffic.pm »
1 package WWW::Hetzner::Robot::CLI::Cmd::Failover;
2 # ABSTRACT: Robot failover IP commands
3  
4 1 our $VERSION = '0.102';
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooX::Options protect_argv => 0, usage_string => 'USAGE: hrobot.pl failover [<failover-ip> [--to <server-ip> | --delete]]';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 1 option to => (
12 is => 'ro',
13 format => 's',
14 short => 't',
15 doc => 'Main IP of the server the failover IP should route to',
16 );
17  
18 1 option delete => (
19 is => 'ro',
20 short => 'd',
21 doc => 'Delete the routing of the failover IP',
22 default => 0,
23 );
24  
25 5 ▶sub execute {
Subroutine execute: called CC=14 SCAR=26.9
Pod undocumented
26 5 my ($self, $args, $chain) = @_;
27 5 my $root = $chain->[0];
28 5 my $robot = $root->robot;
29  
30 5 my $ip = $args->[0];
31  
32 5 ▶ unless ($ip) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 1 4 5
33 1 ▶ die "A failover IP is required with --to\n" if defined $self->to;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->to 0 1 1
34 1 ▶ die "A failover IP is required with --delete\n" if $self->delete;
Branch: 1 branch
BranchTrueFalseTotal
if $self->delete 0 1 1
35 1 return $self->_list($robot, $root);
36 }
37  
38 4 ▶ die "--to and --delete are mutually exclusive\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->to and $self->delete 0 4 4
Condition66%2/3
defined $self->to and $self->delete
!ll&&!rl&&r
310
MC/DC0%0/2
defined $self->to and $self->delete
defined $self->to $self->delete
Truth table66%2/3
A and B
A= defined $self->toB= $self->delete
ABresult
0X0
100
111
39 if defined $self->to && $self->delete;
40  
41 4 my $failover;
42 4 ▶ if ($self->delete) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->delete) { } 1 3 4
elsif (defined $self->to) { } 1 2 3
43 1 $failover = $robot->failover->delete($ip);
44 } elsif (defined $self->to) {
45 1 $failover = $robot->failover->switch($ip, $self->to);
46 } else {
47 2 $failover = $robot->failover->get($ip);
48 }
49  
50 4 ▶ if ($root->output eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->output eq 'json') { } 1 3 4
51 1 require JSON::MaybeXS;
52 1 print JSON::MaybeXS::encode_json(_as_hash($failover));
53 1 print "\n";
54 } else {
55 3 ▶ print "IP: ", $failover->ip // '', "\n";
Condition50%1/2
$failover->ip // ""
l!l
30
MC/DC0%0/1
$failover->ip // ""
$failover->ip
Truth table50%1/2
A
A= $failover->ip
Aresult
11
00
56 3 ▶ print "Netmask: ", $failover->netmask // '', "\n";
Condition50%1/2
$failover->netmask // ""
l!l
30
MC/DC0%0/1
$failover->netmask // ""
$failover->netmask
Truth table50%1/2
A
A= $failover->netmask
Aresult
11
00
57 3 ▶ print "Server Number: ", $failover->server_number // '', "\n";
Condition50%1/2
$failover->server_number // ""
l!l
30
MC/DC0%0/1
$failover->server_number // ""
$failover->server_number
Truth table50%1/2
A
A= $failover->server_number
Aresult
11
00
58 3 ▶ print "Server IP: ", $failover->server_ip // '', "\n";
Condition50%1/2
$failover->server_ip // ""
l!l
30
MC/DC0%0/1
$failover->server_ip // ""
$failover->server_ip
Truth table50%1/2
A
A= $failover->server_ip
Aresult
11
00
59 3 ▶ print "Active Server IP: ", $failover->active_server_ip // '', "\n";
Condition100%2/2
$failover->active_server_ip // ""
l!l
21
MC/DC100%1/1
$failover->active_server_ip // ""
$failover->active_server_ip
Truth table100%2/2
A
A= $failover->active_server_ip
Aresult
11
00
60 }
61 }
62  
63 1 ▶sub _list {
Subroutine _list: called CC=6 SCAR=22.1
64 1 my ($self, $robot, $root) = @_;
65  
66 1 my $failovers = $robot->failover->list;
67  
68 1 ▶ if ($root->output eq 'json') {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->output eq 'json') { } 0 1 1
69 0 require JSON::MaybeXS;
70 0 print JSON::MaybeXS::encode_json([map { _as_hash($_) } @$failovers]);
71 0 print "\n";
72 } else {
73 1 printf "%-40s %-16s %s\n", 'FAILOVER IP', 'SERVER', 'ACTIVE SERVER IP';
74 1 for my $f (@$failovers) {
75 2 ▶ printf "%-40s %-16s %s\n",
Condition50%3/6
$f->ip // ""
l!l
20
$f->server_number // ""
l!l
20
$f->active_server_ip // ""
l!l
20
MC/DC0%0/1
$f->ip // ""
$f->ip
MC/DC0%0/1
$f->server_number // ""
$f->server_number
MC/DC0%0/1
$f->active_server_ip // ""
$f->active_server_ip
Truth table50%1/2
A
A= $f->ip
Aresult
11
00
Truth table50%1/2
A
A= $f->server_number
Aresult
11
00
Truth table50%1/2
A
A= $f->active_server_ip
Aresult
11
00
76 $f->ip // '',
77 $f->server_number // '',
78 $f->active_server_ip // '';
79 }
80 }
81 }
82  
83 1 ▶sub _as_hash {
Subroutine _as_hash: called CC=1 SCAR=0.0
84 1 my ($f) = @_;
85 return {
86 1 ip => $f->ip,
87 netmask => $f->netmask,
88 server_ip => $f->server_ip,
89 server_ipv6_net => $f->server_ipv6_net,
90 server_number => $f->server_number,
91 active_server_ip => $f->active_server_ip,
92 };
93 }
94  
95 1 1.
96  
97 __END__
« lib/WWW/Hetzner/CLI/Cmd/StorageBox/Cmd/AddLabel.pm ↑ Summary lib/WWW/Hetzner/Robot/API/Traffic.pm »