Client/Identity/Tagstore.pm

filter: Statementstmt 91.2% 114 / 125 Branchbran 70.4% 31 / 44 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 83.7% 165 / 197 CC 29 SCAR 18.1
CC
29
Cov
84%
fetch
10.5
add
8.5
delete
5.2
CRAP 6.1
« Client/Request.pm ↑ Summary Client/API.pm »
1 package Net::OpenStack::Client::Identity::Tagstore;
2 2 $Net::OpenStack::Client::Identity::Tagstore::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 parent qw(Net::OpenStack::Client::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # Maximum tags per project
11 # identity v3.10 has 80
12 # leave a few empty for possible future extensions
13 2 Readonly our $MAXTAGS => 70;
14  
15 =head1 DESCRIPTION
16  
17 Make a simple storage-like object that uses the Identity v3 interface
18 based on project tags as a backend.
19  
20 It allows you to store a list of strings, that can be encoded/decoded.
21  
22 =head1 Methods
23  
24 =over
25  
26 =item _initialize
27  
28 =over
29  
30 =item client
31  
32 The C<Net::OpenStack::Client> instance to use.
33 (It will also be used as the reporter instance.)
34  
35 =item project
36  
37 The base project (name or id) that will store the tags.
38 The project has to exist.
39  
40 All tags are stored in child projects with name C<<project_<counter>>>.
41  
42 =back
43  
44 =cut
45  
46 sub _initialize
47 3 ▶{
Subroutine _initialize: called CC=2 SCAR=7.1
48 3 my ($self, $client, $project) = @_;
49  
50 # Use the client as reporter
51 3 $self->{log} = $client;
52  
53 3 $self->{cache} = undef;
54 3 $self->{empty} = []; # list of empty child projects (they are not in the tag cache)
55 3 $self->{counter} = undef; # counter of last child project
56 3 $self->{client} = $client;
57 3 $self->{project} = $project;
58 3 $self->{id} = $self->{client}->api_identity_get_id('project', $project);
59 3 ▶ if (defined($self->{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'id'}) { } 3 0 3
60 3 $self->verbose("Tagstore for $project intialised (id $self->{id})");
61 } else {
62 0 $self->error("No tagstore project id found for project $project");
63 0 return;
64 };
65  
66 3 return 1;
67 }
68  
69 =item _tag
70  
71 Interact with the API.
72  
73 =cut
74  
75 sub _tag
76 14 ▶{
Subroutine _tag: called CC=4 SCAR=15.5
77 14 my ($self, $oper, $project_id, $tag) = @_;
78  
79 14 my $method = "api_identity_";
80 14 my %opts = (
81 project_id => $project_id,
82 );
83 14 ▶ $opts{tag} = $tag if defined($tag);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 14 0 14
84 14 ▶ if ($oper eq 'get') {
Branch: 1 branch
BranchTrueFalseTotal
if ($oper eq 'get') { } 0 14 14
85 0 ▶ $method .= 'tag' . (defined($tag) ? 's' : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $tag ? : 0 0 0
86 } else {
87 14 $method .= "${oper}_tag";
88 }
89  
90 14 return $self->{client}->$method(%opts);
91 }
92  
93 =item fetch
94  
95 Fetch data and populate the cache (and counter).
96 If the cache already exists, it doesn't do anything.
97 If you want to renew the cache, flush it first.
98  
99 =cut
100  
101 sub fetch
102 28 ▶{
Subroutine fetch: called CC=10 SCAR=23.5
Pod documented
103 28 my $self = shift;
104  
105 28 ▶ if ($self->{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cache'}) { } 24 4 28
106 24 $self->debug("fetch: tagstore cache exists, not doing anything");
107 } else {
108 4 $self->verbose("fetching tagstore data cache");
109 # gather all projects with parent_id $self->{project}
110 4 my $resp = $self->{client}->api_identity_projects(parent_id => $self->{id});
111 4 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 4 0 4
112 # get all tags for each project and add them to the cache
113 4 $self->{cache} = {};
114 4 $self->{counter} = 0; # init with 0, no child project will ever have this counter
115 4 ▶ foreach my $proj (@{$resp->result || []}) {
Condition50%1/2
$resp->result || []
l!l
40
MC/DC0%0/1
$resp->result || []
$resp->result
Truth table50%1/2
A
A= $resp->result
Aresult
11
00
116 7 ▶ if ($proj->{name} =~ m/_(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proj->{'name'} =~ /_(\d+)$/) { } 7 0 7
117 7 ▶ $self->{counter} = $1 if $1 > $self->{counter};
Branch: 1 branch
BranchTrueFalseTotal
if $1 > $self->{"counter"} 4 3 7
118 } else {
119 # this is not really a problem, as the the counter is only used
120 # to garantee uniqueness in the naming of the child projects
121 0 $self->warn("Child tagstore project of $self->{project} with name $proj->{name} ".
122 "(id $proj->{id}) does not match counter regex");
123 }
124  
125 7 ▶ my @tags = @{$proj->{tags} || []};
Condition50%1/2
$proj->{'tags'} || []
l!l
70
MC/DC0%0/1
$proj->{'tags'} || []
$proj->{'tags'}
Truth table50%1/2
A
A= $proj->{'tags'}
Aresult
11
00
126 7 ▶ if (@tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags) { } 6 1 7
127 6 foreach my $tag (@tags) {
128 285 $self->{cache}->{$tag} = $proj->{id};
129 }
130 } else {
131 # handle empty projects
132 1 push(@{$self->{empty}}, $proj->{id});
133 }
134 }
135 } else {
136 0 $self->error("Can't get all tagstore projects with parent $self->{project} (id $self->{id})");
137 }
138 }
139 28 return $self->{cache};
140 }
141  
142 =item flush
143  
144 Flushes the cache.
145  
146 =cut
147  
148 sub flush
149 2 ▶{
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
150 2 my ($self) = @_;
151  
152 2 $self->{cache} = undef;
153 2 $self->{counter} = undef;
154 2 $self->info('flushed tagstore cache and counter');
155 }
156  
157 =item get
158  
159 Return (cached) data for C<tag>.
160 If C<tag> is not defined, return all (cached) data as a hashref
161 (key is tag, value is projectid that holds the tag).
162 Data is fetched if cache is undefined.
163  
164 =cut
165  
166 sub get
167 9 ▶{
Subroutine get: called CC=3 SCAR=11.0
Pod documented
168 9 my ($self, $tag) = @_;
169  
170 9 $self->fetch();
171  
172 9 ▶ if (defined($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) { } 8 1 9
173 # no autovivification
174 8 ▶ return exists($self->{cache}->{$tag}) ? $self->{cache}->{$tag} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'cache'}{$tag} ? : 5 3 8
175 } else {
176 1 return $self->{cache};
177 };
178 }
179  
180 =item _sane_data
181  
182 Sanity check on tag data to add/delete.
183  
184 Returns 1 on success, undef on failure (and reports an error).
185  
186 =cut
187  
188 sub _sane_data
189 17 ▶{
Subroutine _sane_data: called CC=3 SCAR=11.0
190 17 my ($self, $method, $data) = @_;
191  
192 17 my $txt = "No sane tag data to $method:";
193 17 ▶ if (!defined($data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 1 16 17
194 1 $self->error("$txt undefined value");
195 1 return;
196 }
197  
198 16 my $ref = ref($data);
199 16 ▶ if ($ref ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref ne "") 1 15 16
200 1 $self->error("$txt only scalar allowed, got $ref.");
201 1 return;
202 }
203  
204 15 return 1;
205 }
206  
207 =item add
208  
209 Add element (to store and cache).
210  
211 Returns 1 on success; undef on failure (and reports an error).
212  
213 =cut
214  
215 sub add
216 9 ▶{
Subroutine add: called CC=8 SCAR=21.5
Pod documented
217 9 my ($self, $data) = @_;
218  
219 # reports an error
220 9 ▶ $self->_sane_data('add', $data) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_sane_data("add", $data) or return 9 0 9
221  
222 9 $self->fetch();
223  
224 # look for projectid that has tagspace left
225 9 my $pid = shift(@{$self->{empty}});
226 9 ▶ if (defined($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) { } 1 8 9
227 1 $self->verbose("Using first empty tagstore project id $pid");
228 } else {
229 8 my %count;
230 8 foreach my $v (values %{$self->{cache}}) {
231 325 $count{$v}++;
232 };
233 8 my @avail = (grep {$count{$_} < $MAXTAGS} sort keys %count);
234  
235 8 ▶ if (@avail) {
Branch: 1 branch
BranchTrueFalseTotal
if (@avail) { } 7 1 8
236 7 $pid = $avail[0];
237 7 $self->verbose("using existing tagstore project $pid for $data");
238 } else {
239 # make new subproject
240 1 my $counter = $self->{counter};
241 1 $counter++; # used counter is never 0
242  
243 1 my $resp = $self->{client}->api_identity_add_project(name => "$self->{project}_$counter", parent_id => $self->{id});
244 1 ▶ if ($resp && $resp->result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp and $resp->result) { } 1 0 1
Condition33%1/3
$resp and $resp->result
!ll&&!rl&&r
001
MC/DC0%0/2
$resp and $resp->result
$resp $resp->result
Truth table33%1/3
A and B
A= $respB= $resp->result
ABresult
0X0
100
111
245 1 $pid = $resp->result->{id};
246 1 $self->{counter} = $counter;
247 } else {
248 0 $self->error("Failed to create child tagstore project for counter $counter");
249 0 return;
250 }
251 }
252 }
253  
254 # add tag to project
255 9 my $resp = $self->_tag('add', $pid, $data);
256 9 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 9 0 9
257 # add tag to cache
258 9 $self->{cache}->{$data} = $pid;
259 9 $self->verbose("Added $data to tagstore");
260 } else {
261 0 $self->error("Failed to add $data to tagstore (project child id $pid)");
262 0 return;
263 }
264  
265 9 return 1;
266 }
267  
268 =item delete
269  
270 Delete item (from store and cache) if it exists in the cache.
271  
272 Returns 1 on success (incl. when the data was not available in the first place);
273 undef on failure (and reports an error).
274  
275 =cut
276  
277 sub delete
278 5 ▶{
Subroutine delete: called CC=5 SCAR=16.5
Pod documented
279 5 my ($self, $data) = @_;
280  
281 # reports an error
282 5 ▶ $self->_sane_data('delete', $data) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_sane_data("delete", $data) or return 5 0 5
283  
284 5 $self->fetch();
285  
286 5 my $pid = $self->{cache}->{$data};
287 5 ▶ if (defined($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 5 0 5
288 # delete tag from project
289 5 my $resp = $self->_tag('delete', $pid, $data);
290 5 ▶ if ($resp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 5 0 5
291 # delete tag from cache
292 5 delete $self->{cache}->{$data};
293 5 $self->verbose("deleted $data from tagstore");
294 5 ▶ if (! grep {$_ eq $pid} values %{$self->{cache}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $pid;} values %{$$self{"cache"};}) 1 4 5
295 1 push(@{$self->{empty}}, $pid);
296 }
297 } else {
298 0 $self->error("Failed to delete $data from tagstore (project child id $pid)");
299 0 return;
300 }
301 }
302  
303 5 return 1;
304 }
305  
306  
307 =back
308  
309 =cut
310  
311  
312 2 1;
« Client/Request.pm ↑ Summary Client/API.pm »