Skip to content

object_service

High-level object cache operations.

GroupNode dataclass

A node in the group membership tree.

Source code in src/arodonata/cache/object_service.py
@dataclass
class GroupNode:
    """A node in the group membership tree."""

    uid: str
    name: str
    domain: str
    depth: int
    children: list[GroupNode] | None = None

    def __post_init__(self) -> None:
        if self.children is None:
            self.children = []

ObjectService

High-level object cache operations.

Provides search and refresh functionality with cache-first queries, API fallback, and SSE streaming for progress tracking.

Source code in src/arodonata/cache/object_service.py
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
class ObjectService:
    """High-level object cache operations.

    Provides search and refresh functionality with cache-first queries,
    API fallback, and SSE streaming for progress tracking.
    """

    # Object types to fetch from API
    OBJECT_TYPES = ["host", "network", "address-range", "group"]

    # Above this many collected objects held in memory before the atomic
    # swap, warn so operators can spot unexpectedly large domain refreshes.
    LARGE_DOMAIN_WARN_THRESHOLD = 50_000

    def __init__(
        self,
        db_manager: DatabaseManager,
        client: ArodonataClient,
        max_incremental_changes: int = 500,
        domain_list_refresh_ttl: int = DOMAIN_LIST_REFRESH_TTL_SECONDS,
        clock: Clock | None = None,
    ) -> None:
        """Initialize ObjectService.

        Args:
            db_manager: DatabaseManager instance.
            client: ArodonataClient instance for API fallback.
            max_incremental_changes: Max in-scope changes an incremental
                apply will accept before falling back to a full reload.
            domain_list_refresh_ttl: Seconds between opportunistic (CHECK/
                INCREMENTAL-mode) re-fetches of a management server's domain
                list. FORCE mode ignores this and always re-fetches. See
                `DOMAIN_LIST_REFRESH_TTL_SECONDS`.
            clock: Injectable time source for the TTL memo (tests only;
                defaults to the real wall clock).
        """
        self._db = db_manager
        self._cache = CacheRepository(db_manager)
        self._client = client
        self.max_incremental_changes = max_incremental_changes
        self._domain_list_refresh = DomainListRefreshTracker(ttl_seconds=domain_list_refresh_ttl, clock=clock)

    async def _fetch_objects_from_db(
        self,
        search_type: SearchType,
        cleaned: str,
        mgmt_names: list[str] | None,
        domain_names: list[str] | None,
    ) -> list[CPObject]:
        """Fetch objects from cache based on search type."""
        objects: list[CPObject] = []

        if search_type == SearchType.HOST:
            objects = await self._cache.get_objects_by_ip(
                ip_address=cleaned,
                mgmt_names=mgmt_names,
                domain_names=domain_names,
            )
        elif search_type == SearchType.NETWORK:
            objects = await self._cache.get_objects_by_subnet(
                subnet=cleaned,
                mgmt_names=mgmt_names,
                domain_names=domain_names,
            )
        elif search_type == SearchType.RANGE:
            if "-" in cleaned:
                start_ip, end_ip = cleaned.split("-", 1)
                start_ip = start_ip.strip()
                end_ip = end_ip.strip()

                objects = await self._cache.get_objects_in_ip_range(
                    start_ip=start_ip,
                    end_ip=end_ip,
                    mgmt_names=mgmt_names,
                    domain_names=domain_names,
                )
        elif search_type == SearchType.NAME:
            objects = await self._cache.get_objects_by_name(
                name=cleaned,
                mgmt_names=mgmt_names,
                domain_names=domain_names,
            )
        return objects

    async def search_objects(
        self,
        search_input: str,
        mgmt_names: list[str] | None = None,
        domain_names: list[str] | None = None,
        max_depth: int = 2,
    ) -> AsyncIterator[SearchResult]:
        """Search for objects by IP, name, UID, or type.

        Args:
            search_input: Comma-separated search terms.
            mgmt_names: Optional management server filter.
            domain_names: Optional domain filter.
            max_depth: Maximum depth for group membership traversal.

        Yields:
            SearchResult for each search term.
        """
        # Parse comma-separated input
        terms = [t.strip() for t in search_input.split(",") if t.strip()]

        if not terms:
            yield SearchResult(
                search_term=search_input,
                search_type=SearchType.NAME,
                objects=[],
            )
            return

        # Get target management servers
        target_mgmt_names = mgmt_names or self._client.get_mgmt_names()

        if not target_mgmt_names:
            log().warning("No management servers configured for search")
            yield SearchResult(
                search_term=search_input,
                search_type=SearchType.NAME,
                objects=[],
            )
            return

        # Search for each term
        for term in terms:
            # Classify the search input
            search_type, cleaned = classify_input(term)

            # Query cache based on search type
            objects = await self._fetch_objects_from_db(search_type, cleaned, target_mgmt_names, domain_names)

            # Resolve group memberships if objects found
            memberships: dict[str, list[GroupNode]] | None = None
            if objects and max_depth > 0:
                memberships = {}
                for obj in objects:
                    obj_groups = await self._resolve_group_memberships(
                        obj_uid=obj.uid,
                        mgmt_name=obj.mgmt_name,
                        domain_name=obj.domain_name,
                        max_depth=max_depth,
                    )
                    if obj_groups:
                        memberships[obj.uid] = obj_groups

            yield SearchResult(
                search_term=term,
                search_type=search_type,
                objects=objects,
                memberships=memberships if memberships else None,
            )

    async def refresh_objects(
        self,
        mgmt_names: list[str] | None = None,
        domain_names: list[str] | None = None,
        mode: str = "force",  # RefreshMode value
        include_global: bool = False,
    ) -> AsyncIterator[dict[str, Any]]:
        """Refresh object cache from API.

        Args:
            mgmt_names: Optional management server filter.
            domain_names: Optional domain filter.
            mode: Refresh mode (skip/check/force/incremental).
            include_global: When False (default), the synthetic "Global" domain
                is excluded from the all-domains refresh path so existing
                callers see today's behavior. An explicit ``domain_names``
                request for "Global" is honored regardless of this flag.

        Yields:
            Progress dictionaries with keys:
                - message: str - Progress message
                - mgmt_name: str - Management server name
                - domain_name: str - Domain name
                - object_type: str - Type being fetched
                - count: int - Number of objects processed
                - total: int - Total objects to process
        """
        # Parse refresh mode
        try:
            refresh_mode = RefreshMode(mode)
        except ValueError:
            log().warning(f"Invalid refresh mode '{mode}', defaulting to 'skip'")
            refresh_mode = RefreshMode.SKIP

        # Handle SKIP mode
        if refresh_mode == RefreshMode.SKIP:
            yield {
                "message": "Refresh skipped (mode=skip)",
                "status": "skipped",
            }
            return

        # Get target management servers
        target_mgmt_names = mgmt_names or self._client.get_mgmt_names()

        if not target_mgmt_names:
            yield {
                "message": "No management servers available",
                "status": "error",
            }
            return

        log().info(f"Refreshing object cache for {len(target_mgmt_names)} server(s), mode={refresh_mode.value}")

        # Process each management server
        for mgmt_name in target_mgmt_names:
            async for progress in self._refresh_mgmt_server(
                mgmt_name=mgmt_name,
                domain_names=domain_names,
                mode=refresh_mode,
                include_global=include_global,
            ):
                yield progress

    async def _refresh_mgmt_server(
        self,
        mgmt_name: str,
        domain_names: list[str] | None,
        mode: RefreshMode,
        include_global: bool = False,
    ) -> AsyncIterator[dict[str, Any]]:
        """Refresh objects for a single management server.

        Args:
            mgmt_name: Management server name.
            domain_names: Optional domain filter.
            mode: Refresh mode.
            include_global: When False (default), the synthetic "Global" domain
                is excluded from the all-domains refresh path.

        Yields:
            Progress dictionaries.
        """
        yield {
            "message": f"Processing {mgmt_name}",
            "mgmt_name": mgmt_name,
            "status": "processing_mgmt",
        }

        # Get domains to refresh
        domains_to_refresh = await self._get_domains_to_refresh(
            mgmt_name=mgmt_name,
            domain_names=domain_names,
            mode=mode,
            include_global=include_global,
        )

        if not domains_to_refresh:
            yield {
                "message": f"No domains to refresh for {mgmt_name}",
                "mgmt_name": mgmt_name,
                "status": "no_domains",
            }
            return

        log().debug(f"Refreshing {len(domains_to_refresh)} domain(s) for {mgmt_name}")

        # Process each domain
        refresh = self._refresh_domain_incremental if mode == RefreshMode.INCREMENTAL else self._refresh_domain
        for domain_name in domains_to_refresh:
            async for progress in refresh(mgmt_name=mgmt_name, domain_name=domain_name):
                yield progress

    async def _get_domains_to_refresh(
        self,
        mgmt_name: str,
        domain_names: list[str] | None,
        mode: RefreshMode,
        include_global: bool = False,
    ) -> list[str]:
        """Get list of domains that need refreshing.

        A domain created in SmartConsole after this mgmt's domains table was
        first seeded used to be invisible to every refresh forever: the old
        code only ever called `populate_domain_cache` when the table came
        back completely empty. That floor behavior is preserved below, but
        it is no longer the *only* trigger for a domain-list re-fetch:

        * FORCE mode always re-fetches unconditionally (no TTL, no
          conditions) - see `_refetch_domain_list`.
        * CHECK/INCREMENTAL ("smart") modes re-fetch opportunistically, at
          most once every `DOMAIN_LIST_REFRESH_TTL_SECONDS` (default) - see
          `self._domain_list_refresh` (a `DomainListRefreshTracker`) - so a
          new domain surfaces within that window without hammering
          `show-domains` on every smart-refresh tick.

        This single mechanism also covers what a separate "backfill a
        missing Global row" special case used to handle on its own: any
        re-fetch (forced or TTL-triggered) re-populates the whole domain
        list, Global row included, so that special case no longer needs to
        exist as its own bypass-the-TTL code path.

        Args:
            mgmt_name: Management server name.
            domain_names: Optional domain filter.
            mode: Refresh mode.
            include_global: When False (default), the synthetic "Global" domain
                is excluded from the table read. An explicit request for
                "Global" via ``domain_names`` is honored regardless, since
                this method treats ``domain_names`` as an intersection filter
                against the table rather than a pass-through - if the table
                read excluded Global, an explicit request for it would be
                filtered out before the intersection ever runs.

        Returns:
            List of domain names to refresh.
        """
        # An explicit request for "Global" must reach the table even if the
        # caller didn't set include_global - otherwise it gets filtered out
        # before the intersection below can match it.
        fetch_include_global = include_global or (GLOBAL_DOMAIN_NAME in (domain_names or []))

        # Get all domains for this mgmt server
        all_domains = await self._cache.get_domains(mgmt_name=mgmt_name, include_global=fetch_include_global)

        # Floor: an empty table must always be populated, regardless of mode/TTL.
        table_was_empty = not all_domains
        should_refetch = table_was_empty or mode == RefreshMode.FORCE or self._domain_list_refresh.is_stale(mgmt_name)

        if should_refetch:
            all_domains = await self._refetch_domain_list(
                mgmt_name=mgmt_name,
                fetch_include_global=fetch_include_global,
                fallback=all_domains,
                required=table_was_empty,
            )

        if not all_domains:
            log().warning(f"No domains found for {mgmt_name}")
            return []

        # Filter by domain_names if specified
        if domain_names:
            filtered_domains = [d for d in all_domains if d.domain_name in domain_names]
        else:
            filtered_domains = all_domains

        # For FORCE mode, refresh all filtered domains
        if mode == RefreshMode.FORCE:
            return [d.domain_name for d in filtered_domains]

        # For CHECK and INCREMENTAL modes, filter by staleness
        stale_domains = []
        for domain in filtered_domains:
            if await self._is_domain_stale(mgmt_name, domain.domain_name):
                stale_domains.append(domain.domain_name)

        return stale_domains

    async def _refetch_domain_list(
        self,
        mgmt_name: str,
        fetch_include_global: bool,
        fallback: list[Domain],
        required: bool,
    ) -> list[Domain]:
        """Re-fetch one mgmt server's domain list from the API and re-read the cache.

        Args:
            mgmt_name: Management server name.
            fetch_include_global: Whether the re-read should include the Global row.
            fallback: The already-cached domains to fall back to if the
                re-fetch can't be attempted or fails. An opportunistic
                re-fetch (TTL/force on an already-populated table) failing
                is not fatal - the existing, possibly slightly stale, cached
                list is safer to serve than nothing.
            required: True when the table was empty before this call - unlike
                the opportunistic case, a failed or unavailable re-fetch here
                must not be silently papered over with a fallback that is
                itself empty, so the caller gets ``[]`` instead.

        Returns:
            The freshly re-read domains, or ``fallback`` if the re-fetch
            could not run or failed.
        """
        if not hasattr(self._client, "_domain_service"):
            log().warning(f"Domain service not available for {mgmt_name}")
            return [] if required else fallback

        try:
            await self._client._domain_service.populate_domain_cache(mgmt_name)
        except Exception as e:
            log().exception(f"Failed to {'populate' if required else 'refresh'} domains for {mgmt_name}: {e}")
            return [] if required else fallback

        self._domain_list_refresh.mark_checked(mgmt_name)
        refreshed = await self._cache.get_domains(mgmt_name=mgmt_name, include_global=fetch_include_global)
        if refreshed:
            log().debug(f"Fetched {len(refreshed)} domain(s) for {mgmt_name}")
            return refreshed

        if required:
            log().warning(f"API returned no domains for {mgmt_name}")
            return []
        return fallback

    async def _resolve_group_memberships(
        self,
        obj_uid: str,
        mgmt_name: str,
        domain_name: str,
        max_depth: int = 2,
        current_depth: int = 0,
        visited: set[str] | None = None,
    ) -> list[GroupNode]:
        """Resolve group memberships for an object.

        Args:
            obj_uid: Object UID to find memberships for.
            mgmt_name: Management server name.
            domain_name: Domain name.
            max_depth: Maximum depth to traverse.
            current_depth: Current depth in recursion.
            visited: Set of visited UIDs to avoid cycles.

        Returns:
            List of GroupNode objects representing group memberships.
        """
        if visited is None:
            visited = set()

        # Prevent infinite recursion
        if current_depth >= max_depth:
            return []

        # Add current UID to visited set
        visited.add(obj_uid)

        # Find groups containing this object
        groups = await self._cache.get_objects_by_members(
            member_uid=obj_uid,
            mgmt_names=[mgmt_name],
            domain_names=[domain_name],
        )

        if not groups:
            return []

        # Build group nodes
        result = []
        for group in groups:
            # Skip if we've already visited this group (avoid cycles)
            if group.uid in visited:
                continue

            node = GroupNode(
                uid=group.uid,
                name=group.name,
                domain=group.domain_name,
                depth=current_depth,
                children=[],
            )

            # Recursively find parent groups
            parent_groups = await self._resolve_group_memberships(
                obj_uid=group.uid,
                mgmt_name=mgmt_name,
                domain_name=domain_name,
                max_depth=max_depth,
                current_depth=current_depth + 1,
                visited=visited.copy(),
            )

            if parent_groups:
                node.children = parent_groups

            result.append(node)

        return result

    async def _is_domain_stale(
        self,
        mgmt_name: str,
        domain_name: str,
    ) -> bool:
        """Check if a domain has stale object cache based on Last Published Session.

        Compares the last publish time from the API with the cached value.
        If no cached value or API value is newer, domain is considered stale.

        Args:
            mgmt_name: Management server name.
            domain_name: Domain name.

        Returns:
            True if domain is stale (needs refresh), False otherwise.
        """
        # 1. Check if there are any cached objects for this domain
        # This is a safety check: if objects are missing entirely, it's definitely stale
        if await self._count_cached_objects(mgmt_name, domain_name) == 0:
            log().debug(f"Domain {mgmt_name}/{domain_name} is stale (no cached objects)")
            return True

        # 2. Check LastPublishedSession comparison
        return await self._compare_published_times(mgmt_name, domain_name)

    async def _count_cached_objects(self, mgmt_name: str, domain_name: str) -> int:
        """Cheapest available check for whether a domain's object cache is empty."""
        count_stmt = select(func.count()).where(
            CPObject.mgmt_name == mgmt_name,  # type: ignore[arg-type]
            CPObject.domain_name == domain_name,  # type: ignore[arg-type]
        )
        async with self._cache._db.session() as session:
            res = await session.execute(count_stmt)
            return res.scalar() or 0

    async def _compare_published_times(self, mgmt_name: str, domain_name: str) -> bool:
        """Compare the API's last-published-session time to the cached value.

        Args:
            mgmt_name: Management server name.
            domain_name: Domain name.

        Returns:
            True if the API's published time is newer than the cached value,
            or if no cached session exists yet, False otherwise (including
            when the check cannot be completed).
        """
        try:
            # Fetch last published session from API
            api_domain = "" if domain_name in ("SMC User", "System Data") else domain_name

            response = await self._client.api_call(
                mgmt_name=mgmt_name,
                domain=api_domain,
                command="show-last-published-session",
                payload={},
            )

            if response.success and response.data:
                # Extract published_time from meta-info
                meta_info = response.data.get("meta-info", {})
                last_modify_time = meta_info.get("last-modify-time", {})

                # Parse timestamp (returns naive UTC)
                api_published_time = self._parse_api_timestamp(last_modify_time)

                if api_published_time:
                    # Get cached record
                    cached = await self._cache.get_last_published_session(mgmt_name, domain_name)

                    if not cached:
                        log().debug(f"Domain {mgmt_name}/{domain_name} is stale (no cached session info)")
                        return True

                    # Session uid comparison is authoritative: CP publish-times
                    # have MINUTE resolution, so a publish in the same minute
                    # as the cached baseline is invisible to the timestamp
                    # check below. Different uid == something was published.
                    api_uid = response.data.get("uid", "")
                    if api_uid and cached.uid:
                        if api_uid != cached.uid:
                            log().debug(
                                f"Domain {mgmt_name}/{domain_name} is stale "
                                f"(session uid {cached.uid[:8]} -> {api_uid[:8]})"
                            )
                            return True
                        log().debug(f"Domain {mgmt_name}/{domain_name} is up to date (uid match)")
                        return False

                    if api_published_time > cached.published_time:
                        log().debug(
                            f"Domain {mgmt_name}/{domain_name} is stale "
                            f"(API: {api_published_time}, Cache: {cached.published_time})"
                        )
                        return True

                    log().debug(f"Domain {mgmt_name}/{domain_name} is up to date")
                    return False

        except InvalidCredentialsError as e:
            # Rejected credentials are categorically different from a transient
            # probe error: they do not clear on their own, they recur on every
            # tick, and the nightly force job cannot repair them either because
            # the same login fails there too. Reporting "fresh" would bury a
            # credential or permission problem under one warning. Report stale
            # instead, so the reload path runs, fails at login, and emits a
            # `domain_failed` event the caller can count and alert on.
            #
            # Deliberately NOT the broader AuthenticationError: the login
            # coordinator raises that for every rejection it cannot classify,
            # including "Database revision is in progress", which clears within
            # seconds of a revert. Those must keep the fail-open path below.
            log().error(
                f"Staleness check for {mgmt_name}/{domain_name} was refused for invalid credentials: {e}. "
                f"Treating the domain as stale so the refresh reports the failure."
            )
            return True
        except Exception as e:
            log().warning(f"Error checking staleness for {mgmt_name}/{domain_name}: {e}")
            # Fallback: if check fails, assume it's NOT stale to avoid excessive refreshes
            # unless it was already empty (handled by the cached-objects check)
            return False

        log().debug(f"Could not determine staleness for {mgmt_name}/{domain_name}, assuming fresh")
        return False

    def _parse_api_timestamp(self, time_data: dict[str, Any] | None) -> datetime | None:
        """Delegate to module-level timestamp parser."""
        return _parse_api_timestamp(time_data)

    async def refresh_last_published_session(
        self,
        mgmt_name: str,
        domain_name: str,
    ) -> LastPublishedSession | None:
        """Refresh and upsert the last-published-session record for one domain.

        Makes a single, lightweight `show-last-published-session` API call —
        does not touch CPObject or Asset caches. Safe to call independently
        of a full object/asset refresh.

        Args:
            mgmt_name: Management server name.
            domain_name: Domain name.

        Returns:
            The upserted LastPublishedSession record, or None if the API
            call failed or returned no usable timestamp.
        """
        record = await self.fetch_last_published_session(mgmt_name, domain_name)
        if record is None:
            return None
        try:
            await self._cache.upsert_last_published_session(record)
        except Exception as e:
            log().warning(f"Failed to store LastPublishedSession for {mgmt_name}/{domain_name}: {e}")
            return None
        return record

    async def fetch_last_published_session(
        self,
        mgmt_name: str,
        domain_name: str,
    ) -> LastPublishedSession | None:
        """Read the domain's current last-published session WITHOUT storing it.

        The read-only half of `refresh_last_published_session`. Callers that need
        to know where the domain's head is *before* deciding what to do with the
        cache must not advance the stored baseline in the process — doing so
        empties the diff window they are about to use. `CacheRefreshCoordinator`
        uses this to tell a forward publish from a revert.

        Args:
            mgmt_name: Management server name.
            domain_name: Domain name.

        Returns:
            An unsaved LastPublishedSession, or None if the API call failed or
            returned no usable timestamp.
        """
        try:
            api_domain = "" if domain_name in ("SMC User", "System Data") else domain_name

            response = await self._client.api_call(
                mgmt_name=mgmt_name,
                domain=api_domain,
                command="show-last-published-session",
                payload={},
            )

            if response.success and response.data:
                data = response.data
                meta_info = data.get("meta-info", {})
                last_modify_time = meta_info.get("last-modify-time", {})
                published_time = self._parse_api_timestamp(last_modify_time)

                if published_time:
                    return LastPublishedSession(
                        id=f"{mgmt_name}:{domain_name}",
                        mgmt_name=mgmt_name,
                        domain_name=domain_name,
                        published_time=published_time,
                        uid=data.get("uid", ""),
                        name=data.get("name", ""),
                        ip_address=data.get("ip-address", ""),
                        creator=data.get("creator", ""),
                        description=data.get("description", ""),
                    )
        except Exception as e:
            log().warning(f"Failed to read LastPublishedSession for {mgmt_name}/{domain_name}: {e}")

        return None

    async def fetch_full_object(
        self,
        mgmt_name: str,
        domain_name: str,
        uid: str,
    ) -> dict[str, Any] | None:
        """Fetch one object in full detail via show-object.

        Returns the raw object dict, or None ONLY when the management server
        cleanly reports the object does not exist (deleted since the diff was
        taken). Any other failure raises RuntimeError — callers treat that as
        "incremental apply unsafe".
        """
        api_domain = "" if domain_name in ("SMC User", "System Data") else domain_name
        response = await self._client.api_call(
            mgmt_name=mgmt_name,
            command="show-object",
            domain=api_domain,
            details_level="full",
            payload={"uid": uid},
        )
        if response.success and response.data:
            obj = response.data.get("object")
            if isinstance(obj, dict):
                return obj
            raise RuntimeError(f"show-object {uid} returned no object payload")
        if "object_not_found" in (response.code or "") or "not found" in (response.message or "").lower():
            return None
        raise RuntimeError(f"show-object {uid} failed: {response.message or response.code or 'unknown error'}")

    def _make_refresher(self) -> IncrementalRefresher:
        # Built per-apply so runtime mutation of max_incremental_changes
        # always takes effect. The client's API adapter provides show_changes.
        return IncrementalRefresher(
            api=self._client._api_adapter,
            cache=self._cache,
            fetch_full_object=self.fetch_full_object,
            to_cpobject=api_object_to_cpobject,
            # Read-only on purpose: refresh_last_published_session would advance
            # the baseline before the apply and empty the diff window.
            fetch_head=self.fetch_last_published_session,
            max_changes=self.max_incremental_changes,
        )

    async def _refresh_domain_incremental(
        self,
        mgmt_name: str,
        domain_name: str,
    ) -> AsyncIterator[dict[str, Any]]:
        """Incrementally refresh one stale domain from its show-changes diff.

        Changed objects are re-fetched in full (show-object) — the diff is
        only a change list. Any unsafe condition falls back to the atomic
        full-domain reload. The baseline stamp advances only on success
        (of either path).
        """
        if await self._count_cached_objects(mgmt_name, domain_name) == 0:
            # A domain with a baseline stamp but zero (or partial) cached rows
            # must not have a diff applied on top of it: the diff only covers
            # changes since the baseline, so an incremental apply here would
            # stamp the domain fresh while leaving it permanently incomplete.
            # Mirrors the coordinator's empty-cache guard
            # (cache_refresh_coordinator.py _ensure_one's `_is_empty` check).
            yield {
                "message": (
                    f"Incremental refresh of {mgmt_name}/{domain_name} not safe "
                    f"(empty domain cache); falling back to full reload"
                ),
                "mgmt_name": mgmt_name,
                "domain_name": domain_name,
                "status": "domain_fallback",
                "reason": "empty domain cache",
            }
            async for progress in self._refresh_domain(mgmt_name, domain_name):
                yield progress
            return

        try:
            applied = await self._make_refresher().apply(mgmt_name, domain_name)
        except FallbackToFull as exc:
            yield {
                "message": (
                    f"Incremental refresh of {mgmt_name}/{domain_name} not safe ({exc}); falling back to full reload"
                ),
                "mgmt_name": mgmt_name,
                "domain_name": domain_name,
                "status": "domain_fallback",
                "reason": str(exc),
            }
            async for progress in self._refresh_domain(mgmt_name, domain_name):
                yield progress
            return

        yield {
            "message": f"Incremental: {mgmt_name}/{domain_name} - {applied} change(s) applied",
            "mgmt_name": mgmt_name,
            "domain_name": domain_name,
            "status": "domain_incremental",
            "count": applied,
        }
        # Success (including a rules-only publish with 0 in-scope changes):
        # advance the freshness stamp so the next probe sees this domain fresh.
        await self.refresh_last_published_session(mgmt_name, domain_name)

    async def _refresh_domain(
        self,
        mgmt_name: str,
        domain_name: str,
    ) -> AsyncIterator[dict[str, Any]]:
        """Refresh all objects for a single domain (collect-then-swap).

        All object types are fetched into memory first; the cache is only
        touched after every type succeeded, via one atomic
        replace_domain_objects transaction. Any API failure aborts the
        domain refresh, leaving the previous cache contents and freshness
        stamp intact.
        """
        log().info(f"Refreshing objects for {mgmt_name}/{domain_name}")

        yield {
            "message": f"Refreshing {mgmt_name}/{domain_name}",
            "mgmt_name": mgmt_name,
            "domain_name": domain_name,
            "status": "refreshing_domain",
        }

        collected: list[CPObject] = []
        for object_type in self.OBJECT_TYPES:
            objects, error = await self._collect_objects_by_type(
                mgmt_name=mgmt_name,
                domain_name=domain_name,
                object_type=object_type,
            )
            if error is not None:
                yield {
                    "message": (
                        f"Aborting refresh of {mgmt_name}/{domain_name}: "
                        f"fetching {object_type}s failed: {error}. "
                        f"Previous cache contents kept."
                    ),
                    "mgmt_name": mgmt_name,
                    "domain_name": domain_name,
                    "object_type": object_type,
                    "status": "domain_failed",
                    "error": error,
                }
                return
            collected.extend(objects)
            yield {
                "message": f"Fetched {len(objects)} {object_type}(s)",
                "mgmt_name": mgmt_name,
                "domain_name": domain_name,
                "object_type": object_type,
                "status": "type_fetched",
                "count": len(objects),
            }

        if len(collected) > self.LARGE_DOMAIN_WARN_THRESHOLD:
            log().warning(
                f"Large domain refresh for {mgmt_name}/{domain_name}: {len(collected)} objects held in memory before swap"
            )

        deleted, inserted = await self._cache.replace_domain_objects(mgmt_name, domain_name, collected)
        log().debug(f"Swapped cache for {mgmt_name}/{domain_name}: -{deleted} +{inserted}")

        yield {
            "message": f"Complete: {mgmt_name}/{domain_name} - {inserted} object(s)",
            "mgmt_name": mgmt_name,
            "domain_name": domain_name,
            "status": "domain_complete",
            "total": inserted,
        }

        # Only a fully successful refresh may advance the freshness stamp.
        await self.refresh_last_published_session(mgmt_name, domain_name)

    async def _collect_objects_by_type(
        self,
        mgmt_name: str,
        domain_name: str,
        object_type: str,
    ) -> tuple[list[CPObject], str | None]:
        """Fetch one object type from the API without touching the cache.

        Args:
            mgmt_name: Management server name.
            domain_name: Domain name.
            object_type: Object type (host, network, etc.).

        Returns:
            (objects, None) on success; ([], error_message) on failure.
        """
        log().debug(f"Fetching {object_type} objects for {mgmt_name}/{domain_name}")
        command = f"show-{object_type}s"

        try:
            result = await self._client.api_query(
                mgmt_name=mgmt_name,
                command=command,
                domain=domain_name,
                details_level="full",
            )
        except Exception as e:  # noqa: BLE001 - any transport error aborts the domain
            log().exception(f"Error fetching {object_type}s for {mgmt_name}/{domain_name}")
            return [], str(e)

        if not result.success:
            return [], result.message or f"{command} returned success=False"

        cp_objects: list[CPObject] = []
        for api_obj in result.objects:
            cp_obj = self._api_object_to_cpobject(
                api_obj=api_obj,
                mgmt_name=mgmt_name,
                domain_name=domain_name,
            )
            if cp_obj:
                cp_objects.append(cp_obj)
        return cp_objects, None

    def _extract_ip_fields(self, obj_type: str, api_obj: dict[str, Any]) -> tuple[str, str, str, str, str]:
        """Delegate to module-level function."""
        return _extract_ip_fields(obj_type, api_obj)

    def _extract_group_members(self, obj_type: str, api_obj: dict[str, Any]) -> str:
        """Delegate to module-level function."""
        return _extract_group_members(obj_type, api_obj)

    def _extract_tags(self, api_obj: dict[str, Any]) -> str:
        """Delegate to module-level function."""
        return _extract_tags(api_obj)

    def _api_object_to_cpobject(
        self,
        api_obj: dict[str, Any],
        mgmt_name: str,
        domain_name: str,
    ) -> CPObject | None:
        """Delegate to the module-level canonical converter."""
        return api_object_to_cpobject(api_obj, mgmt_name, domain_name)

