[libvirt] [PATCH] New APIs for checking some object properties

Introduce a number of new APIs to expose some boolean properties of objects, which cannot otherwise reliably determined, nor are aspects of the XML configuration. * virDomainIsActive: Checking virDomainGetID is not reliable since it is not possible to distinguish between error condition and inactive domain for ID of -1. * virDomainIsPersistent: Check whether a persistent config exists for the domain * virNetworkIsActive: Check whether the network is active * virNetworkIsPersistent: Check whether a persistent config exists for the network * virStoragePoolIsActive: Check whether the storage pool is active * virStoragePoolIsPersistent: Check whether a persistent config exists for the storage pool * virInterfaceIsActive: Check whether the host interface is active * virConnectIsSecure: whether the communication channel to the hypervisor is secure * virConnectIsEncrypted: whether any network based commnunication channels are encrypted NB, a channel can be secure, even if not encrypted, eg if it does not involve the network, like a UNIX socket, or pipe. --- include/libvirt/libvirt.h.in | 16 +++ src/libvirt.c | 298 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+), 0 deletions(-) diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index 6186d4e..05a164b 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -1649,6 +1649,22 @@ int virStreamAbort(virStreamPtr st); int virStreamFree(virStreamPtr st); +int virDomainIsActive(virDomainPtr dom); +int virDomainIsPersistent(virDomainPtr dom); + +int virNetworkIsActive(virNetworkPtr net); +int virNetworkIsPersistent(virNetworkPtr net); + +int virStoragePoolIsActive(virStoragePoolPtr pool); +int virStoragePoolIsPersistent(virStoragePoolPtr pool); + +int virInterfaceIsActive(virInterfacePtr iface); + +int virConnectIsEncrypted(virConnectPtr conn); +int virConnectIsSecure(virConnectPtr conn); + + + #ifdef __cplusplus } #endif diff --git a/src/libvirt.c b/src/libvirt.c index 9e87900..58ea514 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -10321,3 +10321,301 @@ int virStreamFree(virStreamPtr stream) return (-1); return (0); } + + +/** + * virDomainIsActive: + * + * Determine if the domain is currently running + * + * Returns > 0 if running, 0 if inactive, -1 on error + */ +int virDomainIsActive(virDomainPtr dom) +{ + DEBUG("dom=%p", dom); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_DOMAIN(dom)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (dom->conn->driver->domainIsActive) { + int ret; + ret = dom->conn->driver->domainIsActive(dom); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(dom->conn); + return -1; +} + +/** + * virDomainIsPersistent: + * + * Determine if the domain has a persistent configuration + * + * Returns > 0 if persistent, 0 if transient, -1 on error + */ +int virDomainIsPersistent(virDomainPtr dom) +{ + DEBUG("dom=%p", dom); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_DOMAIN(dom)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (dom->conn->driver->domainIsPersistent) { + int ret; + ret = dom->conn->driver->domainIsPersistent(dom); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(dom->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(dom->conn); + return -1; +} + +/** + * virNetworkIsActive: + * + * Determine if the network is currently running + * + * Returns > 0 if running, 0 if inactive, -1 on error + */ +int virNetworkIsActive(virNetworkPtr net) +{ + DEBUG("net=%p", net); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_NETWORK(net)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (net->conn->networkDriver->networkIsActive) { + int ret; + ret = net->conn->networkDriver->networkIsActive(net); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(net->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(net->conn); + return -1; +} + + +/** + * virNetworkIsPersistent: + * + * Determine if the network has a persistent configuration + * + * Returns > 0 if persistent, 0 if transient, -1 on error + */ +int virNetworkIsPersistent(virNetworkPtr net) +{ + DEBUG("net=%p", net); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_NETWORK(net)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (net->conn->networkDriver->networkIsPersistent) { + int ret; + ret = net->conn->networkDriver->networkIsPersistent(net); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(net->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(net->conn); + return -1; +} + + +/** + * virStoragepoolIsActive: + * + * Determine if the storagepool is currently running + * + * Returns > 0 if running, 0 if inactive, -1 on error + */ +int virStoragePoolIsActive(virStoragePoolPtr pool) +{ + DEBUG("pool=%p", pool); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (pool->conn->storageDriver->poolIsActive) { + int ret; + ret = pool->conn->storageDriver->poolIsActive(pool); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(pool->conn); + return -1; +} + + +/** + * virStoragepoolIsPersistent: + * + * Determine if the storagepool has a persistent configuration + * + * Returns > 0 if persistent, 0 if transient, -1 on error + */ +int virStoragePoolIsPersistent(virStoragePoolPtr pool) +{ + DEBUG("pool=%p", pool); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_STORAGE_POOL(pool)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (pool->conn->storageDriver->poolIsPersistent) { + int ret; + ret = pool->conn->storageDriver->poolIsPersistent(pool); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(pool->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(pool->conn); + return -1; +} + + +/** + * virInterfaceIsActive: + * + * Determine if the interface is currently running + * + * Returns > 0 if running, 0 if inactive, -1 on error + */ +int virInterfaceIsActive(virInterfacePtr iface) +{ + DEBUG("iface=%p", iface); + + virResetLastError(); + + if (!VIR_IS_CONNECTED_INTERFACE(iface)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (iface->conn->interfaceDriver->interfaceIsActive) { + int ret; + ret = iface->conn->interfaceDriver->interfaceIsActive(iface); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(iface->conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(iface->conn); + return -1; +} + + +/** + * virConnectIsEncrypted: + * + * Determine if the connection to the hypervisor is encrypted + * + * Returns > 0 if running, 0 if inactive, -1 on error + */ +int virConnectIsEncrypted(virConnectPtr conn) +{ + DEBUG("conn=%p", conn); + + virResetLastError(); + + if (!VIR_IS_CONNECT(conn)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (conn->driver->isEncrypted) { + int ret; + ret = conn->driver->isEncrypted(conn); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(conn); + return -1; +} + +/** + * virConnectIsSecure: + * + * Determine if the connection to the hypervisor is secure + * + * A connection will be classed as secure if it is either + * encrypted, or running over a channel which is not exposed + * to eavesdropping (eg a UNIX domain socket, or pipe) + * + * Returns > 0 if running, 0 if inactive, -1 on error + */ +int virConnectIsSecure(virConnectPtr conn) +{ + DEBUG("conn=%p", conn); + + virResetLastError(); + + if (!VIR_IS_CONNECT(conn)) { + virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__); + return (-1); + } + if (conn->driver->isSecure) { + int ret; + ret = conn->driver->isSecure(conn); + if (ret < 0) + goto error; + return ret; + } + + virLibConnError(conn, VIR_ERR_NO_SUPPORT, __FUNCTION__); +error: + /* Copy to connection error object for back compatability */ + virSetConnError(conn); + return -1; +} -- 1.6.2.5

