Skip to content

Commit 830cbf7

Browse files
committed
refactor(policy-check): Iterator conversion and unit tests added to the policy-check
1 parent 33996a2 commit 830cbf7

5 files changed

Lines changed: 298 additions & 37 deletions

File tree

examples/policy_check.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def main():
3535
action="store_true",
3636
help="Get logs for the specified policy check",
3737
)
38-
parser.add_argument("--page", type=int, default=1)
3938
parser.add_argument("--page-size", type=int, default=20)
4039
args = parser.parse_args()
4140

@@ -50,21 +49,19 @@ def main():
5049
_print_header(f"Listing policy checks for run: {args.run_id}")
5150

5251
options = PolicyCheckListOptions(
53-
page_number=args.page,
5452
page_size=args.page_size,
5553
)
5654

5755
try:
58-
pc_list = client.policy_checks.list(args.run_id, options)
56+
pc_list = list(client.policy_checks.list(args.run_id, options))
5957

60-
print(f"Total policy checks: {pc_list.total_count}")
61-
print(f"Page {pc_list.current_page} of {pc_list.total_pages}")
58+
print(f"Total policy checks fetched: {len(pc_list)}")
6259
print()
6360

64-
if not pc_list.items:
61+
if not pc_list:
6562
print("No policy checks found for this run.")
6663
else:
67-
for pc in pc_list.items:
64+
for pc in pc_list:
6865
print(f"- ID: {pc.id}")
6966
print(f"Status: {pc.status}")
7067
print(f"Scope: {pc.scope}")

src/pytfe/models/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
PolicyActions,
107107
PolicyCheck,
108108
PolicyCheckIncludeOpt,
109-
PolicyCheckList,
110109
PolicyCheckListOptions,
111110
PolicyPermissions,
112111
PolicyResult,
@@ -611,7 +610,6 @@
611610
"PolicyResult",
612611
"PolicyStatusTimestamps",
613612
"PolicyCheckListOptions",
614-
"PolicyCheckList",
615613
# Policy Evaluation
616614
"PolicyAttachable",
617615
"PolicyEvaluation",
@@ -680,4 +678,3 @@
680678

681679
# Rebuild models with forward references after all models are loaded
682680
PolicyCheck.model_rebuild()
683-
PolicyCheckList.model_rebuild()

src/pytfe/models/policy_check.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class PolicyCheckListOptions(BaseModel):
106106
model_config = ConfigDict(populate_by_name=True, validate_by_name=True)
107107

108108
include: list[PolicyCheckIncludeOpt] | None = Field(None, alias="include")
109-
page_number: int | None = Field(None, alias="page[number]")
110109
page_size: int | None = Field(None, alias="page[size]")
111110

112111

src/pytfe/resources/policy_check.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __future__ import annotations
22

33
import time
4+
from collections.abc import Iterator
45

56
from ..errors import (
67
InvalidPolicyCheckIDError,
78
InvalidRunIDError,
89
)
910
from ..models.policy_check import (
1011
PolicyCheck,
11-
PolicyCheckList,
1212
PolicyCheckListOptions,
1313
PolicyStatus,
1414
)
@@ -24,35 +24,19 @@ class PolicyChecks(_Service):
2424

2525
def list(
2626
self, run_id: str, options: PolicyCheckListOptions | None = None
27-
) -> PolicyCheckList:
27+
) -> Iterator[PolicyCheck]:
2828
"""List all policy checks of the given run."""
2929
if not valid_string_id(run_id):
3030
raise InvalidRunIDError()
3131
params = (
3232
options.model_dump(by_alias=True, exclude_none=True) if options else None
3333
)
34-
r = self.t.request(
35-
"GET",
36-
f"/api/v2/runs/{run_id}/policy-checks",
37-
params=params,
38-
)
39-
jd = r.json()
40-
items = []
41-
meta = jd.get("meta", {})
42-
pagination = meta.get("pagination", {})
43-
for d in jd.get("data", []):
44-
attrs = d.get("attributes", {})
45-
attrs["id"] = d.get("id")
46-
attrs["run"] = d.get("relationships", {}).get("run", {})
47-
items.append(PolicyCheck.model_validate(attrs))
48-
return PolicyCheckList(
49-
items=items,
50-
current_page=pagination.get("current-page"),
51-
total_pages=pagination.get("total-pages"),
52-
prev_page=pagination.get("prev-page"),
53-
next_page=pagination.get("next-page"),
54-
total_count=pagination.get("total-count"),
55-
)
34+
path = f"/api/v2/runs/{run_id}/policy-checks"
35+
for item in self._list(path, params=params):
36+
attrs = item.get("attributes", {})
37+
attrs["id"] = item.get("id")
38+
attrs["run"] = item.get("relationships", {}).get("run", {}).get("data")
39+
yield PolicyCheck.model_validate(attrs)
5640

5741
def read(self, policy_check_id: str) -> PolicyCheck:
5842
"""Read a policy check by its ID."""
@@ -66,7 +50,7 @@ def read(self, policy_check_id: str) -> PolicyCheck:
6650
d = jd.get("data", {})
6751
attrs = d.get("attributes", {})
6852
attrs["id"] = d.get("id")
69-
attrs["run"] = d.get("relationships", {}).get("run", {})
53+
attrs["run"] = d.get("relationships", {}).get("run", {}).get("data")
7054
return PolicyCheck.model_validate(attrs)
7155

7256
def override(self, policy_check_id: str) -> PolicyCheck:
@@ -81,7 +65,7 @@ def override(self, policy_check_id: str) -> PolicyCheck:
8165
d = jd.get("data", {})
8266
attrs = d.get("attributes", {})
8367
attrs["id"] = d.get("id")
84-
attrs["run"] = d.get("relationships", {}).get("run", {})
68+
attrs["run"] = d.get("relationships", {}).get("run", {}).get("data")
8569
return PolicyCheck.model_validate(attrs)
8670

8771
def logs(self, policy_check_id: str) -> str:

0 commit comments

Comments
 (0)