__init__(db_manager, client, max_incremental_changes=500, domain_list_refresh_ttl=DOMAIN_LIST_REFRESH_TTL_SECONDS, clock=None)

Initialize ObjectService.

Parameters:

Name Type Description Default
db_manager DatabaseManager

DatabaseManager instance.

required
client ArodonataClient

ArodonataClient instance for API fallback.

required
max_incremental_changes int

Max in-scope changes an incremental apply will accept before falling back to a full reload.

500
domain_list_refresh_ttl int

Seconds between opportunistic (CHECK/ INCREMENTAL-mode) re-fetches of a management server's domain list. FORCE mode ignores this and always re-fetches. See DOMAIN_LIST_REFRESH_TTL_SECONDS.

DOMAIN_LIST_REFRESH_TTL_SECONDS
clock Clock | None

Injectable time source for the TTL memo (tests only; defaults to the real wall clock).

None
Source code in src/arodonata/cache/object_service.py
def __init__(
    self,
    db_manager: DatabaseManager,
    client: ArodonataClient,
    max_incremental_changes: int = 500,
    domain_list_refresh_ttl: int = DOMAIN_LIST_REFRESH_TTL_SECONDS,
    clock: Clock | None = None,
) -> None:
    """Initialize ObjectService.

    Args:
        db_manager: DatabaseManager instance.
        client: ArodonataClient instance for API fallback.
        max_incremental_changes: Max in-scope changes an incremental
            apply will accept before falling back to a full reload.
        domain_list_refresh_ttl: Seconds between opportunistic (CHECK/
            INCREMENTAL-mode) re-fetches of a management server's domain
            list. FORCE mode ignores this and always re-fetches. See
            `DOMAIN_LIST_REFRESH_TTL_SECONDS`.
        clock: Injectable time source for the TTL memo (tests only;
            defaults to the real wall clock).
    """
    self._db = db_manager
    self._cache = CacheRepository(db_manager)
    self._client = client
    self.max_incremental_changes = max_incremental_changes
    self._domain_list_refresh = DomainListRefreshTracker(ttl_seconds=domain_list_refresh_ttl, clock=clock)

fetch_full_object(mgmt_name, domain_name, uid) async

Fetch one object in full detail via show-object.

Returns the raw object dict, or None ONLY when the management server cleanly reports the object does not exist (deleted since the diff was taken). Any other failure raises RuntimeError — callers treat that as "incremental apply unsafe".

Source code in src/arodonata/cache/object_service.py
async def fetch_full_object(
    self,
    mgmt_name: str,
    domain_name: str,
    uid: str,
) -> dict[str, Any] | None:
    """Fetch one object in full detail via show-object.

    Returns the raw object dict, or None ONLY when the management server
    cleanly reports the object does not exist (deleted since the diff was
    taken). Any other failure raises RuntimeError — callers treat that as
    "incremental apply unsafe".
    """
    api_domain = "" if domain_name in ("SMC User", "System Data") else domain_name
    response = await self._client.api_call(
        mgmt_name=mgmt_name,
        command="show-object",
        domain=api_domain,
        details_level="full",
        payload={"uid": uid},
    )
    if response.success and response.data:
        obj = response.data.get("object")
        if isinstance(obj, dict):
            return obj
        raise RuntimeError(f"show-object {uid} returned no object payload")
    if "object_not_found" in (response.code or "") or "not found" in (response.message or "").lower():
        return None
    raise RuntimeError(f"show-object {uid} failed: {response.message or response.code or 'unknown error'}")

fetch_last_published_session(mgmt_name, domain_name) async

Read the domain's current last-published session WITHOUT storing it.

The read-only half of refresh_last_published_session. Callers that need to know where the domain's head is before deciding what to do with the cache must not advance the stored baseline in the process — doing so empties the diff window they are about to use. CacheRefreshCoordinator uses this to tell a forward publish from a revert.

Parameters:

Name Type Description Default
mgmt_name str

Management server name.

required
domain_name str

Domain name.

required

Returns:

Type Description
LastPublishedSession | None

An unsaved LastPublishedSession, or None if the API call failed or

LastPublishedSession | None

returned no usable timestamp.

Source code in src/arodonata/cache/object_service.py
async def fetch_last_published_session(
    self,
    mgmt_name: str,
    domain_name: str,
) -> LastPublishedSession | None:
    """Read the domain's current last-published session WITHOUT storing it.

    The read-only half of `refresh_last_published_session`. Callers that need
    to know where the domain's head is *before* deciding what to do with the
    cache must not advance the stored baseline in the process — doing so
    empties the diff window they are about to use. `CacheRefreshCoordinator`
    uses this to tell a forward publish from a revert.

    Args:
        mgmt_name: Management server name.
        domain_name: Domain name.

    Returns:
        An unsaved LastPublishedSession, or None if the API call failed or
        returned no usable timestamp.
    """
    try:
        api_domain = "" if domain_name in ("SMC User", "System Data") else domain_name

        response = await self._client.api_call(
            mgmt_name=mgmt_name,
            domain=api_domain,
            command="show-last-published-session",
            payload={},
        )

        if response.success and response.data:
            data = response.data
            meta_info = data.get("meta-info", {})
            last_modify_time = meta_info.get("last-modify-time", {})
            published_time = self._parse_api_timestamp(last_modify_time)

            if published_time:
                return LastPublishedSession(
                    id=f"{mgmt_name}:{domain_name}",
                    mgmt_name=mgmt_name,
                    domain_name=domain_name,
                    published_time=published_time,
                    uid=data.get("uid", ""),
                    name=data.get("name", ""),
                    ip_address=data.get("ip-address", ""),
                    creator=data.get("creator", ""),
                    description=data.get("description", ""),
                )
    except Exception as e:
        log().warning(f"Failed to read LastPublishedSession for {mgmt_name}/{domain_name}: {e}")

    return None

