Created
May 10, 2017 15:55
-
-
Save manuelzs/bb3e0ef2db7f5b9dad359aca1a119067 to your computer and use it in GitHub Desktop.
Check Refs in troposphere template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
from troposphere import Template as BaseTemplate, BaseAWSObject, Ref, AWSHelperFn | |
from troposphere import ( | |
AWS_ACCOUNT_ID, | |
AWS_NOTIFICATION_ARNS, | |
AWS_NO_VALUE, | |
AWS_REGION, | |
AWS_STACK_ID, | |
AWS_STACK_NAME, | |
) | |
AWS_PSEUDO_PARAMS = ( | |
AWS_ACCOUNT_ID, | |
AWS_NOTIFICATION_ARNS, | |
AWS_NO_VALUE, | |
AWS_REGION, | |
AWS_STACK_ID, | |
AWS_STACK_NAME, | |
) | |
class Template(BaseTemplate): | |
def validate_refs(self, obj=None): | |
if obj is None: | |
self.validate_refs(self.resources) | |
self.validate_refs(self.conditions) | |
elif isinstance(obj, BaseAWSObject): | |
self.validate_refs(obj.properties) | |
elif isinstance(obj, dict): | |
for name, val in obj.iteritems(): | |
self.validate_refs(val) | |
elif isinstance(obj, list): | |
for item in obj: | |
self.validate_refs(item) | |
elif isinstance(obj, Ref): | |
groups = [self.resources, self.conditions, self.parameters, AWS_PSEUDO_PARAMS] | |
if not any([obj.data.get('Ref') in g for g in groups]): | |
raise ValueError('Invalid reference: %s' % ref_name) | |
elif isinstance(obj, AWSHelperFn): | |
self.validate_refs(obj.data) | |
elif isinstance(obj, basestring): | |
pass | |
else: | |
warnings.warn('Ref Validation::Unknown type %s' % type(obj)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment