File Coverage

blib/lib/WebService/DataDog/Tag.pm
Criterion Covered Total %
statement 18 71 25.3
branch 0 24 0.0
condition 0 24 0.0
subroutine 6 12 50.0
pod 5 5 100.0
total 29 136 21.3


line stmt bran cond sub pod time code
1             package WebService::DataDog::Tag;
2              
3 1     1   59035 use strict;
  1         1  
  1         31  
4 1     1   4 use warnings;
  1         1  
  1         22  
5              
6 1     1   3 use base qw( WebService::DataDog );
  1         1  
  1         323  
7 1     1   7 use Carp qw( carp croak );
  1         1  
  1         58  
8 1     1   4 use Data::Dumper;
  1         1  
  1         35  
9 1     1   3 use Try::Tiny;
  1         2  
  1         989  
10              
11              
12             =head1 NAME
13              
14             WebService::DataDog::Tag - Interface to Tag functions in DataDog's API.
15              
16             =head1 VERSION
17              
18             Version 1.0.2
19              
20             =cut
21              
22             our $VERSION = '1.0.2';
23              
24              
25             =head1 SYNOPSIS
26              
27             This module allows you interact with the Tag endpoint of the DataDog API.
28              
29             Per DataDog: "The tag end point allows you to tag hosts with keywords meaningful
30             to you - like role:database. All metrics sent from a host will have its tags
31             applied. When fetching and applying tags to a particular host, you can refer to
32             hosts by name (yourhost.example.com) or id (12345)."
33              
34             NOTE: all methods, except retrieve_all(), operate on a per-host basis rather
35             than on a per-tag basis. You cannot rename a tag or delete a tag from all hosts,
36             through the DataDog API.
37              
38              
39             =head1 METHODS
40              
41             =head2 retrieve_all()
42              
43             Retrieve a mapping of tags to hosts.
44              
45             my $tag = $datadog->build('Tag');
46             my $tag_host_list = $tag->retrieve_all();
47            
48             Parameters: None
49              
50             =cut
51              
52             sub retrieve_all
53             {
54 0     0 1   my ( $self, %args ) = @_;
55 0           my $verbose = $self->verbose();
56            
57 0           my $url = $WebService::DataDog::API_ENDPOINT . 'tags/hosts';
58            
59 0           my $response = $self->_send_request(
60             method => 'GET',
61             url => $url,
62             data => { '' => [] }
63             );
64            
65 0 0 0       if ( !defined($response) || !defined($response->{'tags'}) )
66             {
67 0           croak "Fatal error. No response or 'tags' missing from response.";
68             }
69            
70 0           return $response->{'tags'};
71             }
72              
73              
74             =head2 retrieve()
75              
76             Return a list of tags for the specified host.
77             NOTE: a 404 response typically indicates you specified an incorrect/unknown
78             host name/id
79              
80             my $tag = $datadog->build('Tag');
81             my $tag_list = $tag->retrieve( host => $host_name_or_id );
82            
83             Parameters:
84              
85             =over 4
86              
87             =item * host
88              
89             Hostname/host id you want to retrieve the tags for.
90              
91             =back
92              
93             =cut
94              
95             sub retrieve
96             {
97 0     0 1   my ( $self, %args ) = @_;
98 0           my $verbose = $self->verbose();
99            
100             # Check for mandatory parameters
101 0           foreach my $arg ( qw( host ) )
102             {
103 0 0 0       croak "ERROR - Argument '$arg' is required for retrieve()."
104             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
105             }
106            
107 0           my $url = $WebService::DataDog::API_ENDPOINT . 'tags/hosts' . '/' . $args{'host'};
108            
109 0           my $response = $self->_send_request(
110             method => 'GET',
111             url => $url,
112             data => { '' => [] }
113             );
114            
115 0 0 0       if ( !defined($response) || !defined($response->{'tags'}) )
116             {
117 0           croak "Fatal error. No response or tag 'tags' missing from response.";
118             }
119            
120 0           return $response->{'tags'};
121             }
122              
123              
124             =head2 update()
125              
126             Update tags for specified host.
127             NOTE: a 404 response typically indicates you specified an incorrect host name/id.
128             WARNING: you must specify all tags that you want attached to this host, not
129             simply new ones you want to add ( use add() for that, instead ).
130            
131             my $tag = $datadog->build('Tag');
132             $tag->update(
133             host => $host, # name/ID of host to modify
134             tags => $tag_list, # Updated full list of tags to apply to host
135             );
136            
137             Example:
138             $tag->update(
139             host => 'my.example.com',
140             tags => [ 'tag1', 'tag2', 'tag3:val' ],
141             );
142            
143             Parameters:
144              
145             =over 4
146              
147             =item * host
148              
149             Host name/id whose tags you want to modify.
150              
151             =item * tags
152              
153             List of tags to apply to host. This must be the full list you want applied,
154             including any already applied.
155              
156             =back
157              
158             =cut
159              
160             sub update
161             {
162 0     0 1   my ( $self, %args ) = @_;
163 0           my $verbose = $self->verbose();
164            
165             # Check for mandatory parameters
166 0           foreach my $arg ( qw( host tags ) )
167             {
168 0 0 0       croak "ERROR - Argument '$arg' is required for update()."
169             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
170             }
171            
172 0           $self->_error_checks( %args );
173            
174 0           my $url = $WebService::DataDog::API_ENDPOINT . 'tags/hosts' . '/' . $args{'host'};
175            
176 0           my $response = $self->_send_request(
177             method => 'PUT',
178             url => $url,
179             data => { tags => $args{'tags'} }
180             );
181            
182 0 0 0       if ( !defined($response) || !defined($response->{'tags'}) )
183             {
184 0           croak "Fatal error. No response or tag 'tags' missing from response.";
185             }
186            
187 0           return $response->{'tags'};
188             }
189              
190              
191              
192             =head2 add()
193              
194             Add tags to specified host.
195             NOTE: a 404 response typically indicates you specified an incorrect host name/id.
196            
197             my $tag = $datadog->build('Tag');
198             $tag->add(
199             host => $host, # name/ID of host to modify
200             tags => $tag_list, # Updated full list of tags to apply to host
201             );
202            
203             Example:
204             $tag->add(
205             host => 'my.example.com',
206             tags => [ 'tag3:val' ],
207             );
208            
209             Parameters:
210              
211             =over 4
212              
213             =item * host
214              
215             Host name/id whose tags you want to modify.
216              
217             =item * tags
218              
219             List of new tags to apply to existing tags on specified host.
220              
221             =back
222              
223             =cut
224              
225             sub add
226             {
227 0     0 1   my ( $self, %args ) = @_;
228 0           my $verbose = $self->verbose();
229            
230             # Check for mandatory parameters
231 0           foreach my $arg ( qw( host tags ) )
232             {
233 0 0 0       croak "ERROR - Argument '$arg' is required for add()."
234             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
235             }
236            
237 0           $self->_error_checks( %args );
238            
239 0           my $url = $WebService::DataDog::API_ENDPOINT . 'tags/hosts' . '/' . $args{'host'};
240            
241 0           my $response = $self->_send_request(
242             method => 'POST',
243             url => $url,
244             data => { tags => $args{'tags'} }
245             );
246            
247 0 0 0       if ( !defined($response) || !defined($response->{'tags'}) )
248             {
249 0           croak "Fatal error. No response or tag 'tags' missing from response.";
250             }
251            
252 0           return $response->{'tags'};
253             }
254              
255              
256             =head2 delete()
257              
258             Delete all tags from the specified host.
259              
260             my $tag = $datadog->build('Tag');
261             $tag->delete( host => $host );
262            
263             Parameters:
264              
265             =over 4
266              
267             =item * host
268              
269             Host name/id whose tags you want to delete.
270              
271             =back
272              
273             =cut
274              
275             sub delete
276             {
277 0     0 1   my ( $self, %args ) = @_;
278            
279 0           my $verbose = $self->verbose();
280            
281             # Check for mandatory parameters
282 0           foreach my $arg ( qw( host ) )
283             {
284 0 0 0       croak "ERROR - Argument '$arg' is required for delete()."
285             if !defined( $args{$arg} ) || ( $args{$arg} eq '' );
286             }
287            
288 0           my $url = $WebService::DataDog::API_ENDPOINT . 'tags/hosts' . '/' . $args{'host'};
289            
290 0           $self->_send_request(
291             method => 'DELETE',
292             url => $url,
293             data => { '' => [] }
294             );
295            
296 0           return;
297             }
298              
299              
300             =head1 INTERNAL FUNCTIONS
301              
302             =head2 _error_checks()
303              
304             Common error checking for adding/updating tags.
305              
306             =cut
307              
308             sub _error_checks
309             {
310 0     0     my ( $self, %args ) = @_;
311 0           my $verbose = $self->verbose();
312            
313             # 'tags' argument is valid
314 0 0         if ( !Data::Validate::Type::is_arrayref( $args{'tags'} ) )
315             {
316 0           croak "ERROR - invalid 'tags' value. Must be an arrayref.";
317             }
318            
319             #TODO centralize this error checking, since it's nearly identical to Metric.pm
320 0           foreach my $tag ( @{ $args{'tags'} } )
  0            
321             {
322             # must start with a letter
323 0 0         croak( "ERROR - invalid tag >" . $tag . "< on host >" . $args{'host'} . "<. Tags must start with a letter, a-z. Not sending." )
324             if ( $tag !~ /^[a-zA-Z]/ );
325            
326             # must be 200 characters max
327 0 0         croak( "ERROR - invalid tag >" . $tag . "< on host >" . $args{'host'} . "<. Tags must be 200 characters or less. Not sending." )
328             if ( length( $tag ) > 200 );
329            
330             # NOTE: This check isn't required by DataDog, they will allow this through.
331             # However, this tag will not behave as expected in the graphs, if we were to allow it.
332 0 0         croak( "ERROR - invalid tag >" . $tag . "< on host >" . $args{'host'} . "<. Tags should only contain a single colon (:). Not sending." )
333             if ( $tag =~ /^\S+:\S+:/ );
334             }
335            
336 0           return;
337             }
338              
339              
340             1;