Client/Identity/v3.pm

filter: Statementstmt 85.8% 243 / 283 Branchbran 67.3% 93 / 138 Conditioncond 76.6% 23 / 30 MC/DCmcdc 64.7% 11 / 17 Subroutinesub 96.3% 26 / 27 Podpod 100% 12 / 12 totaltotal 80.4% 408 / 507 CC 94 SCAR 22.9
CC
94
Cov
80%
sync_rolemap
18.1
sync
15.9
tagstore_init
13.1
CRAP 9.9
« Client/REST.pm ↑ Summary Client/Error.pm »
1 package Net::OpenStack::Client::Identity::v3;
2 2 $Net::OpenStack::Client::Identity::v3::VERSION = '0.1.4';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Net::OpenStack::Client::API::Convert qw(convert);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Net::OpenStack::Client::Identity::Tagstore;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Net::OpenStack::Client::Request qw(mkrequest);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use MIME::Base64 qw(encode_base64url decode_base64url);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 Readonly my $IDREG => qr{[0-9a-z]{33}};
16  
17 # This list is ordered:
18 # Configuration of n-th item does not require
19 # configuration of any items after that, but
20 # might require configuration of previous ones
21 2 Readonly our @SUPPORTED_OPERATIONS => qw(
22 region
23 domain
24 project
25 user
26 group
27 role
28 rolemap
29 service
30 endpoint
31 );
32  
33 2 Readonly my %PARENT_ATTR => {
34 region => 'parent_region_id',
35 project => 'parent_id',
36 };
37  
38 # tagstore cache
39 # key is project id; value is instance
40 2 my $_tagstores = {};
41  
42 =head1 Functions
43  
44 =over
45  
46 =item sort_parent
47  
48 Sort according to parent attribute.
49  
50 =cut
51  
52 # Use toposort?
53 # see https://rosettacode.org/wiki/Topological_sort#Perl
54  
55 sub sort_parent
56 14 ▶{
Subroutine sort_parent: called CC=9 SCAR=22.0
Pod documented
57 # We assume that an empty string or number 0 is not a valid/used region name
58 # force strings, so we can do eq tests
59 14 my $ra = $a->{name};
60 14 my $rb = $b->{name};
61 14 ▶ my $pra = $a->{parent} || '';
Condition100%2/2
$Net::OpenStack::Client::Identity::v3::a->{'parent'} || ''
l!l
95
MC/DC100%1/1
$Net::OpenStack::Client::Identity::v3::a->{'parent'} || ''
$Net::OpenStack::Client::Identity::v3::a->{'parent'}
Truth table100%2/2
A
A= $Net::OpenStack::Client::Identity::v3::a->{'parent'}
Aresult
11
00
62 14 ▶ my $prb = $b->{parent} || '';
Condition100%2/2
$Net::OpenStack::Client::Identity::v3::b->{'parent'} || ''
l!l
95
MC/DC100%1/1
$Net::OpenStack::Client::Identity::v3::b->{'parent'} || ''
$Net::OpenStack::Client::Identity::v3::b->{'parent'}
Truth table100%2/2
A
A= $Net::OpenStack::Client::Identity::v3::b->{'parent'}
Aresult
11
00
63  
64 14 my $res;
65 14 ▶ if ($pra eq $rb) {
Branch: 4 branches
BranchTrueFalseTotal
if ($pra eq $rb) { } 2 12 14
elsif ($prb eq $ra) { } 1 11 12
elsif ($pra and not $prb) { } 1 10 11
elsif ($prb and not $pra) { } 3 7 10
66 # b is parent of a: order b a
67 2 $res = 1;
68 } elsif ($prb eq $ra) {
69 # a is parent of b: order a b
70 1 $res = -1;
71 ▶ } elsif ($pra && !$prb) {
Condition100%3/3
$pra and not $prb
!ll&&!rl&&r
551
MC/DC100%2/2
$pra and not $prb
$pra not $prb
Truth table100%3/3
A and not(B)
A= $praB= not $prb
ABresult
0X0
100
111
72 # a has parent, b does not: order b a
73 1 $res = 1;
74 ▶ } elsif ($prb && !$pra) {
Condition100%3/3
$prb and not $pra
!ll&&!rl&&r
253
MC/DC100%2/2
$prb and not $pra
$prb not $pra
Truth table100%3/3
A and not(B)
A= $prbB= not $pra
ABresult
0X0
100
111
75 # b has parent, a does not: order a b
76 3 $res = -1;
77 } else {
78 # does not matter, use alphabetical sort
79 7 $res = $ra cmp $rb;
80 }
81  
82 14 return $res;
83 }
84  
85 =item sort_parents
86  
87 Sort arrayref of C<names> with data from C<items> using parent C<attr>.
88  
89 =cut
90  
91 sub sort_parents
92 2 ▶{
Subroutine sort_parents: called CC=1 SCAR=0.0
Pod documented
93 2 my ($names, $items, $attr) = @_;
94  
95 # Assume the id is equal to the name of the region
96 2 my @snames = sort sort_parent (map {{name => $_, parent => $items->{$_}->{$attr}}} @$names);
97 2 return map {$_->{name}} @snames;
98 }
99  
100 =item rest
101  
102 Convenience wrapper for direct REST calls
103 for C<method>, C<operation> and options C<ropts>.
104  
105 =cut
106  
107 sub rest
108 25 ▶{
Subroutine rest: called CC=3 SCAR=11.0
Pod documented
109 25 my ($self, $method, $operation, %ropts) = @_;
110 25 my $defropts = {
111 method => $method,
112 version => 'v3',
113 service => 'identity',
114 };
115  
116 25 %ropts = (%$defropts, %ropts);
117  
118 # generate raw data
119 25 ▶ $ropts{raw} = {$operation => delete $ropts{data}} if ($ropts{data});
Branch: 1 branch
BranchTrueFalseTotal
if $ropts{"data"} 10 15 25
120  
121 25 ▶ my $endpoint = "${operation}s/" . (delete $ropts{what} || '') . "?name=name";
Condition100%2/2
delete $ropts{'what'} || ''
l!l
520
MC/DC100%1/1
delete $ropts{'what'} || ''
delete $ropts{'what'}
Truth table100%2/2
A
A= delete $ropts{'what'}
Aresult
11
00
122  
123 25 return $self->rest(mkrequest($endpoint, $method, %ropts));
124 };
125  
126 =item get_id
127  
128 Return the ID of an C<operation>.
129 If the name is an ID, return the ID without a lookup.
130 If the operation is 'region', return the name.
131  
132 Options
133  
134 =over
135  
136 =item error: report an error when no id is found
137  
138 =item msg: use the value as (part of) the reported message
139  
140 =back
141  
142 =cut
143  
144 sub get_id
145 16 ▶{
Subroutine get_id: called CC=9 SCAR=24.4
Pod documented
146 16 my ($self, $operation, $name, %opts) = @_;
147  
148 # region has no id (or no name, whatever you like)
149 16 ▶ return $name if ($name =~ m/$IDREG/ || $operation eq 'region');
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /$IDREG/ or $operation eq "region" 4 12 16
Condition66%2/3
$name =~ /$IDREG/ or $operation eq "region"
l!l&&r!l&&!r
0412
MC/DC50%1/2
$name =~ /$IDREG/ or $operation eq "region"
$name =~ /$IDREG/ $operation eq "region"
Truth table66%2/3
A or B
A= $name =~ /$IDREG/B= $operation eq "region"
ABresult
1X1
011
000
150  
151 # GET the list for name
152 12 my $resp = $self->api_identity_rest('GET', $operation, result => "/${operation}s", params => {name => $name});
153  
154 12 my $msg = "found for $operation with name $name";
155 12 ▶ $msg .= " $opts{msg}" if $opts{msg};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"msg"} 7 5 12
156  
157 12 my $id;
158 12 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 12 0 12
159 12 ▶ my @ids = (map {$_->{id}} @{$resp->result || []});
Condition50%1/2
$resp->result || []
l!l
120
MC/DC0%0/1
$resp->result || []
$resp->result
Truth table50%1/2
A
A= $resp->result
Aresult
11
00
160 12 ▶ if (scalar @ids > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @ids > 1) { } 0 12 12
elsif (@ids) { } 12 0 12
161 # what? do not return anything
162 0 $self->error("More than one ID $msg: @ids");
163 } elsif (@ids) {
164 12 $id = $ids[0];
165 12 $self->verbose("ID $id $msg");
166 } else {
167 0 ▶ my $method = $opts{error} ? 'error' : 'verbose';
Branch: 1 branch
BranchTrueFalseTotal
$opts{'error'} ? : 0 0 0
168 0 $self->$method("No ID $msg");
169 }
170 } else {
171 0 $self->error("get_id invalid request $msg: $resp->{error}");
172 };
173  
174 12 return $id;
175 }
176  
177 # Function to retrun the name attribute based on the the operation
178 sub _name_attribute
179 11 ▶{
Subroutine _name_attribute: called CC=2 SCAR=6.9
180 11 my ($operation) = @_;
181 11 ▶ return $operation eq 'region' ? 'id' : 'name';
Branch: 1 branch
BranchTrueFalseTotal
$operation eq 'region' ? : 4 7 11
182 }
183  
184 # Function to return the name based on the operation and data
185 sub _make_name
186 8 ▶{
Subroutine _make_name: called CC=2 SCAR=6.9
187 8 my ($operation, $data) = @_;
188 8 ▶ if ($operation eq 'endpoint') {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq 'endpoint') { } 3 5 8
189 # for endpoint, we construct an internal unique name based on
190 # interface and url, seperated by a underscore
191 3 return "$data->{interface}_$data->{url}";
192 } else {
193 5 my $attr = _name_attribute($operation);
194 5 return $data->{$attr};
195 }
196 }
197  
198 =item tagstore_init
199  
200 Function to initialise tagstore or return cached version based on tagstore project name.
201  
202 =cut
203  
204 sub tagstore_init
205 3 ▶{
Subroutine tagstore_init: called CC=7 SCAR=25.7
Pod documented
206 3 my ($client, $tagstore_proj) = @_;
207  
208 3 ▶ if (!$_tagstores->{$tagstore_proj}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_tagstores->{$tagstore_proj}) 1 2 3
209  
210 # Does the project exist?
211 1 my $resp = $client->api_identity_projects(name => $tagstore_proj);
212 1 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 1 0 1
213 1 my @proj = @{$resp->result};
214 1 ▶ if (scalar @proj > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @proj > 1) { } 0 1 1
elsif (scalar @proj == 1) { } 1 0 1
215 $client->error("More than one tagstore project $tagstore_proj found: ids ",
216 0 join(",", map {$_->{id}} @proj), ". Unsupported for now");
217 0 return;
218 } elsif (scalar @proj == 1) {
219 1 $client->verbose("Found one tagstore project $tagstore_proj id ", $proj[0]->{id});
220 } else {
221 0 $resp = $client->api_identity_add_project(name => $tagstore_proj,
222 description => "Main tagstore project $tagstore_proj");
223 0 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 0 0 0
224 0 $client->verbose("Created main tagstore project $tagstore_proj id ", $resp->result->{id});
225 } else {
226 0 $client->error("Failed to add main tagstore project $tagstore_proj: $resp->{error}");
227 0 return;
228 }
229 }
230 } else {
231 0 $client->error("Failed to list possible tagstore project $tagstore_proj: $resp->{error}");
232 0 return;
233 }
234  
235 # Get instance
236 1 my $tgst = Net::OpenStack::Client::Identity::Tagstore->new(
237 $client,
238 $tagstore_proj,
239 );
240  
241 1 ▶ if ($tgst) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tgst) { } 1 0 1
242 1 $_tagstores->{$tagstore_proj} = $tgst;
243 } else {
244 0 $client->error("sync: failed to create new tagstore for project $tagstore_proj");
245 0 return;
246 }
247 }
248  
249 3 return $_tagstores->{$tagstore_proj};
250 }
251  
252 =item tagstore_postprocess
253  
254 Function to postprocess sync operations when a tagstore is used.
255  
256 =cut
257  
258 sub tagstore_postprocess
259 7 ▶{
Subroutine tagstore_postprocess: called CC=6 SCAR=18.6
Pod documented
260 7 my ($tagstore, $phase, $operation, $name, $result) = @_;
261  
262 7 my $msg = "sync postprocess $operation $name stopped after failure to $phase";
263 7 ▶ if (exists($result->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{'id'}) { } 7 0 7
264 7 my $id = $result->{id};
265 7 my $ok = 1;
266  
267 7 ▶ if ($phase eq 'create' || $phase eq 'delete') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq 'create' or $phase eq 'delete') { } 6 1 7
Condition100%3/3
$phase eq "create" or $phase eq "delete"
l!l&&r!l&&!r
421
MC/DC100%2/2
$phase eq "create" or $phase eq "delete"
$phase eq "create" $phase eq "delete"
Truth table100%3/3
A or B
A= $phase eq "create"B= $phase eq "delete"
ABresult
1X1
011
000
268 6 ▶ my $method = $phase eq 'create' ? 'add' : $phase;
Branch: 1 branch
BranchTrueFalseTotal
$phase eq 'create' ? : 4 2 6
269 6 $ok = $tagstore->$method("ID_${operation}_${id}");
270 } else {
271 1 $tagstore->verbose("sync: nothing to do for tagstore postprocessing during $phase for $name id $id");
272 }
273  
274 7 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 7 0 7
275 7 return 1;
276 } else {
277 0 $tagstore->error("$msg tag $id to tagstore. See previous error where to add the tag to continue");
278 0 return;
279 }
280 } else {
281 0 $tagstore->error("$msg no id in response");
282 0 return;
283 }
284 }
285  
286 =pod
287  
288 =back
289  
290 =head1 Methods
291  
292 =over
293  
294 =item sync
295  
296 For an C<operation> (like C<user>, C<group>, C<service>, ...),
297 given an hashref of C<items> (key is the name),
298 compare it with all existing items:
299  
300 =over
301  
302 =item Non-existing ones are added/created
303  
304 =item Existing ones are possibly updated
305  
306 =item Existing ones that are not requested are disbaled
307  
308 =back
309  
310 Returns a hasref with responses for the created items. The keys are
311 C<create>, C<update> and C<delete> and the values an arrayref of responses.
312  
313 For C<endpoint> operations, as they have no name, use the C<<<interface>_<url>>>
314 as the name for the C<items> hashref.
315  
316 Following options are supported:
317  
318 =over
319  
320 =item filter: a function to filter the existing items.
321 Return a true value to keep the existing item (false will ignore it).
322 By default, all existing items are considered.
323  
324 =item delete: when the delete option is true, existing items that are
325 not in the C<items> hashref, will be deleted (instead of disabled).
326  
327 =item keep: when the keep option is true, existing items that are
328 not in the C<items> hashref are ignored.
329 This precedes any value of C<delete> option.
330  
331 =item tagstore: use project tagstore to track synced ids.
332 If no filter is set, the tagstore is used to filter known ids
333 as existing tags in the tagstore.
334  
335 =back
336  
337 =cut
338  
339 sub sync
340 3 ▶{
Subroutine sync: called CC=14 SCAR=27.6
Pod documented
341 3 my ($self, $operation, $items, %opts) = @_;
342  
343 3 ▶ if (! grep {$_ eq $operation} @SUPPORTED_OPERATIONS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $operation;} @Net::OpenStack::Client::Identity::v3::SUPPORTED_OPERATIONS) 0 3 3
344 0 $self->error("Unsupported operation $operation");
345 0 return;
346 }
347  
348 3 my $tagstore;
349 3 ▶ $tagstore = tagstore_init($self, $opts{tagstore}) if $opts{tagstore};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"tagstore"} 2 1 3
350  
351 3 my $filter;
352 3 ▶ if ($opts{filter}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'filter'}) { } 1 2 3
elsif ($tagstore) { } 2 0 2
353 1 $filter = $opts{filter};
354 1 ▶ if (ref($filter) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter ne "CODE") 0 1 1
355 0 $self->error("sync filter is not CODE");
356 0 return;
357 }
358 } elsif ($tagstore) {
359 2 ▶ $filter = sub {return $tagstore->get("ID_${operation}_".$_[0]->{id})};
Subroutine __ANON__: called CC=1 SCAR=0.0
360 } else {
361 0 ▶ $filter = sub {return 1};
Subroutine __ANON__: not called CC=1 SCAR=6.9
362 };
363  
364 # GET the list
365 3 my $resp_list = $self->api_identity_rest('GET', $operation, result => "/${operation}s");
366  
367 my $found = {
368 8 map {_make_name($operation, $_) => $_}
369 11 grep {$filter->($_)}
370 3 ▶ @{$resp_list->result || []}
Condition50%1/2
$resp_list->result || []
l!l
30
MC/DC0%0/1
$resp_list->result || []
$resp_list->result
Truth table50%1/2
A
A= $resp_list->result
Aresult
11
00
371 };
372  
373 3 my $existing = Set::Scalar->new(keys %$found);
374 3 my $wanted = Set::Scalar->new(keys %$items);
375  
376 # Add default enabled=1 to all wanted operation
377 3 foreach my $want (@$wanted) {
378 9 ▶ $items->{$want}->{enabled} = convert(1, 'boolean') if ! exists($items->{$want}->{enabled});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $items->{$want}{"enabled"} 9 0 9
379 };
380  
381 # compare
382  
383 3 my @tocreate = sort @{$wanted - $existing};
384  
385 # regions and projects can have parent relations, so they need to be sorted accordingly
386 # we only expect the order to be important with creation, not for updates or deletes
387 # the parent attr might also be the names, not the actual ids
388 # e.g. to support ordering not yet created parent
389 3 my $parentattr = $PARENT_ATTR{$operation};
390 3 ▶ @tocreate = sort_parents(\@tocreate, $items, $parentattr) if $parentattr;
Branch: 1 branch
BranchTrueFalseTotal
if $parentattr 1 2 3
391  
392 3 my $res = {
393 create => [],
394 update => [],
395 delete => [],
396 };
397  
398 3 my $postprocess;
399 3 ▶ $postprocess = sub { return tagstore_postprocess($tagstore, @_) } if ($tagstore);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $tagstore 2 1 3
400  
401 3 ▶ my $created = $self->api_identity_create($operation, \@tocreate, $items, $res, $postprocess) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $created = $self->api_identity_create($operation, \@tocreate, $items, $res, $postprocess) or return 3 0 3
402  
403 3 my @checkupdate = sort @{$wanted * $existing};
404 3 ▶ $self->api_identity_update($operation, \@checkupdate, $found, $items, $res, $postprocess) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->api_identity_update($operation, \@checkupdate, $found, $items, $res, $postprocess) or return 3 0 3
405 # no tagstore operations?
406  
407 3 my @toremove = sort @{$existing - $wanted};
408 3 ▶ $self->api_identity_delete($operation, \@toremove, $found, \%opts, $res, $postprocess) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->api_identity_delete($operation, \@toremove, $found, \%opts, $res, $postprocess) or return 3 0 3
409  
410 3 return $res;
411 }
412  
413 =item get_item
414  
415 Retrieve and augment an item with C<name> from hashref C<items>.
416  
417 Modification to the data
418  
419 =over
420  
421 =item name is inserted (unless this is an endpoint)
422  
423 =item any named ids (either from (other) operation(s) or parenting) are resolved
424 to their actual id.
425  
426 =back
427  
428 =cut
429  
430 sub get_item
431 9 ▶{
Subroutine get_item: called CC=6 SCAR=18.0
Pod documented
432 9 my ($self, $operation, $name, $items) = @_;
433  
434 9 my $new = $items->{$name};
435  
436 9 ▶ if ($operation ne 'endpoint') {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation ne "endpoint") 6 3 9
437 6 my $nameattr = _name_attribute($operation);
438 # add name
439 6 $new->{$nameattr} = $name;
440 }
441  
442 # resolve ids
443 9 my %toresolve = (map {$_."_id" => $_} @SUPPORTED_OPERATIONS);
444 # resolve parent ids
445 9 ▶ $toresolve{$PARENT_ATTR{$operation}} = $operation if $PARENT_ATTR{$operation};
Branch: 1 branch
BranchTrueFalseTotal
if $PARENT_ATTR{$operation} 3 6 9
446  
447 9 foreach my $attr (sort keys %toresolve) {
448 # no autovivification
449 84 ▶ next if ! exists($new->{$attr});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $new->{$attr} 79 5 84
450  
451 5 my $resolved = $self->api_identity_get_id($toresolve{$attr}, $new->{$attr}, error => 1);
452 5 ▶ if (defined($resolved)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resolved) { } 5 0 5
453 5 $new->{$attr} = $resolved;
454 } else {
455 0 $self->error("Failed to resolve id for $operation name $name attr $attr with value $new->{$attr}");
456 0 return;
457 }
458 }
459  
460 9 return $new;
461 }
462  
463 =item _process_response
464  
465 Helper function for all 3 sync phases
466  
467 C<res> is updated in place.
468  
469 Returns 1 on success, undef otherwise (and reports an error).
470  
471 =cut
472  
473 sub _process_response
474 10 ▶{
Subroutine _process_response: called CC=4 SCAR=14.4
475 10 my ($client, $phase, $resp, $res, $operation, $name, $postprocess) = @_;
476  
477 10 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 10 0 10
478 10 my $result = $resp->result("/$operation");
479 10 push(@{$res->{$phase}}, [$name, $result]);
480 10 $client->verbose("sync: ${phase}d $operation $name");
481 10 ▶ if ($postprocess) {
Branch: 1 branch
BranchTrueFalseTotal
if ($postprocess) 7 3 10
482 7 ▶ $postprocess->($phase, $operation, $name, $result) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$postprocess($phase, $operation, $name, $result) or return 7 0 7
483 }
484 10 return 1;
485 } else {
486 0 $client->error("sync: failed to $phase $operation $name: $resp->{error}");
487 0 return;
488 }
489 }
490  
491  
492 =item create
493  
494 Create C<operation> items in arrayref C<tocreate> from configured C<items>
495 (using name attriute C<nameattr>),
496 with result hashref C<res>. C<res> is updated in place.
497  
498 C<postprocess> is a anonymous function called after a succesful REST call,
499 and is passed following arguments:
500  
501 =over
502  
503 =item phase: one of C<create>, C<update> or C<delete>, depending on what pahse of the sync
504 the REST call is made.
505  
506 =item operation: type of operation
507  
508 =item name: name of the operation
509  
510 =item result: result of the REST call
511  
512 =back
513  
514 =cut
515  
516 sub create
517 3 ▶{
Subroutine create: called CC=5 SCAR=16.8
Pod documented
518 3 my ($self, $operation, $tocreate, $items, $res, $postprocess) = @_;
519  
520 3 my @tocreate = @$tocreate;
521  
522 3 ▶ if (@tocreate) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tocreate) { } 3 0 3
523 3 $self->info("Creating ${operation}s: @tocreate");
524 3 foreach my $name (@tocreate) {
525 # POST to create
526 5 ▶ my $new = $self->api_identity_get_item($operation, $name, $items) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $new = $self->api_identity_get_item($operation, $name, $items) or return 5 0 5
527 5 my $resp = $self->api_identity_rest('POST', $operation, data => $new);
528 5 ▶ _process_response($self, 'create', $resp, $res, $operation, $name, $postprocess) or return;
Branch: 1 branch
BranchTrueFalseTotal
_process_response($self, "create", $resp, $res, $operation, $name, $postprocess) or return 5 0 5
529 }
530 } else {
531 0 $self->verbose("No ${operation}s to create");
532 }
533  
534 3 return 1;
535 }
536  
537 =item update
538  
539 Update C<operation> items in arrayref C<checkupdate> from C<found> items
540 with configured C<items>, with result hashref C<res>.
541 C<res> is updated in place.
542  
543 =cut
544  
545 sub update
546 3 ▶{
Subroutine update: called CC=11 SCAR=24.4
Pod documented
547 3 my ($self, $operation, $checkupdate, $found, $items, $res, $postprocess) = @_;
548  
549 3 my @checkupdate = @$checkupdate;
550  
551 3 ▶ if (@checkupdate) {
Branch: 1 branch
BranchTrueFalseTotal
if (@checkupdate) { } 2 1 3
552 2 $self->info("Possibly updating existing ${operation}s: @checkupdate");
553 2 my @toupdate;
554 2 foreach my $name (@checkupdate) {
555 # anything to update?
556 4 my $update;
557 4 ▶ my $update_data = $self->api_identity_get_item($operation, $name, $items) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $update_data = $self->api_identity_get_item($operation, $name, $items) or return 4 0 4
558 4 foreach my $attr (sort keys %$update_data) {
559 16 my $wa = $update_data ->{$attr};
560 16 my $fo = $found->{$name}->{$attr};
561 16 ▶ my $action = $attr eq 'enabled' ? ($wa xor $fo): ($wa ne $fo);
Branch: 1 branch
BranchTrueFalseTotal
$attr eq 'enabled' ? : 4 12 16
Condition25%1/4
$wa xor $fo
l&&rl&&!r!l&&r!l&&!r
4000
MC/DC0%0/2
$wa xor $fo
$wa $fo
Truth table25%1/4
A xor B
A= $waB= $fo
ABresult
110
101
011
000
562 # hmmm, how to keep this JSON safe?
563 16 ▶ $update->{$attr} = $wa if $action;
Branch: 1 branch
BranchTrueFalseTotal
if $action 2 14 16
564 }
565 4 ▶ if (scalar keys %$update) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$update) 2 2 4
566 2 push(@toupdate, $name);
567 2 my $resp = $self->api_identity_rest('PATCH', $operation, what => $found->{$name}->{id}, data => $update);
568 2 ▶ _process_response($self, 'update', $resp, $res, $operation, $name, $postprocess) or return;
Branch: 1 branch
BranchTrueFalseTotal
_process_response($self, "update", $resp, $res, $operation, $name, $postprocess) or return 2 0 2
569 }
570 }
571 2 ▶ $self->info(@toupdate ? "Updated existing ${operation}s: @toupdate" : "No existing ${operation}s updated");
Branch: 1 branch
BranchTrueFalseTotal
@toupdate ? : 2 0 2
572 } else {
573 1 $self->verbose("No existing ${operation}s to update");
574 }
575  
576 3 return 1;
577 }
578  
579 =item delete
580  
581 Delete (or disable) C<operation> items in arrayref C<toremove> from C<found>
582 existing items, with options C<opts> (for C<delete> and C<ignore>)
583 and result hashref C<res>. C<res> is updated in place.
584  
585 When C<ignore> option is true, nothing will happen.
586 When C<delete> is true, items will be delete; when items will be disabled.
587  
588 =cut
589  
590 sub delete
591 3 ▶{
Subroutine delete: called CC=9 SCAR=23.3
Pod documented
592 3 my ($self, $operation, $toremove, $found, $opts, $res, $postprocess) = @_;
593  
594 3 my @toremove = @$toremove;
595  
596 3 ▶ my $dowhat = $opts->{delete} ? 'delet' : 'disabl';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'delete'} ? : 0 3 3
597  
598 3 ▶ if (@toremove) {
Branch: 1 branch
BranchTrueFalseTotal
if (@toremove) { } 3 0 3
599 3 ▶ if ($opts->{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'ignore'}) { } 0 3 3
600 0 $self->info("Ignoring existing ${operation}s (instead of ${dowhat}ing): @toremove");
601 } else {
602 3 $self->info(ucfirst($dowhat)."ing existing ${operation}s: @toremove");
603 3 foreach my $name (@toremove) {
604 4 my $resp;
605 4 ▶ if ($opts->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'delete'}) { } 0 4 4
606 # DELETE to delete
607 0 $resp = $self->api_identity_rest('DELETE', $operation, what => $found->{$name}->{id});
608 } else {
609 # PATCH to disable
610 # do not disable if already disabled
611 4 ▶ if ($found->{$name}->{enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found->{$name}{'enabled'}) { } 3 1 4
612 $resp = $self->api_identity_rest('PATCH', $operation,
613 what => $found->{$name}->{id},
614 3 data => {enabled => convert(0, 'boolean')});
615 } else {
616 $self->verbose("Not disabling already disabled ".
617 1 "$operation $name (id ".$found->{$name}->{id}.")");
618 }
619 }
620  
621 4 ▶ if (defined($resp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resp) 3 1 4
622 3 ▶ _process_response($self, 'delete', $resp, $res, $operation, $name, $postprocess) or return;
Branch: 1 branch
BranchTrueFalseTotal
_process_response($self, "delete", $resp, $res, $operation, $name, $postprocess) or return 3 0 3
623 }
624 }
625 }
626 } else {
627 0 $self->verbose("No existing ${operation}s to ${dowhat}e");
628 }
629  
630 3 return 1;
631 }
632  
633  
634 =item sync_rolemap
635  
636 Add missing roles for project/domain and group/user,
637 and delete any when tagstore is used.
638  
639 The roles are defined with a nested hashref, like
640 the url is structured (with an arrayref of roles as value).
641 E.g.
642 $roles = {
643 domain => {
644 dom1 => {
645 user => {
646 user1 => [role1 role2],
647 ...
648 },
649 group => {
650 ...
651 },
652 },
653 ...
654 project => {
655 ...
656 },
657 }
658  
659 Options
660  
661 =over
662  
663 =item tagstore: use project tagstore to track synced roles.
664  
665 =back
666  
667 =cut
668  
669  
670 sub sync_rolemap
671 1 ▶{
Subroutine sync_rolemap: called CC=15 SCAR=29.0
Pod documented
672 1 my ($self, $roles, %opts) = @_;
673  
674 # Get all roles from tagstore (if defined)
675 # The role tag is ROLE_url
676 # url is
677 # projects/{project_id} OR domains/{domain_id} +
678 # groups/{group_id} OR users/{user_id} +
679 # roles/{role_id}
680  
681 # Will use url as identifier
682  
683 1 my ($tagstore, @found);
684  
685 1 ▶ if ($opts{tagstore}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"tagstore"}) 1 0 1
686 1 ▶ $tagstore = tagstore_init($self, $opts{tagstore}) if $opts{tagstore};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"tagstore"} 1 0 1
687 # Strip ROLE_, decode/unescape the url
688 1 @found = map {my $url = $_; $url =~ s/^ROLE_//; decode_base64url($url)} grep {m/^ROLE_/} sort keys %{$tagstore->fetch};
689 };
690 1 my $existing = Set::Scalar->new(@found);
691  
692 # create hash: key is url, value is 1
693 1 my $items;
694 1 foreach my $base (qw(project domain)) {
695 2 ▶ foreach my $bval (sort keys %{$roles->{$base} || {}}) {
Condition50%1/2
$roles->{$base} || {}
l!l
20
MC/DC0%0/1
$roles->{$base} || {}
$roles->{$base}
Truth table50%1/2
A
A= $roles->{$base}
Aresult
11
00
696 2 ▶ my $bid = $self->api_identity_get_id($base, $bval, error => 1, msg => 'for role sync')
Branch: 1 branch
BranchTrueFalseTotal
my $bid = $self->api_identity_get_id($base, $bval, "error", 1, "msg", "for role sync") or return 2 0 2
697 or return;
698 2 foreach my $who (qw(user group)) {
699 4 ▶ foreach my $wval (sort keys %{$roles->{$base}->{$bval}->{$who} || {}}) {
Condition100%2/2
$roles->{$base}{$bval}{$who} || {}
l!l
22
MC/DC100%1/1
$roles->{$base}{$bval}{$who} || {}
$roles->{$base}{$bval}{$who}
Truth table100%2/2
A
A= $roles->{$base}{$bval}{$who}
Aresult
11
00
700 2 ▶ my $wid = $self->api_identity_get_id($who, $wval, error => 1, msg => 'for role sync')
Branch: 1 branch
BranchTrueFalseTotal
my $wid = $self->api_identity_get_id($who, $wval, "error", 1, "msg", "for role sync") or return 2 0 2
701 or return;
702 2 foreach my $role (@{$roles->{$base}->{$bval}->{$who}->{$wval}}) {
703 3 ▶ my $rid = $self->api_identity_get_id('role', $role, error => 1, msg => 'for role sync')
Branch: 1 branch
BranchTrueFalseTotal
my $rid = $self->api_identity_get_id("role", $role, "error", 1, "msg", "for role sync") or return 3 0 3
704 or return;
705 3 $items->{"${base}s/$bid/${who}s/$wid/roles/$rid"} = 1;
706 }
707 }
708 };
709 };
710 };
711  
712 1 my $wanted = Set::Scalar->new(keys %$items);
713  
714 2 ▶ my $rest = sub {
Subroutine __ANON__: called CC=6 SCAR=21.5
715 2 my ($urls, $method, $tagmethod) = @_;
716  
717 2 ▶ if (@$urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$urls) { } 2 0 2
718 2 $self->verbose("roles sync: going to $tagmethod @$urls");
719 } else {
720 0 $self->verbose("roles sync: nothing to $tagmethod");
721 0 return 1;
722 };
723  
724 2 foreach my $url (@$urls) {
725 3 my $resp = $self->rest(mkrequest($url, $method, version => 'v3', service => 'identity'));
726 3 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 3 0 3
727 3 ▶ if ($tagstore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagstore) 3 0 3
728 3 my $tag = "ROLE_" . encode_base64url($url);
729 3 ▶ if (!$tagstore->$tagmethod($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagstore->$tagmethod($tag)) 0 3 3
730 0 $tagstore->error("Failed to $tagmethod tag $tag to tagstore. ".
731 "See previous error where to add the tag to continue");
732 0 return;
733 }
734 }
735 } else {
736 0 $self->error("Failed to sync role $method $url");
737 0 return;
738 }
739 }
740 2 return 1
741 1 };
742  
743 # Add new ones
744 1 my @tocreate = sort @{$wanted - $existing};
745 1 ▶ $rest->(\@tocreate, 'PUT', 'add') or return;
Branch: 1 branch
BranchTrueFalseTotal
&$rest(\@tocreate, "PUT", "add") or return 1 0 1
746  
747 # Delete unknown
748 1 my @toremove = sort @{$existing - $wanted};
749 1 ▶ $rest->(\@toremove, 'DELETE', 'delete') or return;
Branch: 1 branch
BranchTrueFalseTotal
&$rest(\@toremove, "DELETE", "delete") or return 1 0 1
750  
751 1 return 1;
752 }
753  
754 =pod
755  
756 =back
757  
758 =cut
759  
760 2 1;
« Client/REST.pm ↑ Summary Client/Error.pm »