File Coverage

blib/lib/Opsview/REST.pm
Criterion Covered Total %
statement 15 97 15.4
branch 0 16 0.0
condition 0 6 0.0
subroutine 5 25 20.0
pod 10 11 90.9
total 30 155 19.3


line stmt bran cond sub pod time code
1             package Opsview::REST;
2             {
3             $Opsview::REST::VERSION = '0.013';
4             }
5              
6 9     9   89328 use Moo;
  9         194111  
  9         57  
7 9     9   16929 use Carp;
  9         23  
  9         671  
8 9     9   5473 use Opsview::REST::Config;
  9         34  
  9         365  
9 9     9   5567 use Opsview::REST::Exception;
  9         25  
  9         992  
10              
11             with 'Opsview::REST::APICaller';
12              
13             has [qw/ user base_url /] => (
14             is => 'ro',
15             required => 1,
16             );
17              
18             has [qw/ pass auth_tkt /] => (
19             is => 'ro',
20             );
21              
22             {
23             # install methods in the namespace for configurable objects
24             my @config_objects = qw/
25             contact host role servicecheck hosttemplate attribute timeperiod
26             hostgroup servicegroup notificationmethod hostcheckcommand keyword
27             monitoringserver
28             /;
29              
30             for my $obj_type (@config_objects) {
31 9     9   60 no strict 'refs';
  9         17  
  9         15669  
32              
33             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             *{__PACKAGE__ . "::get_$obj_type"} = sub {
39 0     0     my $self = shift;
40 0           my $id = shift;
41 0 0         croak "Required id" unless defined $id;
42              
43 0           my $uri = Opsview::REST::Config->new($obj_type, $id);
44 0           return $self->get($uri->as_string);
45             };
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             *{__PACKAGE__ . '::get_' . $obj_type . 's'} = sub {
51 0     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             };
60              
61             # Create object
62             # URL: /rest/config/{object_type}
63             # POST - add a new object or a list of object type
64             *{__PACKAGE__ . "::create_$obj_type"} = sub {
65 0     0     my $self = shift;
66 0           my $uri = Opsview::REST::Config->new($obj_type);
67 0           my $to_post;
68 0 0 0       if (ref $_[0] && ref $_[0] eq 'ARRAY') {
69 0           $to_post = { list => shift };
70             } else {
71 0           $to_post = { @_ };
72             }
73 0           return $self->post($uri->as_string, $to_post);
74             };
75              
76             # Alias to call last method in plural
77             *{__PACKAGE__ . "::create_${obj_type}s"} =
78             *{__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             *{__PACKAGE__ . "::clone_$obj_type"} = sub {
85 0     0     my $self = shift;
86 0           my $id = shift;
87 0 0         croak "Required id" unless defined $id;
88              
89 0           my $uri = Opsview::REST::Config->new($obj_type, $id);
90 0           return $self->post($uri->as_string, { @_ });
91             };
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             *{__PACKAGE__ . "::create_or_update_$obj_type"} = sub {
98 0     0     my $self = shift;
99 0           my $uri = Opsview::REST::Config->new($obj_type);
100 0           my $to_post;
101 0 0 0       if (ref $_[0] && ref $_[0] eq 'ARRAY') {
102 0           $to_post = { list => shift };
103             } else {
104 0           $to_post = { @_ };
105             }
106 0           return $self->put($uri->as_string, $to_post);
107             };
108             # Alias to call last method in plural
109             *{__PACKAGE__ . "::create_or_update_${obj_type}s"} =
110             *{__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             *{__PACKAGE__ . "::update_$obj_type"} = sub {
117 0     0     my $self = shift;
118 0           my $id = shift;
119 0 0         croak "Required id" unless defined $id;
120              
121 0           my $uri = Opsview::REST::Config->new($obj_type, $id);
122 0           return $self->put($uri->as_string, { @_ });
123             };
124              
125             # Delete
126             # URL: /rest/config/{object_type}/{id}
127             # DELETE - delete object
128             *{__PACKAGE__ . "::delete_$obj_type"} = sub {
129 0     0     my $self = shift;
130 0           my $id = shift;
131 0 0         croak "Required id" unless defined $id;
132              
133 0           my $uri = Opsview::REST::Config->new($obj_type, $id);
134 0           return $self->delete($uri->as_string, { @_ });
135             };
136             }
137             }
138              
139             sub BUILD {
140 0     0 0   my ($self) = @_;
141            
142 0           my $r;
143 0 0         if (defined $self->pass) {
    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             sub status {
167 0     0 1   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             sub events {
177 0     0 1   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             sub _downtime {
187 0     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             sub downtimes {
196 0     0 1   my $self = shift;
197 0           return $self->get($self->_downtime(@_));
198             }
199              
200             sub create_downtime {
201 0     0 1   my $self = shift;
202 0           return $self->post($self->_downtime(@_));
203             }
204              
205             sub delete_downtime {
206 0     0 1   my $self = shift;
207 0           return $self->delete($self->_downtime(@_));
208             }
209              
210             # Reload
211             sub reload {
212 0     0 1   my $self = shift;
213 0           return $self->post('/reload');
214             }
215              
216             sub reload_info {
217 0     0 1   my $self = shift;
218 0           return $self->get('/reload');
219             }
220              
221             # Acknowledge
222             sub _ack {
223 0     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             sub acknowledge_list {
232 0     0 1   my $self = shift;
233 0           return $self->get($self->_ack(@_));
234             }
235              
236             sub acknowledge {
237 0     0 1   my $self = shift;
238 0           return $self->post($self->_ack(@_));
239             }
240              
241             # Recheck
242             sub recheck {
243 0     0 1   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             __PACKAGE__->meta->make_immutable;
252              
253             1;
254             __END__