Skip to content

Commit 09c58d7

Browse files
Merge pull request #466 from ogajduse/feat/improve-google-system-find-templates
Make GoogleCloudSystem.find_templates more memory-efficient by using a filter
2 parents 62c243b + b470050 commit 09c58d7

File tree

1 file changed

+78
-11
lines changed

1 file changed

+78
-11
lines changed

wrapanapi/systems/google.py

+78-11
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,28 @@ def get_vm(self, name, zone=None, try_all_zones=False):
581581
def create_vm(self):
582582
raise NotImplementedError
583583

584-
def list_templates(self, include_public=False, public_projects=None):
584+
def _list_templates(
585+
self,
586+
include_public=False,
587+
public_projects=None,
588+
filter_expr="",
589+
order_by=None,
590+
max_results=None,
591+
):
592+
"""
593+
List all templates in the GCE account.
594+
595+
This method is used by both list_templates and find_templates.
596+
597+
Args:
598+
include_public: Include public images in search
599+
public_projects: List of projects to search for public images
600+
filter_expr: Filter expression to use in search
601+
order_by: Order by expression
602+
max_results: Maximum number of results to return
603+
Returns:
604+
List of GoogleCloudImage objects
605+
"""
585606
images = self._compute.images()
586607
results = []
587608
projects = [self._project]
@@ -593,11 +614,29 @@ def list_templates(self, include_public=False, public_projects=None):
593614
for project in projects:
594615
results.extend(
595616
GoogleCloudImage(system=self, raw=image, project=project, name=image['name'])
596-
for image in
597-
images.list(project=project).execute().get('items', [])
617+
for image in images.list(
618+
project=project,
619+
filter=filter_expr,
620+
orderBy=order_by,
621+
maxResults=max_results,
622+
)
623+
.execute()
624+
.get("items", [])
598625
)
599626
return results
600627

628+
def list_templates(self, include_public=False, public_projects=None):
629+
"""
630+
List images available.
631+
632+
Args:
633+
include_public: Include public images in search
634+
public_projects: List of projects to search for public images
635+
Returns:
636+
List of GoogleCloudImage objects
637+
"""
638+
return self._list_templates(include_public=include_public, public_projects=public_projects)
639+
601640
def get_template(self, name, project=None):
602641
if not project:
603642
project = self._project
@@ -606,19 +645,47 @@ def get_template(self, name, project=None):
606645
return GoogleCloudImage(system=self, raw=image, project=project, name=name)
607646
except errors.HttpError as error:
608647
if error.resp.status == 404:
609-
raise ImageNotFoundError("'{}' not found in project '{}'".format(name, project))
648+
raise ImageNotFoundError(
649+
"'{}' not found in project '{}'".format(name, project)
650+
)
610651
else:
611652
raise
612653

613-
def find_templates(self, name, include_public=False):
654+
def find_templates(
655+
self,
656+
name=None,
657+
include_public=False,
658+
public_projects=None,
659+
filter_expr=None,
660+
order_by=None,
661+
max_results=None,
662+
):
614663
"""
615-
Find templates with 'name' in any project
664+
Find templates with 'name' or by a 'filter_expr' in any project.
665+
666+
If both 'name' and 'filter_expr' are specified, 'name' is used and 'filter_expr' is ignored.
667+
668+
Args:
669+
name: Name of the GoogleCloudImage to search for
670+
include_public: Include public images in search
671+
filter_expr: Filter expression to use in search
672+
order_by: Order by expression
673+
max_results: Maximum number of results to return
674+
Returns:
675+
List of GoogleCloudImage objects
616676
"""
617-
# TODO: Possibly expand this to truly "find" something using regex, filters, etc.
618-
return [
619-
image for image in self.list_templates(include_public=include_public)
620-
if image.name == name
621-
]
677+
if name:
678+
filter_expr = f"name={name}"
679+
elif not filter_expr:
680+
raise ValueError("Either 'name' or 'filter_expr' must be specified")
681+
682+
return self._list_templates(
683+
filter_expr=filter_expr,
684+
include_public=include_public,
685+
public_projects=public_projects,
686+
order_by=order_by,
687+
max_results=max_results,
688+
)
622689

623690
def create_template(self, name, bucket_url, timeout=360):
624691
"""

0 commit comments

Comments
 (0)