REST.pm

filter: Statementstmt 35.9% 46 / 128 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 20.0% 5 / 25 Podpod 90.9% 10 / 11 totaltotal 32.1% 61 / 190 CC 11 SCAR 15.5
CC
11
Cov
31%
BUILD
12.0
__ANON__
10.1
__ANON__
10.1
CRAP 4.7
« REST/APICaller.pm ↑ Summary REST/QueryBuilder.pm »
1 package Opsview::REST;
2 {
3 9 $Opsview::REST::VERSION = '0.013';
4 }
5  
6 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Opsview::REST::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Opsview::REST::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 with 'Opsview::REST::APICaller';
12  
13 9 has [qw/ user base_url /] => (
14 is => 'ro',
15 required => 1,
16 );
17  
18 9 has [qw/ pass auth_tkt /] => (
19 is => 'ro',
20 );
21  
22 {
23 # install methods in the namespace for configurable objects
24 9 my @config_objects = qw/
25 contact host role servicecheck hosttemplate attribute timeperiod
26 hostgroup servicegroup notificationmethod hostcheckcommand keyword
27 monitoringserver
28 /;
29  
30 9 for my $obj_type (@config_objects) {
31 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 117 my $general_url = Opsview::REST::Config->new($obj_type);
34  
35 # Single object get (get_contact, get_host, ...)
36 # URL: /rest/config/{object_type}/{id}
37 # GET - get object details
38 117 ▶ *{__PACKAGE__ . "::get_$obj_type"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
39 0 my $self = shift;
40 0 my $id = shift;
41 0 ▶ croak "Required id" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
42  
43 0 my $uri = Opsview::REST::Config->new($obj_type, $id);
44 0 return $self->get($uri->as_string);
45 117 };
46  
47 # Multiple object get (get_contacts, get_hosts, ...)
48 # URL: /rest/config/{object_type}
49 # GET - list object type. Can pass in search attributes
50 117 ▶ *{__PACKAGE__ . '::get_' . $obj_type . 's'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=4.1
51 0 my $self = shift;
52 0 require JSON;
53 0 my $uri = Opsview::REST::Config->new(
54 $obj_type,
55 json_filter => JSON::encode_json({@_}),
56 );
57 0 return $self->get($uri->as_string);
58  
59 117 };
60  
61 # Create object
62 # URL: /rest/config/{object_type}
63 # POST - add a new object or a list of object type
64 117 ▶ *{__PACKAGE__ . "::create_$obj_type"} = sub {
Subroutine __ANON__: not called CC=3 SCAR=23.1
65 0 my $self = shift;
66 0 my $uri = Opsview::REST::Config->new($obj_type);
67 0 my $to_post;
68 0 ▶ if (ref $_[0] && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "ARRAY"
ref $_[0] ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
69 0 $to_post = { list => shift };
70 } else {
71 0 $to_post = { @_ };
72 }
73 0 return $self->post($uri->as_string, $to_post);
74 117 };
75  
76 # Alias to call last method in plural
77 117 *{__PACKAGE__ . "::create_${obj_type}s"} =
78 117 *{__PACKAGE__ . "::create_$obj_type"};
79  
80 # Clone object
81 # URL: /rest/config/{object_type}/{id}
82 # POST - clone this object with merged incoming data to create
83 # new object
84 117 ▶ *{__PACKAGE__ . "::clone_$obj_type"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
85 0 my $self = shift;
86 0 my $id = shift;
87 0 ▶ croak "Required id" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
88  
89 0 my $uri = Opsview::REST::Config->new($obj_type, $id);
90 0 return $self->post($uri->as_string, { @_ });
91 117 };
92  
93 # Create or update
94 # URL: /rest/config/{object_type}
95 # PUT - create or update (based on unique keys) object or a list
96 # of objects
97 117 ▶ *{__PACKAGE__ . "::create_or_update_$obj_type"} = sub {
Subroutine __ANON__: not called CC=3 SCAR=23.1
98 0 my $self = shift;
99 0 my $uri = Opsview::REST::Config->new($obj_type);
100 0 my $to_post;
101 0 ▶ if (ref $_[0] && ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and ref $_[0] eq 'ARRAY') { } 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "ARRAY"
ref $_[0] ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
102 0 $to_post = { list => shift };
103 } else {
104 0 $to_post = { @_ };
105 }
106 0 return $self->put($uri->as_string, $to_post);
107 117 };
108 # Alias to call last method in plural
109 117 *{__PACKAGE__ . "::create_or_update_${obj_type}s"} =
110 117 *{__PACKAGE__ . "::create_or_update_$obj_type"};
111  
112  
113 # Update
114 # URL: /rest/config/{object_type}/{id}
115 # PUT - update this object's details
116 117 ▶ *{__PACKAGE__ . "::update_$obj_type"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
117 0 my $self = shift;
118 0 my $id = shift;
119 0 ▶ croak "Required id" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
120  
121 0 my $uri = Opsview::REST::Config->new($obj_type, $id);
122 0 return $self->put($uri->as_string, { @_ });
123 117 };
124  
125 # Delete
126 # URL: /rest/config/{object_type}/{id}
127 # DELETE - delete object
128 117 ▶ *{__PACKAGE__ . "::delete_$obj_type"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=15.4
129 0 my $self = shift;
130 0 my $id = shift;
131 0 ▶ croak "Required id" unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
132  
133 0 my $uri = Opsview::REST::Config->new($obj_type, $id);
134 0 return $self->delete($uri->as_string, { @_ });
135 117 };
136 }
137 }
138  
139 0 ▶sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
140 0 my ($self) = @_;
141
142 0 my $r;
143 0 ▶ if (defined $self->pass) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->pass) { } 0 0 0
elsif (defined $self->auth_tkt) { } 0 0 0
144 0 $r = $self->post('/login', {
145 username => $self->user,
146 password => $self->pass,
147 });
148  
149 } elsif (defined $self->auth_tkt) {
150 0 $self->headers->{'Cookie'} = 'auth_tkt=' . $self->auth_tkt . ';';
151 0 $r = $self->post('/login_tkt', { username => $self->user });
152  
153 # Clean the cookie as this is not required anymore
154 0 delete $self->headers->{'Cookie'};
155  
156 } else {
157 0 croak "Need either a pass or an auth_tkt";
158 }
159  
160 0 $self->headers->{'X-Opsview-Username'} = $self->user;
161 0 $self->headers->{'X-Opsview-Token'} = $r->{token};
162  
163 }
164  
165 # Status
166 0 ▶sub status {
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
167 0 my $self = shift;
168  
169 0 require Opsview::REST::Status;
170 0 my $uri = Opsview::REST::Status->new(@_);
171  
172 0 return $self->get($uri->as_string);
173 }
174  
175 # Event
176 0 ▶sub events {
Subroutine events: not called CC=1 SCAR=6.9
Pod documented
177 0 my $self = shift;
178  
179 0 require Opsview::REST::Event;
180 0 my $uri = Opsview::REST::Event->new(@_);
181  
182 0 return $self->get($uri->as_string);
183 }
184  
185 # Downtime
186 0 ▶sub _downtime {
Subroutine _downtime: not called CC=1 SCAR=6.9
187 0 my $self = shift;
188  
189 0 require Opsview::REST::Downtime;
190 0 my $uri = Opsview::REST::Downtime->new(@_);
191  
192 0 return $uri->as_string;
193 }
194  
195 0 ▶sub downtimes {
Subroutine downtimes: not called CC=1 SCAR=6.9
Pod documented
196 0 my $self = shift;
197 0 return $self->get($self->_downtime(@_));
198 }
199  
200 0 ▶sub create_downtime {
Subroutine create_downtime: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202 0 return $self->post($self->_downtime(@_));
203 }
204  
205 0 ▶sub delete_downtime {
Subroutine delete_downtime: not called CC=1 SCAR=6.9
Pod documented
206 0 my $self = shift;
207 0 return $self->delete($self->_downtime(@_));
208 }
209  
210 # Reload
211 0 ▶sub reload {
Subroutine reload: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213 0 return $self->post('/reload');
214 }
215  
216 0 ▶sub reload_info {
Subroutine reload_info: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 return $self->get('/reload');
219 }
220  
221 # Acknowledge
222 0 ▶sub _ack {
Subroutine _ack: not called CC=1 SCAR=6.9
223 0 my $self = shift;
224  
225 0 require Opsview::REST::Acknowledge;
226 0 my $uri = Opsview::REST::Acknowledge->new(@_);
227  
228 0 return $uri->as_string;
229 }
230  
231 0 ▶sub acknowledge_list {
Subroutine acknowledge_list: not called CC=1 SCAR=6.9
Pod documented
232 0 my $self = shift;
233 0 return $self->get($self->_ack(@_));
234 }
235  
236 0 ▶sub acknowledge {
Subroutine acknowledge: not called CC=1 SCAR=6.9
Pod documented
237 0 my $self = shift;
238 0 return $self->post($self->_ack(@_));
239 }
240  
241 # Recheck
242 0 ▶sub recheck {
Subroutine recheck: not called CC=1 SCAR=6.9
Pod documented
243 0 my $self = shift;
244  
245 0 require Opsview::REST::Recheck;
246 0 my $uri = Opsview::REST::Recheck->new(@_);
247  
248 0 return $self->post($uri->as_string);
249 }
250  
251 9 __PACKAGE__->meta->make_immutable;
252  
253 9 1;
254 __END__
« REST/APICaller.pm ↑ Summary REST/QueryBuilder.pm »