libvirtpy 0.1.0__py3-none-any.whl

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.
Files changed (154) hide show
  1. libvirtpy/__init__.py +5 -0
  2. libvirtpy/client/__init__.py +54 -0
  3. libvirtpy/client/_bases.py +326 -0
  4. libvirtpy/client/_runner.py +57 -0
  5. libvirtpy/client/aio.py +5922 -0
  6. libvirtpy/client/client.py +5695 -0
  7. libvirtpy/client/operations/__init__.py +631 -0
  8. libvirtpy/client/operations/_common.py +495 -0
  9. libvirtpy/client/operations/checkpoint.py +196 -0
  10. libvirtpy/client/operations/connection.py +93 -0
  11. libvirtpy/client/operations/connection_extra.py +256 -0
  12. libvirtpy/client/operations/domain.py +306 -0
  13. libvirtpy/client/operations/domain_agent.py +115 -0
  14. libvirtpy/client/operations/domain_block.py +368 -0
  15. libvirtpy/client/operations/domain_info.py +400 -0
  16. libvirtpy/client/operations/domain_lifecycle.py +338 -0
  17. libvirtpy/client/operations/domain_migration.py +164 -0
  18. libvirtpy/client/operations/domain_network.py +113 -0
  19. libvirtpy/client/operations/domain_tuning.py +494 -0
  20. libvirtpy/client/operations/host.py +246 -0
  21. libvirtpy/client/operations/host_interface.py +100 -0
  22. libvirtpy/client/operations/interface_extra.py +45 -0
  23. libvirtpy/client/operations/network.py +135 -0
  24. libvirtpy/client/operations/network_info.py +155 -0
  25. libvirtpy/client/operations/network_port.py +142 -0
  26. libvirtpy/client/operations/node_device.py +76 -0
  27. libvirtpy/client/operations/node_device_info.py +189 -0
  28. libvirtpy/client/operations/nwfilter.py +67 -0
  29. libvirtpy/client/operations/nwfilter_binding.py +74 -0
  30. libvirtpy/client/operations/secret.py +93 -0
  31. libvirtpy/client/operations/secret_extra.py +24 -0
  32. libvirtpy/client/operations/snapshot.py +100 -0
  33. libvirtpy/client/operations/snapshot_tree.py +114 -0
  34. libvirtpy/client/operations/storage.py +267 -0
  35. libvirtpy/client/operations/storage_info.py +188 -0
  36. libvirtpy/client/operations/streams.py +201 -0
  37. libvirtpy/client/references.py +249 -0
  38. libvirtpy/client/subscribing.py +88 -0
  39. libvirtpy/client/testing/__init__.py +24 -0
  40. libvirtpy/client/testing/_common.py +167 -0
  41. libvirtpy/client/testing/_domains.py +294 -0
  42. libvirtpy/client/testing/_objects.py +205 -0
  43. libvirtpy/client/testing/_state.py +246 -0
  44. libvirtpy/client/testing/_storage.py +171 -0
  45. libvirtpy/client/testing/async_transport.py +30 -0
  46. libvirtpy/client/testing/fake.py +81 -0
  47. libvirtpy/client/transport.py +87 -0
  48. libvirtpy/client/types.py +59 -0
  49. libvirtpy/client/wire.py +436 -0
  50. libvirtpy/client/wire_aio.py +348 -0
  51. libvirtpy/errors.py +7 -0
  52. libvirtpy/py.typed +0 -0
  53. libvirtpy/rpc/__init__.py +1 -0
  54. libvirtpy/rpc/aio.py +387 -0
  55. libvirtpy/rpc/cpumap.py +48 -0
  56. libvirtpy/rpc/errors.py +186 -0
  57. libvirtpy/rpc/events.py +259 -0
  58. libvirtpy/rpc/options.py +66 -0
  59. libvirtpy/rpc/procedure.py +27 -0
  60. libvirtpy/rpc/protocol/__init__.py +0 -0
  61. libvirtpy/rpc/protocol/procedures.py +5163 -0
  62. libvirtpy/rpc/protocol/typed_hints.py +210 -0
  63. libvirtpy/rpc/protocol/types.py +9299 -0
  64. libvirtpy/rpc/remote.py +295 -0
  65. libvirtpy/rpc/remote_aio.py +106 -0
  66. libvirtpy/rpc/sasl.py +467 -0
  67. libvirtpy/rpc/session.py +424 -0
  68. libvirtpy/rpc/sockets.py +251 -0
  69. libvirtpy/rpc/stream.py +323 -0
  70. libvirtpy/rpc/subscription.py +451 -0
  71. libvirtpy/rpc/sync.py +460 -0
  72. libvirtpy/rpc/typed_params.py +97 -0
  73. libvirtpy/rpc/uri.py +85 -0
  74. libvirtpy/rpc/xdr.py +552 -0
  75. libvirtpy/xml/__init__.py +49 -0
  76. libvirtpy/xml/_base.py +145 -0
  77. libvirtpy/xml/_shared/__init__.py +1 -0
  78. libvirtpy/xml/_shared/metadata.py +67 -0
  79. libvirtpy/xml/_shared/network.py +108 -0
  80. libvirtpy/xml/_shared/storage.py +131 -0
  81. libvirtpy/xml/_shared/sysinfo.py +83 -0
  82. libvirtpy/xml/_types.py +197 -0
  83. libvirtpy/xml/_xml_element.py +134 -0
  84. libvirtpy/xml/backup/__init__.py +25 -0
  85. libvirtpy/xml/backup/backup.py +89 -0
  86. libvirtpy/xml/backup/checkpoint.py +48 -0
  87. libvirtpy/xml/capabilities/__init__.py +43 -0
  88. libvirtpy/xml/capabilities/domain.py +436 -0
  89. libvirtpy/xml/capabilities/host.py +413 -0
  90. libvirtpy/xml/capabilities/storagepool.py +59 -0
  91. libvirtpy/xml/domain/__init__.py +493 -0
  92. libvirtpy/xml/domain/blkiotune.py +81 -0
  93. libvirtpy/xml/domain/clock.py +79 -0
  94. libvirtpy/xml/domain/cpu.py +265 -0
  95. libvirtpy/xml/domain/devices/__init__.py +495 -0
  96. libvirtpy/xml/domain/devices/address.py +152 -0
  97. libvirtpy/xml/domain/devices/audio.py +204 -0
  98. libvirtpy/xml/domain/devices/chardev.py +197 -0
  99. libvirtpy/xml/domain/devices/common.py +75 -0
  100. libvirtpy/xml/domain/devices/controller.py +262 -0
  101. libvirtpy/xml/domain/devices/devices.py +79 -0
  102. libvirtpy/xml/domain/devices/disk.py +471 -0
  103. libvirtpy/xml/domain/devices/filesystem.py +206 -0
  104. libvirtpy/xml/domain/devices/graphics.py +245 -0
  105. libvirtpy/xml/domain/devices/hostdev.py +221 -0
  106. libvirtpy/xml/domain/devices/input.py +89 -0
  107. libvirtpy/xml/domain/devices/interface.py +459 -0
  108. libvirtpy/xml/domain/devices/iommu.py +54 -0
  109. libvirtpy/xml/domain/devices/memballoon.py +42 -0
  110. libvirtpy/xml/domain/devices/memory_device.py +101 -0
  111. libvirtpy/xml/domain/devices/misc.py +169 -0
  112. libvirtpy/xml/domain/devices/nvram.py +20 -0
  113. libvirtpy/xml/domain/devices/shmem.py +46 -0
  114. libvirtpy/xml/domain/devices/sound.py +42 -0
  115. libvirtpy/xml/domain/devices/tpm.py +103 -0
  116. libvirtpy/xml/domain/devices/usb.py +86 -0
  117. libvirtpy/xml/domain/devices/video.py +101 -0
  118. libvirtpy/xml/domain/domain.py +157 -0
  119. libvirtpy/xml/domain/features.py +312 -0
  120. libvirtpy/xml/domain/iothread.py +41 -0
  121. libvirtpy/xml/domain/launch_security.py +82 -0
  122. libvirtpy/xml/domain/memory.py +149 -0
  123. libvirtpy/xml/domain/misc.py +113 -0
  124. libvirtpy/xml/domain/namespaces.py +96 -0
  125. libvirtpy/xml/domain/numa.py +140 -0
  126. libvirtpy/xml/domain/os.py +267 -0
  127. libvirtpy/xml/domain/qemu.py +102 -0
  128. libvirtpy/xml/domain/seclabel.py +29 -0
  129. libvirtpy/xml/domain/storage_source.py +466 -0
  130. libvirtpy/xml/domain/sysinfo.py +34 -0
  131. libvirtpy/xml/interface/__init__.py +17 -0
  132. libvirtpy/xml/interface/interface.py +153 -0
  133. libvirtpy/xml/network/__init__.py +101 -0
  134. libvirtpy/xml/network/network.py +311 -0
  135. libvirtpy/xml/network/port.py +84 -0
  136. libvirtpy/xml/nodedev/__init__.py +13 -0
  137. libvirtpy/xml/nodedev/nodedev.py +490 -0
  138. libvirtpy/xml/nwfilter/__init__.py +81 -0
  139. libvirtpy/xml/nwfilter/binding.py +51 -0
  140. libvirtpy/xml/nwfilter/filter.py +92 -0
  141. libvirtpy/xml/nwfilter/rules.py +534 -0
  142. libvirtpy/xml/secret/__init__.py +11 -0
  143. libvirtpy/xml/secret/secret.py +36 -0
  144. libvirtpy/xml/snapshot/__init__.py +29 -0
  145. libvirtpy/xml/snapshot/snapshot.py +136 -0
  146. libvirtpy/xml/storage/__init__.py +75 -0
  147. libvirtpy/xml/storage/pool.py +219 -0
  148. libvirtpy/xml/storage/volume.py +102 -0
  149. libvirtpy/xml/sysinfo.py +40 -0
  150. libvirtpy-0.1.0.dist-info/METADATA +109 -0
  151. libvirtpy-0.1.0.dist-info/RECORD +154 -0
  152. libvirtpy-0.1.0.dist-info/WHEEL +4 -0
  153. libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
  154. libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
