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