Webservice/OVH/Cloud/Project/SSH.pm

filter: Statementstmt 12.3% 11 / 89 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.0% 3 / 15 Podpod 100% 9 / 9 totaltotal 14.6% 23 / 157 CC 23 SCAR 32.3
CC
23
Cov
8%
_new_existing
56.0
_new
56.0
properties
12.0
CRAP 25.3
« Webservice/OVH/Email/Domain/Domain/Account.pm ↑ Summary Webservice/OVH/Email/Domain/Domain.pm »
1 package Webservice::OVH::Cloud::Project::SSH;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Webservice::OVH::Cloud::Project::SSH
8  
9 =head1 SYNOPSIS
10  
11 use Webservice::OVH;
12
13 my $ovh = Webservice::OVH->new_from_json("credentials.json");
14
15 my $projects = $ovh->cloud->projects;
16 my $example_project = $projects->[0];
17
18 my $keys = $project->ssh_keys;
19
20 foreach my $key (@$keys) {
21
22 print $key->name;
23 $key->delete;
24 }
25  
26 =head1 DESCRIPTION
27  
28 Gives access to ssh key functionalty for a specific project.
29  
30 =head1 METHODS
31  
32 =cut
33  
34 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 36 ▶use Carp qw{ carp croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 36 our $VERSION = 0.48;
39  
40 =head2 _new_existing
41  
42 Internal Method to create the SSH object.
43 This method is not ment to be called directly.
44  
45 =over
46  
47 =item * Parameter: %params - key => value
48  
49 =item * Return: L<Webservice::OVH::Cloud::Project::SSH>
50  
51 =item * Synopsis: Webservice::OVH::Cloud::Project::SSH->_new(wrapper => $ovh_api_wrapper, project => $project, module => $module, id => $id );
52  
53 =back
54  
55 =cut
56  
57 0 ▶sub _new_existing {
Subroutine _new_existing: not called CC=7 SCAR=40.3
58  
59 0 my ( $class, %params ) = @_;
60  
61 0 ▶ die "Missing id" unless $params{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"id"} 0 0 0
62 0 ▶ die "Missing project" unless $params{project};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"project"} 0 0 0
63 0 ▶ die "Missing module" unless $params{module};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"module"} 0 0 0
64 0 ▶ die "Missing wrapper" unless $params{wrapper};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"wrapper"} 0 0 0
65  
66 0 my $api_wrapper = $params{wrapper};
67 0 my $module = $params{module};
68 0 my $project = $params{project};
69 0 my $key_id = $params{id};
70  
71 0 my $project_id = $project->id;
72 0 my $response = $api_wrapper->rawCall( method => 'get', path => "/cloud/project/$project_id/sshkey/$key_id", noSignature => 0 );
73 0 ▶ carp $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
74  
75 0 ▶ if ( !$response->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->error) { } 0 0 0
76  
77 0 my $porperties = $response->content;
78 0 my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $key_id, _properties => $porperties, _project => $project }, $class;
79  
80 0 return $self;
81 } else {
82  
83 0 return undef;
84 }
85 }
86  
87 =head2 _new
88  
89 Internal Method to create the SSH object.
90 This method is not ment to be called directly.
91  
92 =over
93  
94 =item * Parameter: %params - key => value
95  
96 =item * Return: L<Webservice::OVH::Cloud::Project::SSH>
97  
98 =item * Synopsis: Webservice::OVH::Cloud::Project::SSH->_new(wrapper => $ovh_api_wrapper, project => $project, module => $module );
99  
100 =back
101  
102 =cut
103  
104 0 ▶sub _new {
Subroutine _new: not called CC=7 SCAR=40.3
105  
106 0 my ( $class, %params ) = @_;
107  
108 0 ▶ die "Missing project" unless $params{project};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"project"} 0 0 0
109 0 ▶ die "Missing module" unless $params{module};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"module"} 0 0 0
110 0 ▶ die "Missing wrapper" unless $params{wrapper};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"wrapper"} 0 0 0
111  
112 0 my $api_wrapper = $params{wrapper};
113 0 my $module = $params{module};
114 0 my $project = $params{project};
115  
116 0 my @keys_needed = qw{ public_key name };
117 0 ▶ if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing_parameters) = grep({not $params{$_};} @keys_needed)) 0 0 0
118  
119 0 croak "Missing parameter: @missing_parameters";
120 }
121  
122 0 my $project_id = $project->id;
123 0 my $body = {};
124 0 $body->{name} = $params{name};
125 0 $body->{publicKey} = $params{public_key};
126 0 ▶ $body->{region} = $params{region} if exists $params{region};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"region"} 0 0 0
127 0 my $response = $api_wrapper->rawCall( method => 'post', path => "/cloud/project/$project_id/sshkey", body => $body, noSignature => 0 );
128 0 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
129  
130 0 my $key_id = $response->content->{id};
131  
132 0 my $properties = $response->content;
133  
134 0 my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $key_id, _properties => $properties, _project => $project }, $class;
135  
136 0 return $self;
137 }
138  
139 =head2 project
140  
141 Root Project.
142  
143 =over
144  
145 =item * Return: L<Webservice::OVH::Cloud::Project>
146  
147 =item * Synopsis: my $project = $ssh_key->project;
148  
149 =back
150  
151 =cut
152  
153 0 ▶sub project {
Subroutine project: not called CC=1 SCAR=6.9
Pod documented
154  
155 0 my ($self) = @_;
156  
157 0 return $self->{_project};
158 }
159  
160 =head2 is_valid
161  
162 When this object is deleted on the api side, this method returns 0.
163  
164 =over
165  
166 =item * Return: VALUE
167  
168 =item * Synopsis: print "Valid" if $ssh_key->is_valid;
169  
170 =back
171  
172 =cut
173  
174 0 ▶sub is_valid {
Subroutine is_valid: not called CC=1 SCAR=6.9
Pod documented
175  
176 0 my ($self) = @_;
177  
178 0 return $self->{_valid};
179 }
180  
181 =head2 _is_valid
182  
183 Intern method to check validity.
184 Difference is that this method carps an error.
185  
186 =over
187  
188 =item * Return: VALUE
189  
190 =item * Synopsis: $ssh_key->_is_valid;
191  
192 =back
193  
194 =cut
195  
196 0 ▶sub _is_valid {
Subroutine _is_valid: not called CC=2 SCAR=17.9
197  
198 0 my ($self) = @_;
199  
200 0 ▶ carp "Key is not valid anymore" unless $self->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid 0 0 0
201 0 return $self->is_valid;
202 }
203  
204 =head2 id
205  
206 Returns the api id
207  
208 =over
209  
210 =item * Return: VALUE
211  
212 =item * Synopsis: my $id = $ssh_key->id;
213  
214 =back
215  
216 =cut
217  
218 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod documented
219  
220 0 my ($self) = @_;
221  
222 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
223  
224 0 return $self->{_id};
225 }
226  
227 =head2 properties
228  
229 Returns the raw properties as a hash.
230 This is the original return value of the web-api.
231  
232 =over
233  
234 =item * Return: HASH
235  
236 =item * Synopsis: my $properties = $ssh_key->properties;
237  
238 =back
239  
240 =cut
241  
242 0 ▶sub properties {
Subroutine properties: not called CC=3 SCAR=24.8
Pod documented
243  
244 0 my ($self) = @_;
245  
246 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
247  
248 0 my $api = $self->{_api_wrapper};
249 0 my $project_id = $self->project->id;
250 0 my $key_id = $self->id;
251 0 my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/sshkey/$key_id", noSignature => 0 );
252 0 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
253 0 $self->{_properties} = $response->content;
254 0 return $self->{_properties};
255 }
256  
257 =head2 finger_print
258  
259 Exposed property value.
260  
261 =over
262  
263 =item * Return: VALUE
264  
265 =item * Synopsis: my $finger_print = $ssh_key->finger_print;
266  
267 =back
268  
269 =cut
270  
271 0 ▶sub finger_print {
Subroutine finger_print: not called CC=2 SCAR=17.9
Pod documented
272  
273 0 my ($self) = @_;
274  
275 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
276  
277 0 return $self->{_properties}->{fingerPrint};
278 }
279  
280 =head2 regions
281  
282 Exposed property value.
283  
284 =over
285  
286 =item * Return: ARRAY
287  
288 =item * Synopsis: my $regions = $ssh_key->regions;
289  
290 =back
291  
292 =cut
293  
294 0 ▶sub regions {
Subroutine regions: not called CC=2 SCAR=17.9
Pod documented
295  
296 0 my ($self) = @_;
297  
298 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
299  
300 0 return $self->{_properties}->{regions};
301 }
302  
303 =head2 name
304  
305 Exposed property value.
306  
307 =over
308  
309 =item * Return: VALUE
310  
311 =item * Synopsis: my $name = $ssh_key->name;
312  
313 =back
314  
315 =cut
316  
317 0 ▶sub name {
Subroutine name: not called CC=2 SCAR=17.9
Pod documented
318  
319 0 my ($self) = @_;
320  
321 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
322  
323 0 return $self->{_properties}->{name};
324 }
325  
326 =head2 public_key
327  
328 Exposed property value.
329  
330 =over
331  
332 =item * Return: VALUE
333  
334 =item * Synopsis: my $public_key = $ssh_key->public_key;
335  
336 =back
337  
338 =cut
339  
340 0 ▶sub public_key {
Subroutine public_key: not called CC=2 SCAR=17.9
Pod documented
341  
342 0 my ($self) = @_;
343  
344 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
345  
346 0 return $self->{_properties}->{publicKey};
347 }
348  
349 =head2 delete
350  
351 Deletes the object api sided and sets it invalid.
352  
353 =over
354  
355 =item * Synopsis: $ssh_key->delete;
356  
357 =back
358  
359 =cut
360  
361 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
362  
363 0 my ($self) = @_;
364  
365 0 ▶ return unless $self->_is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_valid 0 0 0
366  
367 0 my $api = $self->{_api_wrapper};
368 0 my $project_id = $self->project->id;
369 0 my $key_id = $self->id;
370  
371 0 my $response = $api->rawCall( method => 'delete', path => "/cloud/project/$project_id/sshkey/$key_id", noSignature => 0 );
372 0 ▶ croak $response->error if $response->error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->error 0 0 0
373  
374 0 $self->{_valid} = 0;
375 }
376  
377 36 1;
« Webservice/OVH/Email/Domain/Domain/Account.pm ↑ Summary Webservice/OVH/Email/Domain/Domain.pm »