@@ -47,6 +47,28 @@ def get_client(resource: BaseResource) -> GcpClient:
4747 )
4848
4949
50+ def parse_json (
51+ json : Json , clazz : Type [T ], builder : Optional [GraphBuilder ] = None , mapping : Optional [Dict [str , Bender ]] = None
52+ ) -> Optional [T ]:
53+ """
54+ Use this method to parse json into a class. If the json can not be parsed, the error is reported to the core.
55+ Based on configuration, either the exception is raised or None is returned.
56+ :param json: the json to parse.
57+ :param clazz: the class to parse into.
58+ :param builder: the graph builder.
59+ :param mapping: the optional mapping to apply before parsing.
60+ :return: The parsed object or None.
61+ """
62+ try :
63+ mapped = bend (mapping , json ) if mapping is not None else json
64+ return from_js (mapped , clazz )
65+ except Exception as e :
66+ if builder :
67+ # report and log the error
68+ builder .core_feedback .error (f"Failed to parse json into { clazz .__name__ } : { e } . Source: { json } " , log )
69+ return None
70+
71+
5072class GraphBuilder :
5173 def __init__ (
5274 self ,
@@ -379,24 +401,25 @@ def collect(cls: Type[GcpResource], raw: List[Json], builder: GraphBuilder) -> L
379401 result : List [GcpResource ] = []
380402 for js in raw :
381403 # map from api
382- instance = cls .from_api (js )
383- # allow the instance to adjust itself
384- adjusted = instance .adjust_from_api (builder , js )
385- # add to graph
386- if (added := builder .add_node (adjusted , js )) is not None :
387- # post process
388- added .post_process (builder , js )
389- result .append (added )
404+ if instance : = cls .from_api (js , builder ):
405+ # allow the instance to adjust itself
406+ adjusted = instance .adjust_from_api (builder , js )
407+ # add to graph
408+ if (added := builder .add_node (adjusted , js )) is not None :
409+ # post process
410+ added .post_process (builder , js )
411+ result .append (added )
390412 return result
391413
392414 @classmethod
393415 def from_json (cls : Type [GcpResourceType ], json : Json ) -> GcpResourceType :
394416 return from_js (json , cls )
395417
396418 @classmethod
397- def from_api (cls : Type [GcpResourceType ], json : Json ) -> GcpResourceType :
398- mapped = bend (cls .mapping , json )
399- return cls .from_json (mapped )
419+ def from_api (
420+ cls : Type [GcpResourceType ], json : Json , builder : Optional [GraphBuilder ] = None
421+ ) -> Optional [GcpResourceType ]:
422+ return parse_json (json , cls , builder , cls .mapping )
400423
401424 @classmethod
402425 def called_collect_apis (cls ) -> List [GcpApiSpec ]:
@@ -541,9 +564,9 @@ def fallback_global_region(cls: Type[GcpRegion], project: GcpProject) -> GcpRegi
541564 return cls (id = "global" , tags = {}, name = "global" , account = project )
542565
543566 def post_process (self , graph_builder : GraphBuilder , source : Json ) -> None :
544- region_quota = GcpRegionQuota .from_api (source )
545- graph_builder .add_node (region_quota , source )
546- graph_builder .add_edge (self , node = region_quota )
567+ if region_quota : = GcpRegionQuota .from_api (source , graph_builder ):
568+ graph_builder .add_node (region_quota , source )
569+ graph_builder .add_edge (self , node = region_quota )
547570
548571 def connect_in_graph (self , builder : GraphBuilder , source : Json ) -> None :
549572 super ().connect_in_graph (builder , source )
0 commit comments