line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::PagerDuty; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
BEGIN { |
4
|
2
|
|
|
2
|
|
40550
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
5
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
64
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
5
|
local $@; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
8
|
use Exporter; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
82
|
|
10
|
2
|
|
|
2
|
|
1995
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
120808
|
|
|
2
|
|
|
|
|
97
|
|
11
|
2
|
|
|
2
|
|
2902
|
use JSON; |
|
2
|
|
|
|
|
30066
|
|
|
2
|
|
|
|
|
13
|
|
12
|
|
|
|
|
|
|
|
13
|
2
|
|
|
|
|
24
|
our @ISA = qw(Exporter); |
14
|
2
|
|
|
|
|
6
|
our @EXPORT_OK = qw(new trigger resolve); |
15
|
|
|
|
|
|
|
|
16
|
2
|
|
|
2
|
|
454
|
use constant SERVICE_URL => "https://events.pagerduty.com/generic/2010-04-15/create_event.json"; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
115
|
|
17
|
2
|
|
|
2
|
|
11
|
use constant TRIGGER_KEY => 'trigger'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
92
|
|
18
|
2
|
|
|
2
|
|
12
|
use constant RESOLVE_KEY => 'resolve'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
110
|
|
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
2106
|
our $VERSION = 0.2; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub error($$) { |
24
|
1
|
|
|
1
|
0
|
3
|
my ($self, $errstr) = @_; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
|
|
2
|
my $result = { errors => [] }; |
27
|
|
|
|
|
|
|
|
28
|
1
|
|
|
|
|
3
|
push (@{$result->{errors}}, $errstr); |
|
1
|
|
|
|
|
2
|
|
29
|
|
|
|
|
|
|
|
30
|
1
|
|
|
|
|
4
|
return $result; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub new($$) { |
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
2
|
0
|
20
|
my ($self, $params) = @_; |
36
|
|
|
|
|
|
|
|
37
|
2
|
100
|
66
|
|
|
16
|
return $self->error('Error: not enough parameters provided') if (!(defined $params) or ref $params ne 'HASH'); |
38
|
|
|
|
|
|
|
|
39
|
1
|
50
|
|
|
|
3
|
return $self->error("Error: $@") if $@; |
40
|
|
|
|
|
|
|
|
41
|
1
|
50
|
|
|
|
3
|
return $self->error('Error: service_key *must* be defined') if (!defined $params->{service_key}); |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
2
|
my $lwp = undef; |
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
|
|
53
|
eval '$lwp = LWP::UserAgent->new()'; |
46
|
|
|
|
|
|
|
|
47
|
1
|
50
|
|
|
|
2814
|
return $self->error('Error: could not instantiate LWP Object') if $@; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$self = bless { |
50
|
|
|
|
|
|
|
url => SERVICE_URL, |
51
|
|
|
|
|
|
|
user_agent => $lwp, |
52
|
|
|
|
|
|
|
trigger_key => TRIGGER_KEY, |
53
|
|
|
|
|
|
|
resolve_key => RESOLVE_KEY, |
54
|
|
|
|
|
|
|
service_key => $params->{service_key}, |
55
|
|
|
|
|
|
|
incident_key => $params->{incident_key} |
56
|
1
|
|
|
|
|
10
|
}, "WWW::PagerDuty"; |
57
|
|
|
|
|
|
|
|
58
|
1
|
|
|
|
|
3
|
return $self; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub trigger($$) { |
63
|
0
|
|
|
0
|
0
|
|
my ($self, $params) = @_; |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
0
|
|
|
|
return $self->error('Error: not called correctly') if (!(defined $self) or ref $self ne 'WWW::PagerDuty'); |
66
|
|
|
|
|
|
|
|
67
|
0
|
0
|
0
|
|
|
|
return $self->error('Error: parameters not passed') if (!(defined $params) or ref $params ne 'HASH'); |
68
|
|
|
|
|
|
|
|
69
|
0
|
0
|
0
|
|
|
|
return $self->error('Error: incident_key *must* be passed') if (!(defined $params->{incident_key}) and !(defined $self->{incident_key})); |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
return $self->error('Error: description *must* be passed') if !(defined $params->{description}); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $details = $params->{details}; |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
0
|
|
|
|
if (defined $params->{details} && ref $params->{details} eq 'SCALAR') { |
|
|
0
|
0
|
|
|
|
|
76
|
|
|
|
|
|
|
$details = { |
77
|
|
|
|
|
|
|
'additional_info' => $params->{details} |
78
|
0
|
|
|
|
|
|
}; |
79
|
|
|
|
|
|
|
} elsif (defined $params->{details} && ref $params->{details} eq 'HASH') { |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} else { |
82
|
0
|
|
|
|
|
|
$details = undef; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $request_body = { |
86
|
|
|
|
|
|
|
'service_key' => $self->{service_key}, |
87
|
|
|
|
|
|
|
'incident_key' => (defined $params->{incident_key}) ? $params->{incident_key} : $self->{incident_key}, |
88
|
|
|
|
|
|
|
'event_type' => $self->{trigger_key}, |
89
|
|
|
|
|
|
|
'description' => $params->{description}, |
90
|
0
|
0
|
|
|
|
|
'details' => $details |
91
|
|
|
|
|
|
|
}; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $return_body = undef; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
local $@; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my $data = undef; |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$data = eval { JSON::encode_json($request_body); }; |
|
0
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
if ($data) { |
|
|
0
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$return_body = eval { $self->{user_agent}->post($self->{url}, Content_Type => 'application/json', Content => $data); }; |
|
0
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
0
|
0
|
|
|
|
|
if (defined $return_body) { |
|
|
0
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
my $result = JSON::decode_json($return_body->decoded_content); |
107
|
0
|
0
|
0
|
|
|
|
if (defined $result && ref $result eq 'HASH') { |
108
|
0
|
0
|
|
|
|
|
if ($@) { |
109
|
0
|
0
|
0
|
|
|
|
if (defined $result->{errors} && ref $result->{errors} eq 'ARRAY') { |
110
|
0
|
|
|
|
|
|
push(@{$result->{errors}}, $@); |
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
|
return $result; |
114
|
|
|
|
|
|
|
} else { |
115
|
0
|
|
|
|
|
|
return $return_body; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} elsif($@) { |
118
|
0
|
|
|
|
|
|
return $self->error("Error: $@"); |
119
|
|
|
|
|
|
|
} else { |
120
|
0
|
|
|
|
|
|
return $self->error('Error: response body was not returned'); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
} elsif ($@) { |
124
|
0
|
|
|
|
|
|
return $self->error("Error: $@"); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
return undef; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub resolve($$) { |
132
|
0
|
|
|
0
|
0
|
|
my ($self, $params) = @_; |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
0
|
|
|
|
return WWW::PagerDuty::error(undef, "Error: calling convention not followed") if !(defined $self) and ref $self ne 'WWW::PagerDuty'; |
135
|
0
|
0
|
0
|
|
|
|
return $self->error("Error: no parameters passed") if (!(defined $params) or ref $params ne 'HASH' ); |
136
|
0
|
0
|
|
|
|
|
return $self->error("Error: not initialized correctly, service_key *must* be defined") if !(defined $self->{service_key}); |
137
|
0
|
0
|
0
|
|
|
|
return $self->error("Error: incident_key *must* be passed") if !($self->{incident_key}) and !($params->{incident_key}); |
138
|
0
|
0
|
|
|
|
|
return $self->error("Error: description *must* be defined") if !($params->{description}); |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $details = $params->{details}; |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
if (defined $details) { |
143
|
0
|
0
|
|
|
|
|
if (ref $details eq 'SCALAR') { |
|
|
0
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
$details = { |
145
|
|
|
|
|
|
|
additional_information => $details |
146
|
|
|
|
|
|
|
}; |
147
|
|
|
|
|
|
|
} elsif (ref $details eq 'HASH') { |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
} else { |
150
|
0
|
|
|
|
|
|
$details = undef; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $request_body = { |
155
|
|
|
|
|
|
|
service_key => (defined $params->{service_key}) ? $params->{service_key} : $self->{service_key}, |
156
|
|
|
|
|
|
|
incident_key => (defined $params->{service_key}) ? $params->{incident_key} : $self->{incident_key}, |
157
|
|
|
|
|
|
|
event_type => $self->{resolve_key}, |
158
|
|
|
|
|
|
|
description => $params->{details}, |
159
|
0
|
0
|
|
|
|
|
details => $details |
|
|
0
|
|
|
|
|
|
160
|
|
|
|
|
|
|
}; |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
|
|
|
|
my $request_body_json = undef; |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
local $@; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
$request_body_json = eval { JSON::encode_json($request_body); }; |
|
0
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
0
|
|
|
|
if (defined $request_body_json && $request_body_json && !$@) { |
|
|
0
|
0
|
|
|
|
|
169
|
0
|
|
|
|
|
|
my $http_response_body = undef; |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
$http_response_body = eval { $self->{user_agent}->post($self->{url}, Content_Type => 'application/json', Content => $request_body_json); }; |
|
0
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
|
173
|
0
|
0
|
0
|
|
|
|
if (defined $http_response_body && !$@) { |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
my $http_response = undef; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
$http_response = eval { JSON::decode_json($http_response_body->decoded_content); }; |
|
0
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
0
|
0
|
|
|
|
|
if (defined $http_response) { |
179
|
0
|
0
|
|
|
|
|
if ($@) { |
180
|
0
|
0
|
0
|
|
|
|
if (ref $http_response eq 'HASH' && defined $http_response->{errors} && ref $http_response->{errors} eq 'ARRAY') { |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
181
|
0
|
|
|
|
|
|
push(@{$http_response->{errors}}, $@); |
|
0
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
} elsif (ref $http_response eq 'HASH' && !defined $http_response->{errors}) { |
183
|
0
|
|
|
|
|
|
$http_response->{errors} = []; |
184
|
0
|
|
|
|
|
|
push(@{$http_response->{errors}}, $@); |
|
0
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
0
|
|
|
|
|
|
return $http_response; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
} elsif (defined $http_response_body && $@) { |
191
|
0
|
|
|
|
|
|
return { errors => ["Error: $@"], http_response_body => $http_response_body }; |
192
|
|
|
|
|
|
|
} elsif ($@) { |
193
|
0
|
|
|
|
|
|
return $self->error("Error: $@"); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} elsif ($@) { |
196
|
0
|
|
|
|
|
|
return $self->error("Error: $@"); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
0
|
|
|
|
|
|
return undef; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
1 |