File Coverage

lib/Webservice/OVH/Cloud/Project/IP.pm
Criterion Covered Total %
statement 12 53 22.6
branch 0 20 0.0
condition 0 6 0.0
subroutine 4 9 44.4
pod 4 4 100.0
total 20 92 21.7


line stmt bran cond sub pod time code
1             package Webservice::OVH::Cloud::Project::IP;
2              
3             =encoding utf-8
4              
5             =head1 NAME
6              
7             Webservice::OVH::Cloud::Project::IP;
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 $networks = $project->network->privates;
19            
20             foreach my $network (@$networks) {
21            
22             print @$networks->status;
23             }
24              
25             =head1 DESCRIPTION
26              
27             Bridge Object failover ips
28              
29             =head1 METHODS
30              
31             =cut
32              
33 36     36   266 use strict;
  36         87  
  36         1220  
34 36     36   203 use warnings;
  36         92  
  36         997  
35 36     36   193 use Carp qw{ carp croak };
  36         91  
  36         2207  
36              
37             our $VERSION = 0.48;
38              
39 36     36   16529 use Webservice::OVH::Cloud::Project::IP::Failover;
  36         111  
  36         21562  
40              
41             =head2 _new_existing
42              
43             Internal Method to create the object.
44             This method is not ment to be called directly.
45              
46             =over
47              
48             =item * Parameter: %params - key => value
49              
50             =item * Return: L<Webservice::OVH::Cloud::Project::IP>
51              
52             =item * Synopsis: Webservice::OVH::Cloud::Project::IP->_new( wrapper => $ovh_api_wrapper, project => $project, module => $module );
53              
54             =back
55              
56             =cut
57              
58             sub _new {
59              
60 0     0     my ( $class, %params ) = @_;
61              
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 $self = bless { module => $params{module}, _api_wrapper => $params{wrapper}, _project => $params{project}, _available_failovers => [], _failovers => {} }, $class;
67              
68 0           return $self;
69             }
70              
71             =head2 project
72              
73             Shorthand to call $self->project directly for internal usage.
74              
75             =over
76              
77             =item * Return: L<Webservice::OVH::Cloud::Project>
78              
79             =item * Synopsis: my $project = $project->ip->project;
80              
81             =back
82              
83             =cut
84              
85             sub project {
86              
87 0     0 1   my ($self) = @_;
88              
89 0           return $self->{_project};
90             }
91              
92             =head2 failover_exists
93              
94             Returns 1 if failover is available for the connected account, 0 if not.
95              
96             =over
97              
98             =item * Parameter: $failover_id - api id, $no_recheck - (optional)only for internal usage
99              
100             =item * Return: VALUE
101              
102             =item * Synopsis: print "failover exists" if $project->ip->failover_exists(1234);
103              
104             =back
105              
106             =cut
107              
108             sub failover_exists {
109              
110 0     0 1   my ( $self, $failover_id, $no_recheck ) = @_;
111              
112 0 0         if ( !$no_recheck ) {
113              
114 0           my $api = $self->{_api_wrapper};
115 0           my $project_id = $self->project->id;
116 0           my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/ip/failover", noSignature => 0 );
117 0 0         croak $response->error if $response->error;
118              
119 0           my $list = $response->content;
120              
121 0 0         return ( grep { $_ eq $failover_id } @$list ) ? 1 : 0;
  0            
122              
123             } else {
124              
125 0           my $list = $self->{_avaiable_projects};
126              
127 0 0         return ( grep { $_ eq $failover_id } @$list ) ? 1 : 0;
  0            
128             }
129             }
130              
131             =head2 failovers
132              
133             Produces an array of all available failovers that are connected to the project.
134              
135             =over
136              
137             =item * Return: ARRAY
138              
139             =item * Synopsis: my $ips = $project->ip->failovers;
140              
141             =back
142              
143             =cut
144              
145             sub failovers {
146              
147 0     0 1   my ($self) = @_;
148              
149 0           my $api = $self->{_api_wrapper};
150 0           my $project_id = $self->project->id;
151 0           my $response = $api->rawCall( method => 'get', path => "/cloud/project/$project_id/ip/failover", noSignature => 0 );
152 0 0         croak $response->error if $response->error;
153              
154 0           my $failover_array = $response->content;
155 0           my $failovers = [];
156 0           $self->{_available_failovers} = $failover_array;
157              
158 0           foreach my $failover_hash (@$failover_array) {
159              
160 0           my $failover_id = $failover_hash->{id};
161 0 0         if ( $self->failover_exists( $failover_id, 1 ) ) {
162 0   0       my $failover = $self->{_failovers}{$failover_id} = $self->{_failovers}{$failover_id} || Webservice::OVH::Cloud::Project::IP::Failover->_new( wrapper => $api, project => $self->project, id => $failover_id, module => $self->{_module} );
163 0           push @$failovers, $failover;
164             }
165             }
166              
167 0           return $failovers;
168             }
169              
170             =head2 failover
171              
172             Returns a single failover by id
173              
174             =over
175              
176             =item * Parameter: $failover_id - api id
177              
178             =item * Return: L<Webservice::OVH::Cloud::Project::IP::Failover>
179              
180             =item * Synopsis: my $failover = $project->ip->failover(1234);
181              
182             =back
183              
184             =cut
185              
186             sub failover {
187              
188 0     0 1   my ( $self, $failover_id ) = @_;
189              
190 0 0         if ( $self->failover_exists($failover_id) ) {
191              
192 0           my $api = $self->{_api_wrapper};
193 0   0       my $failover = $self->{_failovers}{$failover_id} = $self->{_failovers}{$failover_id} || Webservice::OVH::Cloud::Project::IP::Failover->_new( wrapper => $api, project => $self->project, id => $failover_id, module => $self->{_module} );
194              
195 0           return $failover;
196             } else {
197              
198 0           carp "Failover $failover_id doesn't exists";
199 0           return undef;
200             }
201             }
202              
203             1;