Skip to content

Commit e801ed0

Browse files
committed
refactor(state-version): Iterator conversion and unit tests added to the state-version
1 parent 830cbf7 commit e801ed0

5 files changed

Lines changed: 327 additions & 26 deletions

File tree

examples/state_versions.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,23 @@ def main():
3333
parser.add_argument("--workspace-id", required=True, help="Workspace ID")
3434
parser.add_argument("--download", help="Path to save downloaded current state")
3535
parser.add_argument("--upload", help="Path to a .tfstate (or JSON state) to upload")
36-
parser.add_argument("--page", type=int, default=1)
3736
parser.add_argument("--page-size", type=int, default=10)
3837
args = parser.parse_args()
3938

4039
cfg = TFEConfig(address=args.address, token=args.token)
4140
client = TFEClient(cfg)
4241

4342
options = StateVersionListOptions(
44-
page_number=args.page,
4543
page_size=args.page_size,
4644
organization=args.org,
4745
workspace=args.workspace,
4846
)
4947

50-
sv_list = client.state_versions.list(options)
51-
52-
print(f"Total state versions: {sv_list.total_count}")
53-
print(f"Page {sv_list.current_page} of {sv_list.total_pages}")
48+
sv_list = list(client.state_versions.list(options))
49+
print(f"Total state versions: {len(sv_list)}")
5450
print()
5551

56-
for sv in sv_list.items:
52+
for sv in sv_list:
5753
print(f"- {sv.id} | status={sv.status} | created_at={sv.created_at}")
5854

5955
# 1) List all state versions across org and workspace filters
@@ -63,7 +59,7 @@ def main():
6359
organization=args.org, workspace=args.workspace, page_size=args.page_size
6460
)
6561
)
66-
for sv in all_sv.items:
62+
for sv in all_sv:
6763
print(f"- {sv.id} | status={sv.status} | created_at={sv.created_at}")
6864

6965
# 2) Read the current state version (with outputs included if you want)

src/pytfe/models/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@
294294
StateVersion,
295295
StateVersionCreateOptions,
296296
StateVersionCurrentOptions,
297-
StateVersionList,
298297
StateVersionListOptions,
299298
StateVersionReadOptions,
300299
)
@@ -667,7 +666,6 @@
667666
"StateVersion",
668667
"StateVersionCreateOptions",
669668
"StateVersionCurrentOptions",
670-
"StateVersionList",
671669
"StateVersionListOptions",
672670
"StateVersionReadOptions",
673671
# State Version Outputs

src/pytfe/models/state_version.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class StateVersionListOptions(BaseModel):
7878
model_config = ConfigDict(populate_by_name=True, validate_by_name=True)
7979

8080
# Standard pagination + filters
81-
page_number: int | None = Field(None, alias="page[number]")
8281
page_size: int | None = Field(None, alias="page[size]")
8382
organization: str | None = Field(None, alias="filter[organization][name]")
8483
workspace: str | None = Field(None, alias="filter[workspace][name]")

src/pytfe/resources/state_versions.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from collections.abc import Iterator
34
from typing import Any
45
from urllib.parse import urlencode
56

@@ -10,7 +11,6 @@
1011
StateVersion,
1112
StateVersionCreateOptions,
1213
StateVersionCurrentOptions,
13-
StateVersionList,
1414
StateVersionListOptions,
1515
StateVersionReadOptions,
1616
)
@@ -69,28 +69,19 @@ def _encode_query(params: dict[str, Any]) -> str:
6969
return ""
7070
return "?" + urlencode(clean, doseq=True)
7171

72-
def list(self, options: StateVersionListOptions | None = None) -> StateVersionList:
72+
def list(
73+
self, options: StateVersionListOptions | None = None
74+
) -> Iterator[StateVersion]:
7375
"""
7476
GET /state-versions
7577
Accepts filters for organization and workspace and standard pagination.
7678
"""
7779
params = options.model_dump(by_alias=True, exclude_none=True) if options else {}
7880
path = f"/api/v2/state-versions{self._encode_query(params)}"
79-
r = self.t.request("GET", path)
80-
jd = r.json()
81-
# Expecting JSON:API list. Normalize to models.
82-
items = []
83-
meta = jd.get("meta", {})
84-
for d in jd.get("data", []):
81+
for d in self._list(path, params=params):
8582
attrs = d.get("attributes", {})
8683
attrs["id"] = d.get("id")
87-
items.append(StateVersion.model_validate(attrs))
88-
return StateVersionList(
89-
items=items,
90-
current_page=meta.get("pagination", {}).get("current-page"),
91-
total_pages=meta.get("pagination", {}).get("total-pages"),
92-
total_count=meta.get("pagination", {}).get("total-count"),
93-
)
84+
yield StateVersion.model_validate(attrs)
9485

9586
def read(self, state_version_id: str) -> StateVersion:
9687
"""Read a state version by ID."""

0 commit comments

Comments
 (0)