-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsandbox.py
More file actions
907 lines (780 loc) · 28.6 KB
/
sandbox.py
File metadata and controls
907 lines (780 loc) · 28.6 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
"""
This file is auto generated by the code generation script.
Do not modify this file manually.
Use the `make codegen` command to regenerate.
当前文件为自动生成的控制 API 客户端代码。请勿手动修改此文件。
使用 `make codegen` 命令重新生成。
source: agentrun/sandbox/__sandbox_async_template.py
Sandbox 高层 API / Sandbox High-Level API
此模块定义沙箱资源的高级API。
This module defines the high-level API for sandbox resources.
"""
from typing import (
Any,
Dict,
List,
Literal,
Optional,
overload,
TYPE_CHECKING,
Union,
)
from pydantic import Field
from agentrun.sandbox.model import TemplateType
from agentrun.utils.config import Config
from agentrun.utils.model import BaseModel
if TYPE_CHECKING:
from agentrun.sandbox.aio_sandbox import AioSandbox
from agentrun.sandbox.browser_sandbox import BrowserSandbox
from agentrun.sandbox.code_interpreter_sandbox import CodeInterpreterSandbox
from agentrun.sandbox.custom_sandbox import CustomSandbox
from agentrun.sandbox.model import (
ListSandboxesInput,
ListSandboxesOutput,
NASConfig,
OSSMountConfig,
PageableInput,
PolarFsConfig,
TemplateInput,
)
from agentrun.sandbox.template import Template
class Sandbox(BaseModel):
"""Sandbox 实例
封装了 Sandbox 的基本信息和操作方法
"""
_template_type: Optional[TemplateType]
created_at: Optional[str] = None
"""沙箱创建时间 / Sandbox creation time"""
ended_at: Optional[str] = None
"""沙箱结束时间 / Sandbox end time"""
last_updated_at: Optional[str] = None
"""最后更新时间 / Last updated time"""
metadata: Optional[Dict[str, Any]] = None
"""元数据 / Metadata"""
sandbox_arn: Optional[str] = None
"""沙箱全局唯一资源名称 / Sandbox ARN"""
sandbox_id: Optional[str] = None
"""沙箱 ID / Sandbox ID"""
sandbox_idle_ttlin_seconds: Optional[int] = Field(
None, alias="sandboxIdleTTLInSeconds"
)
"""沙箱空闲 TTL(秒) / Sandbox Idle TTL (seconds)"""
sandbox_idle_timeout_seconds: Optional[int] = None
"""沙箱空闲超时时间(秒) / Sandbox Idle Timeout (seconds)"""
status: Optional[str] = None
"""沙箱状态 / Sandbox status"""
template_id: Optional[str] = None
"""模板 ID / Template ID"""
template_name: Optional[str] = None
"""模板名称 / Template name"""
_config: Optional[Config] = None
"""配置对象,用于子类的 data_api 初始化 / Config object for data_api initialization"""
@classmethod
def __get_client(cls):
"""获取 Sandbox 客户端"""
from .client import SandboxClient
return SandboxClient()
@classmethod
@overload
async def create_async(
cls,
template_type: Literal[TemplateType.CODE_INTERPRETER],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "CodeInterpreterSandbox":
...
@classmethod
@overload
def create(
cls,
template_type: Literal[TemplateType.CODE_INTERPRETER],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "CodeInterpreterSandbox":
...
@classmethod
@overload
async def create_async(
cls,
template_type: Literal[TemplateType.BROWSER],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "BrowserSandbox":
...
@classmethod
@overload
def create(
cls,
template_type: Literal[TemplateType.BROWSER],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "BrowserSandbox":
...
@classmethod
@overload
async def create_async(
cls,
template_type: Literal[TemplateType.AIO],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "AioSandbox":
...
@classmethod
@overload
def create(
cls,
template_type: Literal[TemplateType.AIO],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "AioSandbox":
...
@classmethod
@overload
async def create_async(
cls,
template_type: Literal[TemplateType.CUSTOM],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "CustomSandbox":
...
@classmethod
@overload
def create(
cls,
template_type: Literal[TemplateType.CUSTOM],
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> "CustomSandbox":
...
@classmethod
async def create_async(
cls,
template_type: TemplateType,
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> Union[
"CodeInterpreterSandbox",
"BrowserSandbox",
"AioSandbox",
"CustomSandbox",
]:
if template_name is None:
# todo 可以考虑为用户创建一个模板?
raise ValueError("template_name is required")
# 先根据传入的template_name,获取template的类型
template = await cls.get_template_async(template_name, config=config)
# 根据 template 类型创建相应的 Sandbox 子类
from agentrun.sandbox.aio_sandbox import AioSandbox
from agentrun.sandbox.browser_sandbox import BrowserSandbox
from agentrun.sandbox.code_interpreter_sandbox import (
CodeInterpreterSandbox,
)
from agentrun.sandbox.custom_sandbox import CustomSandbox
if template_type != template.template_type:
raise ValueError(
f"template_type of {template_name} is {template.template_type},"
f" not {template_type}"
)
# 创建 Sandbox(返回基类实例)
base_sandbox = await cls.__get_client().create_sandbox_async(
template_name=template_name,
sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
sandbox_id=sandbox_id,
nas_config=nas_config,
oss_mount_config=oss_mount_config,
polar_fs_config=polar_fs_config,
config=config,
)
# 根据 template 类型转换为对应的子类实例
sandbox = None
if template.template_type == TemplateType.CODE_INTERPRETER:
sandbox = CodeInterpreterSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
elif template.template_type == TemplateType.BROWSER:
sandbox = BrowserSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
elif template.template_type == TemplateType.AIO:
sandbox = AioSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
elif template.template_type == TemplateType.CUSTOM:
sandbox = CustomSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
else:
raise ValueError(
f"template_type {template.template_type} is not supported"
)
sandbox._config = config
return sandbox
@classmethod
def create(
cls,
template_type: TemplateType,
template_name: Optional[str] = None,
sandbox_idle_timeout_seconds: Optional[int] = 600,
sandbox_id: Optional[str] = None,
nas_config: Optional["NASConfig"] = None,
oss_mount_config: Optional["OSSMountConfig"] = None,
polar_fs_config: Optional["PolarFsConfig"] = None,
config: Optional[Config] = None,
) -> Union[
"CodeInterpreterSandbox",
"BrowserSandbox",
"AioSandbox",
"CustomSandbox",
]:
if template_name is None:
# todo 可以考虑为用户创建一个模板?
raise ValueError("template_name is required")
# 先根据传入的template_name,获取template的类型
template = cls.get_template(template_name, config=config)
# 根据 template 类型创建相应的 Sandbox 子类
from agentrun.sandbox.aio_sandbox import AioSandbox
from agentrun.sandbox.browser_sandbox import BrowserSandbox
from agentrun.sandbox.code_interpreter_sandbox import (
CodeInterpreterSandbox,
)
from agentrun.sandbox.custom_sandbox import CustomSandbox
if template_type != template.template_type:
raise ValueError(
f"template_type of {template_name} is {template.template_type},"
f" not {template_type}"
)
# 创建 Sandbox(返回基类实例)
base_sandbox = cls.__get_client().create_sandbox(
template_name=template_name,
sandbox_idle_timeout_seconds=sandbox_idle_timeout_seconds,
sandbox_id=sandbox_id,
nas_config=nas_config,
oss_mount_config=oss_mount_config,
polar_fs_config=polar_fs_config,
config=config,
)
# 根据 template 类型转换为对应的子类实例
sandbox = None
if template.template_type == TemplateType.CODE_INTERPRETER:
sandbox = CodeInterpreterSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
elif template.template_type == TemplateType.BROWSER:
sandbox = BrowserSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
elif template.template_type == TemplateType.AIO:
sandbox = AioSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
elif template.template_type == TemplateType.CUSTOM:
sandbox = CustomSandbox.model_validate(
base_sandbox.model_dump(by_alias=False)
)
else:
raise ValueError(
f"template_type {template.template_type} is not supported"
)
sandbox._config = config
return sandbox
@classmethod
async def stop_by_id_async(
cls, sandbox_id: str, config: Optional[Config] = None
):
"""通过 ID 停止 Sandbox(异步)
Args:
sandbox_id: Sandbox ID
config: 配置对象 / Config object
Returns:
Sandbox: Sandbox 对象
"""
if sandbox_id is None:
raise ValueError("sandbox_id is required")
return await cls.__get_client().stop_sandbox_async(
sandbox_id, config=config
)
@classmethod
def stop_by_id(cls, sandbox_id: str, config: Optional[Config] = None):
"""通过 ID 停止 Sandbox(同步)
Args:
sandbox_id: Sandbox ID
config: 配置对象 / Config object
Returns:
Sandbox: Sandbox 对象
"""
if sandbox_id is None:
raise ValueError("sandbox_id is required")
return cls.__get_client().stop_sandbox(sandbox_id, config=config)
@classmethod
async def delete_by_id_async(
cls, sandbox_id: str, config: Optional[Config] = None
):
"""通过 ID 删除 Sandbox(异步)
Args:
sandbox_id: Sandbox ID
config: 配置对象 / Config object
Returns:
Sandbox: Sandbox 对象
"""
if sandbox_id is None:
raise ValueError("sandbox_id is required")
return await cls.__get_client().delete_sandbox_async(
sandbox_id, config=config
)
@classmethod
def delete_by_id(cls, sandbox_id: str, config: Optional[Config] = None):
"""通过 ID 删除 Sandbox(同步)
Args:
sandbox_id: Sandbox ID
config: 配置对象 / Config object
Returns:
Sandbox: Sandbox 对象
"""
if sandbox_id is None:
raise ValueError("sandbox_id is required")
return cls.__get_client().delete_sandbox(sandbox_id, config=config)
@classmethod
async def list_async(
cls,
input: Optional["ListSandboxesInput"] = None,
config: Optional[Config] = None,
) -> "ListSandboxesOutput":
"""列出 Sandboxes(异步)
Args:
input: 列表查询配置
config: 配置对象
Returns:
ListSandboxesOutput: Sandbox 列表结果
"""
return await cls.__get_client().list_sandboxes_async(input, config)
@classmethod
def list(
cls,
input: Optional["ListSandboxesInput"] = None,
config: Optional[Config] = None,
) -> "ListSandboxesOutput":
"""列出 Sandboxes(同步)
Args:
input: 列表查询配置
config: 配置对象
Returns:
ListSandboxesOutput: Sandbox 列表结果
"""
return cls.__get_client().list_sandboxes(input, config)
@classmethod
@overload
async def connect_async(
cls,
sandbox_id: str,
template_type: Literal[TemplateType.CODE_INTERPRETER],
config: Optional[Config] = None,
) -> "CodeInterpreterSandbox":
...
@classmethod
@overload
def connect(
cls,
sandbox_id: str,
template_type: Literal[TemplateType.CODE_INTERPRETER],
config: Optional[Config] = None,
) -> "CodeInterpreterSandbox":
...
@classmethod
@overload
async def connect_async(
cls,
sandbox_id: str,
template_type: Literal[TemplateType.BROWSER],
config: Optional[Config] = None,
) -> "BrowserSandbox":
...
@classmethod
@overload
def connect(
cls,
sandbox_id: str,
template_type: Literal[TemplateType.BROWSER],
config: Optional[Config] = None,
) -> "BrowserSandbox":
...
@classmethod
@overload
async def connect_async(
cls,
sandbox_id: str,
template_type: Literal[TemplateType.AIO],
config: Optional[Config] = None,
) -> "AioSandbox":
...
@classmethod
@overload
def connect(
cls,
sandbox_id: str,
template_type: Literal[TemplateType.AIO],
config: Optional[Config] = None,
) -> "AioSandbox":
...
@classmethod
@overload
async def connect_async(
cls,
sandbox_id: str,
template_type: None = None,
config: Optional[Config] = None,
) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
...
@classmethod
@overload
def connect(
cls,
sandbox_id: str,
template_type: None = None,
config: Optional[Config] = None,
) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
...
@classmethod
async def connect_async(
cls,
sandbox_id: str,
template_type: Optional[TemplateType] = None,
config: Optional[Config] = None,
) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
"""连接一个SandBox(异步)
Args:
sandbox_id: Sandbox ID
template_type: 可选的类型参数,用于类型提示和运行时类型决定。
提供时直接使用该类型决定返回的子类,不调用 get_template(无需 AKSK)。
未提供时通过 get_template 获取类型(需要 AKSK)。
config: 配置对象
Returns:
Sandbox: 根据模板类型返回对应的 Sandbox 子类对象
Raises:
ValueError: 如果模板类型不支持或与预期类型不匹配
"""
if sandbox_id is None:
raise ValueError("sandbox_id is required")
# 先获取 sandbox 信息
sandbox = await cls.__get_client().get_sandbox_async(
sandbox_id, config=config
)
resolved_type = template_type
if resolved_type is None:
if sandbox.template_name is None:
raise ValueError(f"Sandbox {sandbox_id} has no template_name")
template = await cls.get_template_async(
sandbox.template_name, config=config
)
resolved_type = template.template_type
from agentrun.sandbox.aio_sandbox import AioSandbox
from agentrun.sandbox.browser_sandbox import BrowserSandbox
from agentrun.sandbox.code_interpreter_sandbox import (
CodeInterpreterSandbox,
)
result = None
if resolved_type == TemplateType.CODE_INTERPRETER:
result = CodeInterpreterSandbox.model_validate(
sandbox.model_dump(by_alias=False)
)
elif resolved_type == TemplateType.BROWSER:
result = BrowserSandbox.model_validate(
sandbox.model_dump(by_alias=False)
)
elif resolved_type == TemplateType.AIO:
result = AioSandbox.model_validate(
sandbox.model_dump(by_alias=False)
)
else:
raise ValueError(
f"Unsupported template type: {resolved_type}. "
"Expected 'code-interpreter', 'browser' or 'aio'"
)
result._config = config
return result
# ==================== Template 相关类方法 ====================
@classmethod
def connect(
cls,
sandbox_id: str,
template_type: Optional[TemplateType] = None,
config: Optional[Config] = None,
) -> Union["CodeInterpreterSandbox", "BrowserSandbox", "AioSandbox"]:
"""连接一个SandBox(同步)
Args:
sandbox_id: Sandbox ID
template_type: 可选的类型参数,用于类型提示和运行时类型决定。
提供时直接使用该类型决定返回的子类,不调用 get_template(无需 AKSK)。
未提供时通过 get_template 获取类型(需要 AKSK)。
config: 配置对象
Returns:
Sandbox: 根据模板类型返回对应的 Sandbox 子类对象
Raises:
ValueError: 如果模板类型不支持或与预期类型不匹配
"""
if sandbox_id is None:
raise ValueError("sandbox_id is required")
# 先获取 sandbox 信息
sandbox = cls.__get_client().get_sandbox(sandbox_id, config=config)
resolved_type = template_type
if resolved_type is None:
if sandbox.template_name is None:
raise ValueError(f"Sandbox {sandbox_id} has no template_name")
template = cls.get_template(sandbox.template_name, config=config)
resolved_type = template.template_type
from agentrun.sandbox.aio_sandbox import AioSandbox
from agentrun.sandbox.browser_sandbox import BrowserSandbox
from agentrun.sandbox.code_interpreter_sandbox import (
CodeInterpreterSandbox,
)
result = None
if resolved_type == TemplateType.CODE_INTERPRETER:
result = CodeInterpreterSandbox.model_validate(
sandbox.model_dump(by_alias=False)
)
elif resolved_type == TemplateType.BROWSER:
result = BrowserSandbox.model_validate(
sandbox.model_dump(by_alias=False)
)
elif resolved_type == TemplateType.AIO:
result = AioSandbox.model_validate(
sandbox.model_dump(by_alias=False)
)
else:
raise ValueError(
f"Unsupported template type: {resolved_type}. "
"Expected 'code-interpreter', 'browser' or 'aio'"
)
result._config = config
return result
# ==================== Template 相关类方法 ====================
@classmethod
async def create_template_async(
cls, input: "TemplateInput", config: Optional[Config] = None
) -> "Template":
"""创建 Template(异步)
Args:
input: Template 配置
config: 配置对象
Returns:
Template: 创建的 Template 对象
"""
if input.template_type is None:
raise ValueError("template_type is required")
return await cls.__get_client().create_template_async(
input, config=config
)
@classmethod
def create_template(
cls, input: "TemplateInput", config: Optional[Config] = None
) -> "Template":
"""创建 Template(同步)
Args:
input: Template 配置
config: 配置对象
Returns:
Template: 创建的 Template 对象
"""
if input.template_type is None:
raise ValueError("template_type is required")
return cls.__get_client().create_template(input, config=config)
@classmethod
async def get_template_async(
cls, template_name: str, config: Optional[Config] = None
) -> "Template":
"""获取 Template(异步)
Args:
template_name: Template 名称
config: 配置对象
Returns:
Template: Template 对象
"""
if template_name is None:
raise ValueError("template_name is required")
return await cls.__get_client().get_template_async(
template_name, config=config
)
@classmethod
def get_template(
cls, template_name: str, config: Optional[Config] = None
) -> "Template":
"""获取 Template(同步)
Args:
template_name: Template 名称
config: 配置对象
Returns:
Template: Template 对象
"""
if template_name is None:
raise ValueError("template_name is required")
return cls.__get_client().get_template(template_name, config=config)
@classmethod
async def update_template_async(
cls,
template_name: str,
input: "TemplateInput",
config: Optional[Config] = None,
) -> "Template":
"""更新 Template(异步)
Args:
template_name: Template 名称
input: Template 更新配置
config: 配置对象
Returns:
Template: 更新后的 Template 对象
"""
if template_name is None:
raise ValueError("template_name is required")
return await cls.__get_client().update_template_async(
template_name, input, config=config
)
@classmethod
def update_template(
cls,
template_name: str,
input: "TemplateInput",
config: Optional[Config] = None,
) -> "Template":
"""更新 Template(同步)
Args:
template_name: Template 名称
input: Template 更新配置
config: 配置对象
Returns:
Template: 更新后的 Template 对象
"""
if template_name is None:
raise ValueError("template_name is required")
return cls.__get_client().update_template(
template_name, input, config=config
)
@classmethod
async def delete_template_async(
cls, template_name: str, config: Optional[Config] = None
) -> "Template":
"""删除 Template(异步)
Args:
template_name: Template 名称
config: 配置对象
Returns:
Template: 删除的 Template 对象
"""
if template_name is None:
raise ValueError("template_name is required")
return await cls.__get_client().delete_template_async(
template_name, config=config
)
@classmethod
def delete_template(
cls, template_name: str, config: Optional[Config] = None
) -> "Template":
"""删除 Template(同步)
Args:
template_name: Template 名称
config: 配置对象
Returns:
Template: 删除的 Template 对象
"""
if template_name is None:
raise ValueError("template_name is required")
return cls.__get_client().delete_template(template_name, config=config)
@classmethod
async def list_templates_async(
cls,
input: Optional["PageableInput"] = None,
config: Optional[Config] = None,
) -> List["Template"]:
"""列出 Templates(异步)
Args:
input: 分页配置
config: 配置对象
Returns:
List[Template]: Template 列表
"""
return await cls.__get_client().list_templates_async(
input, config=config
)
@classmethod
def list_templates(
cls,
input: Optional["PageableInput"] = None,
config: Optional[Config] = None,
) -> List["Template"]:
"""列出 Templates(同步)
Args:
input: 分页配置
config: 配置对象
Returns:
List[Template]: Template 列表
"""
return cls.__get_client().list_templates(input, config=config)
async def get_async(self):
if self.sandbox_id is None:
raise ValueError("sandbox_id is required to get a Sandbox")
return await self.connect_async(self.sandbox_id, config=self._config)
def get(self):
if self.sandbox_id is None:
raise ValueError("sandbox_id is required to get a Sandbox")
return self.connect(self.sandbox_id, config=self._config)
async def delete_async(self):
if self.sandbox_id is None:
raise ValueError("sandbox_id is required to delete a Sandbox")
return await self.delete_by_id_async(
self.sandbox_id, config=self._config
)
def delete(self):
if self.sandbox_id is None:
raise ValueError("sandbox_id is required to delete a Sandbox")
return self.delete_by_id(self.sandbox_id, config=self._config)
async def stop_async(self):
if self.sandbox_id is None:
raise ValueError("sandbox_id is required to stop a Sandbox")
return await self.stop_by_id_async(self.sandbox_id, config=self._config)
def stop(self):
if self.sandbox_id is None:
raise ValueError("sandbox_id is required to stop a Sandbox")
return self.stop_by_id(self.sandbox_id, config=self._config)