From 88e3f30e4bbe16175c59dca571815a633825d37c Mon Sep 17 00:00:00 2001 From: Thirumalesh Aaraveti <97395760+athiruma@users.noreply.github.com> Date: Thu, 16 May 2024 17:41:52 +0530 Subject: [PATCH] Added the method for checking nat metrics (#771) --- .../azure/cleanup/unused_nat_gateway.py | 39 +++++++++++-------- .../policy/azure/test_unused_nat_gateway.py | 2 - 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/cloud_governance/policy/azure/cleanup/unused_nat_gateway.py b/cloud_governance/policy/azure/cleanup/unused_nat_gateway.py index 0b5c77e0..91812aca 100644 --- a/cloud_governance/policy/azure/cleanup/unused_nat_gateway.py +++ b/cloud_governance/policy/azure/cleanup/unused_nat_gateway.py @@ -2,7 +2,6 @@ class UnUsedNatGateway(AzurePolicyOperations): - """ This class performs the azure unused nat gateway operations """ @@ -13,6 +12,26 @@ def __init__(self): super().__init__() self.__active_cluster_ids = self._get_active_cluster_ids() + def __check_nat_gateway_metrics(self, resource_id: str): + """ + This method returns bool by verifying nat metrics + :param resource_id: + :type resource_id: + :return: + :rtype: + """ + metrics_data = self.monitor_operations.get_resource_metrics(resource_id=resource_id, + metricnames='SNATConnectionCount', + aggregation='Average') + if metrics_data.get('value'): + metrics_time_series_data = metrics_data.get('value', [])[0].get('timeseries', []) + if metrics_time_series_data: + for metric_time_frame in metrics_time_series_data: + for data in metric_time_frame.get('data'): + if data.get('average', 0) > 0: + return False + return True + def run_policy_operations(self): """ This method returns the list of unused nat gateways @@ -26,20 +45,7 @@ def run_policy_operations(self): cluster_tag = self._get_cluster_tag(tags=tags) cleanup_result = False if cluster_tag not in self.__active_cluster_ids: - metrics_data = self.monitor_operations.get_resource_metrics(resource_id=nat_gateway.get('id'), - metricnames='SNATConnectionCount', - aggregation='Average') - unused = False - if metrics_data.get('value'): - metrics_time_series_data = metrics_data.get('value', [])[0].get('timeseries', []) - if not metrics_time_series_data: - unused = True - else: - for metric_time_frame in metrics_time_series_data: - for data in metric_time_frame.get('data'): - if data.get('average', 0) <= 0: - unused = True - if unused: + if self.__check_nat_gateway_metrics(resource_id=nat_gateway.get('id')): cleanup_days = self.get_clean_up_days_count(tags=tags) cleanup_result = self.verify_and_delete_resource(resource_id=nat_gateway.get('id'), tags=tags, clean_up_days=cleanup_days) @@ -47,7 +53,8 @@ def run_policy_operations(self): user=self.get_tag_name_from_tags(tags=tags, tag_name='User'), skip_policy=self.get_skip_policy_value(tags=tags), cleanup_days=cleanup_days, dry_run=self._dry_run, - name=nat_gateway.get('name'), region=nat_gateway.get('location'), + name=nat_gateway.get('name'), + region=nat_gateway.get('location'), cleanup_result=str(cleanup_result), resource_action=self.RESOURCE_ACTION, cloud_name=self._cloud_name, diff --git a/tests/unittest/cloud_governance/policy/azure/test_unused_nat_gateway.py b/tests/unittest/cloud_governance/policy/azure/test_unused_nat_gateway.py index 8e7f431e..f9bc7548 100644 --- a/tests/unittest/cloud_governance/policy/azure/test_unused_nat_gateway.py +++ b/tests/unittest/cloud_governance/policy/azure/test_unused_nat_gateway.py @@ -4,10 +4,8 @@ from azure.mgmt.monitor import MonitorManagementClient from azure.mgmt.monitor.v2021_05_01.models import TimeSeriesElement, MetricValue from azure.mgmt.network import NetworkManagementClient -from azure.mgmt.network.models import NetworkInterfaceIPConfiguration, PublicIPAddress, IPConfiguration from cloud_governance.main.environment_variables import environment_variables -from cloud_governance.policy.azure.cleanup.ip_unattached import IpUnattached from cloud_governance.policy.azure.cleanup.unused_nat_gateway import UnUsedNatGateway from tests.unittest.configs import SUBSCRIPTION_ID, CURRENT_DATE, NAT_GATEWAY_NAME from tests.unittest.mocks.azure.mock_compute.mock_compute import mock_compute