3 D\@s<dZddlmZddlmZddlmZGdddeZdS)a Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ) bool_compare)CloudFormationLintRule) RuleMatchcsXeZdZdZdZdZdZdZddgZfdd Z d d Z d d Z ddZ ddZ ZS)CacheClusterFailoverz+Check automatic failover on a cache clusterZE3026z*Check Elastic Cache Redis Cluster settingszeEvaluate Redis Cluster groups to make sure automatic failover is enabled when cluster mode is enabledz*https://github.com/awslabs/cfn-python-lint resourcesZ elasticcachecstt|j|jjddS)ZInitz"AWS::ElastiCache::ReplicationGroupN)superr__init__Zresource_property_typesappend)self) __class__/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/rules/resources/elasticache/CacheClusterFailover.pyrszCacheClusterFailover.__init__cCs8t|tr4x(|jD]\}}|dkr|dkrdSqWdS)zTest if cluster is enabled zcluster-enabledyesTF) isinstancedictitems)r propertiesZ property_nameZproperty_valuer r r is_cluster_enabled$s  z'CacheClusterFailover.is_cluster_enabledc Csg}|j|j|}|rFx.|jD]"\} } | |kr | |j| kr |Sq W|j|j||r|j||} | jd} t| dr|dddg} |dkrd}|jt| |j dj t t | n@d}dj dd |jD}|jt| |j |dj t t | | jd }|s| jd d }|d kr|ddd g} |dkrfd}|jt| |j dj t t | n@d}dj dd |jD}|jt| |j |dj t t | |S)z test for each scenario ZAutomaticFailoverEnabledFNzT"AutomaticFailoverEnabled" must be misssing or True when setting up a cluster at {0}/z]"AutomaticFailoverEnabled" must be misssing or True when setting up a cluster when {0} at {1}z and cSsg|]\}}d||fqS)zwhen condition "%s" is %sr ).0kvr r r Csz?CacheClusterFailover._test_cluster_settings..Z NumNodeGroupsZNumCacheClustersrzJ"NumCacheClusters" must be greater than one when creating a cluster at {0}zS"NumCacheClusters" must be greater than one when creating a cluster when {0} at {1}cSsg|]\}}d||fqS)zwhen condition "%s" is %sr )rrrr r r rRs) Zget_conditions_from_pathtemplatergetrZget_value_from_scenariorr rformatjoinmapstr)r rpath pg_propertiespg_pathcfnscenarioresultsZ pg_conditionsZc_nameZc_valueZc_propsZautomatic_failoverZ pathmessagemessageZ scenario_textZnum_node_groupsZnum_cache_nodesr r r _test_cluster_settings-sD           z+CacheClusterFailover._test_cluster_settingsc Csg}|jjdij|ijdijdi}|j||g}|rfxB|D]} |j|j|||||| qBWn|j|j|||||d|S)zE Test cluster settings for the parameter group and Replication Group Z ResourcesZ PropertiesN)rrZ$get_conditions_scenarios_from_objectextendr') r rr Zpg_resource_namer"r#r%r!Z scenariosr$r r r test_cluster_settingsXs& z*CacheClusterFailover.test_cluster_settingsc Csg}|jdd|}xh|D]`}|d}|d} t|trx@|jD]4\} } | dkr@| |jkr@|j|j||| | |q@WqW|S)zCheck CloudFormation PropertiesZCacheParameterGroupNamerrZRef)Zget_saferrr get_resourcesr(r)) r r_r r#matchesZparameter_groupsZparameter_groupZpg_valuer"Zpg_keyZ pg_resourcer r r match_resource_propertiesis  z.CacheClusterFailover.match_resource_properties)__name__ __module__ __qualname____doc__idZ shortdesc descriptionZ source_urltagsrrr'r)r. __classcell__r r )r r rs  +rN)r2Zcfnlint.helpersrZcfnlintrrrr r r r s