SQS.pm

filter: Statementstmt 40.2% 33 / 82 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 55.5% 10 / 18 Podpod 0.0% 0 / 6 totaltotal 27.5% 43 / 156 CC 18 SCAR 36.3
CC
18
Cov
28%
create_queue
110.0
get_queue_attributes
30.0
set_attributes
12.0
CRAP 37.9
« FargateStack/Builder/SecurityGroup.pm ↑ Summary FargateStack/Builder/IAM.pm »
1 package App::SQS;
2  
3 # A simple SQS utility class that can check for the existence of a queue and create one if needed
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use App::FargateStack::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use List::Util qw(pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 with 'App::AWS';
18  
19 1 ▶use parent qw(App::Command);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 __PACKAGE__->mk_accessors(
22 qw(
23 name
24 region
25 profile
26 visibility_timeout
27 message_retention_period
28 receive_message_wait_time_seconds
29 max_receive_count
30 delay_seconds
31 arn
32 url
33 )
34 );
35  
36 ########################################################################
37 0 ▶sub set_attributes {
Subroutine set_attributes: not called CC=3 SCAR=24.8
Pod undocumented
38 ########################################################################
39 0 my ( $self, $queue_url, $attributes ) = @_;
40  
41 0 ▶ if ( ref $attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attributes) 0 0 0
42 0 my %queue_attributes = %{$attributes};
43  
44 0 ▶ if ( $queue_attributes{RedrivePolicy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($queue_attributes{"RedrivePolicy"}) 0 0 0
45 0 $queue_attributes{RedrivePolicy} = encode_json( $queue_attributes{RedrivePolicy} );
46 }
47  
48 0 $attributes = encode_json( \%queue_attributes );
49 }
50  
51 0 return $self->command(
52 'set-queue-attributes',
53 [ '--queue-url' => $queue_url,
54 '--attributes' => $attributes,
55 ]
56 );
57 }
58  
59 ########################################################################
60 0 ▶sub get_queue_arn {
Subroutine get_queue_arn: not called CC=2 SCAR=17.9
Pod undocumented
61 ########################################################################
62 0 my ( $self, $queue_url ) = @_;
63  
64 0 my $arn = $self->get_queue_attributes(
65 queue_url => $queue_url,
66 attribute_names => ['QueueArn'],
67 query => 'Attributes.QueueArn'
68 );
69  
70 0 ▶ croak sprintf "ERROR: could not get queue attributes for queue: [%s]\n%s", $queue_url, $self->get_get_error
Branch: 1 branch
BranchTrueFalseTotal
unless $arn 0 0 0
71 if !$arn;
72  
73 0 return $arn;
74 }
75  
76 ########################################################################
77 0 ▶sub get_queue_attributes {
Subroutine get_queue_attributes: not called CC=5 SCAR=34.0
Pod undocumented
78 ########################################################################
79 0 my ( $self, %args ) = @_;
80  
81 0 my ( $queue_url, $attribute_names, $query ) = @args{qw(queue_url attribute_names query)};
82  
83 0 ▶ $queue_url //= $self->get_queue_url;
Condition0%0/3
$queue_url //= $self->get_queue_url
l!l&&r!l&&!r
000
MC/DC0%0/2
$queue_url //= $self->get_queue_url
$queue_url $self->get_queue_url
Truth table0%0/3
A //= B
A= $queue_urlB= $self->get_queue_url
ABresult
1X1
011
000
84  
85 0 ▶ my $attributes = join q{ }, @{ $attribute_names || [] };
Condition0%0/2
$attribute_names || []
l!l
00
MC/DC0%0/1
$attribute_names || []
$attribute_names
Truth table0%0/2
A
A= $attribute_names
Aresult
11
00
86 0 ▶ $attributes //= 'All';
Condition0%0/2
$attributes //= "All"
l!l
00
MC/DC0%0/1
$attributes //= "All"
$attributes
Truth table0%0/2
A
A= $attributes
Aresult
11
00
87  
88 0 ▶ return $self->command(
Branch: 1 branch
BranchTrueFalseTotal
$query ? : 0 0 0
89 'get-queue-attributes' => [
90 '--queue-url' => $queue_url,
91 '--attribute-names' => $attributes,
92 $query ? ( '--query' => $query ) : ()
93 ]
94 );
95 }
96  
97 ########################################################################
98 0 ▶sub queue_exists { goto &get_queue_url; }
Subroutine queue_exists: not called CC=1 SCAR=6.9
Pod undocumented
99 ########################################################################
100  
101 ########################################################################
102 0 ▶sub get_queue_url {
Subroutine get_queue_url: not called CC=2 SCAR=17.9
Pod undocumented
103 ########################################################################
104 0 my ( $self, $queue_name ) = @_;
105  
106 0 ▶ $queue_name //= $self->get_name;
Condition0%0/3
$queue_name //= $self->get_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$queue_name //= $self->get_name
$queue_name $self->get_name
Truth table0%0/3
A //= B
A= $queue_nameB= $self->get_name
ABresult
1X1
011
000
107  
108 0 my $queue_url = $self->command(
109 'get-queue-url' => [
110 '--queue-name' => $queue_name,
111 '--query' => 'QueueUrl',
112 '--output' => 'text',
113 ]
114 );
115  
116 0 chomp $queue_url;
117  
118 0 return $queue_url;
119 }
120  
121 ########################################################################
122 0 ▶sub create_queue {
Subroutine create_queue: not called CC=10 SCAR=47.0
Pod undocumented
123 ########################################################################
124 0 my ( $self, $attributes ) = @_;
125  
126 0 ▶ $self->get_logger->trace( sub { return Dumper( [ attributes => $attributes ] ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
127  
128 0 my $queue_name = $self->get_name;
129 0 ▶ $queue_name //= $attributes->{name};
Condition0%0/3
$queue_name //= $attributes->{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$queue_name //= $attributes->{"name"}
$queue_name $attributes->{"name"}
Truth table0%0/3
A //= B
A= $queue_nameB= $attributes->{"name"}
ABresult
1X1
011
000
130  
131 return
132 0 ▶ if $self->queue_exists($queue_name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->queue_exists($queue_name) 0 0 0
133  
134 0 ▶ $attributes //= {};
Condition0%0/2
$attributes //= {}
l!l
00
MC/DC0%0/1
$attributes //= {}
$attributes
Truth table0%0/2
A
A= $attributes
Aresult
11
00
135  
136 0 my %default_attrs = (
137 VisibilityTimeout => $DEFAULT_SQS_VISIBILITY_TIMEOUT,
138 MessageRetentionPeriod => $DEFAULT_SQS_MESSAGE_RETENTION_PERIOD,
139 ReceiveMessageWaitTimeSeconds => $DEFAULT_SQS_RECEIVE_MESSAGE_WAIT_TIME_SECONDS,
140 DelaySeconds => $DEFAULT_SQS_DELAY_SECONDS,
141 );
142  
143 0 my @attribute_names = qw(
144 visibility_timeout
145 message_retention_period
146 receive_message_wait_time_seconds
147 delay_seconds
148 );
149  
150 0 my %queue_attributes;
151  
152 0 for my $attr (@attribute_names) {
153 0 my $param = $attr;
154 0 $param = join q{}, map {ucfirst} split /_/xsm, $attr;
155  
156 # apparently these all need to be strings?
157 0 ▶ $queue_attributes{$param} = sprintf '%s', $self->get($attr) // $default_attrs{$param};
Condition0%0/3
$self->get($attr) // $default_attrs{$param}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->get($attr) // $default_attrs{$param}
$self->get($attr) $default_attrs{$param}
Truth table0%0/3
A // B
A= $self->get($attr)B= $default_attrs{$param}
ABresult
1X1
011
000
158 }
159  
160 0 ▶ if ( my $dlq = $attributes->{dlq} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dlq = $attributes->{"dlq"}) 0 0 0
161 croak "ERROR: you must provide an target ARN when creating a dead letter queue\n"
162 0 ▶ if !$dlq->{arn};
Branch: 1 branch
BranchTrueFalseTotal
unless $dlq->{"arn"} 0 0 0
163  
164 croak "ERROR: if you want a DLQ you have to set the max_receive_count\n",
165 0 ▶ if !exists $attributes->{max_receive_count};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attributes->{"max_receive_count"} 0 0 0
166  
167 # really? a string AWS?...apparently... 🤯
168 #
169 # Invalid type for parameter Attributes.RedrivePolicy, value:
170 # OrderedDict({'deadLetterTargetArn':
171 # 'arn:aws:sqs:us-east-1:311974035819:fu-man-q-dlq',
172 # 'maxReceiveCount': '5'}), type: <class
173 # 'collections.OrderedDict'>, valid types: <class 'str'>
174  
175 0 ▶ my $max_receive_count = sprintf '%s', $attributes->{max_receive_count} || $DEFAULT_SQS_MAX_RECEIVE_COUNT;
Condition0%0/3
$attributes->{'max_receive_count'} || $App::SQS::DEFAULT_SQS_MAX_RECEIVE_COUNT
l!l&&r!l&&!r
000
MC/DC0%0/2
$attributes->{'max_receive_count'} || $App::SQS::DEFAULT_SQS_MAX_RECEIVE_COUNT
$attributes->{'max_receive_count'} $App::SQS::DEFAULT_SQS_MAX_RECEIVE_COUNT
Truth table0%0/3
A || B
A= $attributes->{'max_receive_count'}B= $App::SQS::DEFAULT_SQS_MAX_RECEIVE_COUNT
ABresult
1X1
011
000
176  
177 $queue_attributes{RedrivePolicy} = encode_json(
178 { deadLetterTargetArn => $dlq->{arn},
179 0 maxReceiveCount => $max_receive_count, # must be string not an int
180 }
181 );
182 }
183  
184 0 my $attribute_payload = encode_json( \%queue_attributes );
185  
186 0 ▶ $self->get_logger->trace( sub { return Dumper( [ attributes => $attribute_payload ] ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
187  
188 0 return $self->command(
189 'create-queue' => [
190 '--attributes' => $attribute_payload,
191 '--queue-name' => $queue_name,
192 ]
193 );
194 }
195  
196 1 1;
« FargateStack/Builder/SecurityGroup.pm ↑ Summary FargateStack/Builder/IAM.pm »