FargateStack/Builder/SQSQueue.pm

filter: Statementstmt 27.1% 28 / 103 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 5 totaltotal 15.5% 37 / 238 CC 35 SCAR 53.7
CC
35
Cov
15%
build_queue
462.0
apply_queue_defaults
72.0
queue_exists
30.0
CRAP 214.0
« FargateStack/Builder.pm ↑ Summary FargateStack/AutoscalingConfig.pm »
1 package App::FargateStack::Builder::SQSQueue;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use App::SQS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::FargateStack::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use App::FargateStack::Builder::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ########################################################################
17 # Creates an SQS queue and possibly a dead letter queue
18 ########################################################################
19 # YAML configuration syntax:
20 ########################################################################
21 # queue:
22 # name: queue-name # required if queue: defined
23 # max_receive_count: # if exists, we will create a DLQ
24 # optional queue attributes (use Amazon's defaults)
25 # visibility_timeout:
26 # maximum_message_size:
27 # delay_seconds:
28 # receive_message_wait_time_seconds:
29 # message_retention_period:
30 # dlq:
31 # name: optional, default queue-name-dlq
32 # optional queue attributes
33 #
34 ########################################################################
35 0 ▶sub build_queue {
Subroutine build_queue: not called CC=21 SCAR=61.4
Pod undocumented
36 ########################################################################
37 0 my ($self) = @_;
38  
39 0 my ( $config, $dryrun, $cache ) = $self->common_args(qw(config dryrun cache));
40  
41 0 my ( $queue, $dlq ) = @{$config}{qw(queue dlq)};
42  
43 return
44 0 ▶ if !$queue || !$queue->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $queue and $queue->{"name"} 0 0 0
Condition0%0/3
$queue and $queue->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$queue and $queue->{"name"}
$queue $queue->{"name"}
Truth table0%0/3
A and B
A= $queueB= $queue->{"name"}
ABresult
0X0
100
111
45  
46 0 my $sqs = App::SQS->new( $self->get_global_options );
47  
48 0 my $queue_url = $self->queue_exists( sqs => $sqs, %{$queue} );
49  
50 0 ▶ if ($queue_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue_url) { } 0 0 0
51 0 $queue->{url} = $queue_url;
52 0 $self->inc_existing_resources( queue => $queue_url );
53 }
54 else {
55 0 $self->get_logger->info( sprintf 'queue: %s will be created...%s', $queue->{name}, $dryrun );
56 0 $self->inc_required_resources( 'queue' => $queue->{name} );
57 }
58  
59 0 my $want_dlq = exists $queue->{max_receive_count};
60  
61 0 ▶ my $dlq_url = $want_dlq && $self->queue_exists( sqs => $sqs, %{$dlq} );
Condition0%0/3
$want_dlq && $self->queue_exists('sqs', $sqs, %{$dlq;})
!ll&&!rl&&r
000
MC/DC0%0/2
$want_dlq && $self->queue_exists('sqs', $sqs, %{$dlq;})
$want_dlq $self->queue_exists('sqs', $sqs, %{$dlq;})
Truth table0%0/3
A && B
A= $want_dlqB= $self->queue_exists('sqs', $sqs, %{$dlq;})
ABresult
0X0
100
111
62  
63 0 ▶ $dlq = $want_dlq && !$dlq ? {} : $dlq;
Branch: 1 branch
BranchTrueFalseTotal
$want_dlq && !$dlq ? : 0 0 0
Condition0%0/3
$want_dlq and not $dlq
!ll&&!rl&&r
000
MC/DC0%0/2
$want_dlq and not $dlq
$want_dlq not $dlq
Truth table0%0/3
A and not(B)
A= $want_dlqB= not $dlq
ABresult
0X0
100
111
64  
65 0 ▶ if ($want_dlq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_dlq) 0 0 0
66 0 my $default_dlq_name = sprintf '%s-dlq', $queue->{name};
67  
68 0 ▶ $config->{dlq} //= $dlq; # make sure dlq: key is set in config
Condition0%0/3
$config->{"dlq"} //= $dlq
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{"dlq"} //= $dlq
$config->{"dlq"} $dlq
Truth table0%0/3
A //= B
A= $config->{"dlq"}B= $dlq
ABresult
1X1
011
000
69  
70 0 ▶ $dlq->{name} //= $default_dlq_name;
Condition0%0/3
$dlq->{"name"} //= $default_dlq_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$dlq->{"name"} //= $default_dlq_name
$dlq->{"name"} $default_dlq_name
Truth table0%0/3
A //= B
A= $dlq->{"name"}B= $default_dlq_name
ABresult
1X1
011
000
71  
72 0 ▶ if ($dlq_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dlq_url) { } 0 0 0
73 0 $dlq->{url} = $dlq_url;
74  
75 # just in case the create succeeded but we failed to record the arn
76 0 $self->ensure_queue_arn( $dlq, $sqs );
77  
78 0 $self->inc_existing_resources( 'queue:dlq' => $dlq_url );
79 }
80 else {
81 0 $self->get_logger->info( sprintf 'queue: %s will be created...%s', $dlq->{name}, $dryrun );
82 0 $self->inc_required_resources( 'queue:dlq' => $dlq->{name} );
83 }
84 }
85  
86 return
87 0 ▶ if $dryrun || ( $dlq_url && $queue_url );
Branch: 1 branch
BranchTrueFalseTotal
if $dryrun or $dlq_url and $queue_url 0 0 0
Condition0%0/6
$dryrun or $dlq_url and $queue_url
l!l&&r!l&&!r
000
$dlq_url and $queue_url
!ll&&!rl&&r
000
MC/DC0%0/3
$dryrun or $dlq_url and $queue_url
$dryrun $dlq_url $queue_url
Truth table0%0/4
A or B and C
A= $dryrunB= $dlq_urlC= $queue_url
ABCresult
1XX1
0111
00X0
0100
88  
89 $self->log_trace(
90 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 return Dumper(
92 [ dlq_url => $dlq_url,
93 dlq => $dlq
94 ]
95 );
96 }
97 0 );
98  
99 ######################################################################
100 # deadletter queue - if we have a dead letter queue defined, but the
101 # url does not exists in config - we need to create the queue before
102 # creating the main queue
103 ######################################################################
104 0 ▶ if ( $dlq && !$dlq_url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dlq and not $dlq_url) 0 0 0
Condition0%0/3
$dlq and not $dlq_url
!ll&&!rl&&r
000
MC/DC0%0/2
$dlq and not $dlq_url
$dlq not $dlq_url
Truth table0%0/3
A and not(B)
A= $dlqB= not $dlq_url
ABresult
0X0
100
111
105 0 $self->apply_queue_defaults($dlq);
106  
107 0 my $result = $sqs->create_queue($dlq);
108  
109 0 ▶ log_die( $self, "ERROR: could not create queue: [%s]\n%s", $dlq->{name}, $sqs->get_error )
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
110 if !$result;
111  
112 0 $dlq->{url} = $result->{QueueUrl};
113  
114 0 $self->ensure_queue_arn( $dlq, $sqs );
115  
116 0 $self->log_warn( 'queue: successfully created the dead letter queue: [%s]', $dlq->{name} );
117 }
118  
119 return
120 0 ▶ if $queue_url;
Branch: 1 branch
BranchTrueFalseTotal
if $queue_url 0 0 0
121  
122 ######################################################################
123 # create the main queue
124 ######################################################################
125 0 $self->apply_queue_defaults($queue);
126  
127 ######################################################################
128 # we recognized the need for a DLQ because max_receive_count
129 # existed, but maybe not defined?
130 ######################################################################
131 0 ▶ if ($dlq) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dlq) 0 0 0
132  
133 ####################################################################
134 # just is in case we don't have the arn recorded...set it since we
135 # need it for creating the main queue
136 ####################################################################
137 0 $self->ensure_queue_arn( $dlq, $sqs );
138 }
139  
140 0 my %queue_attributes = ( dlq => $dlq, %{$queue} );
141  
142 0 my $result = $sqs->create_queue( \%queue_attributes );
143  
144 0 ▶ croak sprintf "could not create queue: [%s]\n%s", $queue->{name}, $sqs->get_error
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
145 if !$result;
146  
147 0 $queue->{url} = $result->{QueueUrl};
148  
149 my $arn = $sqs->get_queue_attributes(
150 queue_url => $queue->{url},
151 0 attribute_names => ['QueueArn'],
152 query => 'Attributes.QueueArn'
153 );
154  
155 0 ▶ croak sprintf "could not get queue attributes for queue: [%s]\n%s", $queue->{name}, $sqs->get_error
Branch: 1 branch
BranchTrueFalseTotal
unless $arn 0 0 0
156 if !$arn;
157  
158 0 $queue->{arn} = $arn;
159  
160 0 $self->log_warn( 'queue: successfully created queue: [%s]', $queue->{name} );
161  
162 0 return;
163 }
164  
165 ########################################################################
166 0 ▶sub ensure_queue_arn {
Subroutine ensure_queue_arn: not called CC=2 SCAR=17.9
Pod undocumented
167 ########################################################################
168 0 my ( $self, $queue, $sqs ) = @_;
169  
170 0 ▶ return if $queue->{arn};
Branch: 1 branch
BranchTrueFalseTotal
if $queue->{"arn"} 0 0 0
171  
172 0 return $queue->{arn} = $sqs->get_queue_arn( $queue->{url} );
173 }
174  
175 ########################################################################
176 0 ▶sub queue_exists {
Subroutine queue_exists: not called CC=5 SCAR=34.0
Pod undocumented
177 ########################################################################
178 0 my ( $self, %args ) = @_;
179  
180 0 my ( $sqs, $queue_url, $queue_name ) = @args{qw(sqs url name)};
181  
182 0 my $cache = $self->get_cache;
183  
184 0 ▶ $queue_url = $cache && $queue_url ? $queue_url : $sqs->queue_exists($queue_name);
Branch: 1 branch
BranchTrueFalseTotal
$cache && $queue_url ? : 0 0 0
Condition0%0/3
$cache and $queue_url
!ll&&!rl&&r
000
MC/DC0%0/2
$cache and $queue_url
$cache $queue_url
Truth table0%0/3
A and B
A= $cacheB= $queue_url
ABresult
0X0
100
111
185  
186 return
187 0 ▶ if !$queue_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $queue_url 0 0 0
188  
189 0 ▶ $self->get_logger->info( sprintf 'queue: [%s] exists...%s', $queue_name, $cache ? $cache : 'skipping' );
Branch: 1 branch
BranchTrueFalseTotal
$cache ? : 0 0 0
190  
191 0 return $queue_url;
192 }
193  
194 ########################################################################
195 0 ▶sub apply_queue_defaults {
Subroutine apply_queue_defaults: not called CC=8 SCAR=42.8
Pod undocumented
196 ########################################################################
197 0 my ( $self, $q ) = @_;
198  
199 0 ▶ $q->{maximum_message_size} //= $DEFAULT_SQS_MAXIMUM_MESSAGE_SIZE;
Condition0%0/3
$q->{"maximum_message_size"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAXIMUM_MESSAGE_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$q->{"maximum_message_size"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAXIMUM_MESSAGE_SIZE
$q->{"maximum_message_size"} $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAXIMUM_MESSAGE_SIZE
Truth table0%0/3
A //= B
A= $q->{"maximum_message_size"}B= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAXIMUM_MESSAGE_SIZE
ABresult
1X1
011
000
200 0 ▶ $q->{visibility_timeout} //= $DEFAULT_SQS_VISIBILITY_TIMEOUT;
Condition0%0/3
$q->{"visibility_timeout"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_VISIBILITY_TIMEOUT
l!l&&r!l&&!r
000
MC/DC0%0/2
$q->{"visibility_timeout"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_VISIBILITY_TIMEOUT
$q->{"visibility_timeout"} $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_VISIBILITY_TIMEOUT
Truth table0%0/3
A //= B
A= $q->{"visibility_timeout"}B= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_VISIBILITY_TIMEOUT
ABresult
1X1
011
000
201 0 ▶ $q->{message_retention_period} //= $DEFAULT_SQS_MESSAGE_RETENTION_PERIOD;
Condition0%0/3
$q->{"message_retention_period"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MESSAGE_RETENTION_PERIOD
l!l&&r!l&&!r
000
MC/DC0%0/2
$q->{"message_retention_period"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MESSAGE_RETENTION_PERIOD
$q->{"message_retention_period"} $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MESSAGE_RETENTION_PERIOD
Truth table0%0/3
A //= B
A= $q->{"message_retention_period"}B= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MESSAGE_RETENTION_PERIOD
ABresult
1X1
011
000
202 0 ▶ $q->{receive_message_wait_time_seconds} //= $DEFAULT_SQS_RECEIVE_MESSAGE_WAIT_TIME_SECONDS;
Condition0%0/3
$q->{"receive_message_wait_time_seconds"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_RECEIVE_MESSAGE_WAIT_TIME_SECONDS
l!l&&r!l&&!r
000
MC/DC0%0/2
$q->{"receive_message_wait_time_seconds"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_RECEIVE_MESSAGE_WAIT_TIME_SECONDS
$q->{"receive_message_wait_time_seconds"} $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_RECEIVE_MESSAGE_WAIT_TIME_SECONDS
Truth table0%0/3
A //= B
A= $q->{"receive_message_wait_time_seconds"}B= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_RECEIVE_MESSAGE_WAIT_TIME_SECONDS
ABresult
1X1
011
000
203 0 ▶ $q->{delay_seconds} //= $DEFAULT_SQS_DELAY_SECONDS;
Condition0%0/3
$q->{"delay_seconds"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_DELAY_SECONDS
l!l&&r!l&&!r
000
MC/DC0%0/2
$q->{"delay_seconds"} //= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_DELAY_SECONDS
$q->{"delay_seconds"} $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_DELAY_SECONDS
Truth table0%0/3
A //= B
A= $q->{"delay_seconds"}B= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_DELAY_SECONDS
ABresult
1X1
011
000
204  
205 # max_receive_count indicates we have a DLQ
206 0 ▶ if ( exists $q->{max_receive_count} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $q->{"max_receive_count"}) 0 0 0
207 0 ▶ $q->{max_receive_count} ||= $DEFAULT_SQS_MAX_RECEIVE_COUNT;
Condition0%0/3
$q->{"max_receive_count"} ||= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAX_RECEIVE_COUNT
l!l&&r!l&&!r
000
MC/DC0%0/2
$q->{"max_receive_count"} ||= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAX_RECEIVE_COUNT
$q->{"max_receive_count"} $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAX_RECEIVE_COUNT
Truth table0%0/3
A ||= B
A= $q->{"max_receive_count"}B= $App::FargateStack::Builder::SQSQueue::DEFAULT_SQS_MAX_RECEIVE_COUNT
ABresult
1X1
011
000
208 }
209 0 return;
210 }
211  
212 ########################################################################
213 0 ▶sub add_queue_policy {
Subroutine add_queue_policy: not called CC=3 SCAR=24.8
Pod undocumented
214 ########################################################################
215 0 my ($self) = @_;
216  
217 0 my $config = $self->get_config;
218  
219 0 my $dlq = $config->{dlq};
220  
221 0 ▶ my @queue_arns = ( $config->{queue}->{arn}, defined $dlq && $dlq->{arn} ? $dlq->{arn} : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $dlq && $dlq->{'arn'} ? : 0 0 0
Condition0%0/3
defined $dlq and $dlq->{"arn"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dlq and $dlq->{"arn"}
defined $dlq $dlq->{"arn"}
Truth table0%0/3
A and B
A= defined $dlqB= $dlq->{"arn"}
ABresult
0X0
100
111
222  
223 return {
224 0 Effect => 'Allow',
225 Action => ['sqs:*'],
226 Resource => \@queue_arns,
227 };
228 }
229  
230 1 1;
« FargateStack/Builder.pm ↑ Summary FargateStack/AutoscalingConfig.pm »