Skip to content

Commit

Permalink
Merge pull request #465 from akhil-jha/add_image_removal
Browse files Browse the repository at this point in the history
Image removal for azure and aws
  • Loading branch information
mshriver authored Aug 7, 2023
2 parents 09c58d7 + 04462a9 commit 4db24f1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
29 changes: 29 additions & 0 deletions wrapanapi/systems/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,35 @@ def list_templates(self, executable_by_me=True, owned_by_me=True, public=False):
return [EC2Image(system=self, raw=self.ec2_resource.Image(image["ImageId"]))
for image in images]

def list_free_images(self, image_list=None):
"""
Returns images which don't have a VM associated to it
Args:
image_list (list): List of ids of all images in resource group
"""
free_images = []
vm_list = self.list_vms()

if not vm_list:
# No VMs using the images, images are free
return image_list

for vm in vm_list:
if vm.raw.image_id not in image_list:
free_images.append(vm.raw.image_id)
return free_images

def delete_images(self, image_list=None):
"""
Deletes images by ID
Args:
image_list (list): ["imageID_1", "imageID_2"]
"""
for image in image_list:
img=EC2Image(system=self, raw=self.ec2_resource.Image(image)).delete()

def find_templates(self, name=None, id=None, executable_by_me=True, owned_by_me=True,
public=False, filters=None):
"""
Expand Down
51 changes: 51 additions & 0 deletions wrapanapi/systems/msazure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,42 @@ def list_compute_images(self):
return self.resource_client.resources.list(
filter="resourceType eq 'Microsoft.Compute/images'")

def list_compute_images_by_resource_group(self, resource_group=None, free_images=None):
"""
Args:
resource_group (str): Name of the resource group
free_images (bool): Whether to collect image which do not have any resource(VM) linked to it
"""
resource_group = resource_group or self.resource_group
image_list = list(
self.resource_client.resources.list(
filter=f"resourceType eq 'Microsoft.Compute/images' and resourceGroup eq '{resource_group}'"
)
)

if not free_images:
return image_list

vm_list = self.list_vms(resource_group=resource_group)
if not vm_list:
return image_list

images_used_by_vm = []
for vm_name in vm_list:
images_used_by_vm.append(
self.compute_client.virtual_machines.get(
resource_group_name=resource_group,
vm_name=vm_name
).storage_profile.image_reference.id
)

images_with_no_resources = []
for image in image_list:
if image.id not in images_used_by_vm:
images_with_no_resources.append(image)

return images_with_no_resources

def list_all_image_names(self):
blob_image_names = [item.name for item in self.find_templates()]
compute_image_names = [item.name for item in self.list_compute_images()]
Expand Down Expand Up @@ -1130,6 +1166,21 @@ def delete_stack_by_date(self, days_old, resource_group=None):
result)
return results

def delete_compute_image_by_resource_group(self, resource_group=None, image_list=None):
"""
Delete compute images by resource group
:resource_group: (str) Name of the resource group. "FooBar"
:image_list: (list) List of images. ["imagename1", "imagename2", "imagename3"]
"""
result = []
resource_group = resource_group or self.resource_group
for image in image_list:
self.logger.info("Deleting '%s' from '%s'", image, resource_group)
response = self.compute_client.images.delete(resource_group_name=resource_group, image_name=image)
result.append((image, response))
return result

def list_stack_resources(self, stack_name, resource_group=None):
self.logger.info("Checking Stack %s resources ", stack_name)
# todo: weird implementation to refactor this method later
Expand Down

0 comments on commit 4db24f1

Please sign in to comment.