44# not use this file except in compliance with the License. A copy of the
55# License is located at
66#
7- # http://aws.amazon.com/apache2.0/
7+ # http://aws.amazon.com/apache2.0/
88#
99# or in the "license" file accompanying this file. This file is distributed
1010# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
1111# express or implied. See the License for the specific language governing
1212# permissions and limitations under the License.
13+
1314"""Cleans up the resources created by the Elasticache bootstrapping process.
1415"""
1516
1617import boto3
1718import logging
18-
19- from acktest import resources
2019from e2e import bootstrap_directory
21- from e2e .bootstrap_resources import TestBootstrapResources , read_bootstrap_config
22-
23- def delete_sns_topic (topic_ARN : str ):
24- sns = boto3 .client ("sns" )
25- sns .delete_topic (TopicArn = topic_ARN )
26- logging .info (f"Deleted SNS topic { topic_ARN } " )
27-
28- def delete_security_group (sg_id : str ):
29- ec2 = boto3 .client ("ec2" )
30- ec2 .delete_security_group (GroupId = sg_id )
31- logging .info (f"Deleted VPC Security Group { sg_id } " )
32-
33- def delete_user_group (usergroup_id : str ):
34- ec = boto3 .client ("elasticache" )
35- ec .delete_user_group (UserGroupId = usergroup_id )
36- logging .info (f"Deleted ElastiCache User Group { usergroup_id } " )
20+ from e2e .bootstrap_resources import TestBootstrapResources
3721
38- # KMS does not allow immediate key deletion; 7 days is the shortest deletion window
39- def delete_kms_key (key_id : str ):
40- kms = boto3 .client ("kms" )
41- kms .schedule_key_deletion (KeyId = key_id , PendingWindowInDays = 7 )
42- logging .info (f"Deletion scheduled for KMS key { key_id } " )
4322
44- # delete snapshot and also associated cluster/RG
4523def delete_snapshot (snapshot_name : str ):
24+ if not snapshot_name :
25+ return
26+
4627 ec = boto3 .client ("elasticache" )
4728
4829 # delete actual snapshot
@@ -55,97 +36,48 @@ def delete_snapshot(snapshot_name: str):
5536 if snapshot ['CacheClusterId' ]:
5637 ec .delete_cache_cluster (CacheClusterId = snapshot ['CacheClusterId' ])
5738 logging .info (f"Deleted cache cluster { snapshot ['CacheClusterId' ]} " )
58- elif snapshot ['ReplicationGroupId' ]: # should not happen
59- ec .delete_replication_group (ReplicationGroupId = snapshot ['ReplicationGroupId' ])
60- logging .info (f"Deleted replication group { snapshot ['ReplicationGroupId' ]} " )
6139
6240
63- def delete_non_default_user (user_id : str ):
64- ec = boto3 .client ("elasticache" )
65- ec .delete_user (UserId = user_id )
66- logging .info (f"Deleted non default user { user_id } " )
41+ def delete_kms_key (key_id : str ):
42+ if not key_id :
43+ return
6744
45+ kms = boto3 .client ("kms" )
46+ kms .schedule_key_deletion (KeyId = key_id , PendingWindowInDays = 7 )
47+ logging .info (f"Scheduled deletion for KMS key { key_id } " )
6848
69- def delete_log_group (log_group_name : str ):
70- logs = boto3 .client ("logs" )
71- logs .delete_log_group (logGroupName = log_group_name )
72- logging .info (f"Deleted CW log group { log_group_name } " )
7349
50+ def delete_non_default_user (user_id : str ):
51+ if not user_id :
52+ return
7453
75- def delete_cpg (cpg_name : str ):
7654 ec = boto3 .client ("elasticache" )
77- ec .delete_cache_parameter_group ( CacheParameterGroupName = cpg_name )
78- logging .info (f"Deleted ElastiCache cache parameter group { cpg_name } " )
55+ ec .delete_user ( UserId = user_id )
56+ logging .info (f"Deleted non default user { user_id } " )
7957
8058
8159def service_cleanup (config : dict ):
8260 logging .getLogger ().setLevel (logging .INFO )
8361
84- resources = TestBootstrapResources (
85- ** config
86- )
87-
88- try :
89- delete_sns_topic (resources .SnsTopic1 )
90- except :
91- logging .exception (f"Unable to delete SNS topic { resources .SnsTopic1 } " )
62+ resources = TestBootstrapResources (** config )
9263
9364 try :
94- delete_sns_topic (resources .SnsTopic2 )
65+ delete_snapshot (resources .Snapshot )
9566 except :
96- logging .exception (f"Unable to delete SNS topic { resources .SnsTopic2 } " )
97-
98- try :
99- delete_security_group (resources .SecurityGroup1 )
100- except :
101- logging .exception (f"Unable to delete VPC Security Group { resources .SecurityGroup1 } " )
102-
103- try :
104- delete_security_group (resources .SecurityGroup2 )
105- except :
106- logging .exception (f"Unable to delete VPC Security Group { resources .SecurityGroup2 } " )
107-
108- try :
109- delete_user_group (resources .UserGroup1 )
110- except :
111- logging .exception (f"Unable to delete ElastiCache User Group { resources .UserGroup1 } " )
112-
113- try :
114- delete_user_group (resources .UserGroup2 )
115- except :
116- logging .exception (f"Unable to delete ElastiCache User Group { resources .UserGroup2 } " )
67+ logging .exception (f"Unable to delete snapshot { resources .Snapshot } " )
11768
11869 try :
11970 delete_kms_key (resources .KmsKeyID )
12071 except :
121- logging .exception (f"Unable to schedule deletion for KMS key { resources .KmsKeyID } " )
122-
123- try :
124- delete_snapshot (resources .SnapshotName )
125- except :
126- logging .exception (f"Unable to delete snapshot { resources .SnapshotName } " )
72+ logging .exception (
73+ f"Unable to schedule deletion for KMS key { resources .KmsKeyID } " )
12774
12875 try :
12976 delete_non_default_user (resources .NonDefaultUser )
13077 except :
13178 logging .exception (f"Unable to delete user { resources .NonDefaultUser } " )
13279
133- try :
134- delete_log_group (resources .CWLogGroup1 )
135- except :
136- logging .exception (f"Unable to delete CW log group { resources .CWLogGroup1 } " )
137-
138- try :
139- delete_log_group (resources .CWLogGroup2 )
140- except :
141- logging .exception (f"Unable to delete CW log group { resources .CWLogGroup2 } " )
142-
143- try :
144- delete_cpg (resources .CPGName )
145- except :
146- logging .exception (f"Unable to delete Elasticache cache parameter group { resources .CPGName } " )
147-
14880
14981if __name__ == "__main__" :
150- bootstrap_config = read_bootstrap_config (bootstrap_directory )
151- service_cleanup (bootstrap_config )
82+ config = TestBootstrapResources . deserialize (bootstrap_directory )
83+ service_cleanup (config . __dict__ )
0 commit comments