Skip to content

Commit 361b94e

Browse files
agent_pools: return AgentPool from assign/remove workspace operations; update tests and example
1 parent ebe6696 commit 361b94e

3 files changed

Lines changed: 98 additions & 10 deletions

File tree

examples/agent_pool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@ def main():
107107
# remove_from_workspaces sends PATCH /agent-pools/:id with relationships.excluded-workspaces
108108
if workspace_id:
109109
print("\n Assigning workspace to agent pool...")
110-
client.agent_pools.assign_to_workspaces(
110+
updated_pool = client.agent_pools.assign_to_workspaces(
111111
new_pool.id,
112112
AgentPoolAssignToWorkspacesOptions(workspace_ids=[workspace_id]),
113113
)
114-
print(f" Assigned workspace {workspace_id} to pool")
114+
print(f" Assigned workspace {workspace_id} to pool {updated_pool.name}")
115115

116116
print("\n Removing workspace from agent pool...")
117-
client.agent_pools.remove_from_workspaces(
117+
updated_pool = client.agent_pools.remove_from_workspaces(
118118
new_pool.id,
119119
AgentPoolRemoveFromWorkspacesOptions(workspace_ids=[workspace_id]),
120120
)
121-
print(f" Excluded workspace {workspace_id} from pool")
121+
print(f" Removed workspace {workspace_id} from pool {updated_pool.name}")
122122
else:
123123
print("\n Skipping workspace assignment (set TFE_WORKSPACE_ID to test)")
124124

src/pytfe/resources/agent_pools.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ def delete(self, agent_pool_id: str) -> None:
409409

410410
def assign_to_workspaces(
411411
self, agent_pool_id: str, options: AgentPoolAssignToWorkspacesOptions
412-
) -> None:
412+
) -> AgentPool:
413413
"""Assign an agent pool to workspaces by updating the allowed-workspaces
414414
relationship via PATCH /agent-pools/:id.
415415
@@ -420,6 +420,9 @@ def assign_to_workspaces(
420420
agent_pool_id: Agent pool ID
421421
options: Assignment options containing workspace IDs
422422
423+
Returns:
424+
Updated AgentPool object
425+
423426
Raises:
424427
ValueError: If parameters are invalid
425428
TFEError: If API request fails
@@ -450,11 +453,34 @@ def assign_to_workspaces(
450453
},
451454
}
452455
}
453-
self.t.request("PATCH", path, json_body=payload)
456+
response = self.t.request("PATCH", path, json_body=payload)
457+
data = response.json()["data"]
458+
459+
# Extract agent pool data from response
460+
attr = data.get("attributes", {}) or {}
461+
agent_pool_data = {
462+
"id": _safe_str(data.get("id")),
463+
"name": _safe_str(attr.get("name")),
464+
"created_at": attr.get("created-at"),
465+
"organization_scoped": attr.get("organization-scoped"),
466+
"allowed_workspace_policy": attr.get("allowed-workspace-policy"),
467+
"agent_count": attr.get("agent-count", 0),
468+
}
469+
470+
return AgentPool(
471+
id=_safe_str(agent_pool_data["id"]) or "",
472+
name=_safe_str(agent_pool_data["name"]),
473+
created_at=cast(Any, agent_pool_data["created_at"]),
474+
organization_scoped=_safe_bool(agent_pool_data["organization_scoped"]),
475+
allowed_workspace_policy=_safe_workspace_policy(
476+
agent_pool_data["allowed_workspace_policy"]
477+
),
478+
agent_count=_safe_int(agent_pool_data["agent_count"]),
479+
)
454480

455481
def remove_from_workspaces(
456482
self, agent_pool_id: str, options: AgentPoolRemoveFromWorkspacesOptions
457-
) -> None:
483+
) -> AgentPool:
458484
"""Exclude workspaces from an agent pool by updating the excluded-workspaces
459485
relationship via PATCH /agent-pools/:id.
460486
@@ -466,6 +492,9 @@ def remove_from_workspaces(
466492
agent_pool_id: Agent pool ID
467493
options: Removal options containing workspace IDs to exclude
468494
495+
Returns:
496+
Updated AgentPool object
497+
469498
Raises:
470499
ValueError: If parameters are invalid
471500
TFEError: If API request fails
@@ -496,4 +525,27 @@ def remove_from_workspaces(
496525
},
497526
}
498527
}
499-
self.t.request("PATCH", path, json_body=payload)
528+
response = self.t.request("PATCH", path, json_body=payload)
529+
data = response.json()["data"]
530+
531+
# Extract agent pool data from response
532+
attr = data.get("attributes", {}) or {}
533+
agent_pool_data = {
534+
"id": _safe_str(data.get("id")),
535+
"name": _safe_str(attr.get("name")),
536+
"created_at": attr.get("created-at"),
537+
"organization_scoped": attr.get("organization-scoped"),
538+
"allowed_workspace_policy": attr.get("allowed-workspace-policy"),
539+
"agent_count": attr.get("agent-count", 0),
540+
}
541+
542+
return AgentPool(
543+
id=_safe_str(agent_pool_data["id"]) or "",
544+
name=_safe_str(agent_pool_data["name"]),
545+
created_at=cast(Any, agent_pool_data["created_at"]),
546+
organization_scoped=_safe_bool(agent_pool_data["organization_scoped"]),
547+
allowed_workspace_policy=_safe_workspace_policy(
548+
agent_pool_data["allowed_workspace_policy"]
549+
),
550+
agent_count=_safe_int(agent_pool_data["agent_count"]),
551+
)

