From 58dbe5bd99c8d9ca8821c26582e1b7221fd2107e Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti <97395760+athiruma@users.noreply.github.com> Date: Tue, 20 Aug 2024 13:53:51 +0530 Subject: [PATCH] Fixed the count zombie_cluster resources (#822) --- .../zombie_cluster/zombie_cluster_common_methods.py | 12 +++++++++--- .../test_zombie_ec2_cluster_delete_resource.py | 4 ++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cloud_governance/policy/policy_operations/aws/zombie_cluster/zombie_cluster_common_methods.py b/cloud_governance/policy/policy_operations/aws/zombie_cluster/zombie_cluster_common_methods.py index e10e5ada..001a77f2 100644 --- a/cloud_governance/policy/policy_operations/aws/zombie_cluster/zombie_cluster_common_methods.py +++ b/cloud_governance/policy/policy_operations/aws/zombie_cluster/zombie_cluster_common_methods.py @@ -129,13 +129,19 @@ def _get_tags_of_zombie_resources(self, resources: list, resource_id_name: str, cluster_delete_days = self.get_tag_name_from_tags(tags=tags, tag_name='ClusterDeleteDays') if cluster_delete_days: if resource_id in zombies: - tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', - tag_value=str(int(cluster_delete_days) + 1)) + if self.dry_run == 'no': + tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', + tag_value=str(int(cluster_delete_days) + 1)) + else: + tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', tag_value=str(0)) else: tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', tag_value=str(0)) else: if resource_id in zombies: - tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', tag_value=str(1)) + if self.dry_run == 'no': + tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', tag_value=str(1)) + else: + tags = self.update_resource_tags(tags=tags, tag_name='ClusterDeleteDays', tag_value=str(0)) if old_tags != tags: try: if aws_service == 'ec2': diff --git a/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_ec2_cluster_delete_resource.py b/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_ec2_cluster_delete_resource.py index 8f245316..4e67686d 100644 --- a/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_ec2_cluster_delete_resource.py +++ b/tests/unittest/cloud_governance/aws/zombie_cluster/test_zombie_ec2_cluster_delete_resource.py @@ -1,8 +1,11 @@ import boto3 import pytest from moto import mock_ec2, mock_elb, mock_elbv2, mock_s3 + +from cloud_governance.main.environment_variables import environment_variables from cloud_governance.policy.aws.zombie_cluster_resource import ZombieClusterResources from cloud_governance.common.clouds.aws.ec2.ec2_operations import EC2Operations +from tests.unittest.configs import DRY_RUN_YES, DRY_RUN_NO tags = [ {'Key': 'kubernetes.io/cluster/unittest-test-cluster', 'Value': 'Owned'}, @@ -61,6 +64,7 @@ def test_delete_ec2_ami_after_seven(): This method tests the deletion of AMI image after seven days :return: """ + environment_variables.environment_variables_dict['dry_run'] = DRY_RUN_NO ec2_client = boto3.client('ec2', region_name=region_name) default_ami_id = 'ami-03cf127a' ec2_resource = boto3.resource('ec2', region_name=region_name)