refresh_last_published_session(mgmt_name, domain_name) async

Refresh and upsert the last-published-session record for one domain.

Makes a single, lightweight show-last-published-session API call — does not touch CPObject or Asset caches. Safe to call independently of a full object/asset refresh.

Parameters:

Name Type Description Default
mgmt_name str

Management server name.

required
domain_name str

Domain name.

required

Returns:

Type Description
LastPublishedSession | None

The upserted LastPublishedSession record, or None if the API

LastPublishedSession | None

call failed or returned no usable timestamp.

Source code in src/arodonata/cache/object_service.py
async def refresh_last_published_session(
    self,
    mgmt_name: str,
    domain_name: str,
) -> LastPublishedSession | None:
    """Refresh and upsert the last-published-session record for one domain.

    Makes a single, lightweight `show-last-published-session` API call —
    does not touch CPObject or Asset caches. Safe to call independently
    of a full object/asset refresh.

    Args:
        mgmt_name: Management server name.
        domain_name: Domain name.

    Returns:
        The upserted LastPublishedSession record, or None if the API
        call failed or returned no usable timestamp.
    """
    record = await self.fetch_last_published_session(mgmt_name, domain_name)
    if record is None:
        return None
    try:
        await self._cache.upsert_last_published_session(record)
    except Exception as e:
        log().warning(f"Failed to store LastPublishedSession for {mgmt_name}/{domain_name}: {e}")
        return None
    return record

