line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Triglav::Client; |
2
|
2
|
|
|
2
|
|
2884883
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
74
|
|
3
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
57
|
|
4
|
2
|
|
|
2
|
|
10
|
use Carp qw(croak); |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
156
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
46
|
use 5.008008; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
112
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
9174
|
use URI; |
|
2
|
|
|
|
|
18164
|
|
|
2
|
|
|
|
|
60
|
|
10
|
2
|
|
|
2
|
|
2113
|
use URI::QueryParam; |
|
2
|
|
|
|
|
1857
|
|
|
2
|
|
|
|
|
48
|
|
11
|
2
|
|
|
2
|
|
2454
|
use JSON (); |
|
2
|
|
|
|
|
39206
|
|
|
2
|
|
|
|
|
50
|
|
12
|
2
|
|
|
2
|
|
2748
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
100449
|
|
|
2
|
|
|
|
|
2134
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
10
|
|
|
10
|
0
|
29048
|
my ($class, %args) = @_; |
16
|
|
|
|
|
|
|
|
17
|
10
|
100
|
66
|
|
|
470
|
croak 'Both `base_url` and `api_token` are required[u].' |
18
|
|
|
|
|
|
|
if !$args{base_url} || !$args{api_token}; |
19
|
|
|
|
|
|
|
|
20
|
7
|
|
|
|
|
58
|
bless { |
21
|
|
|
|
|
|
|
base_url => $args{base_url}, |
22
|
|
|
|
|
|
|
api_token => $args{api_token}, |
23
|
|
|
|
|
|
|
ua => LWP::UserAgent->new, |
24
|
|
|
|
|
|
|
}, $class; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
0
|
|
|
0
|
0
|
0
|
sub ua { $_[0]->{ua} } |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub services { |
30
|
1
|
|
|
1
|
0
|
355
|
my $self = shift; |
31
|
1
|
|
|
|
|
6
|
$self->dispatch_request('get', '/api/services.json'); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub roles { |
35
|
1
|
|
|
1
|
0
|
266
|
my $self = shift; |
36
|
1
|
|
|
|
|
5
|
$self->dispatch_request('get', '/api/roles.json'); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub roles_in { |
40
|
2
|
|
|
2
|
0
|
2697
|
my ($self, $service) = @_; |
41
|
2
|
100
|
|
|
|
150
|
croak '`service` is required' if !$service; |
42
|
1
|
|
|
|
|
6
|
$self->dispatch_request('get', "/api/services/${service}/roles.json"); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub hosts { |
46
|
2
|
|
|
2
|
0
|
2815
|
my ($self, %options) = @_; |
47
|
2
|
|
|
|
|
7
|
my $response = $self->dispatch_request('get', '/api/hosts.json'); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
[ |
50
|
|
|
|
|
|
|
grep { |
51
|
2
|
100
|
|
|
|
7
|
if ($options{with_inactive}) { |
|
6
|
|
|
|
|
13
|
|
52
|
3
|
|
|
|
|
6
|
1 |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else { |
55
|
3
|
|
|
|
|
7
|
$_->{active} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} @$response |
58
|
|
|
|
|
|
|
]; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub hosts_in { |
62
|
4
|
|
|
4
|
0
|
3488264
|
my ($self, $service, $role, %options) = @_; |
63
|
4
|
|
|
|
|
12
|
my $response; |
64
|
|
|
|
|
|
|
|
65
|
4
|
100
|
|
|
|
278
|
croak "`role` must be passed (even if it's not needed) when you want to pass `%options`." if @_ == 4; |
66
|
|
|
|
|
|
|
|
67
|
3
|
100
|
|
|
|
10
|
if ($role) { |
68
|
2
|
|
|
|
|
11
|
$response = $self->dispatch_request('get', "/api/services/${service}/roles/${role}/hosts.json"); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
1
|
|
|
|
|
12
|
$response = $self->dispatch_request('get', "/api/services/${service}/hosts.json"); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
[ |
75
|
|
|
|
|
|
|
grep { |
76
|
3
|
100
|
|
|
|
12
|
if ($options{with_inactive}) { |
|
9
|
|
|
|
|
22
|
|
77
|
3
|
|
|
|
|
9
|
1 |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
6
|
|
|
|
|
16
|
$_->{active} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} @$response |
83
|
|
|
|
|
|
|
]; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub dispatch_request { |
87
|
4
|
|
|
4
|
0
|
10691
|
my ($self, $method, $path, %params) = @_; |
88
|
|
|
|
|
|
|
|
89
|
4
|
100
|
66
|
|
|
245
|
croak 'Both `method` and `path` are required.' |
90
|
|
|
|
|
|
|
if !$method || !$path; |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
9
|
my $json = $self->do_request($method, $path, %params); |
93
|
1
|
|
|
|
|
15
|
JSON::decode_json($json); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub do_request { |
97
|
0
|
|
|
0
|
0
|
|
my ($self, $method, $path, %params) = @_; |
98
|
0
|
|
|
|
|
|
my $uri = URI->new($self->{base_url}); |
99
|
0
|
|
|
|
|
|
$uri->path($path); |
100
|
0
|
|
|
|
|
|
my $response; |
101
|
0
|
|
|
|
|
|
%params = (%params, api_token => $self->{api_token}); |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
if ($method eq 'get') { |
104
|
0
|
|
|
|
|
|
for my $key (keys %params) { |
105
|
0
|
|
|
|
|
|
$uri->query_param($key => $params{$key}); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$response = $self->ua->get($uri); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
else { |
111
|
0
|
|
|
|
|
|
$response = $self->ua->post($uri, \%params); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
|
if ($response->code >= 300) { |
115
|
0
|
|
|
|
|
|
die "@{[$response->code]}: @{[$response->message]}"; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
$response->content; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
!!1; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
__END__ |