File Coverage

blib/lib/WebService/DataDog/Dashboard.pm
Criterion Covered Total %
statement 18 118 15.2
branch 0 54 0.0
condition 0 42 0.0
subroutine 6 22 27.2
pod 9 9 100.0
total 33 245 13.4


line stmt bran cond sub pod time code
1             package WebService::DataDog::Dashboard;
2              
3 1     1   13322 use strict;
  1         2  
  1         29  
4 1     1   3 use warnings;
  1         1  
  1         21  
5              
6 1     1   3 use base qw( WebService::DataDog );
  1         1  
  1         323  
7 1     1   5 use Carp qw( carp croak );
  1         1  
  1         45  
8 1     1   3 use Try::Tiny;
  1         2  
  1         38  
9 1     1   4 use Data::Dumper;
  1         1  
  1         1115  
10              
11             =head1 NAME
12              
13             WebService::DataDog::Dashboard - Interface to Dashboard functions in DataDog's API.
14              
15             =head1 VERSION
16              
17             Version 1.0.1
18              
19             =cut
20              
21             our $VERSION = '1.0.1';
22              
23              
24             =head1 SYNOPSIS
25              
26             This module allows you interact with the Dashboard endpoint of the DataDog API.
27              
28             Per DataDog: "The Dashboards end point allow you to programmatically create,
29             update delete and query dashboards."
30              
31              
32             =head1 METHODS
33              
34             =head2 get_all_dashboards()
35              
36             Deprecated. Please use retrieve_all() instead.
37              
38             =cut
39              
40             sub get_all_dashboards
41             {
42 0     0 1   my ( $self, %args ) = @_;
43            
44 0           carp "get_all_dashboards() is deprecated. Please use retrieve_all() instead.";
45            
46 0           return $self->retrieve_all( %args );
47             }
48              
49              
50             =head2 retrieve_all()
51              
52             Retrieve details for all user-created dashboards ( does not include
53             system-generated or integration dashboards ).
54              
55             my $dashboard = $datadog->build('Dashboard');
56             my $dashboard_list = $dashboard->retrieve_all();
57            
58             Parameters: None
59              
60             =cut
61              
62             sub retrieve_all
63             {
64 0     0 1   my ( $self, %args ) = @_;
65 0           my $verbose = $self->verbose();
66            
67 0           my $url = $WebService::DataDog::API_ENDPOINT . 'dash';
68            
69 0           my $response = $self->_send_request(
70             method => 'GET',
71             url => $url,
72             data => { '' => [] }
73             );
74            
75 0 0 0       if ( !defined($response) || !defined($response->{'dashes'}) )
76             {
77 0           croak "Fatal error. No response or 'dashes' missing from response.";
78             }
79            
80 0           return $response->{'dashes'};
81             }
82              
83              
84             =head2 get_dashboard()
85              
86             Deprecated. Please use retrieve() instead.
87              
88             =cut
89              
90             sub get_dashboard
91             {
92 0     0 1   my ( $self, %args ) = @_;
93            
94 0           carp "get_dashboard() is deprecated. Please use retrieve() instead.";
95            
96 0           return $self->retrieve( %args );
97             }
98              
99              
100             =head2 retrieve()
101              
102             Retrieve details for specified user-created dashboards ( does not work for
103             system-generated or integration dashboards ).
104              
105             my $dashboard = $datadog->build('Dashboard');
106             my $dashboard_data = $dashboard->retrieve( id => $dash_id );
107            
108             Parameters:
109              
110             =over 4
111              
112             =item * id
113              
114             Id of dashboard you want to retrieve the details for.
115              
116             =back
117              
118             =cut
119              
120             sub retrieve
121             {
122 0     0 1   my ( $self, %args ) = @_;
123 0           my $verbose = $self->verbose();
124            
125             # Check for mandatory parameters
126 0           foreach my $arg ( qw( id ) )
127             {
128 0 0 0       croak "ERROR - Argument '$arg' is required."
129             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
130             }
131            
132             # Check that id specified is a number
133 0 0         croak "ERROR - invalid 'id' >" . $args{'id'} . "<. Dashboard id must be a number."
134             unless $args{'id'} =~ /^\d+$/;
135            
136 0           my $url = $WebService::DataDog::API_ENDPOINT . 'dash' . '/' . $args{'id'};
137 0           my $response;
138            
139             try
140             {
141 0     0     $response = $self->_send_request(
142             method => 'GET',
143             url => $url,
144             data => { '' => [] }
145             );
146             }
147             catch
148             {
149 0 0   0     if ( /404/ )
150             {
151 0           croak "Unknown dashboard id >" . $args{'id'} . "<";
152             }
153             else
154             {
155 0           croak "Error occurred while trying to retrieve details of dashboard >" . $args{'id'} . "<. Error: $_";
156             }
157 0           };
158            
159 0 0 0       if ( !defined($response) || !defined($response->{'dash'}) )
160             {
161 0           croak "Fatal error. No response or 'dash' missing from response.";
162             }
163            
164 0           return $response;
165             }
166              
167              
168             =head2 update_dashboard()
169              
170             Deprecated. Please use update() instead.
171              
172             =cut
173              
174             sub update_dashboard
175             {
176 0     0 1   my ( $self, %args ) = @_;
177            
178 0           carp("update_dashboard() is deprecated. Please use update() instead.");
179            
180 0           return $self->update( %args );
181            
182             }
183              
184              
185             =head2 update()
186              
187             Update details for specified user-created dashboard ( does not work for
188             system-generated or integration dashboards ).
189             Supply at least one of the arguments 'title', 'description', 'graphs'.
190             Any argument not supplied will remain unchanged within the dashboard.
191              
192             WARNING: If you only specify a new graph to add to the dashboard, you WILL
193             LOSE ALL EXISTING GRAPHS. Your 'graphs' section must include ALL graphs
194             that you want to be part of a dashboard.
195              
196             my $dashboard = $datadog->build('Dashboard');
197             $dashboard->update(
198             id => $dash_id,
199             title => $dash_title,
200             description => $dash_description,
201             graphs => $graphs,
202             );
203            
204             Parameters:
205              
206             =over 4
207              
208             =item * id
209              
210             Id of dashboard you want to update.
211              
212             =item * title
213              
214             Optional. Specify updated title for specified dashboard.
215              
216             =item * description
217              
218             Optional. Specify updated description for specified dashboard.
219              
220             =item * graphs
221              
222             Optional. Specify updated graph definition for specified dashboard.
223              
224             =back
225              
226             =cut
227              
228             sub update
229             {
230 0     0 1   my ( $self, %args ) = @_;
231 0           my $verbose = $self->verbose();
232            
233 0           $self->_error_checks(
234             mode => 'update',
235             data => \%args,
236             );
237              
238 0           my $url = $WebService::DataDog::API_ENDPOINT . 'dash' . '/' . $args{'id'};
239            
240 0           my $response;
241             # Try to pull up details on specified dashboard before attempting updates
242             try
243             {
244 0     0     $response = $self->_send_request(
245             method => 'GET',
246             url => $url,
247             data => { '' => [] }
248             );
249             }
250             catch
251             {
252 0     0     croak "Error retrieving details on dashboard id >" . $args{'id'} . "<. Are you sure this is the correct dashboard id?";
253 0           };
254            
255 0 0 0       if ( !defined($response) || !defined($response->{'dash'}) )
256             {
257 0           croak "Fatal error. No response or 'dash' missing from response.";
258             }
259            
260 0           my $dash_original_details = $response->{'dash'};
261            
262 0           $response = undef;
263            
264 0           my $data =
265             {
266             id => $args{'id'}
267             };
268            
269             # Build required API arguments, using original details for anything that user
270             # has not supplied
271 0 0         $data->{'title'} = defined $args{'title'}
272             ? $args{'title'}
273             : $dash_original_details->{'title'};
274            
275 0 0         $data->{'description'} = defined $args{'description'}
276             ? $args{'description'}
277             : $dash_original_details->{'description'};
278            
279 0 0         $data->{'graphs'} = defined $args{'graphs'}
280             ? $args{'graphs'}
281             : $dash_original_details->{'graphs'};
282            
283 0           $response = $self->_send_request(
284             method => 'PUT',
285             url => $url,
286             data => $data,
287             );
288            
289 0           return;
290             }
291              
292              
293             =head2 create()
294              
295             Create new DataDog dashboard with 1+ graphs.
296             If successful, returns created dashboard id.
297              
298             my $dashboard = $datadog->build('Dashboard');
299             my $dashboard_id = $dashboard->create(
300             title => $dash_title,
301             description => $dash_description,
302             graphs => $graphs,
303             );
304            
305             Example:
306             my $new_dashboard_id = $dashboard->create(
307             title => "TEST DASH",
308             description => "test dashboard",
309             graphs =>
310             [
311             {
312             title => "Sum of Memory Free",
313             definition =>
314             {
315             events =>[],
316             requests => [
317             { q => "sum:system.mem.free{*}" }
318             ]
319             },
320             viz => "timeseries"
321             },
322             ],
323             );
324            
325             Parameters:
326              
327             =over 4
328              
329             =item * title
330              
331             Specify title for new dashboard.
332              
333             =item * description
334              
335             Specify description for new dashboard.
336              
337             =item * graphs
338              
339             Specify graph definition for new dashboard.
340              
341             =over 4
342              
343             =item * title
344              
345             Title of graph.
346              
347             =item * definition
348              
349             Definition of graph.
350              
351             =over 4
352              
353             =item * events
354              
355             Overlay any events from the event stream.
356              
357             =item * requests
358              
359             Metrics you want to graph.
360              
361             =back
362              
363             =item * viz
364              
365             Visualisation of graph. Valid values: timeseries (default), treemap.
366              
367             =back
368              
369             =back
370              
371             =cut
372              
373             sub create
374             {
375 0     0 1   my ( $self, %args ) = @_;
376 0           my $verbose = $self->verbose();
377            
378 0           $self->_error_checks(
379             mode => 'create',
380             data => \%args,
381             );
382              
383 0           my $url = $WebService::DataDog::API_ENDPOINT . 'dash';
384            
385 0           my $data =
386             {
387             title => $args{'title'},
388             description => $args{'description'},
389             graphs => $args{'graphs'},
390             };
391            
392 0           my $response = $self->_send_request(
393             method => 'POST',
394             url => $url,
395             data => $data,
396             );
397            
398 0 0 0       if ( !defined($response) || !defined($response->{'dash'}) )
399             {
400 0           croak "Fatal error. No response or 'dash' missing from response.";
401             }
402            
403 0           return $response->{'dash'}->{'id'};
404             }
405              
406              
407             =head2 delete_dashboard()
408              
409             Deprecated. Please use delete() instead.
410              
411             =cut
412              
413             sub delete_dashboard
414             {
415 0     0 1   my ( $self, %args ) = @_;
416            
417 0           carp "delete_dashboard() is deprecated. Please use delete() instead.";
418            
419 0           return $self->delete( %args );
420             }
421              
422              
423             =head2 delete()
424              
425             Delete specified user-created dashboard.
426             NOTE: You cannot remove system-generated or integration dashboards.
427              
428             my $dashboard = $datadog->build('Dashboard');
429             $dashboard->delete( id => $dash_id );
430            
431            
432             Parameters:
433              
434             =over 4
435              
436             =item * id
437              
438             Dashboard id you want to delete.
439              
440             =back
441              
442             =cut
443              
444             sub delete
445             {
446 0     0 1   my ( $self, %args ) = @_;
447            
448 0           my $verbose = $self->verbose();
449            
450             # Check for mandatory parameters
451 0           foreach my $arg ( qw( id ) )
452             {
453 0 0 0       croak "ERROR - Argument '$arg' is required for delete()."
454             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
455             }
456            
457             # Check that id specified is a number
458 0 0         croak "ERROR - invalid 'id' >" . $args{'id'} . "<. Dashboard id must be a number."
459             unless $args{'id'} =~ /^\d+$/;
460            
461 0           my $url = $WebService::DataDog::API_ENDPOINT . 'dash' . '/' . $args{'id'};
462            
463 0           my $should_croak;
464             try
465             {
466 0     0     $self->_send_request(
467             method => 'DELETE',
468             url => $url,
469             data => { '' => [] }
470             );
471             }
472             catch
473             {
474 0 0   0     if ( /404/ )
475             {
476 0           $should_croak = "Error 404 deleting dashboard id >" . $args{'id'} . "<. Are you sure this is the correct dashboard id?";
477             }
478 0           };
479 0 0         croak $should_croak if $should_croak;
480            
481 0           return;
482             }
483              
484              
485             =head1 INTERNAL FUNCTIONS
486              
487             =head2 _error_checks()
488              
489             Common error checking for creating/updating dashboards.
490              
491             =cut
492              
493             sub _error_checks
494             {
495 0     0     my ( $self, %arguments ) = @_;
496 0           my $verbose = $self->verbose();
497            
498 0           my $mode = $arguments{'mode'};
499 0           my %args = %{ $arguments{'data'} };
  0            
500            
501 0 0         if ( $mode eq "update" )
    0          
502             {
503             # Check for mandatory parameters
504 0           foreach my $arg ( qw( id ) )
505             {
506 0 0 0       croak "ERROR - Argument '$arg' is required for update()."
507             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
508             }
509            
510             # Check that id specified is a number
511 0 0         croak "ERROR - invalid 'id' >" . $args{'id'} . "<. Dashboard id must be a number."
512             unless $args{'id'} =~ /^\d+$/;
513            
514             # Check that one update field was supplied
515 0 0 0       if ( !defined( $args{'title'} ) && !defined( $args{'description'} ) && !defined( $args{'graphs'} ) )
      0        
516             {
517 0           croak "ERROR - you must supply at least one of the following arguments: title, description, graphs";
518             }
519             }
520             elsif ( $mode eq "create" )
521             {
522             # Check for mandatory parameters
523 0           foreach my $arg ( qw( title description graphs ) )
524             {
525 0 0 0       croak "ERROR - Argument '$arg' is required for create()."
526             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
527             }
528             }
529            
530 0 0 0       if ( defined( $args{'title'} ) && $args{'title'} eq '' )
531             {
532 0           croak "ERROR - you cannot have a blank dashboard title.";
533             }
534            
535             # Check that title is <= 80 characters. Per Carlo @DDog. Undocumented?
536 0 0 0       croak( "ERROR - invalid 'title' >" . $args{'title'} . "<. Title must be 80 characters or less." )
537             if ( defined( $args{'title'} ) && length( $args{'title'} ) > 80 );
538            
539             # Check that description is <= 4000 characters. Per Carlo @DDog. Undocumented?
540 0 0 0       croak( "ERROR - invalid 'description' >" . $args{'description'} . "<. Description must be 4000 characters or less." )
541             if ( defined( $args{'description'} ) && length( $args{'description'} ) > 4000 );
542            
543             #TODO better graph error checking
544             # ?? disallow any 'graph' section changes without additional config/force/etc?
545             # - compare new definition vs existing. warn if any graphs are removed. print old definition?
546             # - make sure all graph fields are specified:
547             # title, (255 char limit)
548             # definition: events, requests (4000 char limit)
549             # viz?? (docs show it included in example, but not listed in fields, required or optional)
550 0 0         if ( defined ( $args{'graphs'} ) )
551             {
552 0 0         croak "ERROR - 'graphs' argument must be an arrayref"
553             if !Data::Validate::Type::is_arrayref( $args{'graphs'} );
554            
555 0           croak "ERROR - at least one graph definition is required for create()"
556 0 0         if scalar( @{ $args{'graphs'} } == 0 );
557            
558 0           foreach my $graph_item ( @{ $args{'graphs'} } )
  0            
559             {
560             # Check for mandatory parameters
561 0           foreach my $argument ( qw( title definition ) )
562             {
563 0 0 0       croak "ERROR - Argument '$argument' is required within each graph for create()."
564             if !defined( $graph_item->{$argument} ) || ( $graph_item->{$argument} eq '' );
565             }
566             }
567             }
568            
569 0           return;
570             }
571              
572              
573             1;