22from datetime import datetime
33from enum import Enum
44from pathlib import Path
5- from typing import Any , List , Optional , Set , Union
5+ from typing import Any , Callable , List , Optional , Set , Union
66
77from sqlalchemy import ( # type: ignore
88 BigInteger ,
@@ -560,7 +560,7 @@ def update_infra(self, infra: Infra, project: str, commit: bool = True):
560560 )
561561
562562 def get_infra (self , project : str , allow_cache : bool = False ) -> Infra :
563- return self ._get_object (
563+ infra_object = self ._get_object (
564564 managed_infra ,
565565 "infra_obj" ,
566566 project ,
@@ -570,6 +570,8 @@ def get_infra(self, project: str, allow_cache: bool = False) -> Infra:
570570 "infra_proto" ,
571571 None ,
572572 )
573+ infra_object = infra_object or InfraProto ()
574+ return Infra .from_proto (infra_object )
573575
574576 def apply_user_metadata (
575577 self ,
@@ -683,11 +685,18 @@ def commit(self):
683685 pass
684686
685687 def _apply_object (
686- self , table , project : str , id_field_name , obj , proto_field_name , name = None
688+ self ,
689+ table : Table ,
690+ project : str ,
691+ id_field_name ,
692+ obj ,
693+ proto_field_name ,
694+ name = None ,
687695 ):
688696 self ._maybe_init_project_metadata (project )
689697
690- name = name or obj .name
698+ name = name or obj .name if hasattr (obj , "name" ) else None
699+ assert name , f"name needs to be provided for { obj } "
691700 with self .engine .connect () as conn :
692701 update_datetime = datetime .utcnow ()
693702 update_time = int (update_datetime .timestamp ())
@@ -749,7 +758,14 @@ def _maybe_init_project_metadata(self, project):
749758 conn .execute (insert_stmt )
750759 usage .set_current_project_uuid (new_project_uuid )
751760
752- def _delete_object (self , table , name , project , id_field_name , not_found_exception ):
761+ def _delete_object (
762+ self ,
763+ table : Table ,
764+ name : str ,
765+ project : str ,
766+ id_field_name : str ,
767+ not_found_exception : Optional [Callable ],
768+ ):
753769 with self .engine .connect () as conn :
754770 stmt = delete (table ).where (
755771 getattr (table .c , id_field_name ) == name , table .c .project_id == project
@@ -763,14 +779,14 @@ def _delete_object(self, table, name, project, id_field_name, not_found_exceptio
763779
764780 def _get_object (
765781 self ,
766- table ,
767- name ,
768- project ,
769- proto_class ,
770- python_class ,
771- id_field_name ,
772- proto_field_name ,
773- not_found_exception ,
782+ table : Table ,
783+ name : str ,
784+ project : str ,
785+ proto_class : Any ,
786+ python_class : Any ,
787+ id_field_name : str ,
788+ proto_field_name : str ,
789+ not_found_exception : Optional [ Callable ] ,
774790 ):
775791 self ._maybe_init_project_metadata (project )
776792
@@ -782,10 +798,18 @@ def _get_object(
782798 if row :
783799 _proto = proto_class .FromString (row [proto_field_name ])
784800 return python_class .from_proto (_proto )
785- raise not_found_exception (name , project )
801+ if not_found_exception :
802+ raise not_found_exception (name , project )
803+ else :
804+ return None
786805
787806 def _list_objects (
788- self , table , project , proto_class , python_class , proto_field_name
807+ self ,
808+ table : Table ,
809+ project : str ,
810+ proto_class : Any ,
811+ python_class : Any ,
812+ proto_field_name : str ,
789813 ):
790814 self ._maybe_init_project_metadata (project )
791815 with self .engine .connect () as conn :
0 commit comments