Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the method for checking nat metrics #771

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we expect to 0 for unused nat gateway ?

Copy link
Collaborator Author

@athiruma athiruma May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning false, if found any active connection.

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
Loading