blib/lib/Webservice/OVH/Cloud/Project/SSH.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 160 SCAR 63.1
CC
23
Cov
0%
_new_existing
56.0
_new
56.0
properties
12.0
CRAP 552.0
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 use strict;
35 use warnings;
36 use Carp qw{ carp croak };
37  
38 our $VERSION = 0.47;
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 my ( $class, %params ) = @_;
60  
61 die "Missing id" unless $params{id};
62 die "Missing project" unless $params{project};
63 die "Missing module" unless $params{module};
64 die "Missing wrapper" unless $params{wrapper};
65  
66 my $api_wrapper = $params{wrapper};
67 my $module = $params{module};
68 my $project = $params{project};
69 my $key_id = $params{id};
70  
71 my $project_id = $project->id;
72 my $response = $api_wrapper->rawCall( method => 'get', path => "/cloud/project/$project_id/sshkey/$key_id", noSignature => 0 );
73 carp $response->error if $response->error;
74  
75 if ( !$response->error ) {
76  
77 my $porperties = $response->content;
78 my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $key_id, _properties => $porperties, _project => $project }, $class;
79  
80 return $self;
81 } else {
82  
83 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 my ( $class, %params ) = @_;
107  
108 die "Missing project" unless $params{project};
109 die "Missing module" unless $params{module};
110 die "Missing wrapper" unless $params{wrapper};
111  
112 my $api_wrapper = $params{wrapper};
113 my $module = $params{module};
114 my $project = $params{project};
115  
116 my @keys_needed = qw{ public_key name };
117 if ( my @missing_parameters = grep { not $params{$_} } @keys_needed ) {
118  
119 croak "Missing parameter: @missing_parameters";
120 }
121  
122 my $project_id = $project->id;
123 my $body = {};
124 $body->{name} = $params{name};
125 $body->{publicKey} = $params{public_key};
126 $body->{region} = $params{region} if exists $params{region};
127 my $response = $api_wrapper->rawCall( method => 'post', path => "/cloud/project/$project_id/sshkey", body => $body, noSignature => 0 );
128 croak $response->error if $response->error;
129  
130 my $key_id = $response->content->{id};
131  
132 my $properties = $response->content;
133  
134 my $self = bless { _module => $module, _valid => 1, _api_wrapper => $api_wrapper, _id => $key_id, _properties => $properties, _project => $project }, $class;
135  
136 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 my ($self) = @_;
156  
157 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 my ($self) = @_;
177  
178 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 my ($self) = @_;
199  
200 carp "Key is not valid anymore" unless $self->is_valid;
201 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 my ($self) = @_;
221  
222 return unless $self->_is_valid;
223  
224 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 my ($self) = @_;
245  
246 return unless $self->_is_valid;
247  
248 my $api = $self->{_api_wrapper};
249 my $project_id = $self->project->id;
250 my $key_id = $self->id;
251 my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/sshkey/$key_id", noSignature => 0 );
252 croak $response->error if $response->error;
253 $self->{_properties} = $response->content;
254 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 my ($self) = @_;
274  
275 return unless $self->_is_valid;
276  
277 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 my ($self) = @_;
297  
298 return unless $self->_is_valid;
299  
300 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 my ($self) = @_;
320  
321 return unless $self->_is_valid;
322  
323 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 my ($self) = @_;
343  
344 return unless $self->_is_valid;
345  
346 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 my ($self) = @_;
364  
365 return unless $self->_is_valid;
366  
367 my $api = $self->{_api_wrapper};
368 my $project_id = $self->project->id;
369 my $key_id = $self->id;
370  
371 my $response = $api->rawCall( method => 'delete', path => "/cloud/project/$project_id/sshkey/$key_id", noSignature => 0 );
372 croak $response->error if $response->error;
373  
374 $self->{_valid} = 0;
375 }
376  
377 1;