File Coverage

blib/lib/Tapper/Cmd/Host.pm
Criterion Covered Total %
statement 22 54 40.7
branch 5 22 22.7
condition n/a
subroutine 6 9 66.6
pod 4 4 100.0
total 37 89 41.5


line stmt bran cond sub pod time code
1             package Tapper::Cmd::Host;
2             our $AUTHORITY = 'cpan:TAPPER';
3             $Tapper::Cmd::Host::VERSION = '5.0.11';
4 1     1   5592655 use Moose;
  1         513866  
  1         9  
5              
6 1     1   8158 use Tapper::Model 'model';
  1         4227  
  1         64  
7 1     1   7 use YAML::Syck;
  1         3  
  1         53  
8 1     1   20 use 5.010;
  1         3  
9              
10 1     1   6 use parent 'Tapper::Cmd';
  1         2  
  1         5  
11              
12              
13              
14             sub add
15             {
16 2     2 1 18566 my ($self, $data) = @_;
17 2 50       10 if (not ref($data) eq 'HASH') {
18 0         0 die "Wrong data format, expected hash ref, got ",ref $data;
19             }
20 2 100       45 if ($data->{pool_count}) {
21 1 50       18 die "pool_count can not go together with pool_free" if $data->{pool_free};
22 0         0 $data->{pool_free} = $data->{pool_count};
23             }
24 1         5 my $host_r = Tapper::Model::model()->resultset('Host')->search({name => $data->{name}}, {rows => 1})->first;
25 1 50       12278 if ($host_r) {
26 0         0 $host_r->is_deleted(0);
27 0         0 $host_r->update;
28             } else {
29 1         111 $host_r = model('TestrunDB')->resultset('Host')->new($data)->insert;
30             }
31 1         15401 return $host_r->id;
32              
33             }
34              
35              
36              
37             sub del
38             {
39 0     0 1   my ($self, $id) = @_;
40 0           my $host;
41 0 0         if ($id =~ /^\d+$/) {
42 0           $host = model('TestrunDB')->resultset('Host')->find($id);
43             } else {
44 0           $host = model('TestrunDB')->resultset('Host')->find({name => $id});
45             }
46 0 0         die qq(Host "$id" not found) if not $host;;
47 0           $host->is_deleted(1);
48 0           $host->active(0);
49 0           $host->free(0);
50 0           $host->update;
51 0           return 0;
52             }
53              
54              
55             sub update
56       0 1   {
57             }
58              
59              
60             sub list
61             {
62 0     0 1   my ($self, $filter, $order) = @_;
63 0           my %options= (order_by => 'name');
64 0           my %search;
65              
66 0           for my $key ('free', 'active') {
67 0 0         $search{$key} = $filter->{$key} if defined $filter->{$key};
68             }
69 0 0         $search{is_deleted} = {-in => [ 0, undef ] } unless $filter->{deleted};
70 0 0         $search{is_pool} = { not => undef } if $filter->{pool};
71              
72             # ignore all filterions if host is requested by name
73 0 0         %search = (name => $filter->{name}) if $filter->{name};
74              
75 0 0         if ($filter->{queue}) {
76 0           my @queue_ids = map {$_->id} model('TestrunDB')->resultset('Queue')->search({name => {-in => $filter->{queue}}});
  0            
77 0           $search{queue_id} = { -in => [ @queue_ids ]};
78 0           $options{join} = 'queuehosts';
79 0           $options{'+select'} = 'queuehosts.queue_id';
80 0           $options{'+as'} = 'queue_id';
81             }
82 0           my $hosts = model('TestrunDB')->resultset('Host')->search(\%search, \%options);
83 0           return $hosts;
84             }
85              
86             1; # End of Tapper::Cmd::Testrun
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             Tapper::Cmd::Host
97              
98             =head1 SYNOPSIS
99              
100             This project offers backend functions for all projects that manipulate
101             hosts in the database.
102              
103             use Tapper::Cmd::Host;
104              
105             my $host = Tapper::Cmd::Host->new();
106             my $details = { name => 'einstein',
107             queue => ['queue-one', 'queue-two'],
108             active => 1,
109             };
110             my $id = $host->add($details);
111             $details->{name} = "bohr";
112             my $error = $host->update($id, $details);
113             $error = $host->delete($id);
114              
115             =head1 NAME
116              
117             Tapper::Cmd::Host - Backend functions for manipluation of hosts in the database
118              
119             =head1 FUNCTIONS
120              
121             =head2 add
122              
123             Add a new host. Expects all details as a hash reference.
124              
125             @param hash ref - host data
126             * name - host name
127             * comment - comment for host
128             * free - is the host free to have tests running on it?
129             * active - is host activated?
130             * is_deleted - is host deleted?
131             * pool_free - set number of pool elements, not allowed together with pool_count
132             * pool_count - set number of pool elements, not allowed together with pool_free
133              
134             @return success - host id
135             @return error - undef
136              
137             @throws die()
138              
139             =head2 del
140              
141             Delete a host with given id. Its named del instead of delete to prevent
142             confusion with the buildin delete function. The first parameter can be
143             either the host id or the host name.
144              
145             @param int|string - host id|host name
146              
147             @return success - 0
148              
149             @throws die
150              
151             =head2 update
152              
153             Update a given host with new data.
154              
155             =head2 list
156              
157             Get a filtered list of hosts.
158              
159             @param hash ref - filters
160             allowed keys:
161             * free - bool - filter for free/non-free hosts
162             * name - list of strings - filter for host names
163             * active - bool - filter for active/non-active hosts
164             * queue - list of strings - filter for hosts bound to this queue
165             * pool - bool - filter for pool hosts
166             * deleted - bool - allowed deleted hosts too
167              
168             @return success - Host resultset - DBIx::Class list of hosts
169              
170             @throws die
171              
172             =head1 AUTHORS
173              
174             =over 4
175              
176             =item *
177              
178             AMD OSRC Tapper Team <tapper@amd64.org>
179              
180             =item *
181              
182             Tapper Team <tapper-ops@amazon.com>
183              
184             =back
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             This software is Copyright (c) 2020 by Advanced Micro Devices, Inc..
189              
190             This is free software, licensed under:
191              
192             The (two-clause) FreeBSD License
193              
194             =cut