Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions examples/policy_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def main():
action="store_true",
help="Get logs for the specified policy check",
)
parser.add_argument("--page", type=int, default=1)
parser.add_argument("--page-size", type=int, default=20)
args = parser.parse_args()

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

options = PolicyCheckListOptions(
page_number=args.page,
page_size=args.page_size,
)

try:
pc_list = client.policy_checks.list(args.run_id, options)
pc_list = list(client.policy_checks.list(args.run_id, options))

print(f"Total policy checks: {pc_list.total_count}")
print(f"Page {pc_list.current_page} of {pc_list.total_pages}")
print(f"Total policy checks fetched: {len(pc_list)}")
print()

if not pc_list.items:
if not pc_list:
print("No policy checks found for this run.")
else:
for pc in pc_list.items:
for pc in pc_list:
print(f"- ID: {pc.id}")
print(f"Status: {pc.status}")
print(f"Scope: {pc.scope}")
Expand Down
35 changes: 23 additions & 12 deletions examples/state_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,23 @@ def main():
parser.add_argument("--workspace-id", required=True, help="Workspace ID")
parser.add_argument("--download", help="Path to save downloaded current state")
parser.add_argument("--upload", help="Path to a .tfstate (or JSON state) to upload")
parser.add_argument("--page", type=int, default=1)
parser.add_argument("--page-size", type=int, default=10)
args = parser.parse_args()

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

options = StateVersionListOptions(
page_number=args.page,
page_size=args.page_size,
organization=args.org,
workspace=args.workspace,
)

sv_list = client.state_versions.list(options)

print(f"Total state versions: {sv_list.total_count}")
print(f"Page {sv_list.current_page} of {sv_list.total_pages}")
sv_list = list(client.state_versions.list(options))
print(f"Total state versions: {len(sv_list)}")
print()

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

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

# 2) Read the current state version (with outputs included if you want)
Expand All @@ -84,15 +80,30 @@ def main():

