-
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
36 changed files
with
1,014 additions
and
429 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
Empty file.
91 changes: 91 additions & 0 deletions
91
cloud_governance/common/helpers/azure/azure_policy_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,91 @@ | ||
|
||
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.abstract_policy_operations import AbstractPolicyOperations | ||
from cloud_governance.common.logger.init_logger import logger | ||
from cloud_governance.common.utils.utils import Utils | ||
|
||
|
||
class AzurePolicyOperations(AbstractPolicyOperations): | ||
|
||
def __init__(self): | ||
self._cloud_name = 'Azure' | ||
self.compute_operations = ComputeOperations() | ||
self.resource_group_operations = ResourceGroupOperations() | ||
super().__init__() | ||
|
||
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 == 'instance_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 == 'instance_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: | ||
tags.update({tag_name: tag_value}) | ||
return tags | ||
|
||
def _get_al_instances(self): | ||
""" | ||
This method returns the all instances list | ||
:return: | ||
:rtype: | ||
""" | ||
return self.compute_operations.get_all_instances() |
Oops, something went wrong.