forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Used.py
58 lines (49 loc) · 1.91 KB
/
Used.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from __future__ import unicode_literals
import re
import six
from cfnlint.rules import CloudFormationLintRule
from cfnlint.rules import RuleMatch
class Used(CloudFormationLintRule):
"""Check if Parameters are used"""
id = 'W2001'
shortdesc = 'Check if Parameters are Used'
description = 'Making sure the parameters defined are used'
source_url = 'https://github.com/aws-cloudformation/cfn-python-lint'
tags = ['parameters']
def searchstring(self, string, parameter):
"""Search string for tokenized fields"""
regex = re.compile(r'\${(%s)}' % parameter)
return regex.findall(string)
def isparaminref(self, subs, parameter):
"""Search sub strings for parameters"""
for sub in subs:
if isinstance(sub, (six.text_type, six.string_types)):
if self.searchstring(sub, parameter):
return True
return False
def match(self, cfn):
matches = []
reftrees = cfn.transform_pre.get('Ref')
subtrees = cfn.transform_pre.get('Fn::Sub')
refs = []
for reftree in reftrees:
refs.append(reftree[-1])
subs = []
for subtree in subtrees:
if isinstance(subtree[-1], list):
subs.extend(cfn.get_sub_parameters(subtree[-1][0]))
elif isinstance(subtree[-1], six.string_types):
subs.extend(cfn.get_sub_parameters(subtree[-1]))
for paramname, _ in cfn.get_parameters().items():
if paramname not in refs:
if paramname not in subs:
message = 'Parameter {0} not used.'
matches.append(RuleMatch(
['Parameters', paramname],
message.format(paramname)
))
return matches