Skip to content

Commit

Permalink
delete whole azure group
Browse files Browse the repository at this point in the history
  • Loading branch information
laDok8 committed Jun 22, 2022
1 parent f2191e8 commit 2458a28
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion wrapanapi/systems/msazure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import os
from dateutil import parser
from datetime import datetime, timedelta

import pytz
Expand All @@ -15,7 +16,8 @@
from azure.mgmt.iothub import IotHubClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.network.models import NetworkSecurityGroup, SecurityRule
from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
from azure.mgmt.resource import SubscriptionClient
from azure.mgmt.resource.resources import ResourceManagementClient
from azure.mgmt.resource.subscriptions.models import SubscriptionState
from azure.mgmt.storage import StorageManagementClient
from azure.storage.blob import BlockBlobService
Expand Down Expand Up @@ -675,6 +677,13 @@ def list_free_nics(self, nic_template=None, resource_group=None):
return [nic_name for nic_name in all_free_nics if nic_template in nic_name]
return all_free_nics

def list_all_resources_by_resource_group(self, resource_group=None):
"""
List all resources in selected resource_group
"""
resource_group = resource_group or self.resource_group
return list(self.resource_client.resources.list_by_resource_group(resource_group, expand="changedTime,createdTime"))

def list_free_discs(self, disc_template=None, resource_group=None):
"""
List all or specific VMs attached Disc(s) in selected resource_group
Expand Down Expand Up @@ -766,6 +775,44 @@ def list_router(self):
def disconnect(self):
pass

def _age_filter(self, resource, hours_old=0):
now_time = datetime.utcnow().replace(tzinfo=pytz.utc)
start_time = parser.parse(resource.additional_properties['createdTime'])
timediff = now_time - start_time
totalhours = timediff.total_seconds() / 3600

return totalhours > hours_old

def _list_resources(self, resource_group=None, hours_old=0):
"""
Lists group resources older than hours_old
"""
hours_old = float(hours_old)

resources = self.list_all_resources_by_resource_group(resource_group)
return filter(lambda f: self._age_filter(f, hours_old), resources) if bool(hours_old) else resources

def list_resources_from_hours_old(self, resource_group=None, hours_old=0):
"""
Retrieves list of resources older than hours_old numeric value.
Age is calculated as difference of current time and resource creation timestamp.
"""
return [r.name for r in self._list_resources(resource_group, hours_old)]

def remove_resource_group_of_old_resources(self, resource_group=None, hours_old=0):
"""
Used for clean_up jobs to remove group containing resources older than hours_old numeric value.
Age is calculated as difference of current time and resource creation timestamp.
"""

hours_old = float(hours_old)
self.logger.info('Attempting to remove all old resources')
resource_group = resource_group or self.resource_group

resources = self._list_resources(resource_group, hours_old)
if resources:
self.resource_client.resource_groups.delete(resource_group)

def remove_nics_by_search(self, nic_template=None, resource_group=None):
"""
Used for clean_up jobs to remove NIC(s) that are not attached to any test VM
Expand Down

0 comments on commit 2458a28

Please sign in to comment.