gmi-sandbox-sdk 0.2.0b3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ __pycache__/
2
+ .pytest_cache/
3
+ *.egg-info/
4
+ dist/
5
+ build/
6
+ node_modules/
7
+ dist-ts/
8
+ coverage/
9
+ .env
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 GMI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,181 @@
1
+ Metadata-Version: 2.5
2
+ Name: gmi-sandbox-sdk
3
+ Version: 0.2.0b3
4
+ Summary: GMI Sandbox SDK for Python.
5
+ Project-URL: Homepage, https://www.gmicloud.ai/
6
+ Author: GMI
7
+ License: MIT
8
+ License-File: LICENSE
9
+ Keywords: gmi,sandbox,sdk
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Requires-Python: >=3.9
16
+ Provides-Extra: dev
17
+ Requires-Dist: build>=1.2; extra == 'dev'
18
+ Requires-Dist: pytest>=8.0; extra == 'dev'
19
+ Requires-Dist: twine>=5.0; extra == 'dev'
20
+ Description-Content-Type: text/markdown
21
+
22
+ ---
23
+ title: "GMI Sandbox SDK Usage"
24
+ description: "Install, configure, and use the GMI Sandbox Python SDK."
25
+ ---
26
+
27
+ # GMI Sandbox SDK Usage
28
+
29
+ ## Install
30
+
31
+ ```bash
32
+ python -m pip install gmi-sandbox-sdk
33
+ ```
34
+
35
+ ## Configure
36
+
37
+ Use environment variables or pass values directly.
38
+
39
+ To get an API key, log in to `https://console.gmicloud.ai/`, click **API keys**, select **compute**, and click **Create API key**.
40
+
41
+ ```bash
42
+ export GMI_SANDBOX_API_KEY="your-api-key"
43
+ ```
44
+
45
+ `GMI_SANDBOX_IDC_NAME` is optional. Leave it unset to use your organization's default IDC. Set it only when you want requests to target a non-default IDC:
46
+
47
+ ```bash
48
+ export GMI_SANDBOX_IDC_NAME="gmi-sandbox-us"
49
+ ```
50
+
51
+ ## Create a client
52
+
53
+ ```python
54
+ from sandbox_sdk import SandboxClient
55
+
56
+ client = SandboxClient()
57
+ ```
58
+
59
+ You can also pass credentials explicitly:
60
+
61
+ ```python
62
+ client = SandboxClient(
63
+ api_key="your-api-key",
64
+ )
65
+ ```
66
+
67
+ ## Create and use a sandbox
68
+
69
+ ```python
70
+ sandbox = client.sandboxes.create(
71
+ template_id="template-id",
72
+ )
73
+
74
+ sandbox.connect()
75
+ result = sandbox.commands.run("echo hello", wait=True)
76
+ print(result.stdout)
77
+
78
+ sandbox.files.write("/tmp/hello.txt", "hello")
79
+ print(sandbox.files.read("/tmp/hello.txt").decode())
80
+
81
+ sandbox.delete()
82
+ ```
83
+
84
+ If you already have a sandbox ID:
85
+
86
+ ```python
87
+ sandbox = client.sandboxes.get("sandbox-id")
88
+ sandbox.connect()
89
+ ```
90
+
91
+ ## Files
92
+
93
+ ```python
94
+ sandbox.files.read("/tmp/a.txt")
95
+ sandbox.files.write("/tmp/a.txt", "hello")
96
+ sandbox.files.write("/tmp/a.bin", b"binary-data")
97
+ sandbox.files.upload("./local.txt", "/workspace/local.txt")
98
+ sandbox.files.download("/workspace/output.txt", "./output.txt")
99
+ ```
100
+
101
+ `download()` returns bytes and can also write to a destination path.
102
+
103
+ ## Commands
104
+
105
+ ```python
106
+ execution = sandbox.commands.run(
107
+ "python --version",
108
+ cwd="/workspace",
109
+ envs={"PYTHONUNBUFFERED": "1"},
110
+ wait=True,
111
+ wait_timeout_seconds=25,
112
+ )
113
+
114
+ print(execution.status)
115
+ print(execution.exit_code)
116
+ print(execution.stdout)
117
+ print(execution.stderr)
118
+ ```
119
+
120
+ You can refresh or cancel a running execution:
121
+
122
+ ```python
123
+ execution.refresh()
124
+ execution.cancel()
125
+ ```
126
+
127
+ ## Templates
128
+
129
+ ```python
130
+ template = client.templates.get("template-id")
131
+ template.update(name="new-name")
132
+ template.delete()
133
+
134
+ builds = template.builds()
135
+ build = template.build("build-id")
136
+ logs = template.build_logs("build-id", offset=0, limit=100)
137
+ ```
138
+
139
+ Creating a template requires an idempotency key:
140
+
141
+ ```python
142
+ template = client.templates.create(
143
+ name="demo",
144
+ resources={"type": "preset", "product": "gmi.sandbox.small"},
145
+ build={"source": {"type": "image", "image": "ubuntu:22.04"}},
146
+ idempotency_key="template-1",
147
+ )
148
+
149
+ print(template.template_id)
150
+ template.update(name="new-name")
151
+ ```
152
+
153
+ ## Product specifications
154
+
155
+ ```python
156
+ client.product_specifications.list()
157
+ client.product_specifications.list(idc_name="gmi-sandbox-us")
158
+ ```
159
+
160
+ ## Errors
161
+
162
+ The SDK raises typed exceptions for HTTP failures:
163
+
164
+ - `BadRequestError`
165
+ - `AuthenticationError`
166
+ - `PermissionDeniedError`
167
+ - `NotFoundError`
168
+ - `ConflictError`
169
+ - `RateLimitError`
170
+ - `ServerError`
171
+
172
+ Example:
173
+
174
+ ```python
175
+ from sandbox_sdk import NotFoundError
176
+
177
+ try:
178
+ client.sandboxes.get("missing")
179
+ except NotFoundError:
180
+ print("sandbox not found")
181
+ ```
@@ -0,0 +1,424 @@
1
+ ---
2
+ title: "GMI Sandbox SDK Reference"
3
+ description: "Python SDK reference for GMI Sandbox control-plane and data-plane APIs."
4
+ ---
5
+
6
+ # GMI Sandbox SDK Reference
7
+
8
+ Python SDK for the GMI Sandbox control plane and sandbox data plane.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ python -m pip install gmi-sandbox-sdk
14
+ ```
15
+
16
+ The SDK requires Python 3.9 or newer.
17
+
18
+ ## Configuration
19
+
20
+ The client reads these environment variables when explicit arguments are not provided:
21
+
22
+ | Variable | Default | Description |
23
+ | --- | --- | --- |
24
+ | `GMI_SANDBOX_API_KEY` | none | Bearer API key |
25
+ | `GMI_SANDBOX_IDC_NAME` | organization default IDC | Optional. Set only when targeting a non-default IDC. |
26
+
27
+ ```bash
28
+ export GMI_SANDBOX_API_KEY="your-api-key"
29
+ ```
30
+
31
+ ## Client
32
+
33
+ ```python
34
+ from sandbox_sdk import SandboxClient
35
+
36
+ client = SandboxClient()
37
+ ```
38
+
39
+ Explicit configuration:
40
+
41
+ ```python
42
+ client = SandboxClient(
43
+ api_key="your-api-key",
44
+ timeout=30.0,
45
+ )
46
+ ```
47
+
48
+ Constructor arguments:
49
+
50
+ | Argument | Type | Description |
51
+ | --- | --- | --- |
52
+ | `api_key` | `str \| None` | API key. Falls back to `GMI_SANDBOX_API_KEY`. |
53
+ | `base_url` | `str \| None` | Optional control-plane URL for non-default API endpoints. Falls back to `GMI_SANDBOX_BASE_URL`. |
54
+ | `timeout` | `float` | Transport timeout in seconds. Default: `30.0`. |
55
+ | `transport` | callable \| None | Optional transport for tests or custom networking. |
56
+
57
+ `client.health()` returns local client information:
58
+
59
+ ```python
60
+ {
61
+ "status": "ok",
62
+ "baseUrl": "https://console.gmicloud.ai/api/v2",
63
+ "authenticated": True,
64
+ }
65
+ ```
66
+
67
+ ## Basic Workflow
68
+
69
+ ```python
70
+ from sandbox_sdk import SandboxClient
71
+
72
+ client = SandboxClient()
73
+
74
+ sandbox = client.sandboxes.create(
75
+ template_id="template-id",
76
+ idempotency_key="create-request-1",
77
+ )
78
+
79
+ sandbox.connect(timeout=300)
80
+ execution = sandbox.commands.run("echo hello", wait=True, wait_timeout_seconds=25)
81
+ print(execution.stdout)
82
+ sandbox.delete()
83
+ ```
84
+
85
+ `idc_name` is optional for sandbox and template creation: when omitted, the service falls back to the organization's configured default IDC.
86
+
87
+ ## Sandboxes
88
+
89
+ ### `client.sandboxes.create()`
90
+
91
+ ```python
92
+ sandbox = client.sandboxes.create(
93
+ idc_name="gmi-sandbox-us",
94
+ template_id="template-id",
95
+ timeout=300,
96
+ env_vars={"APP_ENV": "production"},
97
+ metadata={"owner": "platform"},
98
+ idempotency_key="sandbox-create-1",
99
+ )
100
+ ```
101
+
102
+ Arguments:
103
+
104
+ | Argument | Description |
105
+ | --- | --- |
106
+ | `idc_name` | Target IDC. Optional; falls back to the organization's default IDC when omitted. |
107
+ | `template_id` | Template source. Required. |
108
+ | `timeout` | Sandbox lifetime in seconds. |
109
+ | `env_vars` | Runtime environment variables. |
110
+ | `metadata` | String metadata attached to the sandbox. |
111
+ | `idempotency_key` | Optional create idempotency key. |
112
+
113
+ Returns a `Sandbox`.
114
+
115
+ ### `client.sandboxes.list()`
116
+
117
+ ```python
118
+ page = client.sandboxes.list(
119
+ page=1,
120
+ page_size=20,
121
+ idc_name="gmi-sandbox-us",
122
+ state=["running"],
123
+ metadata={"owner": "platform"},
124
+ )
125
+ ```
126
+
127
+ `state` accepts a string or a sequence of strings. Metadata is encoded as `metadata[key]` query parameters.
128
+
129
+ ### `client.sandboxes.get()`
130
+
131
+ ```python
132
+ sandbox = client.sandboxes.get("sandbox-id")
133
+ ```
134
+
135
+ ### `client.sandboxes.connect()`
136
+
137
+ ```python
138
+ sandbox = client.sandboxes.connect("sandbox-id", timeout=300)
139
+ ```
140
+
141
+ The returned sandbox contains fresh data-plane connection fields.
142
+
143
+ ## `Sandbox`
144
+
145
+ Properties:
146
+
147
+ | Property | Description |
148
+ | --- | --- |
149
+ | `sandbox_id` | Sandbox identifier (control-plane UUID) |
150
+ | `id` | Alias for `sandbox_id` |
151
+ | `sandbox_key` | Data-plane addressing key, used to build `data_plane_url` |
152
+ | `domain` | Data-plane domain |
153
+ | `sandbox_access_token` | Data-plane access token |
154
+ | `state` | Lifecycle state |
155
+ | `data_plane_url` | Computed data-plane URL |
156
+ | `files` | `SandboxFiles` helper |
157
+ | `commands` | `SandboxCommands` helper |
158
+ | `executions` | Alias for `commands` |
159
+ | `data` | Raw response data dictionary |
160
+
161
+ Lifecycle states include `provisioning`, `running`, `checkpointing`, `updating`, and `failed`.
162
+
163
+ Methods:
164
+
165
+ ```python
166
+ sandbox.refresh()
167
+ sandbox.update_metadata({"team": "platform"})
168
+ sandbox.set_timeout(300)
169
+ sandbox.connect(timeout=300)
170
+ sandbox.delete()
171
+ ```
172
+
173
+ ## Files
174
+
175
+ All file methods use the sandbox data plane and require `connect()` first.
176
+
177
+ ```python
178
+ sandbox.files.read("/tmp/input.txt")
179
+ sandbox.files.write("/tmp/output.txt", "hello")
180
+ sandbox.files.write("/tmp/output.bin", b"\x00\x01")
181
+ sandbox.files.upload("./local.txt", "/tmp/local.txt")
182
+ sandbox.files.download("/tmp/local.txt")
183
+ sandbox.files.download(
184
+ "/tmp/local.txt",
185
+ destination="./downloaded.txt",
186
+ byte_range="bytes=10-",
187
+ )
188
+ ```
189
+
190
+ Methods:
191
+
192
+ | Method | Description |
193
+ | --- | --- |
194
+ | `read(path, username=None)` | Read file bytes |
195
+ | `write(path, content, username=None, filename=None)` | Write text or bytes |
196
+ | `upload(source, path, username=None)` | Upload a local path or bytes |
197
+ | `download(path, destination=None, username=None, byte_range=None)` | Download bytes, optionally to a local path |
198
+
199
+ `download()` always returns `bytes`. `destination` is an additional local write.
200
+
201
+ ## Command Execution
202
+
203
+ ```python
204
+ execution = sandbox.commands.run(
205
+ "python --version",
206
+ envs={"PYTHONUNBUFFERED": "1"},
207
+ cwd="/tmp",
208
+ wait=True,
209
+ wait_timeout_seconds=25,
210
+ request_id="execution-request-1",
211
+ )
212
+ ```
213
+
214
+ Arguments:
215
+
216
+ | Argument | Description |
217
+ | --- | --- |
218
+ | `command` | Shell command |
219
+ | `envs` | Environment variables for the process |
220
+ | `cwd` | Working directory |
221
+ | `wait` | Wait for a result within the wait window |
222
+ | `wait_timeout_seconds` | Server wait window; the API currently accepts 1-25 seconds |
223
+ | `request_id` | Optional request correlation ID |
224
+
225
+ `Execution` properties:
226
+
227
+ ```python
228
+ execution.execution_id
229
+ execution.status
230
+ execution.exit_code
231
+ execution.stdout
232
+ execution.stderr
233
+ execution.data
234
+ ```
235
+
236
+ Execution statuses include `pending`, `running`, `canceling`, `succeeded`, `failed`, and `canceled`.
237
+
238
+ Methods:
239
+
240
+ ```python
241
+ execution.refresh(request_id="refresh-1")
242
+ execution.cancel(request_id="cancel-1")
243
+ ```
244
+
245
+ Cancel only non-terminal executions. The service may reject cancellation after an execution has completed.
246
+
247
+ ## Templates
248
+
249
+ ### Create
250
+
251
+ ```python
252
+ template = client.templates.create(
253
+ name="demo-template",
254
+ idc_name="gmi-sandbox-us",
255
+ resources={"type": "preset", "product": "gmi.sandbox.small"},
256
+ build={
257
+ "source": {"type": "image", "image": "ubuntu:22.04"},
258
+ "commands": ["apt-get update"],
259
+ "envs": {"BUILD_ENV": "production"},
260
+ "start_cmd": "sleep infinity",
261
+ },
262
+ description="Example template",
263
+ labels={"team": "platform"},
264
+ idempotency_key="template-create-1",
265
+ )
266
+ ```
267
+
268
+ `resources` supports preset or custom values.
269
+
270
+ Preset:
271
+
272
+ ```python
273
+ {"type": "preset", "product": "gmi.sandbox.small"}
274
+ ```
275
+
276
+ Custom:
277
+
278
+ ```python
279
+ {
280
+ "type": "custom",
281
+ "cpu_count": 2,
282
+ "memory_mb": 4096,
283
+ "disk_size_mb": 20480,
284
+ "architecture": "x86_64",
285
+ }
286
+ ```
287
+
288
+ Build sources include image and template sources:
289
+
290
+ ```python
291
+ {"type": "image", "image": "ubuntu:22.04"}
292
+ {"type": "template", "template_id": "parent-template-id"}
293
+ ```
294
+
295
+ `idempotency_key` is required for template creation.
296
+
297
+ Returns a `Template`. The returned object can be used with template instance methods such as `update()`, `delete()`, `builds()`, `build()`, and `build_logs()`.
298
+
299
+ ### List and Get
300
+
301
+ ```python
302
+ page = client.templates.list(idc_name="gmi-sandbox-us")
303
+ template = client.templates.get("template-id", idc_name="gmi-sandbox-us")
304
+ ```
305
+
306
+ ### Update and Delete
307
+
308
+ ```python
309
+ template.update(name="renamed-template", description="Updated description")
310
+ template.delete()
311
+ ```
312
+
313
+ The template's `idc_name` is reused automatically when present in the template response.
314
+
315
+ ### Builds and Logs
316
+
317
+ ```python
318
+ builds = template.builds(page=1, page_size=20)
319
+ build = template.build("build-id")
320
+ logs = template.build_logs("build-id", offset=0, limit=100)
321
+ ```
322
+
323
+ ## Product Specifications
324
+
325
+ ```python
326
+ specifications = client.product_specifications.list()
327
+ specifications = client.product_specifications.list(idc_name="gmi-sandbox-us")
328
+ ```
329
+
330
+ ## Pagination
331
+
332
+ List methods return a `Page` object:
333
+
334
+ ```python
335
+ page = client.sandboxes.list(page=1, page_size=20)
336
+
337
+ print(page.items)
338
+ print(page.total)
339
+ print(page.page)
340
+ print(page.page_size)
341
+ print(page.request_id)
342
+ ```
343
+
344
+ `Page` fields:
345
+
346
+ | Field | Type | Description |
347
+ | --- | --- | --- |
348
+ | `items` | `list` | Returned resources |
349
+ | `total` | `int` | Total matching resources |
350
+ | `page` | `int` | Current page |
351
+ | `page_size` | `int` | Requested page size |
352
+ | `request_id` | `str \| None` | Server request ID |
353
+
354
+ ## Errors
355
+
356
+ All SDK exceptions derive from `SandboxSDKError`.
357
+
358
+ | Exception | HTTP status |
359
+ | --- | --- |
360
+ | `BadRequestError` | 400 |
361
+ | `AuthenticationError` | 401 |
362
+ | `PermissionDeniedError` | 403 |
363
+ | `NotFoundError` | 404 |
364
+ | `ConflictError` | 409 |
365
+ | `RateLimitError` | 429 |
366
+ | `ServerError` | 500+ |
367
+ | `TransportError` | Request could not reach the endpoint |
368
+
369
+ `APIError` fields:
370
+
371
+ ```python
372
+ try:
373
+ client.sandboxes.get("missing")
374
+ except NotFoundError as exc:
375
+ print(exc.status_code)
376
+ print(exc.message)
377
+ print(exc.code)
378
+ print(exc.request_id)
379
+ print(exc.details)
380
+ ```
381
+
382
+ Error handling example:
383
+
384
+ ```python
385
+ from sandbox_sdk import ConflictError, NotFoundError, SandboxSDKError
386
+
387
+ try:
388
+ sandbox = client.sandboxes.get("sandbox-id")
389
+ sandbox.connect()
390
+ except NotFoundError:
391
+ print("sandbox no longer exists")
392
+ except ConflictError:
393
+ print("sandbox is in a transitional state; retry later")
394
+ except SandboxSDKError as exc:
395
+ print(f"sandbox request failed: {exc}")
396
+ ```
397
+
398
+ ## OpenAPI Coverage
399
+
400
+ The SDK maps the Sandbox-related OpenAPI operations as follows:
401
+
402
+ | OpenAPI operation | SDK |
403
+ | --- | --- |
404
+ | `createSandbox` | `client.sandboxes.create()` |
405
+ | `listSandboxes` | `client.sandboxes.list()` |
406
+ | `getSandbox` | `client.sandboxes.get()` |
407
+ | `updateSandbox` | `sandbox.update_metadata()` |
408
+ | `deleteSandbox` | `sandbox.delete()` |
409
+ | `connectSandbox` | `sandbox.connect()` |
410
+ | `setSandboxTimeout` | `sandbox.set_timeout()` |
411
+ | `createSandboxExecution` | `sandbox.commands.run()` |
412
+ | `getSandboxExecution` | `execution.refresh()` |
413
+ | `cancelSandboxExecution` | `execution.cancel()` |
414
+ | `downloadSandboxFile` | `sandbox.files.read()` / `download()` |
415
+ | `uploadSandboxFile` | `sandbox.files.write()` / `upload()` |
416
+ | `listProductCatalog` | `client.product_specifications.list()` |
417
+ | `createTemplate` | `client.templates.create()` |
418
+ | `listTemplates` | `client.templates.list()` |
419
+ | `getTemplate` | `client.templates.get()` |
420
+ | `updateTemplate` | `template.update()` |
421
+ | `deleteTemplate` | `template.delete()` |
422
+ | `listTemplateBuilds` | `template.builds()` |
423
+ | `getTemplateBuild` | `template.build()` |
424
+ | `getTemplateBuildLogs` | `template.build_logs()` |