|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from collections.abc import Iterator |
3 | 4 | from typing import Any |
4 | 5 |
|
5 | 6 | from ..errors import ( |
|
10 | 11 | TerraformVersionValidForPlanOnlyError, |
11 | 12 | ) |
12 | 13 | from ..models.run import ( |
13 | | - OrganizationRunList, |
14 | 14 | Run, |
15 | 15 | RunApplyOptions, |
16 | 16 | RunCancelOptions, |
17 | 17 | RunCreateOptions, |
18 | 18 | RunDiscardOptions, |
19 | 19 | RunForceCancelOptions, |
20 | | - RunList, |
21 | 20 | RunListForOrganizationOptions, |
22 | 21 | RunListOptions, |
23 | 22 | RunReadOptions, |
|
27 | 26 |
|
28 | 27 |
|
29 | 28 | class Runs(_Service): |
30 | | - def list(self, workspace_id: str, options: RunListOptions | None = None) -> RunList: |
| 29 | + def list( |
| 30 | + self, workspace_id: str, options: RunListOptions | None = None |
| 31 | + ) -> Iterator[Run]: |
31 | 32 | """List all the runs of the given workspace.""" |
32 | 33 | if not valid_string_id(workspace_id): |
33 | 34 | raise InvalidWorkspaceIDError() |
34 | | - params = ( |
35 | | - options.model_dump(by_alias=True, exclude_none=True) if options else None |
36 | | - ) |
37 | | - r = self.t.request( |
38 | | - "GET", |
39 | | - f"/api/v2/workspaces/{workspace_id}/runs", |
40 | | - params=params, |
41 | | - ) |
42 | | - jd = r.json() |
43 | | - items = [] |
44 | | - meta = jd.get("meta", {}) |
45 | | - pagination = meta.get("pagination", {}) |
46 | | - for d in jd.get("data", []): |
47 | | - attrs = d.get("attributes", {}) |
48 | | - attrs["id"] = d.get("id") |
49 | | - items.append(Run.model_validate(attrs)) |
50 | | - return RunList( |
51 | | - items=items, |
52 | | - current_page=pagination.get("current-page"), |
53 | | - total_pages=pagination.get("total-pages"), |
54 | | - prev_page=pagination.get("prev-page"), |
55 | | - next_page=pagination.get("next-page"), |
56 | | - total_count=pagination.get("total-count"), |
57 | | - ) |
| 35 | + params = options.model_dump(by_alias=True) if options else {} |
| 36 | + path = f"/api/v2/workspaces/{workspace_id}/runs" |
| 37 | + for item in self._list(path, params=params): |
| 38 | + attrs = item.get("attributes", {}) |
| 39 | + attrs["id"] = item.get("id") |
| 40 | + yield Run.model_validate(attrs) |
58 | 41 |
|
59 | 42 | def list_for_organization( |
60 | 43 | self, organization: str, options: RunListForOrganizationOptions | None = None |
61 | | - ) -> OrganizationRunList: |
| 44 | + ) -> Iterator[Run]: |
62 | 45 | """List all the runs of the given organization.""" |
63 | 46 | if not valid_string_id(organization): |
64 | 47 | raise InvalidOrgError() |
65 | | - params = ( |
66 | | - options.model_dump(by_alias=True, exclude_none=True) if options else None |
67 | | - ) |
68 | | - r = self.t.request( |
69 | | - "GET", |
70 | | - f"/api/v2/organizations/{organization}/runs", |
71 | | - params=params, |
72 | | - ) |
73 | | - jd = r.json() |
74 | | - items = [] |
75 | | - meta = jd.get("meta", {}) |
76 | | - pagination = meta.get("pagination", {}) |
77 | | - for d in jd.get("data", []): |
78 | | - attrs = d.get("attributes", {}) |
79 | | - attrs["id"] = d.get("id") |
80 | | - items.append(Run.model_validate(attrs)) |
81 | | - return OrganizationRunList( |
82 | | - items=items, |
83 | | - current_page=pagination.get("current-page"), |
84 | | - prev_page=pagination.get("prev-page"), |
85 | | - next_page=pagination.get("next-page"), |
86 | | - ) |
| 48 | + path = f"/api/v2/organizations/{organization}/runs" |
| 49 | + params = options.model_dump(by_alias=True, exclude_none=True) if options else {} |
| 50 | + # meta = jd.get("meta", {}) |
| 51 | + # pagination = meta.get("pagination", {}) |
| 52 | + for item in self._list(path, params=params): |
| 53 | + attrs = item.get("attributes", {}) |
| 54 | + attrs["id"] = item.get("id") |
| 55 | + yield Run.model_validate(attrs) |
87 | 56 |
|
88 | 57 | def create(self, options: RunCreateOptions) -> Run: |
89 | 58 | """Create a new run for the given workspace.""" |
|
0 commit comments