|
4 | 4 | from typing import Any, Literal, Optional, Union |
5 | 5 |
|
6 | 6 | import requests |
| 7 | +import pathlib |
7 | 8 |
|
8 | 9 | from podman import api |
9 | 10 | from podman.domain.manager import Manager, PodmanResource |
@@ -173,3 +174,49 @@ def remove(self, name: Union[Volume, str], force: Optional[bool] = None) -> None |
173 | 174 | name = name.name |
174 | 175 | response = self.client.delete(f"/volumes/{name}", params={"force": force}) |
175 | 176 | response.raise_for_status() |
| 177 | + |
| 178 | + def export_archive(self, name: Union[Volume, str]) -> bytes: |
| 179 | + """Export a podman volume, returns the exported archive as bytes. |
| 180 | +
|
| 181 | + Args: |
| 182 | + name: Identifier for Volume to be exported. |
| 183 | +
|
| 184 | + Raises: |
| 185 | + APIError: when service reports an error |
| 186 | + """ |
| 187 | + if isinstance(name, Volume): |
| 188 | + name = name.name |
| 189 | + response = self.client.get(f"/volumes/{name}/export") |
| 190 | + response.raise_for_status() |
| 191 | + return response._content |
| 192 | + |
| 193 | + def import_archive( |
| 194 | + self, name: Union[Volume, str], data: Optional[bytes] = None, path: Optional[str] = None |
| 195 | + ): |
| 196 | + """Import a podman volume from tar. |
| 197 | + The podman volume archive must be provided either as bytes or as a path to the archive. |
| 198 | +
|
| 199 | + Args: |
| 200 | + name: Identifier for Volume to be imported. |
| 201 | + data: Uncompressed tar archive as bytes. |
| 202 | + path: Path to uncompressed tar archive. |
| 203 | +
|
| 204 | + Raises: |
| 205 | + APIError: when service reports an error |
| 206 | + """ |
| 207 | + if isinstance(name, Volume): |
| 208 | + name = name.name |
| 209 | + |
| 210 | + if data is None and path is None: |
| 211 | + raise RuntimeError("Either data or path must be provided !") |
| 212 | + elif data is not None and path is not None: |
| 213 | + raise RuntimeError("Data and path must not be set at the same time !") |
| 214 | + |
| 215 | + if data is None: |
| 216 | + file = pathlib.Path(path) |
| 217 | + if not file.exists(): |
| 218 | + raise RuntimeError(f"Archive {path} does not exist !") |
| 219 | + data = file.read_bytes() |
| 220 | + |
| 221 | + response = self.client.post(f"/volumes/{name}/import", data=data) |
| 222 | + response.raise_for_status() |
0 commit comments