-
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
26 changed files
with
719 additions
and
152 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
Empty file.
83 changes: 83 additions & 0 deletions
83
cloud_governance/common/helpers/azure/azure_cleanup_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,83 @@ | ||
|
||
from cloud_governance.common.clouds.azure.compute.compute_operations import ComputeOperations | ||
from cloud_governance.common.clouds.azure.compute.resource_group_operations import ResourceGroupOperations | ||
from cloud_governance.common.helpers.cleanup_operations import AbstractCleanUpOperations | ||
from cloud_governance.common.logger.init_logger import logger | ||
from cloud_governance.common.utils.utils import Utils | ||
|
||
|
||
class AzureCleaUpOperations(AbstractCleanUpOperations): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self._cloud_name = 'Azure' | ||
self.compute_operations = ComputeOperations() | ||
self.resource_group_operations = ResourceGroupOperations() | ||
|
||
def get_tag_name_from_tags(self, tags: dict, tag_name: str): | ||
""" | ||
This method returns the tag value by the tag_name | ||
:param tags: | ||
:type tags: | ||
:param tag_name: | ||
:type tag_name: | ||
:return: | ||
:rtype: | ||
""" | ||
if tags: | ||
for key, value in tags.items(): | ||
if Utils.equal_ignore_case(key, tag_name): | ||
return value | ||
return '' | ||
|
||
def _delete_resource(self, resource_id: str): | ||
""" | ||
This method deletes the | ||
:param resource_id: | ||
:type resource_id: | ||
:return: | ||
:rtype: | ||
""" | ||
action = "deleted" | ||
try: | ||
if self._policy == 'vm_run': | ||
action = "Stopped" | ||
self.compute_operations.stop_vm(resource_id=resource_id) | ||
logger.info(f'{self._policy} {action}: {resource_id}') | ||
except Exception as err: | ||
logger.info(f'Exception raised: {err}: {resource_id}') | ||
|
||
def update_resource_day_count_tag(self, resource_id: str, cleanup_days: int, tags: dict): | ||
tags = self._update_tag_value(tags=tags, tag_name='DaysCount', tag_value=str(cleanup_days)) | ||
try: | ||
if self._policy == 'vm_run': | ||
self.resource_group_operations.creates_or_updates_tags(resource_id=resource_id, tags=tags) | ||
except Exception as err: | ||
logger.info(f'Exception raised: {err}: {resource_id}') | ||
|
||
def _update_tag_value(self, tags: dict, tag_name: str, tag_value: str): | ||
""" | ||
This method returns the updated tag_list by adding the tag_name and tag_value to the tags | ||
@param tags: | ||
@param tag_name: | ||
@param tag_value: | ||
@return: | ||
""" | ||
if self._dry_run == "yes": | ||
tag_value = 0 | ||
tag_value = f'{self.CURRENT_DATE}@{tag_value}' | ||
found = False | ||
updated_tags = {} | ||
if tags: | ||
for key, value in tags.items(): | ||
if Utils.equal_ignore_case(key, tag_name): | ||
if value.split("@")[0] != self.CURRENT_DATE: | ||
updated_tags[key] = tag_value | ||
else: | ||
if int(tag_value.split("@")[-1]) == 0 or int(tag_value.split("@")[-1]) == 1: | ||
updated_tags[key] = tag_value | ||
found = True | ||
tags.update(updated_tags) | ||
if not found: | ||
return {tag_name: tag_value} | ||
return tags |
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 @@ | ||
|
||
import os | ||
|
||
|
||
class Utils: | ||
|
||
def __init__(self): | ||
pass | ||
|
||
@staticmethod | ||
def get_cloud_policies(cloud_name: str, file_type: str = '.py', dir_dict: bool = False, | ||
exclude_policies: list = None): | ||
""" | ||
This method returns the policies by cloud_name | ||
:return: | ||
:rtype: | ||
""" | ||
exclude_policies = [] if not exclude_policies else exclude_policies | ||
policies_dict = {} | ||
policies_list = [] | ||
policies_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'policy', cloud_name) | ||
for (dir_path, _, filenames) in os.walk(policies_path): | ||
immediate_parent = dir_path.split("/")[-1] | ||
for filename in filenames: | ||
if not filename.startswith('__') and filename.endswith(file_type): | ||
filename = os.path.splitext(filename)[0] | ||
if filename not in exclude_policies: | ||
if dir_dict: | ||
policies_dict.setdefault(immediate_parent, []).append(filename) | ||
else: | ||
policies_list.append(filename) | ||
return policies_dict if dir_dict else policies_list | ||
|
||
@staticmethod | ||
def equal_ignore_case(str1: str, str2: str, *args): | ||
""" | ||
This method returns boolean by comparing equal in-case sensitive all strings | ||
:param str1: | ||
:type str1: | ||
:param str2: | ||
:type str2: | ||
:param args: | ||
:type args: | ||
:return: | ||
:rtype: | ||
""" | ||
equal = str1.lower() == str2.lower() | ||
for val in args: | ||
equal = str1.lower() == val.lower() and equal | ||
return equal |
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
Empty file.
Oops, something went wrong.