|
| 1 | +from azure.mgmt.resource import ResourceManagementClient |
| 2 | +from cloud_governance.common.logger.init_logger import logger |
| 3 | +from cloud_governance.common.clouds.azure.subscriptions.azure_operations import AzureOperations |
| 4 | + |
| 5 | + |
| 6 | +class TagAzureResourceGroup: |
| 7 | + def __init__(self): |
| 8 | + """ |
| 9 | + Initialize Azure clients and set the tags to apply. |
| 10 | +
|
| 11 | + :param tags_to_add: Dictionary of tags to add to resource groups and their resources. |
| 12 | + """ |
| 13 | + self.azure_operations = AzureOperations() |
| 14 | + self.subscription_id = self.azure_operations.subscription_id |
| 15 | + self.credential = self.azure_operations._AzureOperations__default_creds # reuse credentials |
| 16 | + self.resource_client = ResourceManagementClient(self.credential, self.subscription_id) |
| 17 | + self.tags_to_add = self.azure_operations.global_tags |
| 18 | + |
| 19 | + def tag_all(self): |
| 20 | + """ |
| 21 | + Tag all resource groups and their contained resources with the specified tags. |
| 22 | + """ |
| 23 | + resource_groups = self.resource_client.resource_groups.list() |
| 24 | + |
| 25 | + for rg in resource_groups: |
| 26 | + logger.info(f"Processing resource group: {rg.name}") |
| 27 | + |
| 28 | + # Tag the resource group |
| 29 | + try: |
| 30 | + self.resource_client.resource_groups.update( |
| 31 | + resource_group_name=rg.name, |
| 32 | + parameters={"tags": self.tags_to_add} |
| 33 | + ) |
| 34 | + logger.info(f"Tagged resource group: {rg.name}") |
| 35 | + except Exception as e: |
| 36 | + logger.error(f"Failed to tag resource group {rg.name}: {e}") |
| 37 | + continue |
| 38 | + |
| 39 | + # Tag all resources in the resource group |
| 40 | + for resource in self.resource_client.resources.list_by_resource_group(rg.name): |
| 41 | + logger.info(f" Tagging resource: {resource.name} ({resource.type})") |
| 42 | + |
| 43 | + existing_tags = resource.tags or {} |
| 44 | + updated_tags = {**existing_tags, **self.tags_to_add} |
| 45 | + |
| 46 | + try: |
| 47 | + provider_ns, resource_type_str = resource.type.split('/') |
| 48 | + provider = self.resource_client.providers.get(provider_ns) |
| 49 | + resource_type_info = next( |
| 50 | + rt for rt in provider.resource_types if rt.resource_type.lower() == resource_type_str.lower() |
| 51 | + ) |
| 52 | + api_version = next( |
| 53 | + (v for v in resource_type_info.api_versions if 'preview' not in v.lower()), |
| 54 | + resource_type_info.api_versions[0] |
| 55 | + ) |
| 56 | + |
| 57 | + poller = self.resource_client.resources.begin_update_by_id( |
| 58 | + resource_id=resource.id, |
| 59 | + api_version=api_version, |
| 60 | + parameters={ |
| 61 | + 'location': resource.location, |
| 62 | + 'tags': updated_tags |
| 63 | + } |
| 64 | + ) |
| 65 | + poller.result() |
| 66 | + logger.info(f" Tagged resource: {resource.name}") |
| 67 | + except Exception as e: |
| 68 | + logger.error(f" Failed to tag resource {resource.name}: {e}") |
| 69 | + |
| 70 | + def run(self): |
| 71 | + """ |
| 72 | + This method tags all Azure resources. |
| 73 | + """ |
| 74 | + self.tag_all() |
0 commit comments