tests/units/test_agent_pools.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,29 @@ def test_assign_to_workspaces(self, agent_pools_service, mock_transport):
290290
pool_id = "apool-123456789abcdef0"
291291
ws_id = "ws-aaaaaaaaaaaaaaa1"
292292

293-
agent_pools_service.assign_to_workspaces(
293+
mock_response = {
294+
"data": {
295+
"id": pool_id,
296+
"type": "agent-pools",
297+
"attributes": {
298+
"name": "test-pool",
299+
"created-at": "2023-01-01T00:00:00Z",
300+
"organization-scoped": True,
301+
"allowed-workspace-policy": "all-workspaces",
302+
"agent-count": 0,
303+
},
304+
}
305+
}
306+
mock_transport.request.return_value.json.return_value = mock_response
307+
308+
agent_pool = agent_pools_service.assign_to_workspaces(
294309
pool_id,
295310
AgentPoolAssignToWorkspacesOptions(workspace_ids=[ws_id]),
296311
)
297312

313+
assert agent_pool.id == pool_id
314+
assert agent_pool.name == "test-pool"
315+
298316
call_args = mock_transport.request.call_args
299317
# Must be PATCH, not POST
300318
assert call_args[0][0] == "PATCH"
@@ -317,11 +335,29 @@ def test_remove_from_workspaces(self, agent_pools_service, mock_transport):
317335
pool_id = "apool-123456789abcdef0"
318336
ws_id = "ws-aaaaaaaaaaaaaaa1"
319337

320-
agent_pools_service.remove_from_workspaces(
338+
mock_response = {
339+
"data": {
340+
"id": pool_id,
341+
"type": "agent-pools",
342+
"attributes": {
343+
"name": "test-pool",
344+
"created-at": "2023-01-01T00:00:00Z",
345+
"organization-scoped": True,
346+
"allowed-workspace-policy": "all-workspaces",
347+
"agent-count": 0,
348+
},
349+
}
350+
}
351+
mock_transport.request.return_value.json.return_value = mock_response
352+
353+
agent_pool = agent_pools_service.remove_from_workspaces(
321354
pool_id,
322355
AgentPoolRemoveFromWorkspacesOptions(workspace_ids=[ws_id]),
323356
)
324357

358+
assert agent_pool.id == pool_id
359+
assert agent_pool.name == "test-pool"
360+
325361
call_args = mock_transport.request.call_args
326362
# Must be PATCH, not DELETE
327363
assert call_args[0][0] == "PATCH"

0 commit comments

Comments
 (0)