File Coverage

blib/lib/WWW/Docker/API/Networks.pm
Criterion Covered Total %
statement 41 45 91.1
branch 11 18 61.1
condition 1 2 50.0
subroutine 12 13 92.3
pod 0 7 0.0
total 65 85 76.4


line stmt bran cond sub pod time code
1             package WWW::Docker::API::Networks;
2             # ABSTRACT: Docker Engine Networks API
3              
4 10     10   64 use Moo;
  10         16  
  10         61  
5 10     10   8930 use WWW::Docker::Network;
  10         45  
  10         508  
6 10     10   119 use Carp qw( croak );
  10         19  
  10         799  
7 10     10   64 use namespace::clean;
  10         21  
  10         117  
8              
9             our $VERSION = '0.101';
10              
11             =head1 SYNOPSIS
12              
13             my $docker = WWW::Docker->new;
14              
15             # Create a network
16             my $result = $docker->networks->create(
17             Name => 'my-network',
18             Driver => 'bridge',
19             );
20              
21             # List networks
22             my $networks = $docker->networks->list;
23              
24             # Connect/disconnect containers
25             $docker->networks->connect($network_id, Container => $container_id);
26             $docker->networks->disconnect($network_id, Container => $container_id);
27              
28             # Remove network
29             $docker->networks->remove($network_id);
30              
31             =head1 DESCRIPTION
32              
33             This module provides methods for managing Docker networks including creation,
34             listing, connecting containers, and removal.
35              
36             Accessed via C<< $docker->networks >>.
37              
38             =cut
39              
40             has client => (
41             is => 'ro',
42             required => 1,
43             weak_ref => 1,
44             );
45              
46             =attr client
47              
48             Reference to L client. Weak reference to avoid circular dependencies.
49              
50             =cut
51              
52             sub _wrap {
53 3     3   9 my ($self, $data) = @_;
54 3         94 return WWW::Docker::Network->new(
55             client => $self->client,
56             %$data,
57             );
58             }
59              
60             sub _wrap_list {
61 1     1   4 my ($self, $list) = @_;
62 1         3 return [ map { $self->_wrap($_) } @$list ];
  2         3174  
63             }
64              
65             sub list {
66 1     1 0 1595 my ($self, %opts) = @_;
67 1         3 my %params;
68 1 50       6 $params{filters} = $opts{filters} if defined $opts{filters};
69 1         14 my $result = $self->client->get('/networks', params => \%params);
70 1   50     27 return $self->_wrap_list($result // []);
71             }
72              
73             =method list
74              
75             my $networks = $networks->list;
76              
77             List networks. Returns ArrayRef of L objects.
78              
79             =cut
80              
81             sub inspect {
82 2     2 0 632 my ($self, $id) = @_;
83 2 100       252 croak "Network ID required" unless $id;
84 1         16 my $result = $self->client->get("/networks/$id");
85 1         22 return $self->_wrap($result);
86             }
87              
88             =method inspect
89              
90             my $network = $networks->inspect($id);
91              
92             Get detailed information about a network. Returns L object.
93              
94             =cut
95              
96             sub create {
97 1     1 0 42 my ($self, %config) = @_;
98 1 50       5 croak "Network name required" unless $config{Name};
99 1         16 my $result = $self->client->post('/networks/create', \%config);
100 1         1218 return $result;
101             }
102              
103             =method create
104              
105             my $result = $networks->create(
106             Name => 'my-network',
107             Driver => 'bridge',
108             );
109              
110             Create a network. Returns hashref with C and C.
111              
112             =cut
113              
114             sub remove {
115 2     2 0 1679 my ($self, $id) = @_;
116 2 100       175 croak "Network ID required" unless $id;
117 1         16 return $self->client->delete_request("/networks/$id");
118             }
119              
120             =method remove
121              
122             $networks->remove($id);
123              
124             Remove a network.
125              
126             =cut
127              
128             sub connect {
129 2     2 0 1359 my ($self, $id, %opts) = @_;
130 2 50       10 croak "Network ID required" unless $id;
131 2 100       228 croak "Container required" unless $opts{Container};
132 1         10 return $self->client->post("/networks/$id/connect", \%opts);
133             }
134              
135             =method connect
136              
137             $networks->connect($network_id, Container => $container_id);
138              
139             Connect a container to a network.
140              
141             =cut
142              
143             sub disconnect {
144 1     1 0 616 my ($self, $id, %opts) = @_;
145 1 50       5 croak "Network ID required" unless $id;
146 1 50       5 croak "Container required" unless $opts{Container};
147 1         9 return $self->client->post("/networks/$id/disconnect", \%opts);
148             }
149              
150             =method disconnect
151              
152             $networks->disconnect($network_id, Container => $container_id, Force => 1);
153              
154             Disconnect a container from a network. Optional C parameter.
155              
156             =cut
157              
158             sub prune {
159 0     0 0   my ($self, %opts) = @_;
160 0           my %params;
161 0 0         $params{filters} = $opts{filters} if defined $opts{filters};
162 0           return $self->client->post('/networks/prune', undef, params => \%params);
163             }
164              
165             =method prune
166              
167             my $result = $networks->prune;
168              
169             Delete unused networks. Returns hashref with C.
170              
171             =cut
172              
173             =seealso
174              
175             =over
176              
177             =item * L - Main Docker client
178              
179             =item * L - Network entity class
180              
181             =back
182              
183             =cut
184              
185             1;