Skip to content

Commit

Permalink
Azure: add the monitor log operations
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Feb 26, 2024
1 parent 1257fa5 commit 3233644
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 3 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.v2023_03_01.models import VirtualMachine

from cloud_governance.common.clouds.azure.compute.common_operations import CommonOperations
from cloud_governance.common.clouds.azure.common.common_operations import CommonOperations
from cloud_governance.common.logger.logger_time_stamp import logger_time_stamp


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.v2022_09_01.models import ResourceGroup, GenericResourceExpanded, TagsResource, Tags

from cloud_governance.common.clouds.azure.compute.common_operations import CommonOperations
from cloud_governance.common.clouds.azure.common.common_operations import CommonOperations
from cloud_governance.common.logger.logger_time_stamp import logger_time_stamp


Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
from datetime import datetime, timedelta

from azure.core.exceptions import HttpResponseError
from azure.mgmt.monitor import MonitorManagementClient

from cloud_governance.common.clouds.azure.common.common_operations import CommonOperations
from cloud_governance.common.logger.init_logger import logger
from cloud_governance.common.utils.configs import LOOK_BACK_DAYS


class MonitorManagementOperations(CommonOperations):

def __init__(self):
super().__init__()
self.__monitor_client = MonitorManagementClient(credential=self._default_creds,
subscription_id=self._subscription_id)

def __get_end_date(self):
"""
This method returns the end date
:return:
:rtype:
"""
return datetime.utcnow()

def __get_start_date(self):
"""
This method returns the start date
:return:
:rtype:
"""
return self.__get_end_date() - timedelta(LOOK_BACK_DAYS)

def get_audit_records(self, resource_id: str, start_date: datetime = None, end_date: datetime = None):
"""
This method returns the audit record for the resource_id
:param start_date:
:type start_date:
:param end_date:
:type end_date:
:param resource_id:
:type resource_id:
:return:
:rtype:
"""
if not start_date:
start_date = self.__get_start_date()
if not end_date:
end_date = self.__get_end_date()
try:
filter_query = f"eventTimestamp ge '{start_date}'" \
f" and eventTimestamp le '{end_date}' " \
f"and resourceUri eq '{resource_id}'.:code:"
records = self.__monitor_client.activity_logs.list(filter=filter_query)
return self._item_paged_iterator(item_paged_object=records, as_dict=True)
except HttpResponseError as http_error:
logger.error(http_error)
except Exception as err:
logger.error(err)
3 changes: 3 additions & 0 deletions cloud_governance/common/utils/configs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


LOOK_BACK_DAYS = 30
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ azure-identity==1.12.0
azure-mgmt-billing==6.0.0
azure-mgmt-resource==23.0.1
azure-mgmt-compute==30.1.0
azure-mgmt-monitor==6.0.2
azure-mgmt-network==25.0.0
azure-mgmt-costmanagement==3.0.0
azure-mgmt-subscription==3.1.1
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
'urllib3==1.26.7', # required by jira
'azure-mgmt-resource==23.0.1',
'azure-mgmt-compute==30.1.0',
'azure-mgmt-network==25.0.0'
'azure-mgmt-network==25.0.0',
'azure-mgmt-monitor==6.0.2'
],

setup_requires=['pytest', 'pytest-runner', 'wheel', 'coverage'],
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from azure.mgmt.monitor import MonitorManagementClient

from cloud_governance.common.clouds.azure.monitor.monitor_management_operations import MonitorManagementOperations
from tests.unittest.configs import AZURE_RESOURCE_ID
from tests.unittest.mocks.azure.mock_monitor.mock_monitor import mock_monitor


@mock_monitor
def test_monitor_management_operations__get_audit_records():
"""
This method tests the get_start date
:return:
:rtype:
"""
monitor_client = MonitorManagementClient(credential='', subscription_id='')
monitor_client.activity_logs.create_log(
caller='[email protected]',
resource_id=AZURE_RESOURCE_ID
)
monitor_operations = MonitorManagementOperations()

assert len(monitor_operations.get_audit_records(resource_id=AZURE_RESOURCE_ID)) == 1


1 change: 1 addition & 0 deletions tests/unittest/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
SUB_ID = f'/subscription/{SUBSCRIPTION_ID}/resourceGroups/{RESOURCE_GROUP}'
NETWORK_PROVIDER = f'providers/Microsoft.Network'
COMPUTE_PROVIDER = 'providers/Microsoft.Compute'
AZURE_RESOURCE_ID = f'{SUB_ID}/{COMPUTE_PROVIDER}/cloud-governance-unittest'
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from azure.mgmt.monitor.models import EventData

from tests.unittest.mocks.azure.common_operations import CustomItemPaged


class MockEventData(EventData):

def __init__(self, **kwargs):
super().__init__(**kwargs)


class MockActivityLogsOperations:

def __init__(self):
super().__init__()
self.__audit_logs = []

def list(self, filter: str, **kwargs):
"""
This method returns the list of EventData
:param filter:
:type filter:
:param kwargs:
:type kwargs:
:return:
:rtype:
"""
return CustomItemPaged(resource_list=self.__audit_logs)

def create_log(self, **kwargs):
"""
This method creates audit log entry
:param kwargs:
:type kwargs:
:return:
:rtype:
"""
record = MockEventData(**kwargs)
self.__audit_logs.append(record)
return record
28 changes: 28 additions & 0 deletions tests/unittest/mocks/azure/mock_monitor/mock_monitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

from functools import wraps
from unittest.mock import patch

from azure.mgmt.monitor import MonitorManagementClient

from tests.unittest.mocks.azure.mock_monitor.mock_activity_logs_operations import MockActivityLogsOperations


def mock_monitor(method):
"""
This method is mocking for Jira class methods which are used in Jira Operations @param method:
@return:
"""

@wraps(method)
def method_wrapper(*args, **kwargs):
"""
This is the wrapper method to wraps the method inside the function
@param args:
@param kwargs:
@return:
"""
with patch.object(MonitorManagementClient, 'activity_logs', MockActivityLogsOperations()):
result = method(*args, **kwargs)
return result

return method_wrapper
1 change: 1 addition & 0 deletions tests_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ aiohttp==3.8.5
azure-identity==1.12.0
azure-mgmt-costmanagement==3.0.0
azure-mgmt-billing==6.0.0
azure-mgmt-monitor==6.0.2
azure-mgmt-subscription==3.1.1
boto3==1.26.4
elasticsearch==7.11.0
Expand Down

0 comments on commit 3233644

Please sign in to comment.