Skip to content

Commit 4a4445f

Browse files
author
Chuck Meyer
authored
Merge pull request #201 from kddejong/Fix/SelectCidr
Fix select and cidr function logic
2 parents 89a8a28 + 4342438 commit 4a4445f

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

src/cfnlint/rules/functions/Cidr.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def match(self, cfn):
9090

9191
supported_functions = [
9292
'Fn::Select',
93-
'Ref'
93+
'Ref',
94+
'Fn::ImportValue'
9495
]
9596

9697
count_parameters = []
@@ -148,7 +149,7 @@ def match(self, cfn):
148149
tree[:] + [2], message.format('/'.join(map(str, tree[:] + [2])))))
149150
if index_key == 'Ref':
150151
size_mask_parameters.append(index_value)
151-
elif not isinstance(count_obj, six.integer_types):
152+
elif not isinstance(size_mask_obj, six.integer_types):
152153
message = 'Cidr sizeMask should be a int for {0}'
153154
matches.append(RuleMatch(
154155
tree[:] + [2], message.format('/'.join(map(str, tree[:] + [2])))))

src/cfnlint/rules/functions/Select.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,30 @@ def match(self, cfn):
5353
list_of_objs = select_value_obj[1]
5454
if isinstance(index_obj, dict):
5555
if len(index_obj) == 1:
56-
for index_key, _ in index_obj:
56+
for index_key, _ in index_obj.items():
5757
if index_key not in ['Ref', 'Fn::FindInMap']:
58-
message = 'Select index should be int, Ref, FindInMap for {0}'
58+
message = 'Select index should be an Integer or a function Ref or FindInMap for {0}'
5959
matches.append(RuleMatch(
6060
tree, message.format('/'.join(map(str, tree)))))
6161
elif not isinstance(index_obj, six.integer_types):
6262
try:
6363
int(index_obj)
6464
except ValueError:
65-
message = 'Select index should be int, Ref, FindInMap for {0}'
65+
message = 'Select index should be an Integer or a function of Ref or FindInMap for {0}'
6666
matches.append(RuleMatch(
6767
tree, message.format('/'.join(map(str, tree)))))
6868
if isinstance(list_of_objs, dict):
6969
if len(list_of_objs) == 1:
7070
for key, _ in list_of_objs.items():
7171
if key not in supported_functions:
72-
message = 'Key {0} should be a list for {1}'
72+
message = 'Select should use a supported function of {0}'
7373
matches.append(RuleMatch(
74-
tree, message.format(key, '/'.join(map(str, tree)))))
74+
tree, message.format(', '.join(map(str, supported_functions)))))
7575
else:
76-
message = 'Select should be a list of 2 elements for {0}'
76+
message = 'Select should use a supported function of {0}'
7777
matches.append(RuleMatch(
78-
tree, message.format('/'.join(map(str, tree)))))
79-
else:
78+
tree, message.format(', '.join(map(str, supported_functions)))))
79+
elif not isinstance(list_of_objs, list):
8080
message = 'Select should be an array of values for {0}'
8181
matches.append(RuleMatch(
8282
tree, message.format('/'.join(map(str, tree)))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
AWSTemplateFormatVersion: '2010-09-09'
3+
Parameters:
4+
SubnetIndex:
5+
Description: 'Index of the subnet'
6+
Type: Number
7+
MinValue: 0
8+
MaxValue: 5
9+
SubnetCount:
10+
Description: 'To slice the IP address ranges you need to specify how many subnets you want to create in the VPC'
11+
Type: Number
12+
MinValue: 1
13+
MaxValue: 6
14+
Resources:
15+
Subnet:
16+
Type: 'AWS::EC2::Subnet'
17+
Properties:
18+
CidrBlock: !Select [!Ref SubnetIndex, !Cidr [{'Fn::ImportValue': 'vpc-CidrBlock'}, !Ref SubnetCount, 12]]
19+
VpcId: 'vpc-123456'

test/rules/functions/test_cidr.py

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def test_file_positive(self):
3232
"""Test Positive"""
3333
self.helper_file_positive()
3434

35+
def test_file_positive_extra(self):
36+
"""Test failure"""
37+
self.helper_file_positive_template('fixtures/templates/good/functions/cidr.yaml')
38+
3539
def test_file_negative(self):
3640
"""Test failure"""
3741
self.helper_file_negative('fixtures/templates/bad/functions_cidr.yaml', 10)

0 commit comments

Comments
 (0)