refresh_objects(mgmt_names=None, domain_names=None, mode='force', include_global=False) async

Refresh object cache from API.

Parameters:

Name Type Description Default
mgmt_names list[str] | None

Optional management server filter.

None
domain_names list[str] | None

Optional domain filter.

None
mode str

Refresh mode (skip/check/force/incremental).

'force'
include_global bool

When False (default), the synthetic "Global" domain is excluded from the all-domains refresh path so existing callers see today's behavior. An explicit domain_names request for "Global" is honored regardless of this flag.

False

Yields:

Type Description
AsyncIterator[dict[str, Any]]

Progress dictionaries with keys: - message: str - Progress message - mgmt_name: str - Management server name - domain_name: str - Domain name - object_type: str - Type being fetched - count: int - Number of objects processed - total: int - Total objects to process

Source code in src/arodonata/cache/object_service.py
async def refresh_objects(
    self,
    mgmt_names: list[str] | None = None,
    domain_names: list[str] | None = None,
    mode: str = "force",  # RefreshMode value
    include_global: bool = False,
) -> AsyncIterator[dict[str, Any]]:
    """Refresh object cache from API.

    Args:
        mgmt_names: Optional management server filter.
        domain_names: Optional domain filter.
        mode: Refresh mode (skip/check/force/incremental).
        include_global: When False (default), the synthetic "Global" domain
            is excluded from the all-domains refresh path so existing
            callers see today's behavior. An explicit ``domain_names``
            request for "Global" is honored regardless of this flag.

    Yields:
        Progress dictionaries with keys:
            - message: str - Progress message
            - mgmt_name: str - Management server name
            - domain_name: str - Domain name
            - object_type: str - Type being fetched
            - count: int - Number of objects processed
            - total: int - Total objects to process
    """
    # Parse refresh mode
    try:
        refresh_mode = RefreshMode(mode)
    except ValueError:
        log().warning(f"Invalid refresh mode '{mode}', defaulting to 'skip'")
        refresh_mode = RefreshMode.SKIP

    # Handle SKIP mode
    if refresh_mode == RefreshMode.SKIP:
        yield {
            "message": "Refresh skipped (mode=skip)",
            "status": "skipped",
        }
        return

    # Get target management servers
    target_mgmt_names = mgmt_names or self._client.get_mgmt_names()

    if not target_mgmt_names:
        yield {
            "message": "No management servers available",
            "status": "error",
        }
        return

    log().info(f"Refreshing object cache for {len(target_mgmt_names)} server(s), mode={refresh_mode.value}")

    # Process each management server
    for mgmt_name in target_mgmt_names:
        async for progress in self._refresh_mgmt_server(
            mgmt_name=mgmt_name,
            domain_names=domain_names,
            mode=refresh_mode,
            include_global=include_global,
        ):
            yield progress

