-
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.
Added the azure volume unattached (#715)
- Loading branch information
Showing
17 changed files
with
378 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
|
||
from cloud_governance.policy.helpers.aws.aws_policy_operations import AWSPolicyOperations | ||
from cloud_governance.common.utils.utils import Utils | ||
|
||
|
||
class UnattachedVolume(AWSPolicyOperations): | ||
|
||
RESOURCE_ACTION = "Delete" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def run_policy_operations(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', []) | ||
resource_id = volume.get('VolumeId') | ||
cleanup_result = False | ||
if Utils.equal_ignore_case(volume.get('State'), 'available'): | ||
cleanup_days = self.get_clean_up_days_count(tags=tags) | ||
cleanup_result = self.verify_and_delete_resource(resource_id=resource_id, tags=tags, | ||
clean_up_days=cleanup_days) | ||
resource_data = self._get_es_schema(resource_id=resource_id, | ||
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=self.get_tag_name_from_tags(tags=tags, tag_name='Name'), | ||
region=self._region, | ||
cleanup_result=str(cleanup_result), | ||
resource_action=self.RESOURCE_ACTION, | ||
cloud_name=self._cloud_name, | ||
resource_type=volume.get('VolumeType', ''), | ||
resource_state=volume.get('State') if not cleanup_result else "Deleted", | ||
volume_size=f"{volume.get('Size')} GB" | ||
) | ||
unattached_volumes.append(resource_data) | ||
else: | ||
cleanup_days = 0 | ||
if not cleanup_result: | ||
self.update_resource_day_count_tag(resource_id=resource_id, cleanup_days=cleanup_days, tags=tags) | ||
|
||
return unattached_volumes | ||
|
This file was deleted.
Oops, something went wrong.
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
49 changes: 49 additions & 0 deletions
49
cloud_governance/policy/azure/cleanup/unattached_volume.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,49 @@ | ||
|
||
from cloud_governance.policy.helpers.azure.azure_policy_operations import AzurePolicyOperations | ||
from cloud_governance.common.utils.utils import Utils | ||
|
||
|
||
class UnattachedVolume(AzurePolicyOperations): | ||
|
||
RESOURCE_ACTION = "Delete" | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
def run_policy_operations(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 Utils.equal_ignore_case(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
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
Oops, something went wrong.