-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
1412 lines (1215 loc) · 54.3 KB
/
Copy pathapi.py
File metadata and controls
1412 lines (1215 loc) · 54.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
create_workspace.py — User-friendly client to create SURF Research Cloud workspaces.
Configuration is read from environment variables (or a .env file).
See env.py for the full list of configurable variables.
Usage:
python create_workspace.py [--dry-run]
--dry-run Print the resolved configuration and the request payload
without actually creating the workspace.
"""
import sys
import json
import logging
import random
import string
import argparse
import asyncio
from datetime import datetime, timedelta, timezone
from pathlib import Path
from urllib.parse import urljoin, quote_plus
import aiohttp
import yaml
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Configuration (all values come from workspace.py / .env)
# ---------------------------------------------------------------------------
from env import (
RESEARCH_CLOUD_TOKEN,
CATALOG_BASE_URL,
USER_BASE_URL,
WALLET_BASE_URL,
WORKSPACE_BASE_URL,
CO_NAME,
WALLET_NAME,
CATALOG_ITEM_NAME,
CLOUD_NAME,
OS_FLAVOUR_NAME,
SIZE_FLAVOUR_NAME,
NETWORK_NAME,
HOST_NAME_BASE,
WORKSPACE_NAME,
WORKSPACE_DESCRIPTION,
WORKSPACE_END_TIME,
STORAGE_IDS,
NETWORK_IDS,
IP_IDS,
DATASET_NAMES,
DATASET_IDS,
)
_REQUIRED = {
"RESEARCH_CLOUD_TOKEN": RESEARCH_CLOUD_TOKEN,
"CO_NAME": CO_NAME,
"WALLET_NAME": WALLET_NAME,
"CATALOG_ITEM_NAME": CATALOG_ITEM_NAME,
"OS_FLAVOUR_NAME": OS_FLAVOUR_NAME,
"SIZE_FLAVOUR_NAME": SIZE_FLAVOUR_NAME,
"WORKSPACE_NAME": WORKSPACE_NAME,
}
HEADERS = {
"authorization": RESEARCH_CLOUD_TOKEN,
"accept": "application/json",
"content-type": "application/json",
}
DEFAULT_CLOUD_NAME = "SURF HPC Cloud"
DEFAULT_WORKSPACE_END_TIME_DAYS = 3
WORKSPACE_CREATE_TIMEOUT_SECONDS = 1800
WORKSPACE_CREATE_POLL_INTERVAL_SECONDS = 10
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _random_suffix(length: int = 5) -> str:
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))
def pretty(data) -> None:
print(json.dumps(data, indent=4))
def to_network_cloud_name(cloud_name: str) -> str:
"""Map a base cloud name (e.g. 'SURF HPC Cloud') to its network cloud name."""
normalized = cloud_name.strip()
if normalized.endswith(" Network"):
return normalized
return f"{normalized} Network"
def _normalize_parameter_map(raw_parameters: object, source_description: str) -> dict[str, str]:
if not isinstance(raw_parameters, dict):
raise ValueError(f"Optional parameters from {source_description} must be a JSON/YAML object with key/value pairs.")
normalized: dict[str, str] = {}
for raw_key, raw_value in raw_parameters.items():
if not isinstance(raw_key, str) or not raw_key.strip():
raise ValueError(f"Optional parameter keys from {source_description} must be non-empty strings.")
if not isinstance(raw_value, str):
raise ValueError(
f"Optional parameter {raw_key!r} from {source_description} must have a string value "
"(ResearchCloud optional parameters only support strings)."
)
normalized[raw_key] = raw_value
return normalized
def _parse_optional_parameters_file(file_path: str) -> dict[str, str]:
path = Path(file_path).expanduser()
if not path.exists():
raise ValueError(f"Optional parameter file not found: {path}")
if not path.is_file():
raise ValueError(f"Optional parameter path is not a file: {path}")
source = str(path)
suffix = path.suffix.lower()
with path.open("r", encoding="utf-8") as f:
content = f.read()
if suffix == ".json":
parsed = json.loads(content)
elif suffix in {".yaml", ".yml"}:
parsed = yaml.safe_load(content)
else:
raise ValueError(
f"Unsupported optional parameter file extension {suffix!r}. "
"Use .json, .yaml or .yml."
)
return _normalize_parameter_map(parsed, source)
def parse_optional_parameters(
cli_parameters: list[str] | None = None,
json_blob: str | None = None,
file_path: str | None = None,
) -> dict[str, str]:
"""Merge optional parameter inputs from file, JSON blob and key=value CLI flags."""
merged: dict[str, str] = {}
if file_path:
merged.update(_parse_optional_parameters_file(file_path))
if json_blob:
try:
parsed_json = json.loads(json_blob)
except json.JSONDecodeError as exc:
raise ValueError(f"Invalid JSON passed via --optional-parameters-json: {exc}") from exc
merged.update(_normalize_parameter_map(parsed_json, "--optional-parameters-json"))
for parameter in cli_parameters or []:
if "=" not in parameter:
raise ValueError(
f"Invalid --optional-parameter value {parameter!r}. "
"Expected format: key=value"
)
key, value = parameter.split("=", 1)
if not key.strip():
raise ValueError(f"Invalid --optional-parameter value {parameter!r}: key cannot be empty.")
merged[key] = value
return merged
def get_expected_optional_parameter_keys(offering: dict) -> set[str]:
expected = set()
for parameter in offering.get("overridable_parameters", []):
key = parameter.get("key")
if isinstance(key, str) and key:
expected.add(key)
return expected
async def make_request(
session: aiohttp.ClientSession,
method: str,
base_url: str,
path: str = "",
params=None,
data=None,
):
"""Make an async HTTP request and return (status_code, response_body)."""
url = urljoin(base_url, path)
logger.info("%-6s %s params=%s", method, url, params)
try:
async with session.request(method, url, params=params, json=data) as response:
content_type = response.headers.get("Content-Type", "")
body = await response.json() if "application/json" in content_type else await response.text()
if not response.ok:
error_body = body if isinstance(body, dict) else {"message": [body]}
logger.error(
"HTTP %s for %s — %s",
response.status,
url,
error_body.get("message", error_body),
)
return response.status, error_body
logger.info("Response: %s", response.status)
return response.status, body
except aiohttp.ClientError as exc:
logger.error("Request failed for %s — %s", url, exc)
return 500, {"message": [str(exc)]}
# ---------------------------------------------------------------------------
# Lookup helpers: resolve names → objects
# ---------------------------------------------------------------------------
async def get_wallets(session: aiohttp.ClientSession, name: str | None = None) -> list:
"""Return all wallets, or filter by name."""
_, wallets = await make_request(session, "GET", WALLET_BASE_URL, "wallets/")
if name:
return [w for w in wallets if w["name"] == name]
return wallets
async def get_user_cos(session: aiohttp.ClientSession, name: str | None = None) -> list:
"""Return all COs the authenticated user belongs to, or filter by co_name."""
_, user = await make_request(session, "GET", USER_BASE_URL, "users/self/")
cos = user.get("COs", [])
if name:
return [co for co in cos if co["co_name"] == name]
return cos
async def get_catalog_items_with_offerings(
session: aiohttp.ClientSession,
co_id: str,
products: list,
name: str | None = None,
application_type: str | None = "Compute",
) -> list:
"""Return catalog items that have valid offerings for the given CO and wallet products."""
params = {"co": co_id, "product": products}
if application_type:
params["type"] = application_type
_, response = await make_request(session, "GET", CATALOG_BASE_URL, "catalog_items/offerings/", params=params)
items = response.get("results", [])
if name:
return [ci for ci in items if ci["name"] == name]
return items
async def get_offerings_for_item(
session: aiohttp.ClientSession,
catalog_item_id: str,
co_id: str,
products: list,
) -> list:
"""Return all application offerings for a specific catalog item."""
params = {"co": co_id, "product": products}
path = f"catalog_items/{quote_plus(catalog_item_id)}/offerings/"
_, response = await make_request(session, "GET", CATALOG_BASE_URL, path, params=params)
return response.get("results", [])
async def get_workspaces(
session: aiohttp.ClientSession,
co_name: str,
catalog_item_name: str,
by_owner: bool = False,
application_type: str = "Compute",
workspace_name: str | None = None,
) -> list:
"""
Return existing (non-deleted) workspaces for a given CO and catalog item name.
Args:
session: Active aiohttp ClientSession.
co_name: Name of the Collaborative Organisation to filter by.
catalog_item_name: Application Offering name (e.g. "Ubuntu Desktop").
Matched against the workspace's meta.application_name.
by_owner: When True, return only workspaces owned by the
authenticated user.
application_type: Workspace type filter, e.g. Compute, Storage, IP, Network.
workspace_name: When given, only workspaces whose ``name`` matches
exactly are returned.
Returns:
List of workspace dicts, sorted newest-first by time_created.
"""
co_matches = await get_user_cos(session, co_name)
if not co_matches:
raise ValueError(f"No CO found with name: {co_name!r}")
co_id = co_matches[0]["id"]
params: dict = {
"co_id": co_id,
"application_type": application_type,
"deleted": "false",
"limit": 100,
}
if by_owner:
params["by_owner"] = "true"
workspaces: list = []
offset = 0
while True:
params["offset"] = offset
status, response = await make_request(session, "GET", WORKSPACE_BASE_URL, "workspaces/", params=params)
if status != 200:
raise RuntimeError(f"Failed to list workspaces (HTTP {status}): {response}")
page = response.get("results", [])
workspaces.extend(page)
if response.get("next") is None:
break
offset += len(page)
if catalog_item_name:
result = [
ws for ws in workspaces
if ws.get("meta", {}).get("application_name") == catalog_item_name
]
else:
result = workspaces
if workspace_name:
result = [ws for ws in result if ws.get("name") == workspace_name]
result.sort(key=lambda ws: ws.get("time_created", ""), reverse=True)
return result
async def get_networks(
session: aiohttp.ClientSession,
co_id: str,
cloud_name: str = DEFAULT_CLOUD_NAME,
by_owner: bool = False,
) -> list:
"""
Return existing (non-deleted) private network workspaces for a given CO.
A private network is itself a workspace with application_type "Network",
so this queries the same workspaces endpoint used for Compute/Storage/IP.
Args:
session: Active aiohttp ClientSession.
co_id: ID of the Collaborative Organisation to filter by.
cloud_name: Base cloud subscription name used to filter network clouds.
by_owner: When True, return only networks owned by the authenticated user.
Returns:
List of network workspace dicts, sorted newest-first by time_created.
"""
params: dict = {
"co_id": co_id,
"application_type": "Network",
"deleted": "false",
"limit": 100,
}
if by_owner:
params["by_owner"] = "true"
networks: list = []
offset = 0
while True:
params["offset"] = offset
status, response = await make_request(session, "GET", WORKSPACE_BASE_URL, "workspaces/", params=params)
if status != 200:
raise RuntimeError(f"Failed to list networks (HTTP {status}): {response}")
page = response.get("results", [])
networks.extend(page)
if response.get("next") is None:
break
offset += len(page)
network_cloud_name = to_network_cloud_name(cloud_name)
networks = [
network
for network in networks
if network.get("meta", {}).get("subscription_name") == network_cloud_name
]
networks.sort(key=lambda ws: ws.get("time_created", ""), reverse=True)
return networks
# ---------------------------------------------------------------------------
# Resolution: names → IDs / full objects
# ---------------------------------------------------------------------------
async def resolve_wallet(session: aiohttp.ClientSession, wallet_name: str) -> dict:
matches = await get_wallets(session, wallet_name)
if not matches:
raise ValueError(f"No wallet found with name: {wallet_name!r}")
if len(matches) > 1:
logger.warning("Multiple wallets match %r — using the first one.", wallet_name)
return matches[0]
async def resolve_co(session: aiohttp.ClientSession, co_name: str) -> dict:
matches = await get_user_cos(session, co_name)
if not matches:
raise ValueError(f"No CO found with name: {co_name!r}")
if len(matches) > 1:
logger.warning("Multiple COs match %r — using the first one.", co_name)
return matches[0]
async def resolve_catalog_item(
session: aiohttp.ClientSession,
catalog_item_name: str,
co_id: str,
products: list,
) -> dict:
matches = await get_catalog_items_with_offerings(session, co_id, products, catalog_item_name)
if not matches:
raise ValueError(
f"No catalog item (Application Offering) found with name: {catalog_item_name!r}. "
"Check CO_NAME, WALLET_NAME and CATALOG_ITEM_NAME."
)
if len(matches) > 1:
logger.warning("Multiple catalog items match %r — using the first one.", catalog_item_name)
return matches[0]
async def resolve_offering_and_flavours(
session: aiohttp.ClientSession,
catalog_item: dict,
co_id: str,
products: list,
cloud_name: str,
os_flavour_name: str,
size_flavour_name: str | None,
) -> tuple[dict, dict | None, dict]:
"""
Return (offering, size_flavour, os_flavour) for the given cloud/flavour names.
When *size_flavour_name* is ``None`` the size flavour is not resolved and
``None`` is returned in its place (useful when the caller will select a
size flavour via :func:`match_size_flavour`).
"""
offerings = await get_offerings_for_item(session, catalog_item["id"], co_id, products)
cloud_offerings = [o for o in offerings if o["subscription"]["name"] == cloud_name]
if not cloud_offerings:
available = [o["subscription"]["name"] for o in offerings]
raise ValueError(
f"No offering found for cloud {cloud_name!r}. "
f"Available cloud subscriptions: {available}"
)
offering = cloud_offerings[0]
flavours = offering.get("flavours", [])
os_flavours = [f for f in flavours if f["name"] == os_flavour_name]
if not os_flavours:
available_os = [f["name"] for f in flavours if f.get("category") == "os"]
raise ValueError(
f"OS flavour {os_flavour_name!r} not found. "
f"Available OS flavours: {available_os}"
)
if size_flavour_name is None:
return offering, None, os_flavours[0]
size_flavours = [f for f in flavours if f["name"] == size_flavour_name]
if not size_flavours:
available_sizes = [f["name"] for f in flavours if f.get("category") == "size"]
raise ValueError(
f"Size flavour {size_flavour_name!r} not found. "
f"Available size flavours: {available_sizes}"
)
return offering, size_flavours[0], os_flavours[0]
import re as _re
def _parse_size_flavour(name: str) -> dict:
"""
Parse a size flavour name into structured specs.
Recognises two broad naming conventions used by SURF ResearchCloud:
1. ``"GPU <cpu> Core - <mem> - <n>x <type>"`` e.g. "GPU 16 Core - 64 GB - 1x A10"
2. ``"<type> - <n> GPU"`` e.g. "A10 - 1 GPU"
3. ``"<n> Core - <mem> RAM"`` e.g. "4 Core - 32 GB RAM"
4. ``"GPU <cpu> Core - <mem>"`` (no explicit GPU count/type)
Returns a dict with keys:
``cpu`` – int or None
``gpu`` – int or None (number of *GPU cards*, not GPU-cores)
``gpu_type`` – str or None (e.g. "A10", "V100", "A100", "RTX2080")
"""
name = name.strip()
cpu = gpu = gpu_type = None
# Pattern 1 & 4: starts with "GPU <n> Core"
m = _re.match(r"GPU\s+(\d+)\s+Core", name, _re.IGNORECASE)
if m:
cpu = int(m.group(1))
# Explicit "<n>x <TYPE>" anywhere in the name (e.g. "1x A10", "4x V100")
m2 = _re.search(r"(\d+)x\s+([A-Za-z0-9]+)", name)
if m2:
gpu = int(m2.group(1))
gpu_type = m2.group(2).upper()
# Pattern 2: "<TYPE> - <n> GPU" (e.g. "A10 - 2 GPU")
if gpu is None:
m3 = _re.match(r"([A-Za-z0-9]+)\s*-\s*(\d+)\s+GPU", name, _re.IGNORECASE)
if m3:
gpu_type = m3.group(1).upper()
gpu = int(m3.group(2))
# Pattern 3: plain CPU-only "<n> Core"
if cpu is None:
m4 = _re.match(r"(\d+)\s+[Cc]ore", name)
if m4:
cpu = int(m4.group(1))
return {"cpu": cpu, "gpu": gpu, "gpu_type": gpu_type}
def match_size_flavour(
flavours: list[dict],
num_cpu: int | None = None,
num_gpu: int | None = None,
gpu_type: str | None = None,
) -> dict:
"""
Return the best-matching *size* flavour from *flavours* for the given
resource request.
Exactly one of *num_cpu* or *num_gpu* must be supplied.
Rules
-----
* Only flavours whose ``category`` is ``"size"`` are considered.
* When *gpu_type* is given, only flavours whose parsed GPU type matches
(case-insensitive) are eligible.
* The best match is the flavour with the largest value that is still
≤ the requested value ("round down"). If every candidate exceeds the
requested value the smallest available one is returned instead of
raising, and a warning is logged.
Raises
------
``ValueError`` if *flavours* contains no eligible size flavour.
``ValueError`` if neither *num_cpu* nor *num_gpu* is provided, or both
are provided.
"""
if (num_cpu is None) == (num_gpu is None):
raise ValueError("Exactly one of num_cpu or num_gpu must be provided.")
size_flavours = [f for f in flavours if f.get("category") == "size"]
if not size_flavours:
raise ValueError("No size flavours available in this offering.")
# Filter by gpu_type first (if requested)
if gpu_type is not None:
gpu_type_upper = gpu_type.upper()
typed = [
f for f in size_flavours
if (_parse_size_flavour(f["name"]).get("gpu_type") or "").upper() == gpu_type_upper
]
if not typed:
available_types = sorted({
_parse_size_flavour(f["name"]).get("gpu_type") or ""
for f in size_flavours
} - {""})
raise ValueError(
f"No size flavour found for GPU type {gpu_type!r}. "
f"Available GPU types: {available_types}"
)
size_flavours = typed
# Build (parsed_value, flavour) pairs
key = "cpu" if num_cpu is not None else "gpu"
requested = num_cpu if num_cpu is not None else num_gpu
candidates: list[tuple[int, dict]] = []
for f in size_flavours:
parsed = _parse_size_flavour(f["name"])
val = parsed.get(key)
if val is not None:
candidates.append((val, f))
if not candidates:
available_names = [f["name"] for f in size_flavours]
raise ValueError(
f"Could not parse {key!r} count from any size flavour. "
f"Available size flavours: {available_names}"
)
# Round-down: largest value ≤ requested
below = [(v, f) for v, f in candidates if v <= requested]
if below:
best_val, best = max(below, key=lambda x: x[0])
else:
# All candidates exceed the requested value — pick the smallest
best_val, best = min(candidates, key=lambda x: x[0])
logger.warning(
"No size flavour with %s ≤ %d found; using closest available: %r (%s=%d).",
key, requested, best["name"], key, best_val,
)
return best
async def resolve_network_and_offering(
session: aiohttp.ClientSession,
co_id: str,
products: list,
cloud_name: str,
network_name_hint: str | None = None,
) -> tuple[dict, dict]:
"""
Return (network, offering) for a private-network workspace offering.
The "network" here refers to the application/offer name the workspace API
exposes for a private network. In SURF ResearchCloud this is colloquially
treated as the network itself, so we keep the naming simple and human-facing.
If network_name_hint is not given, the first network offering available to
the CO/wallet is used. If no offering matches cloud_name, the first
available offering is used instead.
"""
networks = await get_catalog_items_with_offerings(
session, co_id, products, name=network_name_hint, application_type="Network"
)
if not networks:
raise ValueError(
"No network found"
+ (f" with name: {network_name_hint!r}." if network_name_hint else " for this CO/wallet.")
)
if len(networks) > 1:
logger.warning("Multiple network entries found — using the first one: %r", networks[0]["name"])
network = networks[0]
offerings = await get_offerings_for_item(session, network["id"], co_id, products)
if not offerings:
raise ValueError(f"No offerings found for network {network['name']!r}.")
network_cloud_name = to_network_cloud_name(cloud_name)
cloud_offerings = [o for o in offerings if o["subscription"]["name"] == network_cloud_name]
if not cloud_offerings:
available = [o["subscription"]["name"] for o in offerings]
logger.warning(
"No network offering found for cloud %r (available: %s) — using the first available offering instead.",
network_cloud_name, available,
)
cloud_offerings = offerings
return network, cloud_offerings[0]
# ---------------------------------------------------------------------------
# Build request payload
# ---------------------------------------------------------------------------
def build_create_payload(
co: dict,
wallet: dict,
catalog_item: dict,
offering: dict,
os_flavour: dict,
size_flavour: dict,
workspace_name: str,
workspace_description: str,
end_time: str,
host_name: str,
storage_ids: list[str | dict] | None = None,
network_ids: list[str | dict] | None = None,
ip_ids: list[str | dict] | None = None,
dataset_names: list | None = None,
dataset_ids: list | None = None,
optional_parameters: dict[str, str] | None = None,
) -> dict:
"""Assemble the full workspace creation payload."""
def _to_meta_ref(value: str | dict, default_type: str) -> dict[str, str]:
if isinstance(value, str):
if not value:
raise ValueError(f"Invalid empty {default_type} reference.")
return {"id": value, "name": value, "type": default_type}
if not isinstance(value, dict):
raise ValueError(f"Invalid {default_type} reference {value!r}; expected string id or object.")
resource_id = value.get("id")
if not isinstance(resource_id, str) or not resource_id:
raise ValueError(f"Invalid {default_type} reference {value!r}; missing non-empty 'id'.")
resource_name = value.get("name")
if not isinstance(resource_name, str) or not resource_name:
resource_name = resource_id
resource_type = value.get("type")
if not isinstance(resource_type, str) or not resource_type:
resource_type = default_type
return {"id": resource_id, "name": resource_name, "type": resource_type}
meta = {
"application_offering_id": offering["id"],
"application_name": offering["application"]["name"],
"application_icon": catalog_item.get("icon", ""),
"application_type": "Compute",
"subscription_tag": offering["subscription"]["tag"],
"subscription_name": offering["subscription"]["name"],
"subscription_group_id": offering["subscription"]["subscription_group"]["id"],
"co_name": co["co_name"],
"host_name": host_name,
"subscription_resource_type": "VM",
"flavours": [os_flavour, size_flavour],
"storages": [_to_meta_ref(storage, "storage") for storage in (storage_ids or [])],
"ips": [_to_meta_ref(ip, "ip") for ip in (ip_ids or [])],
"networks": [_to_meta_ref(network, "network") for network in (network_ids or [])],
"dataset_names": dataset_names or [],
"dataset_ids": dataset_ids or [],
"interactive_parameters": [{"key": key, "value": value} for key, value in (optional_parameters or {}).items()],
"wallet_name": wallet["name"],
"wallet_id": wallet["id"],
}
return {
"co_id": co["id"],
"wallet_id": wallet["id"],
"name": workspace_name,
"description": workspace_description,
"end_time": end_time,
"meta": meta,
}
def build_create_network_payload(
co: dict,
wallet: dict,
catalog_item: dict,
offering: dict,
network_name: str,
network_description: str = "",
) -> dict:
"""Assemble the request payload to create a private network workspace."""
meta = {
"application_offering_id": offering["id"],
"application_name": offering["application"]["name"],
"application_icon": catalog_item.get("icon", ""),
"application_type": "Network",
"subscription_tag": offering["subscription"]["tag"],
"subscription_name": offering["subscription"]["name"],
"subscription_group_id": offering["subscription"]["subscription_group"]["id"],
"co_name": co["co_name"],
"host_name": network_name,
"subscription_resource_type": "Private-Network",
"flavours": [],
"storages": [],
"ips": [],
"networks": [],
"dataset_names": [],
"dataset_ids": [],
"interactive_parameters": [],
"wallet_name": wallet["name"],
"wallet_id": wallet["id"],
}
return {
"co_id": co["id"],
"wallet_id": wallet["id"],
"name": network_name,
"description": network_description,
"meta": meta,
}
# ---------------------------------------------------------------------------
# Network creation
# ---------------------------------------------------------------------------
async def create_network(
session: aiohttp.ClientSession,
co: dict,
wallet: dict,
products: list,
cloud_name: str,
network_name: str,
network_name_hint: str | None = None,
network_description: str = "",
) -> str:
"""
Create a new private network workspace for the given CO/wallet.
Returns:
The id of the newly created network workspace.
"""
network, offering = await resolve_network_and_offering(
session, co["co_id"], products, cloud_name, network_name_hint
)
payload = build_create_network_payload(co, wallet, network, offering, network_name, network_description)
status, response = await make_request(session, "POST", WORKSPACE_BASE_URL, "workspaces/", data=payload)
if status != 201:
raise RuntimeError(f"Failed to create network {network_name!r} (HTTP {status}): {response}")
return response["id"]
async def wait_for_network(
session: aiohttp.ClientSession,
network_id: str,
timeout: float = 300,
poll_interval: float = 5,
) -> dict:
"""
Poll a network workspace by ID until it becomes available (or fails/times out).
Returns:
The final workspace dict once status is "available".
"""
failure_statuses = {"failed", "unhealthy", "deleted"}
elapsed = 0.0
while True:
status, response = await make_request(
session, "GET", WORKSPACE_BASE_URL, f"workspaces/{quote_plus(network_id)}/"
)
if status != 200:
raise RuntimeError(f"Failed to poll network {network_id} (HTTP {status}): {response}")
ws_status = response.get("status")
logger.info("Network %s status: %s", network_id, ws_status)
if ws_status == "available":
return response
if ws_status in failure_statuses:
raise RuntimeError(f"Network {network_id} entered failure status {ws_status!r}: {response}")
if elapsed >= timeout:
raise TimeoutError(f"Timed out after {timeout}s waiting for network {network_id} to become available.")
await asyncio.sleep(poll_interval)
elapsed += poll_interval
def _is_workspace_ready_status(status: str | None) -> bool:
return status in {"available", "running", "in-use", "paused", "full"}
def _is_workspace_failure_status(status: str | None) -> bool:
return status in {"failed", "unhealthy", "deleted", "deleting", "unknown", "unaccounted"}
async def wait_for_workspace(
session: aiohttp.ClientSession,
workspace_id: str,
timeout: float = WORKSPACE_CREATE_TIMEOUT_SECONDS,
poll_interval: float = WORKSPACE_CREATE_POLL_INTERVAL_SECONDS,
) -> dict:
"""
Poll a workspace by ID until creation reaches a terminal success/failure state.
Returns:
The final workspace dict once it reaches a ready state.
"""
elapsed = 0.0
last_status: str | None = None
while True:
status_code, response = await make_request(
session, "GET", WORKSPACE_BASE_URL, f"workspaces/{quote_plus(workspace_id)}/"
)
if status_code != 200:
raise RuntimeError(f"Failed to poll workspace {workspace_id} (HTTP {status_code}): {response}")
ws_status = response.get("status")
if ws_status != last_status:
human_status = str(ws_status).replace("-", " ")
print(f" Workspace status: {human_status} ({int(elapsed)}s elapsed)")
last_status = ws_status
if _is_workspace_ready_status(ws_status):
return response
if _is_workspace_failure_status(ws_status):
raise RuntimeError(f"Workspace {workspace_id} entered failure status {ws_status!r}: {response}")
if elapsed >= timeout:
raise TimeoutError(f"Timed out after {int(timeout)}s waiting for workspace {workspace_id} to become ready.")
await asyncio.sleep(poll_interval)
elapsed += poll_interval
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def validate_config(required: list[str] | None = None):
keys = required or list(_REQUIRED.keys())
missing = [k for k in keys if not _REQUIRED.get(k)]
if missing:
print("ERROR: The following required variables are not set:")
for name in missing:
print(f" {name}")
print("\nSet them in your .env file or as environment variables.")
sys.exit(1)
def validate_workspace_end_time(end_time: str) -> None:
"""Ensure workspace end_time is ISO-8601 and in the future."""
normalized = end_time.strip()
if normalized.endswith("Z"):
normalized = f"{normalized[:-1]}+00:00"
try:
parsed = datetime.fromisoformat(normalized)
except ValueError as exc:
raise ValueError(
"WORKSPACE_END_TIME must be a valid ISO 8601 datetime "
"(for example 2026-12-31T23:59:59Z)."
) from exc
if parsed.tzinfo is None:
raise ValueError("WORKSPACE_END_TIME must include a timezone (use trailing 'Z' for UTC).")
now_utc = datetime.now(timezone.utc)
if parsed <= now_utc:
raise ValueError(
f"WORKSPACE_END_TIME must be in the future. Current UTC time is "
f"{now_utc.isoformat().replace('+00:00', 'Z')}."
)
def resolve_workspace_end_time(end_time: str | None) -> str:
"""Return provided end time, or default to 3 days in the future (UTC)."""
if end_time and end_time.strip():
return end_time.strip()
return (datetime.now(timezone.utc) + timedelta(days=DEFAULT_WORKSPACE_END_TIME_DAYS)).strftime(
"%Y-%m-%dT%H:%M:%SZ"
)
async def list_networks_for_co(
co_name: str | None = None,
cloud_name: str = DEFAULT_CLOUD_NAME,
by_owner: bool = False,
dry_run: bool = False,
):
async with aiohttp.ClientSession(headers=HEADERS) as session:
co = await resolve_co(session, co_name or CO_NAME)
if dry_run:
print(f"Dry run: would list private networks for CO {co['co_name']!r} (id: {co['id']})")
print(f" params: {{'co_id': {co['id']}, 'application_type': 'Network', 'deleted': 'false', 'by_owner': {str(by_owner).lower()}}}")
print(f" cloud filter: {cloud_name!r}")
print(f" network cloud filter: {to_network_cloud_name(cloud_name)!r}")
return
networks = await get_networks(session, co["id"], cloud_name=cloud_name, by_owner=by_owner)
pretty(networks)
async def create_network_for_co(
co_name: str | None = None,
wallet_name: str | None = None,
cloud_name: str | None = None,
network_name: str | None = None,
dry_run: bool = False,
):
async with aiohttp.ClientSession(headers=HEADERS) as session:
co, wallet = await asyncio.gather(
resolve_co(session, co_name or CO_NAME),
resolve_wallet(session, wallet_name or WALLET_NAME),
)
if dry_run:
print(f"Dry run: would create private network in CO {co['co_name']!r} using wallet {wallet['name']!r}")
print(f" network_name: {network_name or f'{HOST_NAME_BASE}-network-{_random_suffix()}'}")
print(f" cloud_name: {cloud_name or CLOUD_NAME}")
print(f" network_cloud_name: {to_network_cloud_name(cloud_name or CLOUD_NAME)}")
return
net_name = network_name or f"{HOST_NAME_BASE}-network-{_random_suffix()}"
products = wallet["budgets"][0]["products"]
network_id = await create_network(
session,
co,
wallet,
products,
cloud_name or CLOUD_NAME,
net_name,
network_name_hint=NETWORK_NAME,
)
print(f"Created private network {net_name!r} (id: {network_id})")
network = await wait_for_network(session, network_id)
print(f"Private network available: {network['id']}")
pretty(network)
async def list_workspaces_for_co(
co_name: str | None = None,
cloud_name: str = DEFAULT_CLOUD_NAME,