Skip to content

Commit

Permalink
azure: Add support for cleaning up Image Galleries
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Jul 26, 2023
1 parent 1a60608 commit 640c9ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 27 additions & 1 deletion ocw/lib/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from azure.storage.blob import BlobServiceClient
from azure.core.exceptions import ResourceNotFoundError
from msrest.exceptions import AuthenticationError
from dateutil.parser import parse
from webui.PCWConfig import PCWConfig
from .provider import Provider
from ..models import Instance
Expand Down Expand Up @@ -96,6 +97,9 @@ def get_vm_types_in_resource_group(self, resource_group: str) -> str:
return ', '.join(type_set)
return "N/A"

def get_resource_properties(self, resource_id):
return self.resource_mgmt_client().resources.get_by_id(resource_id, api_version="2023-07-03").properties

Check warning on line 101 in ocw/lib/azure.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/azure.py#L101

Added line #L101 was not covered by tests

def list_resource_groups(self) -> list:
return list(self.resource_mgmt_client().resource_groups.list())

Expand All @@ -114,13 +118,18 @@ def list_disks_by_resource_group(self, resource_group):
return self.list_by_resource_group(resource_group,
filters="resourceType eq 'Microsoft.Compute/disks'")

def list_galleries_by_resource_group(self, resource_group):
return self.list_by_resource_group(resource_group,

Check warning on line 122 in ocw/lib/azure.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/azure.py#L122

Added line #L122 was not covered by tests
filters="resourceType eq 'Microsoft.Compute/galleries'")

def list_by_resource_group(self, resource_group, filters=None) -> list:
return list(self.resource_mgmt_client().resources.list_by_resource_group(
resource_group, filter=filters, expand="changedTime"))

def cleanup_all(self) -> None:
self.log_info("Call cleanup_all")
self.cleanup_images_from_rg()
self.cleanup_versions_from_rg()
self.cleanup_disks_from_rg()
self.cleanup_blob_containers()

Expand Down Expand Up @@ -172,10 +181,27 @@ def cleanup_disks_from_rg(self) -> None:
for item in self.list_disks_by_resource_group(self.__resource_group):
if self.is_outdated(item.changed_time):
if self.compute_mgmt_client().disks.get(self.__resource_group, item.name).managed_by:
self.log_warn(f"Disk is in use - unable delete {item.name}")
self.log_warn(f"Disk is in use - skipping {item.name}")
else:
if self.dry_run:
self.log_info(f"Deletion of disk {item.name} skipped due to dry run mode")
else:
self.log_info(f"Delete disk '{item.name}'")
self.compute_mgmt_client().disks.begin_delete(self.__resource_group, item.name)

def cleanup_versions_from_rg(self) -> None:
self.log_dbg("Call cleanup_versions_from_rg")

Check warning on line 193 in ocw/lib/azure.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/azure.py#L193

Added line #L193 was not covered by tests
# for gallery in self.compute_mgmt_client().galleries.list_by_resource_group(self.__resource_group):
for gallery in self.list_galleries_by_resource_group(self.__resource_group):
for image in self.compute_mgmt_client().gallery_images.list_by_gallery(self.__resource_group, gallery.name):
for version in self.compute_mgmt_client().gallery_image_versions.list_by_gallery_image(

Check warning on line 197 in ocw/lib/azure.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/azure.py#L195-L197

Added lines #L195 - L197 were not covered by tests
self.__resource_group, gallery.name, image.name):
properties = self.get_resource_properties(version.id)
if self.is_outdated(parse(properties['publishingProfile']['publishedDate'])):
if self.dry_run:
self.log_info(f"Deletion of version {gallery.name}/{image.name}/{version.name} skipped due to dry run mode")

Check warning on line 202 in ocw/lib/azure.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/azure.py#L199-L202

Added lines #L199 - L202 were not covered by tests
else:
self.log_info(f"Delete version '{gallery.name}/{image.name}/{version.name}'")
self.compute_mgmt_client().gallery_image_versions.begin_delete(

Check warning on line 205 in ocw/lib/azure.py

View check run for this annotation

Codecov / codecov/patch

ocw/lib/azure.py#L204-L205

Added lines #L204 - L205 were not covered by tests
self.__resource_group, gallery.name, image.name, version.name
) # .wait()
3 changes: 2 additions & 1 deletion tests/test_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,12 @@ def count_call(*args, **kwargs):
monkeypatch.setattr(Azure, 'cleanup_blob_containers', count_call)
monkeypatch.setattr(Azure, 'cleanup_disks_from_rg', count_call)
monkeypatch.setattr(Azure, 'cleanup_images_from_rg', count_call)
monkeypatch.setattr(Azure, 'cleanup_versions_from_rg', count_call)
monkeypatch.setattr(Provider, 'read_auth_json', lambda *args, **kwargs: '{}')

az = Azure('fake')
az.cleanup_all()
assert called == 3
assert called == 4


def test_check_credentials(monkeypatch):
Expand Down

0 comments on commit 640c9ce

Please sign in to comment.