| 1 | package Cfn::Crawler::Path; | ||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||
| 4 | 1 | has path => (is => 'ro', isa => 'Str', required => 1); | |||||||||||||||||||||||||||||
| 5 | 1 | has element => (is => 'ro', required => 1); | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | package Cfn::Crawler; | ||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||
| 10 | 1 | has resolve_dynamicvalues => (is => 'ro', isa => 'Bool', default => 0); | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 1 | has cfn => (is => 'ro', isa => 'Cfn', required => 1); | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||
| 14 | has _resolved_cfn => (is => 'ro', isa => 'Cfn', lazy => 1, default => sub { | ||||||||||||||||||||||||||||||
| 15 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 16 | 93 | ▶ | if ($self->resolve_dynamicvalues) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 17 | 1 | return $self->cfn->resolve_dynamicvalues; | |||||||||||||||||||||||||||||
| 18 | } else { | ||||||||||||||||||||||||||||||
| 19 | 92 | return $self->cfn; | |||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||
| 21 | 1 | }); | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 1 | has criteria => (is => 'ro', isa => 'CodeRef', required => 1); | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||
| 25 | 1 | has _all => ( | |||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||
| 27 | lazy => 1, | ||||||||||||||||||||||||||||||
| 28 | builder => '_scan_all', | ||||||||||||||||||||||||||||||
| 29 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||
| 30 | traits => ['Array'], | ||||||||||||||||||||||||||||||
| 31 | handles => { | ||||||||||||||||||||||||||||||
| 32 | all => 'elements' | ||||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||||
| 35 | 1 | for my $property (qw/resources outputs parameters | |||||||||||||||||||||||||||||
| 36 | metadata mappings conditions/){ | ||||||||||||||||||||||||||||||
| 37 | 6 | has "_$property" => ( | |||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||||
| 39 | lazy => 1, | ||||||||||||||||||||||||||||||
| 40 | builder => "_scan_$property", | ||||||||||||||||||||||||||||||
| 41 | isa => 'ArrayRef', | ||||||||||||||||||||||||||||||
| 42 | traits => [ 'Array' ], | ||||||||||||||||||||||||||||||
| 43 | handles => { | ||||||||||||||||||||||||||||||
| 44 | $property => 'elements', | ||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | sub _match { | ||||||||||||||||||||||||||||||
| 50 | 8698 | ▶ | my ($self, $path, $element) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_match: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 51 | 8698 | ▶ | if ($self->criteria->($element)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 52 | 8695 | return Cfn::Crawler::Path->new( | |||||||||||||||||||||||||||||
| 53 | path => $path, | ||||||||||||||||||||||||||||||
| 54 | element => $element, | ||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||||
| 57 | 3 | return (); | |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||
| 61 | sub _scan_all { | ||||||||||||||||||||||||||||||
| 62 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_all: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||
| 64 | 93 | my @results; | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||
| 66 | 93 | push @results, $self->resources; | |||||||||||||||||||||||||||||
| 67 | 93 | push @results, $self->outputs; | |||||||||||||||||||||||||||||
| 68 | 93 | push @results, $self->parameters; | |||||||||||||||||||||||||||||
| 69 | 93 | push @results, $self->mappings; | |||||||||||||||||||||||||||||
| 70 | 93 | push @results, $self->metadata; | |||||||||||||||||||||||||||||
| 71 | 93 | push @results, $self->conditions; | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||
| 76 | sub _scan_conditions { | ||||||||||||||||||||||||||||||
| 77 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_conditions: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 78 | 93 | my @results; | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | 93 | foreach my $c_name (sort $self->_resolved_cfn->ConditionList) { | |||||||||||||||||||||||||||||
| 81 | 4 | my $path = "Conditions.$c_name"; | |||||||||||||||||||||||||||||
| 82 | 4 | my $condition = $self->_resolved_cfn->Condition($c_name); | |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | 4 | push @results, $self->_match($path, $condition); | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | #TODO: Can't crawl into conditions, because they are not proper | ||||||||||||||||||||||||||||||
| 87 | # objects yet | ||||||||||||||||||||||||||||||
| 88 | #foreach my $cond_key (keys %$condition) { | ||||||||||||||||||||||||||||||
| 89 | # push @results, $self->_crawl_values("$path\.$cond_key", $condition->{ $cond_key }); | ||||||||||||||||||||||||||||||
| 90 | #} | ||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||
| 93 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||
| 96 | sub _scan_metadata { | ||||||||||||||||||||||||||||||
| 97 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_metadata: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||||
| 98 | 93 | my @results; | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||
| 100 | 93 | foreach my $md_name (sort $self->_resolved_cfn->MetadataList) { | |||||||||||||||||||||||||||||
| 101 | 0 | my $path = "Metadata.$md_name"; | |||||||||||||||||||||||||||||
| 102 | 0 | my $metadata = $self->_resolved_cfn->MetadataItem($md_name); | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||
| 104 | 0 | push @results, $self->_match($path, $metadata); | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||
| 107 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | sub _scan_mappings { | ||||||||||||||||||||||||||||||
| 111 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_mappings: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 112 | 93 | my @results; | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | 93 | foreach my $m_name (sort $self->_resolved_cfn->MappingList) { | |||||||||||||||||||||||||||||
| 115 | 98 | my $path = "Mappings.$m_name"; | |||||||||||||||||||||||||||||
| 116 | 98 | my $mapping = $self->_resolved_cfn->Mapping($m_name); | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | 98 | push @results, $self->_match($path, $mapping); | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||
| 121 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | sub _scan_parameters { | ||||||||||||||||||||||||||||||
| 125 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_parameters: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 126 | 93 | my @results; | |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||
| 128 | 93 | foreach my $p_name (sort $self->_resolved_cfn->ParameterList) { | |||||||||||||||||||||||||||||
| 129 | 294 | my $path = "Parameters.$p_name"; | |||||||||||||||||||||||||||||
| 130 | 294 | my $parameter = $self->_resolved_cfn->Parameter($p_name); | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||
| 132 | 294 | push @results, $self->_match($path, $parameter); | |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||
| 135 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||
| 138 | sub _scan_outputs { | ||||||||||||||||||||||||||||||
| 139 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_outputs: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||
| 140 | 93 | my @results; | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | 93 | foreach my $o_name (sort $self->_resolved_cfn->OutputList) { | |||||||||||||||||||||||||||||
| 143 | 135 | my $path = "Outputs.$o_name"; | |||||||||||||||||||||||||||||
| 144 | 135 | my $output = $self->_resolved_cfn->Output($o_name); | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | 135 | push @results, $self->_match($path, $output); | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | 135 | push @results, $self->_crawl_values("$path\.Value", $output->Value); | |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||
| 154 | sub _scan_resources { | ||||||||||||||||||||||||||||||
| 155 | 93 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||
|
Subroutine
_scan_resources: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||
| 156 | 93 | my @results; | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||
| 158 | 93 | foreach my $r_name (sort $self->_resolved_cfn->ResourceList) { | |||||||||||||||||||||||||||||
| 159 | 591 | my $path = "Resources.$r_name"; | |||||||||||||||||||||||||||||
| 160 | 591 | my $resource = $self->_resolved_cfn->Resource($r_name); | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | 591 | push @results, $self->_match($path, $resource); | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | 591 | ▶ | next if (not defined $resource->Properties); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||
| 166 | 563 | foreach my $prop ($resource->Properties->meta->get_all_attributes) { | |||||||||||||||||||||||||||||
| 167 | 5947 | my $prop_name = $prop->name; | |||||||||||||||||||||||||||||
| 168 | 5947 | my $prop_value = $resource->Property($prop_name); | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||
| 170 | 5947 | push @results, $self->_crawl_values("$path\.Properties\.$prop_name", $prop_value); | |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||
| 174 | 93 | return \@results; | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||
| 177 | # | ||||||||||||||||||||||||||||||
| 178 | # since Cfn::Values are recursive (can contain other Cfn::Values) | ||||||||||||||||||||||||||||||
| 179 | # this guy will recurse over them, and for the ones matching the | ||||||||||||||||||||||||||||||
| 180 | # criteria, will give you a path to them | ||||||||||||||||||||||||||||||
| 181 | # | ||||||||||||||||||||||||||||||
| 182 | sub _crawl_values { | ||||||||||||||||||||||||||||||
| 183 | 12677 | ▶ | my ($self, $path, $value) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
_crawl_values: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||
| 185 | 12677 | ▶ | return if (not blessed $value); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 186 | 7576 | ▶ | die "Found something that isn't a Cfn::Value" if (not $value->isa('Cfn::Value')); | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||
| 188 | 7576 | my @results; | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||
| 190 | 7576 | push @results, $self->_match($path, $value); | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||
| 192 | 7576 | ▶ | if ($value->isa('Cfn::Value::Array')) { | ||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||
| 193 | 960 | my $index = 0; | |||||||||||||||||||||||||||||
| 194 | 960 | foreach my $element (@{ $value->Value }) { | |||||||||||||||||||||||||||||
| 195 | 2297 | push @results, $self->_crawl_values("$path\.$index", $element); | |||||||||||||||||||||||||||||
| 196 | 2297 | $index++; | |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||
| 198 | } elsif ($value->isa('Cfn::Value::Function')) { | ||||||||||||||||||||||||||||||
| 199 | 1672 | push @results, $self->_crawl_values("$path\." . $value->Function, $value->Value); | |||||||||||||||||||||||||||||
| 200 | } elsif ($value->isa('Cfn::Value::Hash')) { | ||||||||||||||||||||||||||||||
| 201 | 143 | my $index; | |||||||||||||||||||||||||||||
| 202 | 143 | foreach my $key (sort keys %{ $value->Value }){ | |||||||||||||||||||||||||||||
| 203 | 305 | push @results, $self->_crawl_values("$path\.$key", $value->Value->{ $key }); | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||
| 205 | } elsif ($value->isa('Cfn::Value::Primitive')) { | ||||||||||||||||||||||||||||||
| 206 | # A primitives value is not traversable | ||||||||||||||||||||||||||||||
| 207 | } elsif ($value->isa('Cfn::Value::TypedValue')) { | ||||||||||||||||||||||||||||||
| 208 | 445 | foreach my $property ($value->meta->get_all_attributes) { | |||||||||||||||||||||||||||||
| 209 | 2321 | my $prop_name = $property->name; | |||||||||||||||||||||||||||||
| 210 | 2321 | my $prop_value = $value->$prop_name; | |||||||||||||||||||||||||||||
| 211 | 2321 | push @results, $self->_crawl_values("$path\.$prop_name", $prop_value); | |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||
| 213 | } elsif ($value->isa('Cfn::DynamicValue')) { | ||||||||||||||||||||||||||||||
| 214 | # A DynamicValue is not traversable | ||||||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||||
| 216 | 0 | die "Unknown $value at $path"; | |||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||
| 219 | 7576 | return @results; | |||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||
| 222 | 1 | 1; | |||||||||||||||||||||||||||||