-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure: add the monitor log operations
- Loading branch information
Showing
17 changed files
with
162 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
cloud_governance/common/clouds/azure/compute/compute_operations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
60 changes: 60 additions & 0 deletions
60
cloud_governance/common/clouds/azure/monitor/monitor_management_operations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
LOOK_BACK_DAYS = 30 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions
24
...ittest/cloud_governance/common/clouds/azure/monitor/test_monitor_management_operations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
40 changes: 40 additions & 0 deletions
40
tests/unittest/mocks/azure/mock_monitor/mock_activity_logs_operations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters