Debian Codegraph @

src/apps/automation/ansible/tower/custom/api.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::automation::ansible::tower::custom::api;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::http;
26 use JSON::XS;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = {};
31 bless $self, $class;
32
33 if (!defined($options{output})) {
34 print "Class Custom: Need to specify 'output' argument.\n";
35 exit 3;
36 }
37 if (!defined($options{options})) {
38 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
39 $options{output}->option_exit();
40 }
41
42 if (!defined($options{noptions})) {
43 $options{options}->add_options(arguments => {
44 'username:s' => { name => 'username' },
45 'password:s' => { name => 'password' },
46 'hostname:s' => { name => 'hostname' },
47 'port:s' => { name => 'port' },
48 'proto:s' => { name => 'proto' },
49 'timeout:s' => { name => 'timeout' },
50 'api-path:s' => { name => 'api_path' },
51 'unknown-http-status:s' => { name => 'unknown_http_status' },
52 'warning-http-status:s' => { name => 'warning_http_status' },
53 'critical-http-status:s' => { name => 'critical_http_status' }
54 });
55 }
56 $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
57
58 $self->{output} = $options{output};
59 $self->{http} = centreon::plugins::http->new(%options);
60
61 return $self;
62 }
63
64 sub set_options {
65 my ($self, %options) = @_;
66
67 $self->{option_results} = $options{option_results};
68 }
69
70 sub set_defaults {}
71
72 sub check_options {
73 my ($self, %options) = @_;
74
75 $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : '';
76 $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 80;
77 $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http';
78 $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
79 $self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : '';
80 $self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : '';
81 $self->{api_path} = (defined($self->{option_results}->{api_path})) ? $self->{option_results}->{api_path} : '/api/v2';
82 $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300';
83 $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : '';
84 $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : '';
85
86 if ($self->{hostname} eq '') {
87 $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option.");
88 $self->{output}->option_exit();
89 }
90 if ($self->{username} eq '') {
91 $self->{output}->add_option_msg(short_msg => "Need to specify --username option.");
92 $self->{output}->option_exit();
93 }
94 if ($self->{password} eq '') {
95 $self->{output}->add_option_msg(short_msg => "Need to specify --password option.");
96 $self->{output}->option_exit();
97 }
98
99 return 0;
100 }
101
102 sub build_options_for_httplib {
103 my ($self, %options) = @_;
104
105 $self->{option_results}->{hostname} = $self->{hostname};
106 $self->{option_results}->{timeout} = $self->{timeout};
107 $self->{option_results}->{port} = $self->{port};
108 $self->{option_results}->{proto} = $self->{proto};
109 $self->{option_results}->{timeout} = $self->{timeout};
110 $self->{option_results}->{credentials} = 1;
111 $self->{option_results}->{basic} = 1;
112 $self->{option_results}->{username} = $self->{username};
113 $self->{option_results}->{password} = $self->{password};
114 }
115
116 sub settings {
117 my ($self, %options) = @_;
118
119 return if (defined($self->{settings_done}));
120 $self->build_options_for_httplib();
121 $self->{http}->add_header(key => 'Accept', value => 'application/json');
122 $self->{http}->add_header(key => 'Content-Type', value => 'application/json');
123 $self->{http}->set_options(%{$self->{option_results}});
124 $self->{settings_done} = 1;
125 }
126
127 sub get_hostname {
128 my ($self, %options) = @_;
129
130 return $self->{hostname};
131 }
132
133 sub request_api {
134 my ($self, %options) = @_;
135
136 my $results = [];
137 my $page = 1;
138
139 $self->settings();
140 while (1) {
141 my $path = defined($options{force_endpoint}) ? $self->{api_path} . $options{force_endpoint} : $self->{api_path} . $options{endpoint} . '?page_size=100&page=' . $page;
142 my $content = $self->{http}->request(
143 method => defined($options{method}) ? $options{method} : 'GET',
144 url_path => $path,
145 query_form_post => $options{query_form_post},
146 unknown_status => $self->{unknown_http_status},
147 warning_status => $self->{warning_http_status},
148 critical_status => $self->{critical_http_status}
149 );
150
151 my $code = $self->{http}->get_code();
152 if (!defined($content) || $content eq '') {
153 $self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
154 $self->{output}->option_exit();
155 }
156
157 my $decoded;
158 eval {
159 $decoded = JSON::XS->new->utf8->decode($content);
160 };
161 if ($@) {
162 $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
163 $self->{output}->option_exit();
164 }
165
166 return $decoded if (defined($options{direct_hash}) && $options{direct_hash} == 1);
167
168 push @$results, @{$decoded->{results}};
169 last if (!defined($decoded->{next}));
170 last if (defined($options{nonext}) && $options{nonext} == 1);
171
172 $page++;
173 }
174
175 return $results;
176 }
177
178 sub tower_list_hosts {
179 my ($self, %options) = @_;
180
181 return $self->request_api(endpoint => '/hosts/');
182 }
183
184 sub tower_list_inventories {
185 my ($self, %options) = @_;
186
187 return $self->request_api(endpoint => '/inventories/');
188 }
189
190 sub tower_list_projects {
191 my ($self, %options) = @_;
192
193 return $self->request_api(endpoint => '/projects/');
194 }
195
196 sub tower_list_job_templates {
197 my ($self, %options) = @_;
198
199 return $self->request_api(endpoint => '/job_templates/');
200 }
201
202 sub tower_list_schedules {
203 my ($self, %options) = @_;
204
205 my $schedules = $self->request_api(endpoint => '/schedules/');
206 if (defined($options{add_job_status})) {
207 for (my $i = 0; $i < scalar(@$schedules); $i++) {
208 my $job = $self->request_api(
209 force_endpoint => '/schedules/' . $schedules->[$i]->{id} . '/jobs/?order_by=-id&page_size=1',
210 nonext => 1
211 );
212 $schedules->[$i]->{last_job} = $job->[0];
213 }
214 }
215
216 return $schedules;
217 }
218
219 sub tower_list_unified_jobs {
220 my ($self, %options) = @_;
221
222 return $self->request_api(endpoint => '/unified_jobs/');
223 }
224
225 sub tower_launch_job_template {
226 my ($self, %options) = @_;
227
228 my $json_request = {};
229 $json_request->{inventory} = $options{launch_inventory} if (defined($options{launch_inventory}) && $options{launch_inventory} ne '');
230 $json_request->{credential} = $options{launch_credential} if (defined($options{launch_credential}));
231 $json_request->{job_tags} = $options{launch_tags} if (defined($options{launch_tags}));
232 $json_request->{limit} = $options{launch_limit} if (defined($options{launch_limit}));
233 $json_request->{extra_vars} = $options{launch_extra_vars} if (defined($options{launch_extra_vars}));
234
235 my $encoded;
236 eval {
237 $encoded = JSON::XS->new->utf8->encode($json_request);
238 };
239 if ($@) {
240 $self->{output}->add_option_msg(short_msg => 'Cannot encode request: ' . $@);
241 $self->{output}->option_exit();
242 }
243
244 my $job = $self->request_api(
245 method => 'POST',
246 force_endpoint => '/job_templates/' . $options{launch_job_template_id} . '/launch/',
247 query_form_post => $encoded,
248 direct_hash => 1
249 );
250
251 return $job;
252 }
253
254 sub tower_get_job {
255 my ($self, %options) = @_;
256
257 return $self->request_api(force_endpoint => '/jobs/' . $options{job_id} . '/', direct_hash => 1);
258 }
259
260 1;
261
262 __END__
263
264 =head1 NAME
265
266 Ansible Tower Rest API
267
268 =head1 REST API OPTIONS
269
270 Ansible Tower Rest API
271
272 =over 8
273
274 =item B<--hostname>
275
276 Santricity hostname.
277
278 =item B<--port>
279
280 Port used (default: 80)
281
282 =item B<--proto>
283
284 Specify https if needed (default: 'http')
285
286 =item B<--username>
287
288 API username.
289
290 =item B<--password>
291
292 API password.
293
294 =item B<--api-path>
295
296 Specify api path (default: '/api/v2')
297
298 =item B<--timeout>
299
300 Set timeout in seconds (default: 10).
301
302 =back
303
304 =head1 DESCRIPTION
305
306 B<custom>.
307
308 =cut