@@ -581,7 +581,28 @@ def get_vm(self, name, zone=None, try_all_zones=False):
581
581
def create_vm (self ):
582
582
raise NotImplementedError
583
583
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
+ """
585
606
images = self ._compute .images ()
586
607
results = []
587
608
projects = [self ._project ]
@@ -593,11 +614,29 @@ def list_templates(self, include_public=False, public_projects=None):
593
614
for project in projects :
594
615
results .extend (
595
616
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" , [])
598
625
)
599
626
return results
600
627
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
+
601
640
def get_template (self , name , project = None ):
602
641
if not project :
603
642
project = self ._project
@@ -606,19 +645,47 @@ def get_template(self, name, project=None):
606
645
return GoogleCloudImage (system = self , raw = image , project = project , name = name )
607
646
except errors .HttpError as error :
608
647
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
+ )
610
651
else :
611
652
raise
612
653
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
+ ):
614
663
"""
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
616
676
"""
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
+ )
622
689
623
690
def create_template (self , name , bucket_url , timeout = 360 ):
624
691
"""
0 commit comments