search_objects(search_input, mgmt_names=None, domain_names=None, max_depth=2) async

Search for objects by IP, name, UID, or type.

Parameters:

Name Type Description Default
search_input str

Comma-separated search terms.

required
mgmt_names list[str] | None

Optional management server filter.

None
domain_names list[str] | None

Optional domain filter.

None
max_depth int

Maximum depth for group membership traversal.

2

Yields:

Type Description
AsyncIterator[SearchResult]

SearchResult for each search term.

Source code in src/arodonata/cache/object_service.py
async def search_objects(
    self,
    search_input: str,
    mgmt_names: list[str] | None = None,
    domain_names: list[str] | None = None,
    max_depth: int = 2,
) -> AsyncIterator[SearchResult]:
    """Search for objects by IP, name, UID, or type.

    Args:
        search_input: Comma-separated search terms.
        mgmt_names: Optional management server filter.
        domain_names: Optional domain filter.
        max_depth: Maximum depth for group membership traversal.

    Yields:
        SearchResult for each search term.
    """
    # Parse comma-separated input
    terms = [t.strip() for t in search_input.split(",") if t.strip()]

    if not terms:
        yield SearchResult(
            search_term=search_input,
            search_type=SearchType.NAME,
            objects=[],
        )
        return

    # Get target management servers
    target_mgmt_names = mgmt_names or self._client.get_mgmt_names()

    if not target_mgmt_names:
        log().warning("No management servers configured for search")
        yield SearchResult(
            search_term=search_input,
            search_type=SearchType.NAME,
            objects=[],
        )
        return

    # Search for each term
    for term in terms:
        # Classify the search input
        search_type, cleaned = classify_input(term)

        # Query cache based on search type
        objects = await self._fetch_objects_from_db(search_type, cleaned, target_mgmt_names, domain_names)

        # Resolve group memberships if objects found
        memberships: dict[str, list[GroupNode]] | None = None
        if objects and max_depth > 0:
            memberships = {}
            for obj in objects:
                obj_groups = await self._resolve_group_memberships(
                    obj_uid=obj.uid,
                    mgmt_name=obj.mgmt_name,
                    domain_name=obj.domain_name,
                    max_depth=max_depth,
                )
                if obj_groups:
                    memberships[obj.uid] = obj_groups

        yield SearchResult(
            search_term=term,
            search_type=search_type,
            objects=objects,
            memberships=memberships if memberships else None,
        )

RefreshMode

Bases: StrEnum

Cache refresh mode for object operations.

Attributes:

Name Type Description
SKIP

Use cache as-is without refresh.

CHECK

Refresh stale domains only (based on LastPublishedSession); stale domains get a full atomic reload.

FORCE

Refresh all domains unconditionally (full atomic reload).

INCREMENTAL

Refresh stale domains only (same staleness probe as CHECK); stale domains get a show-changes incremental apply with fallback to a full atomic reload on any unsafe condition.

Source code in src/arodonata/core/protocols.py
class RefreshMode(StrEnum):
    """Cache refresh mode for object operations.

    Attributes:
        SKIP: Use cache as-is without refresh.
        CHECK: Refresh stale domains only (based on LastPublishedSession);
            stale domains get a full atomic reload.
        FORCE: Refresh all domains unconditionally (full atomic reload).
        INCREMENTAL: Refresh stale domains only (same staleness probe as
            CHECK); stale domains get a show-changes incremental apply with
            fallback to a full atomic reload on any unsafe condition.
    """

    SKIP = "skip"
    CHECK = "check"
    FORCE = "force"
    INCREMENTAL = "incremental"