On Wed, Oct 21, 2009 at 11:54:14AM +0100, Daniel P. Berrange wrote:
Introduce a number of new APIs to expose some boolean properties of objects, which cannot otherwise reliably determined, nor are aspects of the XML configuration.
* virDomainIsActive: Checking virDomainGetID is not reliable since it is not possible to distinguish between error condition and inactive domain for ID of -1. * virDomainIsPersistent: Check whether a persistent config exists for the domain
* virNetworkIsActive: Check whether the network is active * virNetworkIsPersistent: Check whether a persistent config exists for the network
* virStoragePoolIsActive: Check whether the storage pool is active * virStoragePoolIsPersistent: Check whether a persistent config exists for the storage pool
* virInterfaceIsActive: Check whether the host interface is active
* virConnectIsSecure: whether the communication channel to the hypervisor is secure * virConnectIsEncrypted: whether any network based commnunication channels are encrypted
NB, a channel can be secure, even if not encrypted, eg if it does not involve the network, like a UNIX socket, or pipe.
Okay, this makes sense !
include/libvirt/libvirt.h.in | 16 +++ src/libvirt.c | 298 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+), 0 deletions(-)
diff --git a/include/libvirt/libvirt.h.in b/include/libvirt/libvirt.h.in index 6186d4e..05a164b 100644 --- a/include/libvirt/libvirt.h.in +++ b/include/libvirt/libvirt.h.in @@ -1649,6 +1649,22 @@ int virStreamAbort(virStreamPtr st); int virStreamFree(virStreamPtr st);
+int virDomainIsActive(virDomainPtr dom); +int virDomainIsPersistent(virDomainPtr dom); + +int virNetworkIsActive(virNetworkPtr net); +int virNetworkIsPersistent(virNetworkPtr net); + +int virStoragePoolIsActive(virStoragePoolPtr pool); +int virStoragePoolIsPersistent(virStoragePoolPtr pool); + +int virInterfaceIsActive(virInterfacePtr iface); + +int virConnectIsEncrypted(virConnectPtr conn); +int virConnectIsSecure(virConnectPtr conn);
The only small remark I would have is that it's not consistent with int virDomainGetAutostart (virDomainPtr domain, int *autostart); which is also looking at a boolean value. But since we don't expect to add a way to modify those properties (i.e. no Set operation) I think it's fine to go with the simpler signature you suggest. [...]
+/** + * virDomainIsPersistent: + * + * Determine if the domain has a persistent configuration
maybe emphasise a bit on the definition and try to hook up to the 'Defined' terminology used in the API for example: * which means it will still be defined after being terminated
+/** + * virNetworkIsPersistent: + * + * Determine if the network has a persistent configuration
similar
+ * Returns > 0 if persistent, 0 if transient, -1 on error + */ C[...] +/** + * virStoragepoolIsActive:
typo virStoragePoolIsActive
+ * + * Determine if the storagepool is currently running
missing space * Determine if the storage pool is currently running
+ * + * Returns > 0 if running, 0 if inactive, -1 on error
+/** + * virStoragepoolIsPersistent:
StoragePool
+ * + * Determine if the storagepool has a persistent configuration
-> defined
+ * + * Returns > 0 if persistent, 0 if transient, -1 on error + */
With those small fixes in, ACK ! thanks ! Daniel -- Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/ daniel@veillard.com | Rpmfind RPM search engine http://rpmfind.net/ http://veillard.com/ | virtualization library http://libvirt.org/

On Wed, Oct 21, 2009 at 01:54:00PM +0200, Daniel Veillard wrote:
+int virInterfaceIsActive(virInterfacePtr iface); + +int virConnectIsEncrypted(virConnectPtr conn); +int virConnectIsSecure(virConnectPtr conn);
The only small remark I would have is that it's not consistent with int virDomainGetAutostart (virDomainPtr domain, int *autostart);
which is also looking at a boolean value. But since we don't expect to add a way to modify those properties (i.e. no Set operation) I think it's fine to go with the simpler signature you suggest.
I think the GetAutostart API is rather cumbersome to use as it is :-) The compelling reason for not doing it though, is that with the syntax I used in this patch, the python bindings are fully auto-generated. Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
participants (2)
-
Daniel P. Berrange
-
Daniel Veillard