|
23 | 23 | from fixlib.config import current_config |
24 | 24 | from fixlib.core.actions import CoreFeedback |
25 | 25 | from fixlib.graph import Graph, EdgeKey, NodeSelector, ByNodeId |
| 26 | +from fixlib.json import from_json |
26 | 27 | from fixlib.json_bender import AsFloat, Bender, bend, S, ForallBend, Bend |
27 | 28 | from fixlib.lock import RWLock |
28 | 29 | from fixlib.threading import ExecutorQueue |
@@ -50,6 +51,30 @@ def get_client(subscription_id: str) -> MicrosoftClient: |
50 | 51 | T = TypeVar("T") |
51 | 52 |
|
52 | 53 |
|
| 54 | +def parse_json( |
| 55 | + json: Json, clazz: Type[T], builder: GraphBuilder, mapping: Optional[Dict[str, Bender]] = None |
| 56 | +) -> Optional[T]: |
| 57 | + """ |
| 58 | + Use this method to parse json into a class. If the json can not be parsed, the error is reported to the core. |
| 59 | + Based on configuration, either the exception is raised or None is returned. |
| 60 | + :param json: the json to parse. |
| 61 | + :param clazz: the class to parse into. |
| 62 | + :param builder: the graph builder. |
| 63 | + :param mapping: the optional mapping to apply before parsing. |
| 64 | + :return: The parsed object or None. |
| 65 | + """ |
| 66 | + try: |
| 67 | + mapped = bend(mapping, json) if mapping is not None else json |
| 68 | + return from_json(mapped, clazz) |
| 69 | + except Exception as e: |
| 70 | + # report and log the error |
| 71 | + builder.core_feedback.error(f"Failed to parse json into {clazz.__name__}: {e}. Source: {json}", log) |
| 72 | + # based on the strict flag, either raise the exception or return None |
| 73 | + if builder.config.discard_account_on_resource_error: |
| 74 | + raise |
| 75 | + return None |
| 76 | + |
| 77 | + |
53 | 78 | class MicrosoftResource(BaseResource): |
54 | 79 | kind: ClassVar[str] = "microsoft_resource" |
55 | 80 | # The mapping to transform the incoming API json into the internal representation. |
|
0 commit comments