@@ -0,0 +1,495 @@
1
+ """Helpers shared by the operation modules (private)."""
2
+
3
+ from collections.abc import Callable, Iterator
4
+ from functools import partial
5
+ from typing import TypeVar
6
+
7
+ from libvirtpy.client.references import (
8
+ CheckpointRef,
9
+ DomainRef,
10
+ HostInterfaceRef,
11
+ NetworkPortRef,
12
+ NetworkRef,
13
+ NodeDeviceRef,
14
+ NwFilterBindingRef,
15
+ NwFilterRef,
16
+ PoolRef,
17
+ SecretRef,
18
+ SnapshotRef,
19
+ VolumeRef,
20
+ checkpoint_lookup,
21
+ domain_lookup,
22
+ host_interface_lookup,
23
+ network_lookup,
24
+ network_port_lookup,
25
+ node_device_lookup,
26
+ nwfilter_binding_lookup,
27
+ nwfilter_lookup,
28
+ pool_lookup,
29
+ secret_lookup,
30
+ snapshot_lookup,
31
+ volume_lookup,
32
+ )
33
+ from libvirtpy.client.transport import Handle, Operation, Request
34
+ from libvirtpy.rpc import typed_params
35
+ from libvirtpy.rpc.protocol.types import TypedParam
36
+ from libvirtpy.rpc.typed_params import TypedValue
37
+ from libvirtpy.xml._base import LibvirtXmlModel
38
+
39
+ ModelT = TypeVar("ModelT", bound=LibvirtXmlModel)
40
+ StructT = TypeVar("StructT")
41
+
42
+
43
+ DOMAINS_ACTIVE, DOMAINS_INACTIVE = 1, 2
44
+
45
+
46
+ INACTIVE, ACTIVE = 1, 2
47
+
48
+
49
+ OBJECT_FIELDS = {
50
+ "domain_get_xml_desc": "dom",
51
+ "network_get_xml_desc": "net",
52
+ "storage_pool_get_xml_desc": "pool",
53
+ "storage_vol_get_xml_desc": "vol",
54
+ "secret_get_xml_desc": "secret",
55
+ "nwfilter_get_xml_desc": "nwfilter",
56
+ "interface_get_xml_desc": "iface",
57
+ "node_device_get_xml_desc": "name",
58
+ "domain_snapshot_get_xml_desc": "snap",
59
+ "domain_checkpoint_get_xml_desc": "checkpoint",
60
+ "network_port_get_xml_desc": "port",
61
+ "nwfilter_binding_get_xml_desc": "nwfilter",
62
+ }
63
+
64
+
65
+ """The argument that names the object, in each ``*_get_xml_desc`` procedure."""
66
+
67
+
68
+ def one_handle(request: Request) -> Operation[Handle]:
69
+ """Send ``request`` and expect one object handle back.
70
+
71
+ :param request: the request.
72
+ :returns: the handle in the reply.
73
+ """
74
+ reply = yield request
75
+ if not isinstance(reply, Handle):
76
+ raise TypeError(f"expected a Handle for {request.procedure}, got {reply!r}")
77
+ return reply
78
+
79
+
80
+ def many_handles(request: Request) -> Operation[list[Handle]]:
81
+ """Send ``request`` and expect a list of object handles back.
82
+
83
+ :param request: the request.
84
+ :returns: the handles in the reply.
85
+ """
86
+ reply = yield request
87
+ if not isinstance(reply, list) or not all(isinstance(h, Handle) for h in reply):
88
+ raise TypeError(f"expected Handles for {request.procedure}, got {reply!r}")
89
+ return list(_iterate_handles(reply))
90
+
91
+
92
+ def fetch_model[ModelT: LibvirtXmlModel](
93
+ procedure: str, handle: Handle, model_type: type[ModelT]
94
+ ) -> Operation[ModelT]:
95
+ """Ask for the XML of an object and parse it.
96
+
97
+ :param procedure: the ``*_get_xml_desc`` procedure of the object kind.
98
+ :param handle: the object.
99
+ :param model_type: the model class to parse the XML with.
100
+ :returns: the parsed model.
101
+ """
102
+ field = OBJECT_FIELDS[procedure]
103
+ xml = as_text((yield Request(procedure, {field: handle, "flags": 0})))
104
+ return model_type.from_xml(xml)
105
+
106
+
107
+ def fetch_models[ModelT: LibvirtXmlModel](
108
+ procedure: str, handles: list[Handle], model_type: type[ModelT]
109
+ ) -> Operation[list[ModelT]]:
110
+ """Fetch several objects, one request each.
111
+
112
+ :param procedure: the ``*_get_xml_desc`` procedure of the object kind.
113
+ :param handles: the objects.
114
+ :param model_type: the model class to parse the XML with.
115
+ :returns: the models, in the order of ``handles``.
116
+ """
117
+ models = []
118
+ for handle in handles:
119
+ models.append( # noqa: PERF401 (a yield from cannot be a comprehension)
120
+ (yield from fetch_model(procedure, handle, model_type))
121
+ )
122
+ return models
123
+
124
+
125
+ def as_text(reply: object) -> str:
126
+ """Check that a reply is text.
127
+
128
+ :param reply: what the daemon sent.
129
+ :returns: the text.
130
+ :raises TypeError: if it is not text.
131
+ """
132
+ if not isinstance(reply, str):
133
+ raise TypeError(f"expected text, got {reply!r}")
134
+ return reply
135
+
136
+
137
+ def as_int(reply: object) -> int:
138
+ """Check that a reply is a number.
139
+
140
+ :param reply: what the daemon sent.
141
+ :returns: the number.
142
+ :raises TypeError: if it is not a number.
143
+ """
144
+ if not isinstance(reply, int) or isinstance(reply, bool):
145
+ raise TypeError(f"expected a number, got {reply!r}")
146
+ return reply
147
+
148
+
149
+ def as_xml(value: LibvirtXmlModel | str) -> str:
150
+ """Turn a model into XML text; text passes through.
151
+
152
+ :param value: a model or XML text.
153
+ :returns: the XML text.
154
+ """
155
+ if isinstance(value, str):
156
+ return value
157
+ text = value.to_xml(encoding="unicode")
158
+ if not isinstance(text, str):
159
+ raise TypeError("to_xml(encoding='unicode') did not return text")
160
+ return text
161
+
162
+
163
+ def _iterate_handles(replies: list[object]) -> Iterator[Handle]:
164
+ """Yield the handles among the replies.
165
+
166
+ :param replies: the items of a list reply.
167
+ :returns: the items that are handles.
168
+ """
169
+ for reply in replies:
170
+ if isinstance(reply, Handle):
171
+ yield reply
172
+
173
+
174
+ def on_object(
175
+ lookup: Request, field: str, procedure: str, arguments: dict[str, object]
176
+ ) -> Operation[object]:
177
+ """Find an object, then call a procedure on it.
178
+
179
+ :param lookup: the request that finds the object.
180
+ :param field: the argument of ``procedure`` that names the object.
181
+ :param procedure: the procedure to call.
182
+ :param arguments: its other arguments.
183
+ :returns: the daemon's reply.
184
+ """
185
+ handle = yield from one_handle(lookup)
186
+ return (yield Request(procedure, {field: handle, **arguments}))
187
+
188
+
189
+ def on_domain(
190
+ domain: DomainRef, procedure: str, **arguments: object
191
+ ) -> Operation[object]:
192
+ """Call a procedure on a domain.
193
+
194
+ :param domain: the domain.
195
+ :param procedure: the procedure; the domain is its ``dom`` argument.
196
+ :param arguments: the other arguments of the procedure, by name.
197
+ :returns: the daemon's reply.
198
+ """
199
+ return (yield from on_object(domain_lookup(domain), "dom", procedure, arguments))
200
+
201
+
202
+ def on_network(
203
+ network: NetworkRef, procedure: str, **arguments: object
204
+ ) -> Operation[object]:
205
+ """Call a procedure on a network.
206
+
207
+ :param network: the network.
208
+ :param procedure: the procedure; the network is its ``net`` argument.
209
+ :param arguments: the other arguments of the procedure, by name.
210
+ :returns: the daemon's reply.
211
+ """
212
+ return (yield from on_object(network_lookup(network), "net", procedure, arguments))
213
+
214
+
215
+ def on_pool(pool: PoolRef, procedure: str, **arguments: object) -> Operation[object]:
216
+ """Call a procedure on a storage pool.
217
+
218
+ :param pool: the pool.
219
+ :param procedure: the procedure; the pool is its ``pool`` argument.
220
+ :param arguments: the other arguments of the procedure, by name.
221
+ :returns: the daemon's reply.
222
+ """
223
+ return (yield from on_object(pool_lookup(pool), "pool", procedure, arguments))
224
+
225
+
226
+ def on_volume(
227
+ pool: PoolRef, volume: VolumeRef, procedure: str, **arguments: object
228
+ ) -> Operation[object]:
229
+ """Call a procedure on a storage volume.
230
+
231
+ :param pool: the pool of the volume.
232
+ :param volume: the volume.
233
+ :param procedure: the procedure; the volume is its ``vol`` argument.
234
+ :param arguments: the other arguments of the procedure, by name.
235
+ :returns: the daemon's reply.
236
+ """
237
+ pool_handle = yield from one_handle(pool_lookup(pool))
238
+ lookup = volume_lookup(pool_handle, volume)
239
+ return (yield from on_object(lookup, "vol", procedure, arguments))
240
+
241
+
242
+ def on_snapshot(
243
+ domain: DomainRef, snapshot: SnapshotRef, procedure: str, **arguments: object
244
+ ) -> Operation[object]:
245
+ """Call a procedure on a snapshot of a domain.
246
+
247
+ :param domain: the domain.
248
+ :param snapshot: the snapshot.
249
+ :param procedure: the procedure; the snapshot is its ``snap`` argument.
250
+ :param arguments: the other arguments of the procedure, by name.
251
+ :returns: the daemon's reply.
252
+ """
253
+ domain_handle = yield from one_handle(domain_lookup(domain))
254
+ lookup = snapshot_lookup(domain_handle, snapshot)
255
+ return (yield from on_object(lookup, "snap", procedure, arguments))
256
+
257
+
258
+ def on_secret(
259
+ secret: SecretRef, procedure: str, **arguments: object
260
+ ) -> Operation[object]:
261
+ """Call a procedure on a secret.
262
+
263
+ :param secret: the secret.
264
+ :param procedure: the procedure; the secret is its ``secret`` argument.
265
+ :param arguments: the other arguments of the procedure, by name.
266
+ :returns: the daemon's reply.
267
+ """
268
+ return (yield from on_object(secret_lookup(secret), "secret", procedure, arguments))
269
+
270
+
271
+ def on_nwfilter(
272
+ nwfilter: NwFilterRef, procedure: str, **arguments: object
273
+ ) -> Operation[object]:
274
+ """Call a procedure on a network filter.
275
+
276
+ :param nwfilter: the filter.
277
+ :param procedure: the procedure; the filter is its ``nwfilter`` argument.
278
+ :param arguments: the other arguments of the procedure, by name.
279
+ :returns: the daemon's reply.
280
+ """
281
+ lookup = nwfilter_lookup(nwfilter)
282
+ return (yield from on_object(lookup, "nwfilter", procedure, arguments))
283
+
284
+
285
+ def on_host_interface(
286
+ interface: HostInterfaceRef, procedure: str, **arguments: object
287
+ ) -> Operation[object]:
288
+ """Call a procedure on a network interface of the host.
289
+
290
+ :param interface: the interface.
291
+ :param procedure: the procedure; the interface is its ``iface`` argument.
292
+ :param arguments: the other arguments of the procedure, by name.
293
+ :returns: the daemon's reply.
294
+ """
295
+ lookup = host_interface_lookup(interface)
296
+ return (yield from on_object(lookup, "iface", procedure, arguments))
297
+
298
+
299
+ def on_node_device(
300
+ device: NodeDeviceRef, procedure: str, **arguments: object
301
+ ) -> Operation[object]:
302
+ """Call a procedure on a host device.
303
+
304
+ :param device: the device.
305
+ :param procedure: the procedure; the device is its ``name`` argument.
306
+ :param arguments: the other arguments of the procedure, by name.
307
+ :returns: the daemon's reply.
308
+ """
309
+ lookup = node_device_lookup(device)
310
+ return (yield from on_object(lookup, "name", procedure, arguments))
311
+
312
+
313
+ def on_checkpoint(
314
+ domain: DomainRef, checkpoint: CheckpointRef, procedure: str, **arguments: object
315
+ ) -> Operation[object]:
316
+ """Call a procedure on a checkpoint of a domain.
317
+
318
+ :param domain: the domain.
319
+ :param checkpoint: the checkpoint.
320
+ :param procedure: the procedure; the checkpoint is its ``checkpoint`` argument.
321
+ :param arguments: the other arguments of the procedure, by name.
322
+ :returns: the daemon's reply.
323
+ """
324
+ domain_handle = yield from one_handle(domain_lookup(domain))
325
+ lookup = checkpoint_lookup(domain_handle, checkpoint)
326
+ return (yield from on_object(lookup, "checkpoint", procedure, arguments))
327
+
328
+
329
+ def on_network_port(
330
+ network: NetworkRef, port: NetworkPortRef, procedure: str, **arguments: object
331
+ ) -> Operation[object]:
332
+ """Call a procedure on a port of a network.
333
+
334
+ :param network: the network.
335
+ :param port: the port.
336
+ :param procedure: the procedure; the port is its ``port`` argument.
337
+ :param arguments: the other arguments of the procedure, by name.
338
+ :returns: the daemon's reply.
339
+ """
340
+ network_handle = yield from one_handle(network_lookup(network))
341
+ lookup = network_port_lookup(network_handle, port)
342
+ return (yield from on_object(lookup, "port", procedure, arguments))
343
+
344
+
345
+ def on_nwfilter_binding(
346
+ binding: NwFilterBindingRef, procedure: str, **arguments: object
347
+ ) -> Operation[object]:
348
+ """Call a procedure on a network filter binding.
349
+
350
+ :param binding: the binding.
351
+ :param procedure: the procedure; the binding is its ``nwfilter`` argument.
352
+ :param arguments: the other arguments of the procedure, by name.
353
+ :returns: the daemon's reply.
354
+ """
355
+ lookup = nwfilter_binding_lookup(binding)
356
+ return (yield from on_object(lookup, "nwfilter", procedure, arguments))
357
+
358
+
359
+ def as_bool(reply: object) -> bool:
360
+ """Check that a reply is a yes/no number and make it a ``bool``.
361
+
362
+ :param reply: what the daemon sent.
363
+ :returns: ``True`` for a non-zero number.
364
+ :raises TypeError: if it is not a number.
365
+ """
366
+ return bool(as_int(reply))
367
+
368
+
369
+ def as_params(reply: object) -> dict[str, TypedValue]:
370
+ """Check that a reply is a dict of typed parameters.
371
+
372
+ :param reply: what the daemon sent.
373
+ :returns: the parameters.
374
+ :raises TypeError: if it is not a dict.
375
+ """
376
+ if not isinstance(reply, dict):
377
+ raise TypeError(f"expected typed parameters, got {reply!r}")
378
+ return reply
379
+
380
+
381
+ def as_struct[StructT](reply: object, struct_type: type[StructT]) -> StructT:
382
+ """Check that a reply is a given wire struct.
383
+
384
+ :param reply: what the daemon sent.
385
+ :param struct_type: the generated return struct class.
386
+ :returns: the struct.
387
+ :raises TypeError: if it is something else.
388
+ """
389
+ if not isinstance(reply, struct_type):
390
+ raise TypeError(f"expected {struct_type.__name__}, got {reply!r}")
391
+ return reply
392
+
393
+
394
+ def as_list(reply: object) -> list[object]:
395
+ """Check that a reply is a list.
396
+
397
+ :param reply: what the daemon sent.
398
+ :returns: the items.
399
+ :raises TypeError: if it is not a list.
400
+ """
401
+ if not isinstance(reply, list):
402
+ raise TypeError(f"expected a list, got {reply!r}")
403
+ return reply
404
+
405
+
406
+ def as_bytes(reply: object) -> bytes:
407
+ """Check that a reply is raw bytes.
408
+
409
+ :param reply: what the daemon sent.
410
+ :returns: the bytes.
411
+ :raises TypeError: if it is something else.
412
+ """
413
+ if not isinstance(reply, bytes):
414
+ raise TypeError(f"expected bytes, got {reply!r}")
415
+ return reply
416
+
417
+
418
+ def get_typed_parameters(
419
+ domain: DomainRef, procedure: str, flags: int, **arguments: object
420
+ ) -> Operation[dict[str, TypedValue]]:
421
+ """Read the typed parameters of a domain: the count, then the values.
422
+
423
+ :param domain: the domain.
424
+ :param procedure: a ``domain_get_*`` procedure that has ``nparams`` and
425
+ returns ``params``.
426
+ :param flags: the flags of the call.
427
+ :param arguments: the other arguments of the procedure, by name.
428
+ :returns: the parameters by name.
429
+ """
430
+ return (
431
+ yield from read_parameters(
432
+ partial(on_domain, domain), procedure, flags, **arguments
433
+ )
434
+ )
435
+
436
+
437
+ def read_parameters(
438
+ caller: Callable[..., Operation[object]],
439
+ procedure: str,
440
+ flags: int,
441
+ **arguments: object,
442
+ ) -> Operation[dict[str, TypedValue]]:
443
+ """Read typed parameters with libvirt's two calls: the count, then the values.
444
+
445
+ :param caller: calls a procedure on the object, e.g. ``partial(on_domain, d)``.
446
+ :param procedure: a ``*_get_*`` procedure that has ``nparams`` and returns
447
+ ``params``.
448
+ :param flags: the flags of the call.
449
+ :param arguments: the other arguments of the procedure, by name.
450
+ :returns: the parameters by name.
451
+ """
452
+ first = yield from caller(procedure, nparams=0, flags=flags, **arguments)
453
+ count = parameter_count(first)
454
+
455
+ reply = yield from caller(procedure, nparams=count, flags=flags, **arguments)
456
+ return typed_params.from_wire(parameters_of(reply))
457
+
458
+
459
+ def parameter_count(reply: object) -> int:
460
+ """Read ``nparams`` from the reply to a count request.
461
+
462
+ :param reply: a return struct that has an ``nparams`` field.
463
+ :returns: the number of parameters.
464
+ :raises TypeError: if the reply has none.
465
+ """
466
+ count = getattr(reply, "nparams", None)
467
+ if not isinstance(count, int):
468
+ raise TypeError(f"expected a reply with nparams, got {reply!r}")
469
+ return count
470
+
471
+
472
+ def parameters_of(reply: object) -> list[TypedParam]:
473
+ """Read ``params`` from a reply of several fields.
474
+
475
+ :param reply: a return struct that has a ``params`` field.
476
+ :returns: the typed parameters.
477
+ :raises TypeError: if the reply has none.
478
+ """
479
+ parameters = getattr(reply, "params", None)
480
+ if not isinstance(parameters, list):
481
+ raise TypeError(f"expected a reply with params, got {reply!r}")
482
+ return parameters
483
+
484
+
485
+ def as_handle(operation: Operation[object]) -> Operation[Handle]:
486
+ """Run an operation and check that it gave an object handle.
487
+
488
+ :param operation: an operation whose result is an object handle.
489
+ :returns: the handle.
490
+ :raises TypeError: if it is something else.
491
+ """
492
+ reply = yield from operation
493
+ if not isinstance(reply, Handle):
494
+ raise TypeError(f"expected a Handle, got {reply!r}")
495
+ return reply
@@ -0,0 +1,196 @@
1
+ """Operations on the checkpoints and backups of a domain."""
2
+
3
+ from libvirtpy.client.operations._common import (
4
+ as_handle,
5
+ as_text,
6
+ as_xml,
7
+ fetch_model,
8
+ fetch_models,
9
+ many_handles,
10
+ on_checkpoint,
11
+ on_domain,
12
+ one_handle,
13
+ )
14
+ from libvirtpy.client.references import (
15
+ CheckpointRef,
16
+ DomainRef,
17
+ checkpoint_lookup,
18
+ domain_lookup,
19
+ )
20
+ from libvirtpy.client.transport import Operation, Request
21
+ from libvirtpy.xml.backup import Backup, Checkpoint
22
+
23
+ __all__ = [
24
+ "abort_domain_backup",
25
+ "begin_domain_backup",
26
+ "create_domain_checkpoint",
27
+ "delete_domain_checkpoint",
28
+ "get_domain_backup",
29
+ "get_domain_checkpoint_parent",
30
+ "list_domain_checkpoint_children",
31
+ "list_domain_checkpoints",
32
+ "lookup_domain_checkpoint",
33
+ ]
34
+
35
+
36
+ def list_domain_checkpoints(
37
+ domain: DomainRef, *, flags: int = 0
38
+ ) -> Operation[list[Checkpoint]]:
39
+ """Return the checkpoints of a domain (dirty-block tracking points).
40
+
41
+ :param domain: the domain.
42
+ :param flags: ``virDomainCheckpointListFlags``: roots, leaves, topological...
43
+ :returns: the checkpoints.
44
+ """
45
+ handle = yield from one_handle(domain_lookup(domain))
46
+ handles = yield from many_handles(
47
+ Request(
48
+ "domain_list_all_checkpoints",
49
+ {"dom": handle, "need_results": 1, "flags": flags},
50
+ )
51
+ )
52
+ return (
53
+ yield from fetch_models("domain_checkpoint_get_xml_desc", handles, Checkpoint)
54
+ )
55
+
56
+
57
+ def lookup_domain_checkpoint(
58
+ domain: DomainRef, checkpoint: CheckpointRef
59
+ ) -> Operation[Checkpoint]:
60
+ """Return one checkpoint of a domain.
61
+
62
+ :param domain: the domain.
63
+ :param checkpoint: the checkpoint name, or a ``Checkpoint`` model.
64
+ :returns: the checkpoint.
65
+ :raises NoCheckpointError: if there is no such checkpoint.
66
+ """
67
+ domain_handle = yield from one_handle(domain_lookup(domain))
68
+ found = yield from one_handle(checkpoint_lookup(domain_handle, checkpoint))
69
+ return (yield from fetch_model("domain_checkpoint_get_xml_desc", found, Checkpoint))
70
+
71
+
72
+ def create_domain_checkpoint(
73
+ domain: DomainRef, checkpoint: Checkpoint | str, *, flags: int = 0
74
+ ) -> Operation[Checkpoint]:
75
+ """Create a checkpoint of a domain.
76
+
77
+ :param domain: the domain.
78
+ :param checkpoint: the checkpoint description, as a model or XML.
79
+ :param flags: ``virDomainCheckpointCreateFlags``: redefine, quiesce...
80
+ :returns: the checkpoint as the daemon stored it.
81
+ """
82
+ created = yield from as_handle(
83
+ on_domain(
84
+ domain,
85
+ "domain_checkpoint_create_xml",
86
+ xml_desc=as_xml(checkpoint),
87
+ flags=flags,
88
+ )
89
+ )
90
+ return (
91
+ yield from fetch_model("domain_checkpoint_get_xml_desc", created, Checkpoint)
92
+ )
93
+
94
+
95
+ def get_domain_checkpoint_parent(
96
+ domain: DomainRef, checkpoint: CheckpointRef, *, flags: int = 0
97
+ ) -> Operation[Checkpoint]:
98
+ """Return the parent of a checkpoint.
99
+
100
+ :param domain: the domain.
101
+ :param checkpoint: the checkpoint.
102
+ :param flags: reserved, 0.
103
+ :returns: the parent checkpoint.
104
+ :raises NoCheckpointError: if it has no parent.
105
+ """
106
+ parent = yield from as_handle(
107
+ on_checkpoint(domain, checkpoint, "domain_checkpoint_get_parent", flags=flags)
108
+ )
109
+ return (
110
+ yield from fetch_model("domain_checkpoint_get_xml_desc", parent, Checkpoint)
111
+ )
112
+
113
+
114
+ def list_domain_checkpoint_children(
115
+ domain: DomainRef, checkpoint: CheckpointRef, *, flags: int = 0
116
+ ) -> Operation[list[Checkpoint]]:
117
+ """Return the checkpoints that descend from a checkpoint.
118
+
119
+ :param domain: the domain.
120
+ :param checkpoint: the checkpoint.
121
+ :param flags: ``virDomainCheckpointListFlags``: 1 all descendants, ...
122
+ :returns: the children.
123
+ """
124
+ domain_handle = yield from one_handle(domain_lookup(domain))
125
+ parent = yield from one_handle(checkpoint_lookup(domain_handle, checkpoint))
126
+ children = yield from many_handles(
127
+ Request(
128
+ "domain_checkpoint_list_all_children",
129
+ {"checkpoint": parent, "need_results": 1, "flags": flags},
130
+ )
131
+ )
132
+ return (
133
+ yield from fetch_models("domain_checkpoint_get_xml_desc", children, Checkpoint)
134
+ )
135
+
136
+
137
+ def delete_domain_checkpoint(
138
+ domain: DomainRef, checkpoint: CheckpointRef, *, flags: int = 0
139
+ ) -> Operation[None]:
140
+ """Delete a checkpoint.
141
+
142
+ :param domain: the domain.
143
+ :param checkpoint: the checkpoint.
144
+ :param flags: ``virDomainCheckpointDeleteFlags``: children, metadata only...
145
+ """
146
+ yield from on_checkpoint(
147
+ domain, checkpoint, "domain_checkpoint_delete", flags=flags
148
+ )
149
+
150
+
151
+ def begin_domain_backup(
152
+ domain: DomainRef,
153
+ backup: Backup | str,
154
+ checkpoint: Checkpoint | str | None = None,
155
+ *,
156
+ flags: int = 0,
157
+ ) -> Operation[None]:
158
+ """Start a backup job of a running domain.
159
+
160
+ The job runs until it is aborted with ``abort_domain_backup``.
161
+
162
+ :param domain: the domain.
163
+ :param backup: the backup description (push or pull mode), as a model or XML.
164
+ :param checkpoint: a checkpoint to create with the backup, or ``None``.
165
+ :param flags: ``virDomainBackupBeginFlags``: reuse external.
166
+ """
167
+ checkpoint_xml = None if checkpoint is None else as_xml(checkpoint)
168
+ yield from on_domain(
169
+ domain,
170
+ "domain_backup_begin",
171
+ backup_xml=as_xml(backup),
172
+ checkpoint_xml=checkpoint_xml,
173
+ flags=flags,
174
+ )
175
+
176
+
177
+ def get_domain_backup(domain: DomainRef, *, flags: int = 0) -> Operation[Backup]:
178
+ """Return the description of the running backup job of a domain.
179
+
180
+ :param domain: the domain.
181
+ :param flags: reserved, 0.
182
+ :returns: the backup as the daemon runs it.
183
+ """
184
+ xml = as_text(
185
+ (yield from on_domain(domain, "domain_backup_get_xml_desc", flags=flags))
186
+ )
187
+ return Backup.from_xml(xml)
188
+
189
+
190
+ def abort_domain_backup(domain: DomainRef, *, flags: int = 0) -> Operation[None]:
191
+ """Cancel the running backup job of a domain.
192
+
193
+ :param domain: the domain.
194
+ :param flags: ``virDomainAbortJobFlagsValues``.
195
+ """
196
+ yield from on_domain(domain, "domain_abort_job_flags", flags=flags)