File Coverage

blib/lib/Metabrik/Api/Splunk.pm
Criterion Covered Total %
statement 9 89 10.1
branch 0 40 0.0
condition 0 14 0.0
subroutine 3 16 18.7
pod 1 13 7.6
total 13 172 7.5


line stmt bran cond sub pod time code
1             #
2             # $Id$
3             #
4             # api::splunk Brik
5             #
6             package Metabrik::Api::Splunk;
7 2     2   686 use strict;
  2         4  
  2         58  
8 2     2   9 use warnings;
  2         4  
  2         54  
9              
10 2     2   10 use base qw(Metabrik::Client::Rest);
  2         3  
  2         2626  
11              
12             sub brik_properties {
13             return {
14 0     0 1   revision => '$Revision$',
15             tags => [ qw(unstable rest) ],
16             author => 'GomoR ',
17             license => 'http://opensource.org/licenses/BSD-3-Clause',
18             attributes => {
19             uri => [ qw(uri) ], # Inherited
20             username => [ qw(username) ], # Inherited
21             password => [ qw(password) ], # Inherited
22             ssl_verify => [ qw(0|1) ], # Inherited
23             output_mode => [ qw(json|xml|csv) ],
24             count => [ qw(number) ],
25             offset => [ qw(number) ],
26             },
27             attributes_default => {
28             uri => 'https://localhost:8089',
29             username => 'admin',
30             ssl_verify => 0,
31             output_mode => 'json',
32             count => 1000, # 0 means return everything
33             offset => 0, # 0 means return everything
34             },
35             commands => {
36             get => [ qw(path) ],
37             reset_user_agent => [ ], # Inherited
38             apps_local => [ ],
39             search_jobs => [ qw(search) ],
40             search_jobs_sid => [ qw(sid) ],
41             search_jobs_sid_results => [ qw(sid count|OPTIONAL offset|OPTIONAL) ],
42             licenser_groups => [ ],
43             data_lookup_table_files_acl => [ qw(app csv_file perm|OPTIONAL) ],
44             cluster_config => [ ],
45             cluster_config_config => [ ],
46             deployment_client => [ ],
47             deployment_client_config => [ ],
48             search_data_lookuptablefiles => [ qw(username|OPTIONAL) ],
49             },
50             };
51             }
52              
53             #
54             # API reference:
55             # http://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTlist
56             #
57              
58             sub get {
59 0     0 0   my $self = shift;
60 0           my ($path, $count) = @_;
61              
62 0   0       $count ||= $self->count;
63 0 0         $self->brik_help_run_undef_arg('get', $path) or return;
64              
65 0           my $uri = $self->uri;
66              
67 0 0         my $resp = $self->SUPER::get($uri.$path.'?count='.$count) or return;
68              
69 0 0         my $content = $self->content('xml') or return;
70 0 0         my $code = $self->code or return;
71              
72 0           $self->log->verbose("get: returned code [$code]");
73              
74 0           return $content;
75             }
76              
77             sub apps_local {
78 0     0 0   my $self = shift;
79              
80 0           return $self->get('/services/apps/local');
81             }
82              
83             #
84             # run api::splunk search_jobs "{ search => 'search index=main' }" https://localhost:8089
85             #
86             sub search_jobs {
87 0     0 0   my $self = shift;
88 0           my ($post) = @_;
89              
90 0 0         $self->brik_help_run_undef_arg('search_jobs', $post) or return;
91 0 0         $self->brik_help_run_invalid_arg('search_jobs', $post, 'HASH') or return;
92              
93 0           my $uri = $self->uri;
94              
95 0 0         my $resp = $self->post($post, $uri.'/services/search/jobs') or return;
96              
97 0           my $code = $self->code;
98              
99 0           $self->log->verbose("search_jobs: returned code [$code]");
100 0           $self->log->debug("search_jobs: content [".$resp->{content}."]");
101              
102 0 0         if ($code == 201) { # Job created
103 0           return $self->content('xml');
104             }
105              
106 0           return $self->log->error("search_jobs: failed with code [$code]");
107             }
108              
109             sub search_jobs_sid {
110 0     0 0   my $self = shift;
111 0           my ($sid) = @_;
112              
113 0 0         $self->brik_help_run_undef_arg('search_jobs_sid', $sid) or return;
114              
115 0           my $uri = $self->uri;
116              
117 0 0         my $resp = $self->SUPER::get($uri.'/services/search/jobs/'.$sid) or return;
118              
119 0           my $code = $self->code;
120              
121 0           $self->log->verbose("search_jobs_sid: returned code [$code]");
122 0           $self->log->debug("search_jobs_sid: content [".$resp->{content}."]");
123              
124 0 0         if ($code == 404) {
    0          
125 0           return 0;
126             }
127             elsif ($code == 200) {
128 0           return $self->content('xml');
129             }
130              
131 0           return $self->log->error("search_jobs_sid: failed with code [$code]");
132             }
133              
134             #
135             # http://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTsearch#search.2Fjobs.2F.7Bsearch_id.7D.2Fresults
136             #
137             sub search_jobs_sid_results {
138 0     0 0   my $self = shift;
139 0           my ($sid, $count, $offset) = @_;
140              
141 0   0       $count ||= $self->count;
142 0   0       $offset ||= $self->offset;
143 0 0         $self->brik_help_run_undef_arg('search_jobs_sid_results', $sid) or return;
144              
145 0           my $uri = $self->uri;
146 0           my $output_mode = $self->output_mode;
147              
148 0 0         my $resp = $self->SUPER::get(
149             $uri.'/services/search/jobs/'.$sid.
150             '/results/?output_mode='.$output_mode."&offset=$offset&count=$count"
151             ) or return;
152              
153 0           my $code = $self->code;
154              
155 0           $self->log->verbose("search_jobs_sid_results: returned code [$code]");
156 0           $self->log->debug("search_jobs_sid_results: content [".$resp->{content}."]");
157              
158 0 0         if ($code == 200) { # Job finished
    0          
159 0           return $self->content($output_mode);
160             }
161             elsif ($code == 204) { # Job not finished
162 0           return $self->log->error("search_jobs_sid_results: job not done");
163             }
164              
165 0           return $self->log->error("search_jobs_sid_results: failed with code [$code]");
166             }
167              
168             #
169             # http://docs.splunk.com/Documentation/Splunk/6.1.3/RESTAPI/RESTlicense
170             #
171             sub licenser_groups {
172 0     0 0   my $self = shift;
173              
174 0           return $self->get('/services/licenser/groups');
175             }
176              
177             #
178             # curl -k -u admin https://localhost:8089/servicesNS/admin/$APP/data/lookup-table-files/$FILE.csv/acl -d owner=nobody -d sharing=global
179             #
180             # http://docs.splunk.com/Documentation/Splunk/6.1.3/RESTAPI/RESTknowledge
181             #
182             sub data_lookup_table_files_acl {
183 0     0 0   my $self = shift;
184 0           my ($app, $csv_file, $perm) = @_;
185              
186 0   0       $perm ||= { owner => 'nobody', sharing => 'global' };
187 0 0         $self->brik_help_run_undef_arg('data_lookup_table_files_acl', $app) or return;
188 0 0         $self->brik_help_run_undef_arg('data_lookup_table_files_acl', $csv_file) or return;
189              
190 0           my $uri = $self->uri;
191 0           my $username = $self->username;
192              
193 0 0         if ($csv_file !~ m{\.csv$}) {
194 0           return $self->log->error("data_lookup_table_files_acl: csv file [$csv_file] must ends with .csv extension");
195             }
196              
197 0 0         my $resp = $self->post(
198             $perm, $uri.'/servicesNS/'.$username.'/'.$app.'/data/lookup-table-files/'.$csv_file.'/acl'
199             ) or return;
200              
201 0           my $code = $self->code;
202              
203 0           $self->log->verbose("data_lookup_table_files_acl: returned code [$code]");
204 0           $self->log->debug("data_lookup_table_files_acl: content [".$resp->{content}."]");
205              
206 0           return $self->content('xml');
207             }
208              
209             sub cluser_config {
210 0     0 0   my $self = shift;
211              
212 0           return $self->get('/services/cluster/config');
213             }
214              
215             sub cluser_config_config {
216 0     0 0   my $self = shift;
217              
218 0           return $self->get('/services/cluster/config/config');
219             }
220              
221             sub deployment_client {
222 0     0 0   my $self = shift;
223              
224 0           return $self->get('/services/deployment/client');
225             }
226              
227             sub deployment_client_config {
228 0     0 0   my $self = shift;
229              
230 0           return $self->get('/services/deployment/client/config');
231             }
232              
233             sub search_data_lookuptablefiles {
234 0     0 0   my $self = shift;
235 0           my ($username) = @_;
236              
237 0   0       $username ||= $self->username;
238              
239 0           return $self->get('/servicesNS/'.$username.'/search/data/lookup-table-files');
240             }
241              
242             1;
243              
244             __END__