|
1 | | -"""ISO/snippet upload + listing helpers. |
2 | | -
|
3 | | -The PVE `/nodes/{node}/storage/{storage}/upload` endpoint is multipart/form-data |
4 | | -with a binary file part. The OpenAPI spec models the file as a `tmpfilename` |
5 | | -reference, so the generated `NodesStorageApi.upload` method cannot carry actual |
6 | | -bytes — it just sends the metadata. We use a raw multipart POST that reuses |
7 | | -the ApiClient's host + Authorization header. |
8 | | -
|
9 | | -Tracked as a generator gap; document on the SC-35 test. |
10 | | -""" |
| 1 | +"""ISO/snippet upload + content-listing helpers — thin SDK shims.""" |
11 | 2 | from __future__ import annotations |
12 | 3 |
|
13 | 4 | from typing import TYPE_CHECKING |
14 | 5 |
|
15 | | -import requests |
16 | | - |
17 | 6 | if TYPE_CHECKING: |
18 | 7 | from clientapi_pve import Pve |
19 | 8 |
|
20 | 9 |
|
21 | | -class UploadError(RuntimeError): |
22 | | - """Raised when the multipart upload returns a non-2xx response.""" |
23 | | - |
24 | | - |
25 | | -def _auth_url(pve: "Pve", path: str) -> tuple[str, dict[str, str], bool]: |
26 | | - cfg = pve.api_client.configuration |
27 | | - url = f"{cfg.host.rstrip('/')}{path}" |
28 | | - auth_header = cfg.api_key.get("PVEApiToken") |
29 | | - if not auth_header: |
30 | | - raise UploadError("raw-upload helpers require PVEApiToken auth") |
31 | | - return url, {"Authorization": auth_header}, cfg.verify_ssl |
32 | | - |
33 | | - |
34 | 10 | def upload_iso(pve: "Pve", node: str, storage: str, filename: str, data: bytes) -> str: |
35 | | - url, headers, verify = _auth_url( |
36 | | - pve, f"/nodes/{node}/storage/{storage}/upload" |
| 11 | + response = pve.nodesStorage.upload( |
| 12 | + node=node, |
| 13 | + storage=storage, |
| 14 | + content="iso", |
| 15 | + filename=(filename, data), |
37 | 16 | ) |
38 | | - files = { |
39 | | - "content": (None, "iso"), |
40 | | - "filename": (filename, data, "application/octet-stream"), |
41 | | - } |
42 | | - response = requests.post(url, headers=headers, files=files, verify=verify, timeout=120) |
43 | | - if response.status_code >= 400: |
44 | | - raise UploadError( |
45 | | - f"upload failed: HTTP {response.status_code} {response.text[:300]}" |
46 | | - ) |
47 | | - return (response.json() or {}).get("data") or "" |
| 17 | + return getattr(response, "data", "") or "" |
48 | 18 |
|
49 | 19 |
|
50 | | -def list_storage_content(pve: "Pve", node: str, storage: str) -> list[dict]: |
51 | | - """Return the raw content list for a storage. |
52 | | -
|
53 | | - The SDK's `NodesStorageApi.diridx` response model mistypes the inner items |
54 | | - (data deserializes as the wrong class and `volid` comes back empty), so we |
55 | | - parse the raw JSON ourselves. Tracked as a generator gap. |
56 | | - """ |
57 | | - url, headers, verify = _auth_url( |
58 | | - pve, f"/nodes/{node}/storage/{storage}/content" |
59 | | - ) |
60 | | - response = requests.get(url, headers=headers, verify=verify, timeout=30) |
61 | | - if response.status_code >= 400: |
62 | | - raise UploadError( |
63 | | - f"list failed: HTTP {response.status_code} {response.text[:300]}" |
64 | | - ) |
65 | | - return (response.json() or {}).get("data") or [] |
| 20 | +def list_storage_content(pve: "Pve", node: str, storage: str) -> list: |
| 21 | + response = pve.nodesStorage.get_content(node=node, storage=storage) |
| 22 | + return getattr(response, "data", None) or [] |
0 commit comments