Skip to content

Commit

Permalink
Added the method for checking nat metrics (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma authored May 16, 2024
1 parent 76ea6bb commit 88e3f30
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 23 additions & 16 deletions cloud_governance/policy/azure/cleanup/unused_nat_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class UnUsedNatGateway(AzurePolicyOperations):

"""
This class performs the azure unused nat gateway operations
"""
Expand All @@ -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
Expand All @@ -26,28 +45,16 @@ 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)
resource_data = self._get_es_schema(resource_id=nat_gateway.get('name'),
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 88e3f30

Please sign in to comment.