SearchResult dataclass

Search result for a single term.

Source code in src/arodonata/cache/object_service.py
@dataclass
class SearchResult:
    """Search result for a single term."""

    search_term: str
    search_type: SearchType
    objects: list[CPObject]
    memberships: dict[str, list[GroupNode]] | None = None

SearchType

Bases: StrEnum

Classification of search input.

Source code in src/arodonata/cache/object_service.py
class SearchType(StrEnum):
    """Classification of search input."""

    HOST = "host"
    NETWORK = "network"
    RANGE = "address-range"
    NAME = "name"

api_object_to_cpobject(api_obj, mgmt_name, domain_name)

Convert a full-detail API object dict to a CPObject row.

The single canonical converter: both the full-reload path and the incremental re-fetch path produce rows through this function.

Source code in src/arodonata/cache/object_service.py
def api_object_to_cpobject(
    api_obj: dict[str, Any],
    mgmt_name: str,
    domain_name: str,
) -> CPObject | None:
    """Convert a full-detail API object dict to a CPObject row.

    The single canonical converter: both the full-reload path and the
    incremental re-fetch path produce rows through this function.
    """
    try:
        # Extract common fields
        uid = api_obj.get("uid", "")
        name = api_obj.get("name", "")
        obj_type = api_obj.get("type", "")

        if not uid or not name:
            log().warning(f"API object missing uid or name: {api_obj}")
            return None

        # Build compound key
        obj_id = f"{mgmt_name}:{domain_name}:{uid}"

        # Extract IP fields
        ipv4_address, subnet4, subnet_mask, ipv4_address_first, ipv4_address_last = _extract_ip_fields(
            obj_type, api_obj
        )

        # Extract group members
        members = _extract_group_members(obj_type, api_obj)

        # Extract other common fields
        comments = api_obj.get("comments", "")
        tags = _extract_tags(api_obj)

        color = api_obj.get("color", "")

        # Extract new fields (interfaces, nat_settings, version, cluster_uid, original_domain_uid)
        interfaces = api_obj.get("interfaces")  # Returns list or None
        nat_settings = api_obj.get("nat-settings")  # Returns dict or None
        version = api_obj.get("version", "")
        cluster_uid = api_obj.get("cluster-uid", "")
        domain_data = api_obj.get("domain", {})
        if isinstance(domain_data, dict):
            original_domain_uid = domain_data.get("uid", "")
        else:
            original_domain_uid = ""

        # Extract timestamps
        # Check for malformed timestamp dicts (not iso-8601 or posix keys)
        # to match the old to_db_datetime behavior for backwards compatibility
        creation_time_data = api_obj.get("creation-time")
        if isinstance(creation_time_data, dict) and creation_time_data:
            if "iso-8601" not in creation_time_data and "posix" not in creation_time_data:
                raise ValueError(f"Malformed timestamp dict: {creation_time_data}")
        creation_time = _parse_api_timestamp(creation_time_data)

        last_modify_time_data = api_obj.get("last-modify-time")
        if isinstance(last_modify_time_data, dict) and last_modify_time_data:
            if "iso-8601" not in last_modify_time_data and "posix" not in last_modify_time_data:
                raise ValueError(f"Malformed timestamp dict: {last_modify_time_data}")
        last_modify_time = _parse_api_timestamp(last_modify_time_data)

        # Get original domain (for global objects)
        original_domain = api_obj.get("domain", {}).get("name", "")

        # Create CPObject
        cp_obj = CPObject(
            id=obj_id,
            uid=uid,
            name=name,
            type=obj_type,
            mgmt_name=mgmt_name,
            domain_name=domain_name,
            original_domain=original_domain,
            ipv4_address=ipv4_address,
            subnet4=subnet4,
            subnet_mask=subnet_mask,
            ipv4_address_first=ipv4_address_first,
            ipv4_address_last=ipv4_address_last,
            members=members,
            comments=comments,
            tags=tags,
            color=color,
            interfaces=interfaces,
            nat_settings=nat_settings,
            version=version,
            cluster_uid=cluster_uid,
            original_domain_uid=original_domain_uid,
            creation_time=creation_time,
            last_modify_time=last_modify_time,
            update_time=utc_now_naive(),
            raw_data=api_obj,
        )

        return cp_obj

    except Exception as e:
        log().exception(f"Error converting API object to CPObject: {e}")
        return None

classify_input(raw)

Classify search input and return (SearchType, cleaned_input).

Parameters:

Name Type Description Default
raw str

User input string (IP, network, range, or name).

required

Returns:

Type Description
tuple[SearchType, str]

Tuple of (SearchType, cleaned_input).

Examples:

>>> classify_input("127.0.0.1")
(SearchType.HOST, '127.0.0.1')
>>> classify_input("192.168.1.0/24")
(SearchType.NETWORK, '192.168.1.0/24')
>>> classify_input("10.0.0.1-10.0.0.10")
(SearchType.RANGE, '10.0.0.1-10.0.0.10')
>>> classify_input("web-server-01")
(SearchType.NAME, 'web-server-01')
Source code in src/arodonata/cache/object_service.py
def classify_input(raw: str) -> tuple[SearchType, str]:
    """Classify search input and return (SearchType, cleaned_input).

    Args:
        raw: User input string (IP, network, range, or name).

    Returns:
        Tuple of (SearchType, cleaned_input).

    Examples:
        >>> classify_input("127.0.0.1")
        (SearchType.HOST, '127.0.0.1')
        >>> classify_input("192.168.1.0/24")
        (SearchType.NETWORK, '192.168.1.0/24')
        >>> classify_input("10.0.0.1-10.0.0.10")
        (SearchType.RANGE, '10.0.0.1-10.0.0.10')
        >>> classify_input("web-server-01")
        (SearchType.NAME, 'web-server-01')
    """
    text = raw.strip()

    if _IP_RANGE_RE.match(text):
        return SearchType.RANGE, text

    if _NETWORK_RE.match(text):
        return SearchType.NETWORK, text

    if _IPV4_RE.match(text):
        return SearchType.HOST, text

    return SearchType.NAME, text