-
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.
- Loading branch information
Showing
15 changed files
with
264 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
25 changes: 25 additions & 0 deletions
25
cloud_governance/policy/abstract_policies/cleanup/abstract_volumes_unattached.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,25 @@ | ||
from abc import ABC | ||
|
||
|
||
class AbstractUnattachedVolumes(ABC): | ||
|
||
RESOURCE_ACTION = 'Delete' | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def _volumes_unattached(self): | ||
""" | ||
This method returns the unattached available volumes | ||
:return: | ||
:rtype: | ||
""" | ||
raise NotImplementedError("This method not yet implemented") | ||
|
||
def run(self): | ||
""" | ||
This method starts the unattached volume operations | ||
:return: | ||
:rtype: | ||
""" | ||
return self._volumes_unattached() |
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
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
48 changes: 48 additions & 0 deletions
48
cloud_governance/policy/azure/cleanup/volumes_unattached.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,48 @@ | ||
import json | ||
|
||
from cloud_governance.common.helpers.azure.azure_policy_operations import AzurePolicyOperations | ||
from cloud_governance.policy.abstract_policies.cleanup.abstract_volumes_unattached import AbstractUnattachedVolumes | ||
|
||
|
||
class VolumesUnattached(AbstractUnattachedVolumes, AzurePolicyOperations): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def _volumes_unattached(self): | ||
""" | ||
This method returns the list of unattached volumes | ||
:return: | ||
:rtype: | ||
""" | ||
unattached_volumes = [] | ||
available_volumes = self._get_all_volumes() | ||
for volume in available_volumes: | ||
tags = volume.get('tags') | ||
cleanup_result = False | ||
if volume.get('disk_state') == 'Unattached': | ||
cleanup_days = self.get_clean_up_days_count(tags=tags) | ||
cleanup_result = self.verify_and_delete_resource( | ||
resource_id=volume.get('id'), tags=tags, | ||
clean_up_days=cleanup_days) | ||
resource_data = self._get_es_schema(resource_id=volume.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=volume.get('name'), region=volume.get('location'), | ||
cleanup_result=str(cleanup_result), | ||
resource_action=self.RESOURCE_ACTION, | ||
cloud_name=self._cloud_name, | ||
resource_type=f"{volume.get('sku', {}).get('tier')} " | ||
f"{volume.get('sku', {}).get('name')}", | ||
resource_state=volume.get('disk_state') if not cleanup_result else "Deleted", | ||
volume_size=f"{volume.get('disk_size_gb')} GB" | ||
) | ||
unattached_volumes.append(resource_data) | ||
else: | ||
cleanup_days = 0 | ||
if not cleanup_result: | ||
self.update_resource_day_count_tag(resource_id=volume.get("id"), cleanup_days=cleanup_days, tags=tags) | ||
|
||
return unattached_volumes | ||
|
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
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
83 changes: 83 additions & 0 deletions
83
tests/unittest/cloud_governance/policy/azure/test_volumes_unattached.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,83 @@ | ||
from unittest.mock import Mock, patch | ||
|
||
from azure.mgmt.compute import ComputeManagementClient | ||
|
||
from cloud_governance.main.environment_variables import environment_variables | ||
from cloud_governance.policy.azure.cleanup.volumes_unattached import VolumesUnattached | ||
from tests.unittest.mocks.azure.mock_compute import MockDisk, MockAzure | ||
|
||
|
||
def test_volumes_unattached_dry_run_yes_0_unattached(): | ||
environment_variables.environment_variables_dict['dry_run'] = 'yes' | ||
mock_disk1 = MockDisk(disk_state='Attached', disk_size_gb=4) | ||
mock_azure = MockAzure(disks=[mock_disk1]) | ||
mock_virtual_machines = Mock() | ||
mock_virtual_machines.list.side_effect = mock_azure.mock_list_disks | ||
with patch.object(ComputeManagementClient, 'disks', mock_virtual_machines): | ||
volume_run = VolumesUnattached() | ||
response = volume_run.run() | ||
assert len(response) == 0 | ||
|
||
|
||
def test_volumes_unattached_dry_run_yes(): | ||
environment_variables.environment_variables_dict['dry_run'] = 'yes' | ||
mock_disk1 = MockDisk(disk_state='Unattached', disk_size_gb=4) | ||
mock_azure = MockAzure(disks=[mock_disk1]) | ||
mock_virtual_machines = Mock() | ||
mock_virtual_machines.list.side_effect = mock_azure.mock_list_disks | ||
with patch.object(ComputeManagementClient, 'disks', mock_virtual_machines): | ||
volume_run = VolumesUnattached() | ||
response = volume_run.run() | ||
assert len(response) > 0 | ||
response = response[0] | ||
assert response.get('ResourceDelete') == 'False' | ||
assert response.get('SkipPolicy') == 'NA' | ||
|
||
|
||
def test_volumes_unattached_dry_run_no(): | ||
environment_variables.environment_variables_dict['dry_run'] = 'no' | ||
environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 1 | ||
mock_disk1 = MockDisk(disk_state='Unattached', disk_size_gb=4) | ||
mock_azure = MockAzure(disks=[mock_disk1]) | ||
mock_virtual_machines = Mock() | ||
mock_virtual_machines.list.side_effect = mock_azure.mock_list_disks | ||
with patch.object(ComputeManagementClient, 'disks', mock_virtual_machines): | ||
volume_run = VolumesUnattached() | ||
response = volume_run.run() | ||
assert len(response) > 0 | ||
response = response[0] | ||
assert response.get('ResourceDelete') == 'True' | ||
assert response.get('SkipPolicy') == 'NA' | ||
|
||
|
||
def test_volumes_unattached_dry_run_no_7_days_action(): | ||
environment_variables.environment_variables_dict['dry_run'] = 'no' | ||
environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 7 | ||
mock_disk1 = MockDisk(disk_state='Unattached', disk_size_gb=4) | ||
mock_azure = MockAzure(disks=[mock_disk1]) | ||
mock_virtual_machines = Mock() | ||
mock_virtual_machines.list.side_effect = mock_azure.mock_list_disks | ||
with patch.object(ComputeManagementClient, 'disks', mock_virtual_machines): | ||
volume_run = VolumesUnattached() | ||
response = volume_run.run() | ||
assert len(response) > 0 | ||
response = response[0] | ||
assert response.get('ResourceDelete') == 'False' | ||
assert response.get('SkipPolicy') == 'NA' | ||
|
||
|
||
def test_volumes_unattached_dry_run_no_skip(): | ||
tags = {'Policy': 'notdelete'} | ||
environment_variables.environment_variables_dict['dry_run'] = 'no' | ||
environment_variables.environment_variables_dict['DAYS_TO_TAKE_ACTION'] = 1 | ||
mock_disk1 = MockDisk(disk_state='Unattached', disk_size_gb=4, tags=tags) | ||
mock_azure = MockAzure(disks=[mock_disk1]) | ||
mock_virtual_machines = Mock() | ||
mock_virtual_machines.list.side_effect = mock_azure.mock_list_disks | ||
with patch.object(ComputeManagementClient, 'disks', mock_virtual_machines): | ||
volume_run = VolumesUnattached() | ||
response = volume_run.run() | ||
assert len(response) > 0 | ||
response = response[0] | ||
assert response.get('ResourceDelete') == 'False' | ||
assert response.get('SkipPolicy') == 'NOTDELETE' |
Oops, something went wrong.