File Coverage

lib/Webservice/OVH/Cloud/Project/SSH.pm
Criterion Covered Total %
statement 9 87 10.3
branch 0 44 0.0
condition n/a
subroutine 3 15 20.0
pod 9 9 100.0
total 21 155 13.5


line stmt bran cond sub pod time code
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     36   262 use strict;
  36         102  
  36         1070  
35 36     36   211 use warnings;
  36         78  
  36         1066  
36 36     36   219 use Carp qw{ carp croak };
  36         81  
  36         39437  
37              
38             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             sub _new_existing {
58              
59 0     0     my ( $class, %params ) = @_;
60              
61 0 0         die "Missing id" unless $params{id};
62 0 0         die "Missing project" unless $params{project};
63 0 0         die "Missing module" unless $params{module};
64 0 0         die "Missing wrapper" unless $params{wrapper};
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 0         carp $response->error if $response->error;
74              
75 0 0         if ( !$response->error ) {
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             sub _new {
105              
106 0     0     my ( $class, %params ) = @_;
107              
108 0 0         die "Missing project" unless $params{project};
109 0 0         die "Missing module" unless $params{module};
110 0 0         die "Missing wrapper" unless $params{wrapper};
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 0         if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
  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 0         $body->{region} = $params{region} if exists $params{region};
127 0           my $response = $api_wrapper->rawCall( method => 'post', path => "/cloud/project/$project_id/sshkey", body => $body, noSignature => 0 );
128 0 0         croak $response->error if $response->error;
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             sub project {
154              
155 0     0 1   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             sub is_valid {
175              
176 0     0 1   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             sub _is_valid {
197              
198 0     0     my ($self) = @_;
199              
200 0 0         carp "Key is not valid anymore" unless $self->is_valid;
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             sub id {
219              
220 0     0 1   my ($self) = @_;
221              
222 0 0         return unless $self->_is_valid;
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             sub properties {
243              
244 0     0 1   my ($self) = @_;
245              
246 0 0         return unless $self->_is_valid;
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 0         croak $response->error if $response->error;
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             sub finger_print {
272              
273 0     0 1   my ($self) = @_;
274              
275 0 0         return unless $self->_is_valid;
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             sub regions {
295              
296 0     0 1   my ($self) = @_;
297              
298 0 0         return unless $self->_is_valid;
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             sub name {
318              
319 0     0 1   my ($self) = @_;
320              
321 0 0         return unless $self->_is_valid;
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             sub public_key {
341              
342 0     0 1   my ($self) = @_;
343              
344 0 0         return unless $self->_is_valid;
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             sub delete {
362              
363 0     0 1   my ($self) = @_;
364              
365 0 0         return unless $self->_is_valid;
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 0         croak $response->error if $response->error;
373              
374 0           $self->{_valid} = 0;
375             }
376              
377             1;