Skip to content

Commit 0087ef2

Browse files
committed
Make CLI use SDK
1 parent f78b916 commit 0087ef2

16 files changed

Lines changed: 2077 additions & 1188 deletions

File tree

src/seqera/commands/computeenvs/__init__.py

Lines changed: 39 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99

1010
import typer
1111

12-
from seqera.api.client import SeqeraClient
1312
from seqera.exceptions import (
1413
AuthenticationError,
1514
ComputeEnvNotFoundException,
1615
NotFoundError,
1716
SeqeraError,
1817
)
19-
from seqera.main import get_client, get_output_format
18+
from seqera.main import get_sdk, get_output_format
2019
from seqera.responses.computeenvs import (
2120
ComputeEnvAdded,
2221
ComputeEnvDeleted,
@@ -159,11 +158,11 @@ def delete_compute_env(
159158
) -> None:
160159
"""Delete a compute environment."""
161160
try:
162-
client = get_client()
161+
sdk = get_sdk()
163162
output_format = get_output_format()
164163

165-
# Delete compute environment
166-
client.delete(f"/compute-envs/{compute_env_id}")
164+
# Delete compute environment using SDK
165+
sdk.compute_envs.delete(compute_env_id)
167166

168167
# Output response
169168
result = ComputeEnvDeleted(
@@ -184,12 +183,14 @@ def delete_compute_env(
184183
def list_compute_envs() -> None:
185184
"""List all compute environments in the workspace."""
186185
try:
187-
client = get_client()
186+
sdk = get_sdk()
188187
output_format = get_output_format()
189188

190-
# Get compute environments list
191-
response = client.get("/compute-envs")
192-
compute_envs = response.get("computeEnvs", [])
189+
# Get compute environments using SDK
190+
envs = list(sdk.compute_envs.list())
191+
192+
# Convert to dicts for response formatting (mode='json' to serialize datetimes)
193+
compute_envs = [ce.model_dump(by_alias=True, mode="json") for ce in envs]
193194

194195
# Output response
195196
result = ComputeEnvList(
@@ -217,27 +218,19 @@ def view_compute_env(
217218
) -> None:
218219
"""View compute environment details."""
219220
try:
220-
client = get_client()
221+
sdk = get_sdk()
221222
output_format = get_output_format()
222223

223-
# Resolve compute environment ID if name is provided
224-
if compute_env_name and not compute_env_id:
225-
# Get list and find by name
226-
response = client.get("/compute-envs")
227-
compute_envs = response.get("computeEnvs", [])
228-
for ce in compute_envs:
229-
if ce.get("name") == compute_env_name:
230-
compute_env_id = ce.get("id")
231-
break
232-
if not compute_env_id:
233-
raise ComputeEnvNotFoundException(compute_env_name, USER_WORKSPACE_NAME)
234-
235-
if not compute_env_id:
224+
if not compute_env_id and not compute_env_name:
236225
output_error("Either --id or --name must be provided")
237226
sys.exit(1)
238227

239-
# Get compute environment details
240-
compute_env = client.get(f"/compute-envs/{compute_env_id}")
228+
# Get compute environment using SDK (supports both ID and name)
229+
ce_ref = compute_env_id or compute_env_name
230+
ce = sdk.compute_envs.get(ce_ref)
231+
232+
# Convert to dict for response formatting (mode='json' to serialize datetimes)
233+
compute_env = {"computeEnv": ce.model_dump(by_alias=True, mode="json")}
241234

242235
# Output response
243236
result = ComputeEnvView(
@@ -248,30 +241,18 @@ def view_compute_env(
248241
output_response(result, output_format)
249242

250243
except Exception as e:
251-
# Handle 403 as not found
252-
if hasattr(e, "status_code") and e.status_code == 403:
253-
ref = compute_env_id or compute_env_name
254-
handle_compute_env_error(ComputeEnvNotFoundException(ref, USER_WORKSPACE_NAME))
255244
handle_compute_env_error(e)
256245

257246

258247
@primary_app.command("get")
259248
def primary_get() -> None:
260249
"""Get the primary compute environment."""
261250
try:
262-
client = get_client()
251+
sdk = get_sdk()
263252
output_format = get_output_format()
264253

265-
# Get compute environments list to find primary
266-
response = client.get("/compute-envs")
267-
compute_envs = response.get("computeEnvs", [])
268-
269-
# Find the primary compute environment
270-
primary_ce = None
271-
for ce in compute_envs:
272-
if ce.get("primary"):
273-
primary_ce = ce
274-
break
254+
# Get primary compute environment using SDK
255+
primary_ce = sdk.compute_envs.get_primary()
275256

276257
if not primary_ce:
277258
# No primary set
@@ -281,12 +262,9 @@ def primary_get() -> None:
281262
workspace=USER_WORKSPACE_NAME,
282263
)
283264
else:
284-
# Get full details of primary compute environment
285-
ce_id = primary_ce.get("id")
286-
ce_details = client.get(f"/compute-envs/{ce_id}")
287265
result = ComputeEnvsPrimaryGet(
288-
compute_env_id=ce_id,
289-
name=ce_details.get("name"),
266+
compute_env_id=primary_ce.id,
267+
name=primary_ce.name,
290268
workspace=USER_WORKSPACE_NAME,
291269
)
292270

@@ -309,35 +287,24 @@ def primary_set(
309287
) -> None:
310288
"""Set a compute environment as primary."""
311289
try:
312-
client = get_client()
290+
sdk = get_sdk()
313291
output_format = get_output_format()
314292

315-
# Resolve compute environment ID if name is provided
316-
if compute_env_name and not compute_env_id:
317-
# Get list and find by name
318-
response = client.get("/compute-envs")
319-
compute_envs = response.get("computeEnvs", [])
320-
for ce in compute_envs:
321-
if ce.get("name") == compute_env_name:
322-
compute_env_id = ce.get("id")
323-
break
324-
if not compute_env_id:
325-
raise ComputeEnvNotFoundException(compute_env_name, USER_WORKSPACE_NAME)
326-
327-
if not compute_env_id:
293+
if not compute_env_id and not compute_env_name:
328294
output_error("Either --id or --name must be provided")
329295
sys.exit(1)
330296

331-
# Get compute environment details first
332-
ce_details = client.get(f"/compute-envs/{compute_env_id}")
297+
# Get compute environment using SDK (for name resolution and validation)
298+
ce_ref = compute_env_id or compute_env_name
299+
ce = sdk.compute_envs.get(ce_ref)
333300

334-
# Set as primary
335-
client.post(f"/compute-envs/{compute_env_id}/primary", json={})
301+
# Set as primary using SDK
302+
sdk.compute_envs.set_primary(ce.id)
336303

337304
# Output response
338305
result = ComputeEnvsPrimarySet(
339-
compute_env_id=compute_env_id,
340-
name=ce_details.get("name"),
306+
compute_env_id=ce.id,
307+
name=ce.name,
341308
workspace=USER_WORKSPACE_NAME,
342309
)
343310

@@ -360,42 +327,24 @@ def export_compute_env(
360327
) -> None:
361328
"""Export compute environment configuration to JSON."""
362329
try:
363-
client = get_client()
330+
sdk = get_sdk()
364331
output_format = get_output_format()
365332

366-
# Get list and find by name
367-
response = client.get("/compute-envs")
368-
compute_envs = response.get("computeEnvs", [])
369-
370-
compute_env_id = None
371-
for ce in compute_envs:
372-
if ce.get("name") == compute_env_name:
373-
compute_env_id = ce.get("id")
374-
break
375-
376-
if not compute_env_id:
377-
raise ComputeEnvNotFoundException(compute_env_name, USER_WORKSPACE_NAME)
378-
379-
# Get full compute environment details
380-
ce_details = client.get(f"/compute-envs/{compute_env_id}")
381-
382-
# Extract config for export
383-
config = ce_details.get("config", {})
333+
# Export config using SDK
334+
export_data = sdk.compute_envs.export_config(compute_env_name)
384335

385-
# Create export format (config only for simplified version)
386-
export_data = {
387-
"config": config,
388-
}
336+
# Wrap config for export format
337+
export_config = {"config": export_data.get("config", {})}
389338

390339
# Save to file if specified
391340
if filename and filename != "-":
392341
import json
393342

394343
with open(filename, "w") as f:
395-
json.dump(export_data, f, indent=2)
344+
json.dump(export_config, f, indent=2)
396345

397346
# Output response
398-
result = ComputeEnvExport(config=export_data)
347+
result = ComputeEnvExport(config=export_config)
399348
output_response(result, output_format)
400349

401350
except Exception as e:

0 commit comments

Comments
 (0)