|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import functools |
3 | 4 | from dataclasses import dataclass |
4 | 5 | from pathlib import Path |
5 | | -from typing import Any, Dict, List, Optional, Union, cast |
| 6 | +from typing import Any, Dict, List, Optional, Tuple, Union, cast |
6 | 7 |
|
7 | 8 | from sift.annotations.v1.annotations_pb2 import AnnotationType |
8 | 9 | from sift.assets.v1.assets_pb2 import Asset |
@@ -262,7 +263,7 @@ def detach_asset(self, rule: Union[str, RuleConfig], asset_names: List[str]) -> |
262 | 263 | def _attach_or_detach_asset( |
263 | 264 | self, rule: Union[str, RuleConfig], asset_names: List[str], attach: bool |
264 | 265 | ) -> RuleConfig: |
265 | | - assets = self._get_assets(names=asset_names) |
| 266 | + assets = self._get_assets(names=tuple(sorted(asset_names))) |
266 | 267 | if not assets: |
267 | 268 | raise ValueError( |
268 | 269 | f"Cannot find all assets in list '{asset_names}'. One of these assets does not exist." |
@@ -388,7 +389,11 @@ def _update_req_from_rule_config( |
388 | 389 | ) |
389 | 390 |
|
390 | 391 | # TODO: once we have TagService_ListTags we can do asset-agnostic rules via tags |
391 | | - assets = self._get_assets(names=config.asset_names) if config.asset_names else None |
| 392 | + assets = ( |
| 393 | + self._get_assets(names=tuple(sorted(config.asset_names))) |
| 394 | + if config.asset_names |
| 395 | + else None |
| 396 | + ) |
392 | 397 |
|
393 | 398 | actions = [] |
394 | 399 | if config.action.kind() == RuleActionKind.NOTIFICATION: |
@@ -557,7 +562,7 @@ def get_rule(self, rule: str) -> Optional[RuleConfig]: |
557 | 562 | ) |
558 | 563 |
|
559 | 564 | assets = self._get_assets( |
560 | | - ids=[asset_id for asset_id in rule_pb.asset_configuration.asset_ids] |
| 565 | + ids=tuple(sorted([asset_id for asset_id in rule_pb.asset_configuration.asset_ids])) |
561 | 566 | ) |
562 | 567 | asset_names = [asset.name for asset in assets] |
563 | 568 |
|
@@ -597,7 +602,8 @@ def _get_rule_from_rule_id(self, rule_id: str) -> Optional[Rule]: |
597 | 602 | except: |
598 | 603 | return None |
599 | 604 |
|
600 | | - def _get_assets(self, names: List[str] = [], ids: List[str] = []) -> List[Asset]: |
| 605 | + @functools.cache |
| 606 | + def _get_assets(self, names: Tuple[str] = [], ids: Tuple[str] = []) -> List[Asset]: |
601 | 607 | return list_assets_impl(self._asset_service_stub, names, ids) |
602 | 608 |
|
603 | 609 |
|
|
0 commit comments