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

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