lib/WWW/Hetzner/Cloud/API/SSHKeys.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 53.8% 14 / 26 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 85.5% 95 / 111 CC 15 SCAR 11.1
CC
15
Cov
83%
ensure
5.3
create
4.2
update
4.2
CRAP 3.0
« lib/WWW/Hetzner/Cloud/PlacementGroup.pm ↑ Summary lib/WWW/Hetzner/Robot/API/Reset.pm »
1 package WWW::Hetzner::Cloud::API::SSHKeys;
2 # ABSTRACT: Hetzner Cloud SSH Keys API
3  
4 25 our $VERSION = '0.100';
5  
6 25 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use WWW::Hetzner::Cloud::SSHKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 25 has client => (
13 is => 'ro',
14 required => 1,
15 weak_ref => 1,
16 );
17  
18 sub _wrap {
19 7 ▶ my ($self, $data) = @_;
Subroutine _wrap: called CC=1 SCAR=0.0
20 7 return WWW::Hetzner::Cloud::SSHKey->new(
21 client => $self->client,
22 %$data,
23 );
24 }
25  
26 sub _wrap_list {
27 4 ▶ my ($self, $list) = @_;
Subroutine _wrap_list: called CC=1 SCAR=0.0
28 4 return [ map { $self->_wrap($_) } @$list ];
29 }
30  
31  
32 sub list {
33 4 ▶ my ($self, %params) = @_;
Subroutine list: called CC=2 SCAR=7.1
Pod documented
34  
35 4 my $result = $self->client->get('/ssh_keys', params => \%params);
36 4 ▶ return $self->_wrap_list($result->{ssh_keys} // []);
Condition50%1/2
$result->{"ssh_keys"} // []
l!l
40
MC/DC0%0/1
$result->{"ssh_keys"} // []
$result->{"ssh_keys"}
Truth table50%1/2
A
A= $result->{"ssh_keys"}
Aresult
11
00
37 }
38  
39  
40 sub get {
41 1 ▶ my ($self, $id) = @_;
Subroutine get: called CC=2 SCAR=7.0
Pod documented
42 1 ▶ croak "SSH Key ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
43  
44 1 my $result = $self->client->get("/ssh_keys/$id");
45 1 return $self->_wrap($result->{ssh_key});
46 }
47  
48  
49 sub get_by_name {
50 3 ▶ my ($self, $name) = @_;
Subroutine get_by_name: called CC=2 SCAR=7.0
Pod documented
51 3 ▶ croak "Name required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 3 3
52  
53 3 my $keys = $self->list(name => $name);
54 3 return $keys->[0];
55 }
56  
57  
58 sub create {
59 2 ▶ my ($self, %params) = @_;
Subroutine create: called CC=4 SCAR=14.3
Pod documented
60  
61 2 ▶ croak "name required" unless $params{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"name"} 0 2 2
62 2 ▶ croak "public_key required" unless $params{public_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"public_key"} 0 2 2
63  
64 my $body = {
65 name => $params{name},
66 public_key => $params{public_key},
67 2 };
68  
69 2 ▶ $body->{labels} = $params{labels} if $params{labels};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"labels"} 0 2 2
70  
71 2 my $result = $self->client->post('/ssh_keys', $body);
72 2 return $self->_wrap($result->{ssh_key});
73 }
74  
75  
76 sub update {
77 1 ▶ my ($self, $id, %params) = @_;
Subroutine update: called CC=4 SCAR=14.3
Pod documented
78 1 ▶ croak "SSH Key ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
79  
80 1 my $body = {};
81 1 ▶ $body->{name} = $params{name} if exists $params{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"name"} 1 0 1
82 1 ▶ $body->{labels} = $params{labels} if exists $params{labels};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"labels"} 0 1 1
83  
84 1 my $result = $self->client->put("/ssh_keys/$id", $body);
85 1 return $self->_wrap($result->{ssh_key});
86 }
87  
88  
89 sub delete {
90 1 ▶ my ($self, $id) = @_;
Subroutine delete: called CC=2 SCAR=7.1
Pod documented
91 1 ▶ croak "SSH Key ID required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 1 1
92  
93 1 return $self->client->delete("/ssh_keys/$id");
94 }
95  
96  
97 sub ensure {
98 2 ▶ my ($self, $name, $public_key) = @_;
Subroutine ensure: called CC=5 SCAR=16.7
Pod documented
99 2 ▶ croak "name required" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 2 2
100 2 ▶ croak "public_key required" unless $public_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $public_key 0 2 2
101  
102 # Check if exists
103 2 my $existing = $self->get_by_name($name);
104  
105 2 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 1 1 2
106 # Check if key matches
107 1 my $existing_key = $existing->public_key;
108 1 $existing_key =~ s/\s+$//;
109 1 my $new_key = $public_key;
110 1 $new_key =~ s/\s+$//;
111  
112 1 ▶ if ($existing_key ne $new_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing_key ne $new_key) 0 1 1
113 # Delete and recreate
114 0 $self->delete($existing->id);
115 0 return $self->create(name => $name, public_key => $public_key);
116 }
117 1 return $existing;
118 }
119  
120 1 return $self->create(name => $name, public_key => $public_key);
121 }
122  
123  
124 25 1;
125  
126 __END__
« lib/WWW/Hetzner/Cloud/PlacementGroup.pm ↑ Summary lib/WWW/Hetzner/Robot/API/Reset.pm »