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,201 @@
1
+ """Operations that open a data stream: console, screenshot, volume upload/download.
2
+
3
+ They return an open stream (``RemoteStream`` in the blocking client,
4
+ ``AsyncRemoteStream`` in the async one) that you read or write and then close.
5
+ """
6
+
7
+ from typing import cast
8
+
9
+ from libvirtpy.client.operations._common import one_handle
10
+ from libvirtpy.client.references import (
11
+ DomainRef,
12
+ PoolRef,
13
+ VolumeRef,
14
+ domain_lookup,
15
+ pool_lookup,
16
+ volume_lookup,
17
+ )
18
+ from libvirtpy.client.transport import Operation, Request
19
+ from libvirtpy.rpc.stream import AsyncRemoteStream, Mode, RemoteStream
20
+
21
+ __all__ = [
22
+ "download_pool_volume",
23
+ "open_domain_channel",
24
+ "open_domain_console",
25
+ "take_domain_screenshot",
26
+ "upload_pool_volume",
27
+ ]
28
+
29
+
30
+ def open_domain_console(
31
+ domain: DomainRef, device: str | None = None, *, flags: int = 0
32
+ ) -> Operation[RemoteStream]:
33
+ """Open the text console of a domain, to read its output and type into it.
34
+
35
+ :param domain: the domain; it must be running.
36
+ :param device: the console or serial device alias (``serial0``); ``None`` for
37
+ the first one.
38
+ :param flags: ``virDomainConsoleFlags``: 1 force (take it from another client),
39
+ 2 safe.
40
+ :returns: a stream you read and write; close it to disconnect.
41
+ """
42
+ return _stream(
43
+ (
44
+ yield from _open(
45
+ domain, "domain_open_console", "both", dev_name=device, flags=flags
46
+ )
47
+ )
48
+ )
49
+
50
+
51
+ def open_domain_channel(
52
+ domain: DomainRef, name: str | None = None, *, flags: int = 0
53
+ ) -> Operation[RemoteStream]:
54
+ """Open a named channel to the guest (for example the guest agent channel).
55
+
56
+ :param domain: the domain; it must be running.
57
+ :param name: the channel name; ``None`` for the first one.
58
+ :param flags: ``virDomainChannelFlags``: 1 force.
59
+ :returns: a stream you read and write; close it to disconnect.
60
+ """
61
+ return _stream(
62
+ (
63
+ yield from _open(
64
+ domain, "domain_open_channel", "both", name=name, flags=flags
65
+ )
66
+ )
67
+ )
68
+
69
+
70
+ def take_domain_screenshot(
71
+ domain: DomainRef, screen: int = 0, *, flags: int = 0
72
+ ) -> Operation[RemoteStream]:
73
+ """Take a screenshot of a running domain.
74
+
75
+ :param domain: the domain.
76
+ :param screen: which screen, for a domain with several.
77
+ :param flags: reserved, 0.
78
+ :returns: a stream to read the image from; its ``reply`` is the MIME type
79
+ (e.g. ``image/x-portable-pixmap``, or ``image/png``).
80
+ """
81
+ return _stream(
82
+ (
83
+ yield from _open(
84
+ domain, "domain_screenshot", "read", screen=screen, flags=flags
85
+ )
86
+ )
87
+ )
88
+
89
+
90
+ def download_pool_volume(
91
+ pool: PoolRef,
92
+ volume: VolumeRef,
93
+ offset: int = 0,
94
+ length: int = 0,
95
+ *,
96
+ flags: int = 0,
97
+ ) -> Operation[RemoteStream]:
98
+ """Read the contents of a volume.
99
+
100
+ :param pool: the pool.
101
+ :param volume: the volume.
102
+ :param offset: where to start, in bytes.
103
+ :param length: how many bytes; 0 for everything from ``offset``.
104
+ :param flags: ``virStorageVolDownloadFlags``.
105
+ :returns: a stream to read the bytes from.
106
+ """
107
+ reply = yield from _on_volume(
108
+ pool, volume, "storage_vol_download", "read", offset, length, flags
109
+ )
110
+ return _stream(reply)
111
+
112
+
113
+ def upload_pool_volume(
114
+ pool: PoolRef,
115
+ volume: VolumeRef,
116
+ offset: int = 0,
117
+ length: int = 0,
118
+ *,
119
+ flags: int = 0,
120
+ ) -> Operation[RemoteStream]:
121
+ """Write bytes into a volume.
122
+
123
+ :param pool: the pool.
124
+ :param volume: the volume; it must already exist and be big enough.
125
+ :param offset: where to start, in bytes.
126
+ :param length: how many bytes you will write; 0 for the rest of the volume.
127
+ :param flags: ``virStorageVolUploadFlags``.
128
+ :returns: a stream to write the bytes to; close it to finish the upload.
129
+ """
130
+ reply = yield from _on_volume(
131
+ pool, volume, "storage_vol_upload", "write", offset, length, flags
132
+ )
133
+ return _stream(reply)
134
+
135
+
136
+ def _open(
137
+ domain: DomainRef, procedure: str, mode: Mode, **arguments: object
138
+ ) -> Operation[object]:
139
+ """Call a procedure of a domain that opens a stream.
140
+
141
+ :param domain: the domain.
142
+ :param procedure: the procedure.
143
+ :param mode: which way the data flows: ``read``, ``write`` or ``both``.
144
+ :param arguments: the arguments of the procedure other than the domain.
145
+ :returns: the open stream.
146
+ """
147
+ handle = yield from _handle_of(domain)
148
+ request = Request(procedure, {"dom": handle, **arguments}, stream=mode)
149
+ return (yield request)
150
+
151
+
152
+ def _on_volume(
153
+ pool: PoolRef,
154
+ volume: VolumeRef,
155
+ procedure: str,
156
+ mode: Mode,
157
+ offset: int,
158
+ length: int,
159
+ flags: int,
160
+ ) -> Operation[object]:
161
+ """Call a procedure of a volume that opens a stream.
162
+
163
+ :param pool: the pool.
164
+ :param volume: the volume.
165
+ :param procedure: the procedure.
166
+ :param mode: which way the data flows.
167
+ :param offset: where to start, in bytes.
168
+ :param length: how many bytes.
169
+ :param flags: the flags of the call.
170
+ :returns: the open stream.
171
+ """
172
+ pool_handle = yield from one_handle(pool_lookup(pool))
173
+ volume_handle = yield from one_handle(volume_lookup(pool_handle, volume))
174
+ arguments = {
175
+ "vol": volume_handle,
176
+ "offset": offset,
177
+ "length": length,
178
+ "flags": flags,
179
+ }
180
+ return (yield Request(procedure, arguments, stream=mode))
181
+
182
+
183
+ def _handle_of(domain: DomainRef) -> Operation[object]:
184
+ """Find a domain.
185
+
186
+ :param domain: the domain.
187
+ :returns: its handle.
188
+ """
189
+ return (yield from one_handle(domain_lookup(domain)))
190
+
191
+
192
+ def _stream(reply: object) -> RemoteStream:
193
+ """Check that a reply is an open stream.
194
+
195
+ :param reply: what the transport returned.
196
+ :returns: the stream (an ``AsyncRemoteStream`` for the async client).
197
+ :raises TypeError: if it is not a stream.
198
+ """
199
+ if not isinstance(reply, RemoteStream | AsyncRemoteStream):
200
+ raise TypeError(f"expected a stream, got {reply!r}")
201
+ return cast(RemoteStream, reply)
@@ -0,0 +1,249 @@
1
+ """References: the ways a caller can name an object.
2
+
3
+ ``client.lookup_domain(x)`` and ``client.start_domain(x)`` accept any
4
+ ``DomainRef``: a runtime id (``int``), a name or UUID text (``str``), a
5
+ ``uuid.UUID``, or a ``libvirtpy.xml.Domain`` model (resolved by its UUID, else its
6
+ name). Text that looks like a UUID is looked up by UUID, anything else by name.
7
+ """
8
+
9
+ import re
10
+ from uuid import UUID
11
+
12
+ from libvirtpy.client.transport import Handle, Request
13
+ from libvirtpy.xml.backup import Checkpoint
14
+ from libvirtpy.xml.domain import Domain
15
+ from libvirtpy.xml.interface import HostInterface
16
+ from libvirtpy.xml.network import Network, NetworkPort
17
+ from libvirtpy.xml.nodedev import Device
18
+ from libvirtpy.xml.nwfilter import Filter, FilterBinding
19
+ from libvirtpy.xml.secret import Secret
20
+ from libvirtpy.xml.snapshot import Snapshot
21
+ from libvirtpy.xml.storage import Pool, Volume
22
+
23
+ __all__ = [
24
+ "CheckpointRef",
25
+ "DomainRef",
26
+ "HostInterfaceRef",
27
+ "NetworkPortRef",
28
+ "NetworkRef",
29
+ "NodeDeviceRef",
30
+ "NwFilterBindingRef",
31
+ "NwFilterRef",
32
+ "PoolRef",
33
+ "SecretRef",
34
+ "SnapshotRef",
35
+ "VolumeRef",
36
+ "domain_lookup",
37
+ "host_interface_lookup",
38
+ "network_lookup",
39
+ "node_device_lookup",
40
+ "nwfilter_lookup",
41
+ "pool_lookup",
42
+ "secret_lookup",
43
+ "snapshot_lookup",
44
+ "volume_lookup",
45
+ ]
46
+
47
+ type DomainRef = int | str | UUID | Domain
48
+ type NetworkRef = str | UUID | Network
49
+ type PoolRef = str | UUID | Pool
50
+ type VolumeRef = str | Volume
51
+ type SecretRef = str | UUID | Secret
52
+ type NwFilterRef = str | UUID | Filter
53
+ type HostInterfaceRef = str | HostInterface
54
+ type NodeDeviceRef = str | Device
55
+ type SnapshotRef = str | Snapshot
56
+ type CheckpointRef = str | Checkpoint
57
+ type NetworkPortRef = str | UUID | NetworkPort
58
+ type NwFilterBindingRef = str | FilterBinding
59
+
60
+ _UUID_TEXT = re.compile(
61
+ r"[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}"
62
+ )
63
+
64
+
65
+ def domain_lookup(reference: DomainRef) -> Request:
66
+ """Build the request that finds a domain.
67
+
68
+ :param reference: a runtime id, name, UUID or ``Domain`` model.
69
+ :returns: the lookup request.
70
+ """
71
+ if isinstance(reference, int) and not isinstance(reference, bool):
72
+ return Request("domain_lookup_by_id", {"id": reference})
73
+ return _lookup_by_name_or_uuid("domain", reference)
74
+
75
+
76
+ def network_lookup(reference: NetworkRef) -> Request:
77
+ """Build the request that finds a network.
78
+
79
+ :param reference: a name, UUID or ``Network`` model.
80
+ :returns: the lookup request.
81
+ """
82
+ return _lookup_by_name_or_uuid("network", reference)
83
+
84
+
85
+ def pool_lookup(reference: PoolRef) -> Request:
86
+ """Build the request that finds a storage pool.
87
+
88
+ :param reference: a name, UUID or ``Pool`` model.
89
+ :returns: the lookup request.
90
+ """
91
+ return _lookup_by_name_or_uuid("storage_pool", reference)
92
+
93
+
94
+ def nwfilter_lookup(reference: NwFilterRef) -> Request:
95
+ """Build the request that finds a network filter.
96
+
97
+ :param reference: a name, UUID or ``Filter`` model.
98
+ :returns: the lookup request.
99
+ """
100
+ return _lookup_by_name_or_uuid("nwfilter", reference)
101
+
102
+
103
+ def secret_lookup(reference: SecretRef) -> Request:
104
+ """Build the request that finds a secret (by UUID).
105
+
106
+ :param reference: a UUID (as text or ``UUID``) or a ``Secret`` model.
107
+ :returns: the lookup request.
108
+ :raises TypeError: if the reference has no UUID.
109
+ """
110
+ uuid_text = _uuid_text(reference)
111
+ if uuid_text is None:
112
+ raise TypeError(f"a secret is found by UUID, not by {reference!r}")
113
+ return Request("secret_lookup_by_uuid", {"uuid": uuid_text})
114
+
115
+
116
+ def host_interface_lookup(reference: HostInterfaceRef) -> Request:
117
+ """Build the request that finds a host network interface.
118
+
119
+ :param reference: a device name or MAC address, or a ``HostInterface`` model.
120
+ :returns: the lookup request.
121
+ """
122
+ if isinstance(reference, HostInterface):
123
+ return Request("interface_lookup_by_name", {"name": reference.name})
124
+ if re.fullmatch(r"([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}", reference):
125
+ return Request("interface_lookup_by_mac_string", {"mac": reference})
126
+ return Request("interface_lookup_by_name", {"name": reference})
127
+
128
+
129
+ def node_device_lookup(reference: NodeDeviceRef) -> Request:
130
+ """Build the request that finds a host device.
131
+
132
+ :param reference: the device name (``pci_0000_00_1f_2``) or a ``Device`` model.
133
+ :returns: the lookup request.
134
+ """
135
+ name = reference.name if isinstance(reference, Device) else reference
136
+ return Request("node_device_lookup_by_name", {"name": name})
137
+
138
+
139
+ def volume_lookup(pool: Handle, reference: VolumeRef) -> Request:
140
+ """Build the request that finds a volume of a pool.
141
+
142
+ :param pool: the pool that holds the volume.
143
+ :param reference: a name, a key or an absolute path, or a ``Volume`` model.
144
+ :returns: the lookup request.
145
+ """
146
+ if isinstance(reference, Volume):
147
+ if reference.key is not None:
148
+ return Request("storage_vol_lookup_by_key", {"key": reference.key})
149
+ return Request(
150
+ "storage_vol_lookup_by_name", {"pool": pool, "name": reference.name}
151
+ )
152
+ if reference.startswith("/"):
153
+ return Request("storage_vol_lookup_by_path", {"path": reference})
154
+ return Request("storage_vol_lookup_by_name", {"pool": pool, "name": reference})
155
+
156
+
157
+ def snapshot_lookup(domain: Handle, reference: SnapshotRef) -> Request:
158
+ """Build the request that finds a snapshot of a domain.
159
+
160
+ :param domain: the domain that has the snapshot.
161
+ :param reference: the snapshot name, or a ``Snapshot`` model.
162
+ :returns: the lookup request.
163
+ :raises TypeError: if a model has no name.
164
+ """
165
+ name = reference.name if isinstance(reference, Snapshot) else reference
166
+ if name is None:
167
+ raise TypeError("a snapshot is found by name; the model has none")
168
+ return Request(
169
+ "domain_snapshot_lookup_by_name",
170
+ {"dom": domain, "name": name, "flags": 0},
171
+ )
172
+
173
+
174
+ def checkpoint_lookup(domain: Handle, reference: CheckpointRef) -> Request:
175
+ """Build the request that finds a checkpoint of a domain.
176
+
177
+ :param domain: the domain that has the checkpoint.
178
+ :param reference: the checkpoint name, or a ``Checkpoint`` model.
179
+ :returns: the lookup request.
180
+ :raises TypeError: if a model has no name.
181
+ """
182
+ name = reference.name if isinstance(reference, Checkpoint) else reference
183
+ if name is None:
184
+ raise TypeError("a checkpoint is found by name; the model has none")
185
+ return Request(
186
+ "domain_checkpoint_lookup_by_name",
187
+ {"dom": domain, "name": name, "flags": 0},
188
+ )
189
+
190
+
191
+ def network_port_lookup(network: Handle, reference: NetworkPortRef) -> Request:
192
+ """Build the request that finds a port of a network.
193
+
194
+ :param network: the network that has the port.
195
+ :param reference: the port's UUID (text or ``UUID``), or a ``NetworkPort`` model.
196
+ :returns: the lookup request.
197
+ :raises TypeError: if the reference has no UUID.
198
+ """
199
+ uuid_text = _uuid_text(reference)
200
+ if uuid_text is None:
201
+ raise TypeError("a network port is found by UUID")
202
+ return Request(
203
+ "network_port_lookup_by_uuid", {"network": network, "uuid": uuid_text}
204
+ )
205
+
206
+
207
+ def nwfilter_binding_lookup(reference: NwFilterBindingRef) -> Request:
208
+ """Build the request that finds a network filter binding.
209
+
210
+ :param reference: the name of the port device (``vnet0``) or a ``FilterBinding``.
211
+ :returns: the lookup request.
212
+ :raises TypeError: if a model has no port device.
213
+ """
214
+ if isinstance(reference, FilterBinding):
215
+ if reference.portdev is None:
216
+ raise TypeError("a binding is found by port device; the model has none")
217
+ reference = reference.portdev.name
218
+ return Request("nwfilter_binding_lookup_by_port_dev", {"name": reference})
219
+
220
+
221
+ def _lookup_by_name_or_uuid(prefix: str, reference: object) -> Request:
222
+ """Build a ``<prefix>_lookup_by_uuid`` or ``<prefix>_lookup_by_name`` request.
223
+
224
+ :param prefix: the object kind in the procedure name, e.g. ``"domain"``.
225
+ :param reference: a UUID, a name or a model.
226
+ :returns: the lookup request.
227
+ :raises TypeError: if the reference has neither a UUID nor a name.
228
+ """
229
+ uuid_text = _uuid_text(reference)
230
+ if uuid_text is not None:
231
+ return Request(f"{prefix}_lookup_by_uuid", {"uuid": uuid_text})
232
+ name = reference if isinstance(reference, str) else getattr(reference, "name", None)
233
+ if not isinstance(name, str):
234
+ raise TypeError(f"cannot find a {prefix} by {reference!r}")
235
+ return Request(f"{prefix}_lookup_by_name", {"name": name})
236
+
237
+
238
+ def _uuid_text(reference: object) -> str | None:
239
+ """Return the UUID a reference stands for, if it has one.
240
+
241
+ :param reference: a ``UUID``, text, or a model.
242
+ :returns: the UUID as text, or ``None`` if the reference is a name.
243
+ """
244
+ if isinstance(reference, UUID):
245
+ return str(reference)
246
+ if isinstance(reference, str) and _UUID_TEXT.fullmatch(reference):
247
+ return reference
248
+ model_uuid = getattr(reference, "uuid", None)
249
+ return model_uuid if isinstance(model_uuid, str) else None
@@ -0,0 +1,88 @@
1
+ """Planning an event subscription: which procedures, which arguments."""
2
+
3
+ from collections.abc import Callable, Iterable, Iterator
4
+ from dataclasses import dataclass
5
+ from enum import IntEnum
6
+ from typing import Any
7
+
8
+ from libvirtpy.client.references import (
9
+ domain_lookup,
10
+ network_lookup,
11
+ node_device_lookup,
12
+ pool_lookup,
13
+ secret_lookup,
14
+ )
15
+ from libvirtpy.client.transport import Handle, Request
16
+ from libvirtpy.rpc.events import SOURCES
17
+ from libvirtpy.rpc.procedure import Procedure
18
+ from libvirtpy.rpc.protocol.procedures import PROCEDURES
19
+
20
+ __all__ = ["SubscriptionPlan", "event_lookup", "plan_subscription"]
21
+
22
+ PROCEDURES_BY_NAME = {procedure.name: procedure for procedure in PROCEDURES.values()}
23
+ LOOKUPS: dict[str, Callable[[Any], Request]] = {
24
+ "domain": domain_lookup,
25
+ "network": network_lookup,
26
+ "pool": pool_lookup,
27
+ "node_device": node_device_lookup,
28
+ "secret": secret_lookup,
29
+ }
30
+
31
+
32
+ @dataclass(frozen=True)
33
+ class SubscriptionPlan:
34
+ """What to send to the daemon to start a subscription."""
35
+
36
+ register: Procedure[Any, Any]
37
+ deregister: Procedure[Any, Any]
38
+ kinds: list[IntEnum]
39
+ object_argument: str
40
+
41
+ def arguments(self, handle: object) -> Iterator[dict[str, object]]:
42
+ """Make the arguments of each registration.
43
+
44
+ :param handle: the ``Handle`` of the object to limit the events to, or
45
+ ``None`` for all.
46
+ :returns: one argument dict per event kind.
47
+ :raises TypeError: if ``handle`` is neither a handle nor ``None``.
48
+ """
49
+ if handle is not None and not isinstance(handle, Handle):
50
+ raise TypeError(f"expected a Handle, got {handle!r}")
51
+
52
+ for kind in self.kinds:
53
+ yield {"event_id": int(kind), self.object_argument: handle}
54
+
55
+
56
+ def plan_subscription(source: str, kinds: Iterable[IntEnum] | None) -> SubscriptionPlan:
57
+ """Work out the registrations for a source and some event kinds.
58
+
59
+ :param source: ``domain``, ``network``, ``pool``, ``node_device`` or ``secret``.
60
+ :param kinds: members of the source's kind enum; ``None`` for ``LIFECYCLE``.
61
+ :returns: the plan.
62
+ :raises ValueError: if the source or a kind is unknown.
63
+ """
64
+ if source not in SOURCES:
65
+ raise ValueError(f"unknown event source {source!r}; use one of {list(SOURCES)}")
66
+
67
+ description = SOURCES[source]
68
+ chosen = list(kinds) if kinds is not None else [description.kinds(0)]
69
+ for kind in chosen:
70
+ if not isinstance(kind, description.kinds):
71
+ raise ValueError(f"{kind!r} is not an event kind of {source!r}")
72
+
73
+ return SubscriptionPlan(
74
+ register=PROCEDURES_BY_NAME[description.register],
75
+ deregister=PROCEDURES_BY_NAME[description.deregister],
76
+ kinds=chosen,
77
+ object_argument=description.object_argument,
78
+ )
79
+
80
+
81
+ def event_lookup(source: str, target: object) -> Request:
82
+ """Make the request that finds the object an event subscription is limited to.
83
+
84
+ :param source: the event source.
85
+ :param target: a reference of that kind of object (a name, UUID or model).
86
+ :returns: the lookup request.
87
+ """
88
+ return LOOKUPS[source](target)
@@ -0,0 +1,24 @@
1
+ """An in-memory libvirt daemon, for examples and tests (a test double).
2
+
3
+ This package follows the exemptions of tests: ``tests/tools`` does not check its
4
+ docstrings or its layout.
5
+
6
+ ``FakeLibvirt`` answers the same ``Request``s a real daemon would and keeps its
7
+ objects as the real XML models, so everything the clients return is what a
8
+ user would get: UUIDs are filled in, a started domain gets an ``id``, a pool
9
+ gets a target path, a volume gets a key.
10
+
11
+ It serves only the first operations of the client (domains, networks, pools,
12
+ volumes, snapshots, secrets, filters, interfaces, devices); the others answer
13
+ with ``NotSupportedError``. It is a test aid, not a simulator: it knows the
14
+ state rules that make the examples meaningful (you cannot start a running
15
+ domain) and nothing about a real hypervisor.
16
+
17
+ One module per area: ``_state`` (the objects and what all kinds share),
18
+ ``_domains``, ``_storage``, ``_objects``; ``fake`` puts them together.
19
+ """
20
+
21
+ from libvirtpy.client.testing.async_transport import AsyncFakeTransport
22
+ from libvirtpy.client.testing.fake import FakeLibvirt
23
+
24
+ __all__ = ["AsyncFakeTransport", "FakeLibvirt"]