script/create-queue.pl untested

filter: Statementstmt 0.0% 0 / 184 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 288 CC 19 SCAR 19.0
CC
19
Cov
0%
main
20.0
command_create
12.0
command_delete_message
12.0
CRAP 6.7
1 #!/usr/bin/env perl
2  
3 # script to create a queue w/an associated dead letter queue
4 # perl create-queue.pl -h
5  
6 use strict;
7 use warnings;
8  
9 use Amazon::SQS::Client;
10  
11 use Data::Dumper;
12 use English qw(-no_match_vars);
13  
14 use Getopt::Long qw(:config no_ignore_case);
15 use JSON;
16 use Pod::Usage;
17  
18 use Readonly;
19  
20 Readonly::Scalar our $DEFAULT_MAX_RECEIVE_COUNT => 10;
21 Readonly::Scalar our $DEFAULT_MAXIMUM_MESSAGE_SIZE => 256 * 1024;
22 Readonly::Scalar our $DEFAULT_VISIBILITY_TIMEOUT => 30;
23 Readonly::Scalar our $DEFAULT_MESSAGE_RETENTION_PERIOD => 4 * 24 * 60 * 60;
24 Readonly::Scalar our $DEFAULT_DELAY_SECONDS => 0;
25 Readonly::Scalar our $DEFAULT_RECEIVE_MESSAGE_WAIT_TIME_SECONDS => 0;
26 Readonly::Scalar our $DEFAULT_ENDPOINT_URL => 'https://queue.amazonaws.com';
27  
28 ########################################################################
29 sub list_queues {
30 ########################################################################
31 my ($client) = @_;
32  
33 my $queues = $client->listQueues()->getListQueuesResult->getQueueUrl;
34  
35 return $queues;
36 }
37  
38 ########################################################################
39 sub get_queue_names {
40 ########################################################################
41 my ( $client, $options ) = @_;
42  
43 my $queue_list = list_queues($client);
44  
45 return map { /([^\/]+)$/xsm ? ( "$1" => $_ ) : () } @{$queue_list};
46 }
47  
48 ########################################################################
49 sub get_queue_url {
50 ########################################################################
51 my ( $client, $options ) = @_;
52  
53 my %queue_names = get_queue_names( $client, $options );
54  
55 return $queue_names{ $options->{queue} };
56 }
57  
58 ########################################################################
59 sub get_queue_attributes {
60 ########################################################################
61 my ( $client, $queue_url ) = @_;
62  
63 my $rslt = $client->getQueueAttributes(
64 { QueueUrl => $queue_url,
65 AttributeName => ['All'],
66 }
67 );
68  
69 my $attributesResult = $rslt->getGetQueueAttributesResult;
70  
71 return $attributesResult->getAttribute;
72 }
73  
74 ########################################################################
75 sub get_queue_arn {
76 ########################################################################
77 my ( $client, $queue_url ) = @_;
78  
79 my $attributes = get_queue_attributes( $client, $queue_url );
80  
81 my ($arn) = grep {/arn/xsmi} map { $_->getValue } @{$attributes};
82  
83 die "could not find queue ($queue_url) arn\n"
84 if !$arn;
85  
86 return $arn;
87 }
88  
89 ########################################################################
90 sub create_dlq {
91 ########################################################################
92 my ( $client, $options ) = @_;
93  
94 my $name = $options->{queue};
95  
96 my $dlq = $name . 'DLQ';
97  
98 my $redriveAllowPolicy = { redrivePermission => 'allowAll' };
99  
100 my @attributes = (
101 { Name => 'RedriveAllowPolicy',
102 Value => JSON->new->encode($redriveAllowPolicy)
103 },
104 { Name => 'VisibilityTimeout',
105 Value => $options->{'visibility-timeout'},
106 },
107 );
108  
109 local $options->{queue} = $dlq;
110  
111 return create_queue( $client, $options, @attributes );
112 }
113  
114 ########################################################################
115 sub create_test_queue {
116 ########################################################################
117 my ( $client, %options ) = @_;
118  
119 my $dlq_url = $options{dlq_url};
120  
121 if ($dlq_url) {
122  
123 $options{'redrive-policy'} = JSON->new->encode(
124 { deadLetterTargetArn => get_queue_arn( $client, $dlq_url ),
125 maxReceiveCount => $options{'max-receive-count'},
126 }
127 );
128 }
129  
130 my @attribute_names = qw(
131 delay-seconds
132 maximum-message-size
133 message-retention-period
134 redrive-policy
135 visibility-timeout
136 receive-message-wait-time-seconds
137 );
138  
139 my @attributes = map { { Name => toCamelCase($_), Value => $options{$_} } } @attribute_names;
140  
141 return create_queue( $client, \%options, @attributes );
142 }
143  
144 ########################################################################
145 sub toCamelCase {
146 ########################################################################
147 my ($var) = @_;
148  
149 return join q{}, map { ucfirst $_ } split /[\-]/xsm, $var;
150 }
151  
152 ########################################################################
153 sub create_test_queues {
154 ########################################################################
155 my ( $client, $options ) = @_;
156  
157 my $dlq_url;
158  
159 if ( $options->{dlq} ) {
160 $dlq_url = create_dlq( $client, $options );
161 }
162  
163 my $url = create_test_queue( $client, dlq_url => $dlq_url, %{$options} );
164  
165 return [ $url, $dlq_url ];
166 }
167  
168 ########################################################################
169 sub create_queue {
170 ########################################################################
171 my ( $client, $options, @attributes ) = @_;
172  
173 my $rslt = $client->createQueue(
174 { QueueName => $options->{queue},
175 @attributes ? ( Attribute => \@attributes ) : (),
176 }
177 );
178  
179 return $rslt->getCreateQueueResult->getQueueUrl;
180 }
181  
182 ########################################################################
183 sub delete_test_queues {
184 ########################################################################
185 my ( $client, $options ) = @_;
186  
187 my $name = $options->{queue};
188 my $dlq = $name . 'DLQ';
189  
190 my %queue_names = get_queue_names( $client, $options );
191  
192 foreach my $queue ( $name, $dlq ) {
193 next
194 if !$queue_names{$queue};
195  
196 $client->deleteQueue( { QueueUrl => $queue_names{$queue} } );
197 }
198  
199 return 0;
200 }
201  
202 ########################################################################
203 sub command_send_message {
204 ########################################################################
205 my ( $client, $options ) = @_;
206  
207 die "--queue is a required argument\n"
208 if !$options->{queue};
209  
210 my $message = shift @ARGV;
211 $message //= 'Hello World!';
212  
213 my $queue_url = get_queue_url( $client, $options );
214  
215 my $response = $client->sendMessage(
216 { MessageBody => $message,
217 QueueUrl => $queue_url
218 }
219 );
220  
221 print {*STDOUT} JSON->new->pretty->encode( { MessageId => $response->getSendMessageResult->getMessageId() } );
222  
223 return 0;
224 }
225  
226 ########################################################################
227 sub command_receive_message {
228 ########################################################################
229 my ( $client, $options ) = @_;
230  
231 die "--queue is a required argument\n"
232 if !$options->{queue};
233  
234 my $queue_url = get_queue_url( $client, $options );
235 my $max_messages = shift @ARGV;
236  
237 my $response = $client->receiveMessage(
238 { QueueUrl => $queue_url,
239 MaxNumberOfMessage => $max_messages // 1,
240 }
241 );
242  
243 my $result = $response->getReceiveMessageResult();
244  
245 my @messages = @{ $result->getMessage };
246  
247 my @message_list;
248  
249 foreach (@messages) {
250 push @message_list,
251 {
252 ReceiptHandle => $_->getReceiptHandle,
253 MessageBody => $_->getBody
254 };
255 }
256  
257 print {*STDOUT} JSON->new->pretty->encode( \@message_list );
258  
259 return 0;
260 }
261  
262 ########################################################################
263 sub command_create {
264 ########################################################################
265 my ( $client, $options ) = @_;
266  
267 die "--queue is a required argument\n"
268 if !$options->{queue};
269  
270 delete_test_queues( $client, $options );
271  
272 eval { create_test_queues( $client, $options ); };
273  
274 if ($EVAL_ERROR) {
275 print {*STDERR} Dumper( [ error => $EVAL_ERROR ] );
276 }
277  
278 return command_list( $client, $options );
279 }
280  
281 ########################################################################
282 sub command_delete {
283 ########################################################################
284 my ( $client, $options ) = @_;
285  
286 die "--queue is a required argument\n"
287 if !$options->{queue};
288  
289 delete_test_queues( $client, $options );
290  
291 return command_list( $client, $options );
292  
293 return 0;
294 }
295  
296 ########################################################################
297 sub command_delete_message {
298 ########################################################################
299 my ( $client, $options ) = @_;
300  
301 die "--queue is a required argument\n"
302 if !$options->{queue};
303  
304 my $queue_url = get_queue_url( $client, $options );
305  
306 my $receipt_handle = shift @ARGV;
307  
308 die "no receipt handle\n"
309 if !$receipt_handle;
310  
311 $client->deleteMessage(
312 { QueueUrl => $queue_url,
313 ReceiptHandle => $receipt_handle,
314 }
315 );
316  
317 return 0;
318 }
319  
320 ########################################################################
321 sub command_list {
322 ########################################################################
323 my ( $client, $options ) = @_;
324  
325 my $queues = list_queues( $client, $options );
326  
327 print {*STDOUT} JSON->new->pretty->encode($queues);
328  
329 return 0;
330 }
331  
332 ########################################################################
333 sub command_attributes {
334 ########################################################################
335 my ( $client, $options ) = @_;
336  
337 die "--queue is a required argument\n"
338 if !$options->{queue};
339  
340 my $queue_url = get_queue_url( $client, $options );
341 my $attributes = get_queue_attributes( $client, $queue_url );
342  
343 print {*STDOUT} JSON->new->pretty->encode( { map { $_->getName, $_->getValue } @{$attributes} } );
344  
345 return 0;
346 }
347  
348 ########################################################################
349 sub init_client {
350 ########################################################################
351 my ($options) = @_;
352  
353 my $client_options = {
354 ServiceURL => $options->{'endpoint-url'},
355 $options->{debug} ? ( loglevel => 'debug' ) : ( loglevel => 'info' ),
356 };
357  
358 my @credentials = ( $ENV{AWS_ACCESS_KEY_ID}, $ENV{AWS_SECRET_ACCESS_KEY} );
359  
360 return Amazon::SQS::Client->new( @credentials, $client_options );
361 }
362  
363 ########################################################################
364 sub main {
365 ########################################################################
366 my @option_specs = qw(
367 debug|d
368 dlq!
369 delay-seconds|D=i
370 endpoint-url|e=s
371 help|h
372 max-receive-count|c=i
373 maximum-message-size|S=i
374 message-retention-period|p=i
375 queue|q=s
376 receive-message-wait-time-seconds|w=i
377 visibility-timeout|v=i
378 );
379  
380 my %options = (
381 dlq => 1,
382 'delay-seconds' => $DEFAULT_DELAY_SECONDS,
383 'endpoint-url' => $DEFAULT_ENDPOINT_URL,
384 'max-receive-count' => $DEFAULT_MAX_RECEIVE_COUNT,
385 'endpoint-url' => $DEFAULT_ENDPOINT_URL,
386 'maximum-message-size' => $DEFAULT_MAXIMUM_MESSAGE_SIZE,
387 'message-retention-period' => $DEFAULT_MESSAGE_RETENTION_PERIOD,
388 'receive-message-wait-time-seconds' => $DEFAULT_RECEIVE_MESSAGE_WAIT_TIME_SECONDS,
389 );
390  
391 my %dispatch = (
392 attributes => \&command_attributes,
393 create => \&command_create,
394 'send-message' => \&command_send_message,
395 'receive-message' => \&command_receive_message,
396 delete => \&command_delete,
397 'delete-message' => \&command_delete_message,
398 list => \&command_list,
399 );
400  
401 my $retval = GetOptions( \%options, @option_specs );
402  
403 if ( !$retval || $options{help} ) {
404 pod2usage(1);
405 }
406  
407 my $client = init_client( \%options );
408  
409 my $command = shift @ARGV;
410 $command //= q{};
411  
412 if ( !$command || !$dispatch{$command} ) {
413 warn "invalid command [$command]\n"
414 if $command;
415  
416 pod2usage(1);
417 }
418  
419 return $dispatch{$command}->( $client, \%options );
420 }
421  
422 exit main();
423  
424 1;
425  
426 ## no critic
427  
428 __END__