lib/WWW/Hetzner/CLI/Cmd/Firewall/Cmd/AddRule.pm

filter: Statementstmt 94.1% 32 / 34 Branchbran 50.0% 4 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 80.0% 4 / 5 Podpod 0.0% 0 / 1 totaltotal 80.3% 41 / 51 CC 6 SCAR 14.7
CC
6
Cov
84%
execute
6.4
__ANON__
2.0
BEGIN
1.0
CRAP 4.3
« lib/WWW/Hetzner/LWPIO.pm ↑ Summary lib/WWW/Hetzner/Cloud/Network.pm »
1 package WWW::Hetzner::CLI::Cmd::Firewall::Cmd::AddRule;
2 # ABSTRACT: Add a rule to a firewall
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: hcloud.pl firewall add-rule <id> --direction <in|out> --protocol <tcp|udp|icmp|gre|esp> --port <port> [--source-ips <ips>]';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 with 'WWW::Hetzner::CLI::Role::WaitsForAction';
10  
11 1 option direction => (
12 is => 'ro',
13 format => 's',
14 required => 1,
15 doc => 'Direction: in or out',
16 );
17  
18 1 option protocol => (
19 is => 'ro',
20 format => 's',
21 required => 1,
22 doc => 'Protocol: tcp, udp, icmp, gre, esp',
23 );
24  
25 1 option port => (
26 is => 'ro',
27 format => 's',
28 doc => 'Port or port range (e.g. 22 or 80-443)',
29 );
30  
31 option 'source_ips' => (
32 is => 'ro',
33 format => 's@',
34 long_doc => 'source-ips',
35 doc => 'Source IPs (can specify multiple)',
36 0 ▶ default => sub { ['0.0.0.0/0', '::/0'] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 1 );
38  
39 1 ▶sub execute {
Subroutine execute: called CC=6 SCAR=18.6
Pod undocumented
40 1 my ($self, $args, $chain) = @_;
41 1 ▶ my $id = $args->[0] or die "Usage: hcloud.pl firewall add-rule <id> --direction <in|out> --protocol <tcp|udp|icmp> --port <port>\n";
Branch: 1 branch
BranchTrueFalseTotal
my $id = $args->[0] or die "Usage: hcloud.pl firewall add-rule <id> --direction <in|out> --protocol <tcp|udp|icmp> --port <port>\n" 1 0 1
42  
43 1 my $main = $chain->[0];
44 1 my $cloud = $main->cloud;
45  
46 # Get existing firewall
47 1 my $fw = $cloud->firewalls->get($id);
48 1 ▶ my @rules = @{ $fw->rules // [] };
Condition50%1/2
$fw->rules // []
l!l
10
MC/DC0%0/1
$fw->rules // []
$fw->rules
Truth table50%1/2
A
A= $fw->rules
Aresult
11
00
49  
50 # Build new rule
51 1 my $rule = {
52 direction => $self->direction,
53 protocol => $self->protocol,
54 };
55 1 ▶ $rule->{port} = $self->port if $self->port;
Branch: 1 branch
BranchTrueFalseTotal
if $self->port 1 0 1
56  
57 1 ▶ if ($self->direction eq 'in') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->direction eq 'in') { } 1 0 1
58 1 $rule->{source_ips} = $self->source_ips;
59 } else {
60 0 $rule->{destination_ips} = $self->source_ips; # reuse for simplicity
61 }
62  
63 1 push @rules, $rule;
64  
65 1 print "Adding rule to firewall $id...\n";
66 1 my $actions = $cloud->firewalls->set_rules($id, \@rules);
67 1 $self->handle_action($actions);
68 1 ▶ print $self->no_wait ? "Rule add requested.\n" : "Rule added.\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->no_wait ? : 1 0 1
69 }
70  
71 1 1;
72  
73 __END__
« lib/WWW/Hetzner/LWPIO.pm ↑ Summary lib/WWW/Hetzner/Cloud/Network.pm »