Docker/API/Plugins.pm

filter: Statementstmt 100% 94 / 94 Branchbran 96.3% 52 / 54 Conditioncond 71.4% 5 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 96.2% 181 / 188 CC 31 SCAR 12.3
CC
31
Cov
97%
configure
6.0
disable
4.0
upgrade
4.0
CRAP 3.4
« Docker/API/Configs.pm ↑ Summary Docker/API/Secrets.pm »
1 package API::Docker::API::Plugins;
2 # ABSTRACT: Docker Engine Plugins API
3 52 our $VERSION = '0.006';
4 52 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 52 with 'API::Docker::Role::Filters', 'API::Docker::Role::RegistryAuth',
6 'API::Docker::Role::Using';
7 52 ▶use API::Docker::Role::Entity::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 52 ▶use API::Docker::Type::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 52 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 52 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 52 has client => (
14 is => 'ro',
15 required => 1,
16 weak_ref => 1,
17 );
18  
19  
20 # The class is the caller's argument, as it is on the resource classes whose
21 # list and inspect really are two definitions -- here both are the swagger's
22 # one `Plugin`, and passing it keeps the seam in the same place.
23 #
24 # from_data, not new: this is a daemon response, and the two entry points of
25 # API::Docker::Role::Type read it differently. from_data takes the swagger's
26 # wire names and nothing else, so a key it has not heard of keeps its own
27 # spelling instead of being read as the Perl name of one it has, and a value
28 # that disagrees with the swagger costs its own field rather than the whole
29 # response. `client` is ours rather than the engine's, so it goes beside the
30 # data instead of into it.
31 8 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
32 8 my ($self, $class, $data) = @_;
33 8 return $class->from_data($data, client => $self->client);
34 }
35  
36 7 ▶sub _wrap_list {
Subroutine _wrap_list: called CC=1 SCAR=0.0
37 7 my ($self, $class, $list) = @_;
38 7 return [ map { $self->_wrap($class, $_) } @$list ];
39 }
40  
41 # The plugin router calls registry.DecodeAuthConfig on the header and
42 # discards the error -- "Ignore invalid AuthConfig to increase compatibility
43 # with the existing API" -- so unlike /images/{name}/push, which rejects a
44 # missing header, an anonymous plugin operation needs no header at all. It is
45 # sent only when the caller asked for it. The encoding is
46 # API::Docker::Role::RegistryAuth; what stays here is the policy.
47 45 ▶sub _auth_headers {
Subroutine _auth_headers: called CC=2 SCAR=6.9
48 45 my ($self, $opts) = @_;
49 45 ▶ return () unless defined $opts->{auth};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"auth"} 41 4 45
50 4 return (headers => { 'X-Registry-Auth' => $self->_registry_auth_header($opts->{auth}) });
51 }
52  
53 25 ▶sub _privileges_body {
Subroutine _privileges_body: called CC=4 SCAR=13.9
54 25 my ($self, $method, $remote, %opts) = @_;
55  
56 25 ▶ return $self->privileges($remote, %opts) if $opts{accept_privileges};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"accept_privileges"} 8 17 25
57  
58 17 my $privileges = $opts{privileges};
59 17 ▶ croak __PACKAGE__ . '->' . $method . ' requires privileges: fetch them with '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $privileges 2 15 17
60 . '->privileges(' . $remote . ') and pass them back as privileges => '
61 . '$privileges, or pass accept_privileges => 1 to grant whatever the '
62 . 'plugin asks for. The engine compares the list you send against the one '
63 . 'the plugin demands and fails the operation when they differ'
64 unless defined $privileges;
65  
66 15 ▶ croak __PACKAGE__ . '->' . $method . ' privileges must be an ArrayRef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $privileges eq "ARRAY" 1 14 15
67 unless ref $privileges eq 'ARRAY';
68  
69 14 return $privileges;
70 }
71  
72 8 ▶sub list {
Subroutine list: called CC=3 SCAR=11.0
Pod documented
73 8 my ($self, %opts) = @_;
74 8 my %params;
75 $params{filters} = $self->_normalise_filters($opts{filters})
76 8 ▶ if defined $opts{filters};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"filters"} 2 6 8
77 my $result = $self->client->get('/plugins',
78 params => \%params,
79 7 %{ $self->_request_options },
80 );
81 7 ▶ return $self->_wrap_list('API::Docker::Type::Plugin', $result // []);
Condition50%1/2
$result // []
l!l
70
MC/DC0%0/1
$result // []
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
82 }
83  
84  
85 17 ▶sub privileges {
Subroutine privileges: called CC=3 SCAR=11.0
Pod documented
86 17 my ($self, $remote, %opts) = @_;
87 17 ▶ croak __PACKAGE__ . '->privileges remote reference required' unless $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless $remote 1 16 17
88  
89 my $result = $self->client->get('/plugins/privileges',
90 params => { remote => $remote },
91 $self->_auth_headers(\%opts),
92 16 %{ $self->_request_options },
93 );
94  
95 # A plugin that demands nothing answers a bare `null`: computePrivileges
96 # builds its result with `var privileges types.PluginPrivileges` and
97 # appends only what the config asks for, so a nil Go slice reaches the
98 # wire. The transport decodes that to undef, which no caller can iterate
99 # and which accept_privileges => 1 would post straight back to
100 # /plugins/pull as a JSON null. Normalised to the empty list it means.
101 16 ▶ return [] unless ref $result eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "ARRAY" 10 6 16
102 6 return $result;
103 }
104  
105  
106 14 ▶sub install {
Subroutine install: called CC=4 SCAR=13.9
Pod documented
107 14 my ($self, $remote, %opts) = @_;
108 14 ▶ croak __PACKAGE__ . '->install remote reference required' unless $remote;
Branch: 1 branch
BranchTrueFalseTotal
unless $remote 1 13 14
109  
110 13 my $privileges = $self->_privileges_body('install', $remote, %opts);
111  
112 11 my %params = ( remote => $remote );
113 11 ▶ $params{name} = $opts{name} if defined $opts{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"name"} 1 10 11
114  
115 # exists, not truth: an unset callback is a caller bug, and falling back to
116 # the buffered path for it would hand a long pull back as silence.
117 return $self->client->post('/plugins/pull', $privileges,
118 params => \%params,
119 $self->_auth_headers(\%opts),
120 11 ▶ %{ $self->_request_options },
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'on_event'} ? : 1 10 11
121 11 exists $opts{on_event} ? ( on_event => $opts{on_event} ) : ( ndjson => 1 ),
122 );
123 }
124  
125  
126 7 ▶sub inspect {
Subroutine inspect: called CC=2 SCAR=6.9
Pod documented
127 7 my ($self, $name) = @_;
128 7 ▶ croak __PACKAGE__ . '->inspect plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 6 7
129 return $self->_wrap('API::Docker::Type::Plugin',
130 $self->client->get("/plugins/$name/json",
131 6 %{ $self->_request_options },
132 ));
133 }
134  
135  
136 8 ▶sub remove {
Subroutine remove: called CC=4 SCAR=13.9
Pod documented
137 8 my ($self, $name, %opts) = @_;
138 8 ▶ croak __PACKAGE__ . '->remove plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 7 8
139 7 my %params;
140 7 ▶ $params{force} = $opts{force} ? 1 : 0 if defined $opts{force};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"force"} 3 4 7
$opts{'force'} ? : 2 1 3
141 return $self->client->delete_request("/plugins/$name",
142 params => \%params,
143 7 %{ $self->_request_options },
144 );
145 }
146  
147  
148 8 ▶sub enable {
Subroutine enable: called CC=3 SCAR=11.0
Pod documented
149 8 my ($self, $name, %opts) = @_;
150 8 ▶ croak __PACKAGE__ . '->enable plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 7 8
151 # Always sent, and not conditional on the caller passing it: the daemon
152 # parses this parameter with strconv.Atoi and has no default, so an absent
153 # timeout is parsed as the empty string and answers 400. See the POD.
154 7 ▶ my %params = ( timeout => $opts{timeout} // 0 );
Condition100%2/2
$opts{"timeout"} // 0
l!l
34
MC/DC100%1/1
$opts{"timeout"} // 0
$opts{"timeout"}
Truth table100%2/2
A
A= $opts{"timeout"}
Aresult
11
00
155 return $self->client->post("/plugins/$name/enable", undef,
156 params => \%params,
157 7 %{ $self->_request_options },
158 );
159 }
160  
161  
162 7 ▶sub disable {
Subroutine disable: called CC=4 SCAR=13.9
Pod documented
163 7 my ($self, $name, %opts) = @_;
164 7 ▶ croak __PACKAGE__ . '->disable plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 6 7
165 6 my %params;
166 6 ▶ $params{force} = $opts{force} ? 1 : 0 if defined $opts{force};
Branch: 2 branches
BranchTrueFalseTotal
if defined $opts{"force"} 2 4 6
$opts{'force'} ? : 2 0 2
167 return $self->client->post("/plugins/$name/disable", undef,
168 params => \%params,
169 6 %{ $self->_request_options },
170 );
171 }
172  
173  
174 13 ▶sub upgrade {
Subroutine upgrade: called CC=4 SCAR=13.9
Pod documented
175 13 my ($self, $name, %opts) = @_;
176 13 ▶ croak __PACKAGE__ . '->upgrade plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 12 13
177  
178 12 ▶ my $remote = $opts{remote} // $name;
Condition66%2/3
$opts{"remote"} // $name
l!l&&r!l&&!r
2100
MC/DC0%0/2
$opts{"remote"} // $name
$opts{"remote"} $name
Truth table66%2/3
A // B
A= $opts{"remote"}B= $name
ABresult
1X1
011
000
179 12 my $privileges = $self->_privileges_body('upgrade', $remote, %opts);
180  
181 return $self->client->post("/plugins/$name/upgrade", $privileges,
182 params => { remote => $remote },
183 $self->_auth_headers(\%opts),
184 11 ▶ %{ $self->_request_options },
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'on_event'} ? : 1 10 11
185 11 exists $opts{on_event} ? ( on_event => $opts{on_event} ) : ( ndjson => 1 ),
186 );
187 }
188  
189  
190 8 ▶sub push {
Subroutine push: called CC=3 SCAR=11.0
Pod documented
191 8 my ($self, $name, %opts) = @_;
192 8 ▶ croak __PACKAGE__ . '->push plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 7 8
193 return $self->client->post("/plugins/$name/push", undef,
194 $self->_auth_headers(\%opts),
195 7 ▶ %{ $self->_request_options },
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'on_event'} ? : 1 6 7
196 7 exists $opts{on_event} ? ( on_event => $opts{on_event} ) : ( ndjson => 1 ),
197 );
198 }
199  
200  
201 10 ▶sub configure {
Subroutine configure: called CC=6 SCAR=17.9
Pod documented
202 10 my ($self, $name, @settings) = @_;
203 10 ▶ croak __PACKAGE__ . '->configure plugin name required' unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 1 9 10
204  
205 # One ArrayRef or a plain list, and nothing after either: this method reads
206 # no options at all. The ArrayRef form used to be where the transport bounds
207 # went, because a trailing `read_timeout => 2` in the plain list would be two
208 # more settings as far as this method can tell -- they now go on the resource
209 # class instead (karr k74), and what is left is a form, not a split.
210 9 ▶ if (ref $settings[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $settings[0] eq "ARRAY") 6 3 9
211 6 my $list = shift @settings;
212 6 ▶ croak __PACKAGE__ . '->configure takes nothing after the ArrayRef of '
Branch: 1 branch
BranchTrueFalseTotal
if @settings 0 6 6
213 . 'settings; a transport bound goes on the resource class, as '
214 . '$docker->plugins->using(read_timeout => 5)->configure(...)'
215 if @settings;
216 6 @settings = @$list;
217 }
218  
219 9 ▶ croak __PACKAGE__ . '->configure requires at least one setting, as an '
Branch: 1 branch
BranchTrueFalseTotal
unless @settings 2 7 9
220 . 'ArrayRef or a list of "KEY=value" strings' unless @settings;
221  
222 croak __PACKAGE__ . '->configure settings must be plain strings'
223 7 ▶ if grep { ref $_ } @settings;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_;} @settings 1 6 7
224  
225 return $self->client->post("/plugins/$name/set", \@settings,
226 6 %{ $self->_request_options },
227 );
228 }
229  
230  
231  
232 52 1;
233  
234 __END__
« Docker/API/Configs.pm ↑ Summary Docker/API/Secrets.pm »