# 4) List outputs for the current state version (paged)
_print_header("Listing outputs (current state version)")
outs = client.state_versions.list_outputs(
current.id, options=StateVersionOutputsListOptions(page_size=50)
outs = list(
client.state_versions.list_outputs(
current.id, options=StateVersionOutputsListOptions(page_size=50)
)
)
if not outs.items:
if not outs:
print("No outputs found.")
for o in outs.items:
for o in outs:
# Sensitive outputs will have value = None
print(f"- {o.name}: sensitive={o.sensitive} type={o.type} value={o.value}")

if args.workspace_id:
# 4b) List outputs for the current state version via workspace endpoint
_print_header("Listing outputs via workspace endpoint")
outs2 = list(
client.state_version_outputs.read_current(
args.workspace_id, options=StateVersionOutputsListOptions(page_size=50)
)
)
if not outs2:
print("No outputs found.")
for o in outs2:
print(f"- {o.name}: sensitive={o.sensitive} type={o.type} value={o.value}")

# 5) (Optional) Upload a new state file
if args.upload:
_print_header(f"Uploading new state from: {args.upload}")
Expand Down
41 changes: 31 additions & 10 deletions examples/variable_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def variable_set_example():
list_options = VariableSetListOptions(
page_size=10, include=[VariableSetIncludeOpt.WORKSPACES]
)
variable_sets = client.variable_sets.list(org_name, list_options)
variable_sets = list(client.variable_sets.list(org_name, list_options))
print(f"Found {len(variable_sets)} existing variable sets")

for vs in variable_sets[:3]: # Show first 3
Expand Down Expand Up @@ -92,6 +92,15 @@ def variable_set_example():
print(f"Priority: {new_variable_set.priority}")
print()

print("Listing existing variable sets...")
list_options = VariableSetListOptions(page_size=10)
variable_sets = list(client.variable_sets.list(org_name, list_options))
print(f"Found {len(variable_sets)} existing variable sets")

for vs in variable_sets: # Show first 3
print(f"- {vs.name} (ID: {vs.id}, Global: {vs.global_})")
print()

# 3. Create variables in the variable set
print("3. Creating variables in the variable set...")

Expand Down Expand Up @@ -147,12 +156,16 @@ def variable_set_example():
# 4. List variables in the variable set
print("4. Listing variables in the variable set...")
var_list_options = VariableSetVariableListOptions(page_size=50)
variables = client.variable_set_variables.list(
created_variable_set_id, var_list_options
variables = list(
client.variable_set_variables.list(
created_variable_set_id, var_list_options
)
)
print(f"Found {len(variables)} variables in the set:")

for var in variables:
for var in client.variable_set_variables.list(
created_variable_set_id, var_list_options
):
sensitive_note = " (sensitive)" if var.sensitive else ""
hcl_note = " (HCL)" if var.hcl else ""
print(f"- {var.key}: {var.category.value}{sensitive_note}{hcl_note}")
Expand Down Expand Up @@ -212,10 +225,14 @@ def variable_set_example():
print("Successfully applied to workspace")

# List variable sets for this workspace
workspace_varsets = client.variable_sets.list_for_workspace(
print(f"Listing variable sets for workspace: {first_workspace.name}")
workspace_varsets = 0
for ws_varset in client.variable_sets.list_for_workspace(
first_workspace.id
)
print(f"Workspace now has {len(workspace_varsets)} variable sets")
):
print(f"- {ws_varset.name} (ID: {ws_varset.id})")
workspace_varsets += 1
print(f"Workspace now has {workspace_varsets} variable sets")

# Remove from workspace
remove_ws_options = VariableSetRemoveFromWorkspacesOptions(
Expand Down Expand Up @@ -250,10 +267,14 @@ def variable_set_example():
print("Successfully applied to project")

# List variable sets for this project
project_varsets = client.variable_sets.list_for_project(
print(f"Listing variable sets for project: {first_project.name}")
project_varsets = 0
for proj_varset in client.variable_sets.list_for_project(
first_project.id
)
print(f"Project now has {len(project_varsets)} variable sets")
):
print(f"- {proj_varset.name} (ID: {proj_varset.id})")
project_varsets += 1
print(f"Project now has {project_varsets} variable sets")

# Remove from project
remove_proj_options = VariableSetRemoveFromProjectsOptions(
Expand Down
7 changes: 0 additions & 7 deletions src/pytfe/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
PolicyActions,
PolicyCheck,
PolicyCheckIncludeOpt,
PolicyCheckList,
PolicyCheckListOptions,
PolicyPermissions,
PolicyResult,
Expand Down Expand Up @@ -295,13 +294,11 @@
StateVersion,
StateVersionCreateOptions,
StateVersionCurrentOptions,
StateVersionList,
StateVersionListOptions,
StateVersionReadOptions,
)
from .state_version_output import (
StateVersionOutput,
StateVersionOutputsList,
StateVersionOutputsListOptions,
)
from .team import (
Expand Down Expand Up @@ -611,7 +608,6 @@
"PolicyResult",
"PolicyStatusTimestamps",
"PolicyCheckListOptions",
"PolicyCheckList",
# Policy Evaluation
"PolicyAttachable",
"PolicyEvaluation",
Expand Down Expand Up @@ -669,15 +665,12 @@
"StateVersion",
"StateVersionCreateOptions",
"StateVersionCurrentOptions",
"StateVersionList",
"StateVersionListOptions",
"StateVersionReadOptions",
# State Version Outputs
"StateVersionOutput",
"StateVersionOutputsList",
"StateVersionOutputsListOptions",
]

# Rebuild models with forward references after all models are loaded
PolicyCheck.model_rebuild()
PolicyCheckList.model_rebuild()
1 change: 0 additions & 1 deletion src/pytfe/models/policy_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class PolicyCheckListOptions(BaseModel):
model_config = ConfigDict(populate_by_name=True, validate_by_name=True)

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


Expand Down
1 change: 0 additions & 1 deletion src/pytfe/models/state_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class StateVersionListOptions(BaseModel):
model_config = ConfigDict(populate_by_name=True, validate_by_name=True)

# Standard pagination + filters
page_number: int | None = Field(None, alias="page[number]")
page_size: int | None = Field(None, alias="page[size]")
organization: str | None = Field(None, alias="filter[organization][name]")
workspace: str | None = Field(None, alias="filter[workspace][name]")
Expand Down
1 change: 0 additions & 1 deletion src/pytfe/models/state_version_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class StateVersionOutput(BaseModel):
class StateVersionOutputsListOptions(BaseModel):
model_config = ConfigDict(populate_by_name=True, validate_by_name=True)

page_number: int | None = Field(None, alias="page[number]")
page_size: int | None = Field(None, alias="page[size]")


Expand Down
1 change: 0 additions & 1 deletion src/pytfe/models/variable_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ class VariableSetListOptions(BaseModel):
"""Options for listing variable sets."""

# Pagination options
page_number: int | None = None
page_size: int | None = None
include: list[VariableSetIncludeOpt] | None = None
query: str | None = None # Filter by name
Expand Down
36 changes: 10 additions & 26 deletions src/pytfe/resources/policy_check.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

import time
from collections.abc import Iterator

from ..errors import (
InvalidPolicyCheckIDError,
InvalidRunIDError,
)
from ..models.policy_check import (
PolicyCheck,
PolicyCheckList,
PolicyCheckListOptions,
PolicyStatus,
)
Expand All @@ -24,35 +24,19 @@ class PolicyChecks(_Service):

def list(
self, run_id: str, options: PolicyCheckListOptions | None = None
) -> PolicyCheckList:
) -> Iterator[PolicyCheck]:
"""List all policy checks of the given run."""
if not valid_string_id(run_id):
raise InvalidRunIDError()
params = (
options.model_dump(by_alias=True, exclude_none=True) if options else None
)
r = self.t.request(
"GET",
f"/api/v2/runs/{run_id}/policy-checks",
params=params,
)
jd = r.json()
items = []
meta = jd.get("meta", {})
pagination = meta.get("pagination", {})
for d in jd.get("data", []):
attrs = d.get("attributes", {})
attrs["id"] = d.get("id")
attrs["run"] = d.get("relationships", {}).get("run", {})
items.append(PolicyCheck.model_validate(attrs))
return PolicyCheckList(
items=items,
current_page=pagination.get("current-page"),
total_pages=pagination.get("total-pages"),
prev_page=pagination.get("prev-page"),
next_page=pagination.get("next-page"),
total_count=pagination.get("total-count"),
)
path = f"/api/v2/runs/{run_id}/policy-checks"
for item in self._list(path, params=params):
attrs = item.get("attributes", {})
attrs["id"] = item.get("id")
attrs["run"] = item.get("relationships", {}).get("run", {}).get("data")
yield PolicyCheck.model_validate(attrs)

def read(self, policy_check_id: str) -> PolicyCheck:
"""Read a policy check by its ID."""
Expand All @@ -66,7 +50,7 @@ def read(self, policy_check_id: str) -> PolicyCheck:
d = jd.get("data", {})
attrs = d.get("attributes", {})
attrs["id"] = d.get("id")
attrs["run"] = d.get("relationships", {}).get("run", {})
attrs["run"] = d.get("relationships", {}).get("run", {}).get("data")
return PolicyCheck.model_validate(attrs)

def override(self, policy_check_id: str) -> PolicyCheck:
Expand All @@ -81,7 +65,7 @@ def override(self, policy_check_id: str) -> PolicyCheck:
d = jd.get("data", {})
attrs = d.get("attributes", {})
attrs["id"] = d.get("id")
attrs["run"] = d.get("relationships", {}).get("run", {})
attrs["run"] = d.get("relationships", {}).get("run", {}).get("data")
return PolicyCheck.model_validate(attrs)

def logs(self, policy_check_id: str) -> str:
Expand Down
Loading
Loading