File Coverage

blib/lib/OpenStack/MetaAPI/API/Network.pm
Criterion Covered Total %
statement 26 27 96.3
branch 5 10 50.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 0 3 0.0
total 38 49 77.5


line stmt bran cond sub pod time code
1             package OpenStack::MetaAPI::API::Network;
2              
3 3     3   18 use strict;
  3         4  
  3         78  
4 3     3   15 use warnings;
  3         5  
  3         60  
5              
6 3     3   12 use Moo;
  3         5  
  3         22  
7              
8             extends 'OpenStack::MetaAPI::API::Service';
9              
10             # roles
11             with 'OpenStack::MetaAPI::Roles::Listable';
12             with 'OpenStack::MetaAPI::Roles::GetFromId';
13              
14             has '+name' => (default => 'network');
15             has '+version_prefix' => (default => 'v2.0');
16             has '+version' => (default => 'v2'); # use the v2 specs
17              
18             ## FIXME can be defined from specs
19             sub delete_floatingip {
20 1     1 0 3 my ($self, $uid) = @_;
21              
22 1         7 my $uri = $self->root_uri('/floatingips/' . $uid);
23 1         16 return $self->delete($uri);
24             }
25              
26             # REQ: curl -g -i -X POST http://service01a-c2.cpanel.net:9696/v2.0/floatingips
27             # -H "Content-Type: application/json" -H "User-Agent: openstacksdk/0.27.0 keystoneauth1/3.13.1 python-requests/2.21.0 CPython/3.6.6" -H "X-Auth-Token: {SHA256}b0ba0e5595347e63c0b6f56e7f035977abe209f6fa034f41f7dfe491e6e984a1" -d '{"floatingip": {"floating_network_id": "8a10163f-072c-483a-9834-78395cf8a2e7"}}'
28              
29             sub create_floating_ip {
30 1     1 0 3 my ($self, $network_id) = @_;
31              
32 1 50       5 die "Missing network_id" unless defined $network_id;
33              
34 1         7 my $uri = $self->root_uri('/floatingips');
35 1         20 my $answer = $self->post(
36             $uri,
37             {floatingip => {floating_network_id => $network_id}});
38              
39 1 50 33     1588 return $answer->{floatingip} if ref $answer && $answer->{floatingip};
40 0         0 return $answer;
41             }
42              
43             sub add_floating_ip_to_server {
44 1     1 0 2 my ($self, $floatingip_id, $server_id) = @_;
45              
46 1 50       4 die "floatingip_id is required" unless defined $floatingip_id;
47 1 50       6 die "server_id is required" unless defined $server_id;
48              
49 1         3 my $uri = $self->root_uri('/ports');
50 1         19 my $ports = $self->get($uri, device_id => $server_id);
51              
52             # pick the first port for now (maybe need to check the network_id...)
53 1         3884 my $port_id = eval { $ports->{ports}->[0]->{id} };
  1         3  
54 1 50       4 die "Cannot find a port for server $server_id: $@" unless defined $port_id;
55              
56             # now link the floating ip to the port
57 1         5 return $self->put(
58             $self->root_uri('/floatingips/' . $floatingip_id),
59             {floatingip => {port_id => $port_id}});
60             }
61              
62             1;
63              
64             __END__