[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] PATCH: Add a new error code for invalid operations



A number of operations are only valid when the corresponding object is in
a particular state. eg,  you can only suspend a domain, if it is currently
running. We don't have a very consistent way of reporting this problem 
across drivers, so this patch introduces a new error code for this situation
called  VIR_ERR_INVALID_OPERATION. This code is intended to be used if the
operation is supported, but not applicable for the object at this time.
It updates the Xen and QEMU drivers to use this error code.

Daniel

diff -r 2e97361b7242 include/libvirt/virterror.h
--- a/include/libvirt/virterror.h	Mon Apr 27 11:44:32 2009 +0100
+++ b/include/libvirt/virterror.h	Mon Apr 27 12:12:23 2009 +0100
@@ -157,6 +157,7 @@ typedef enum {
     VIR_ERR_INVALID_NODE_DEVICE,/* invalid node device object */
     VIR_ERR_NO_NODE_DEVICE,/* node device not found */
     VIR_ERR_NO_SECURITY_MODEL, /* security model not found */
+    VIR_ERR_OPERATION_INVALID, /* operation is not applicable at this time */
 } virErrorNumber;
 
 /**
diff -r 2e97361b7242 src/qemu_driver.c
--- a/src/qemu_driver.c	Mon Apr 27 11:44:32 2009 +0100
+++ b/src/qemu_driver.c	Mon Apr 27 12:12:23 2009 +0100
@@ -1325,7 +1325,7 @@ static int qemudStartVMDaemon(virConnect
     FD_ZERO(&keepfd);
 
     if (virDomainIsActive(vm)) {
-        qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR,
+        qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("VM is already active"));
         return -1;
     }
@@ -2214,7 +2214,7 @@ static int qemudDomainSuspend(virDomainP
         goto cleanup;
     }
     if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("domain is not running"));
         goto cleanup;
     }
@@ -2267,7 +2267,7 @@ static int qemudDomainResume(virDomainPt
         goto cleanup;
     }
     if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("domain is not running"));
         goto cleanup;
     }
@@ -2755,7 +2755,7 @@ static int qemudDomainSave(virDomainPtr 
     }
 
     if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("domain is not running"));
         goto cleanup;
     }
@@ -2890,7 +2890,7 @@ static int qemudDomainSetVcpus(virDomain
     }
 
     if (virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT, "%s",
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID, "%s",
                          _("cannot change vcpu count of an active domain"));
         goto cleanup;
     }
@@ -2942,8 +2942,16 @@ qemudDomainPinVcpu(virDomainPtr dom,
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
-    if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
+                         _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (!virDomainIsActive(vm)) {
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s",_("cannot pin vcpus on an inactive domain"));
         goto cleanup;
     }
@@ -3003,8 +3011,16 @@ qemudDomainGetVcpus(virDomainPtr dom,
     vm = virDomainFindByUUID(&driver->domains, dom->uuid);
     qemuDriverUnlock(driver);
 
-    if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INVALID_ARG,
+    if (!vm) {
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
+        virUUIDFormat(dom->uuid, uuidstr);
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_DOMAIN,
+                         _("no domain with matching uuid '%s'"), uuidstr);
+        goto cleanup;
+    }
+
+    if (!virDomainIsActive(vm)) {
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s",_("cannot pin vcpus on an inactive domain"));
         goto cleanup;
     }
@@ -3248,7 +3264,7 @@ static int qemudDomainRestore(virConnect
         vm = virDomainFindByName(&driver->domains, def->name);
     if (vm) {
         if (virDomainIsActive(vm)) {
-            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
+            qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
                              _("domain is already active as '%s'"), vm->def->name);
             goto cleanup;
         } else {
@@ -3503,7 +3519,7 @@ static int qemudDomainUndefine(virDomain
     }
 
     if (virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("cannot delete active domain"));
         goto cleanup;
     }
@@ -3900,7 +3916,7 @@ static int qemudDomainAttachDevice(virDo
 
     if (!virDomainIsActive(vm)) {
         qemuDriverUnlock(driver);
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("cannot attach device on inactive domain"));
         goto cleanup;
     }
@@ -4052,7 +4068,7 @@ static int qemudDomainDetachDevice(virDo
 
     if (!virDomainIsActive(vm)) {
         qemuDriverUnlock(driver);
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_INTERNAL_ERROR,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("cannot detach device on inactive domain"));
         goto cleanup;
     }
@@ -4212,7 +4228,7 @@ qemudDomainBlockStats (virDomainPtr dom,
         goto cleanup;
     }
     if (!virDomainIsActive (vm)) {
-        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                           "%s", _("domain is not running"));
         goto cleanup;
     }
@@ -4351,7 +4367,7 @@ qemudDomainInterfaceStats (virDomainPtr 
     }
 
     if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("domain is not running"));
         goto cleanup;
     }
@@ -4497,7 +4513,7 @@ qemudDomainMemoryPeek (virDomainPtr dom,
     }
 
     if (!virDomainIsActive(vm)) {
-        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                          "%s", _("domain is not running"));
         goto cleanup;
     }
@@ -4824,7 +4840,7 @@ qemudDomainMigratePerform (virDomainPtr 
     }
 
     if (!virDomainIsActive(vm)) {
-        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
+        qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_INVALID,
                           "%s", _("domain is not running"));
         goto cleanup;
     }
diff -r 2e97361b7242 src/virterror.c
--- a/src/virterror.c	Mon Apr 27 11:44:32 2009 +0100
+++ b/src/virterror.c	Mon Apr 27 12:12:23 2009 +0100
@@ -1018,6 +1018,12 @@ virErrorMsg(virErrorNumber error, const 
             else
                     errmsg = _("Security model not found: %s");
             break;
+        case VIR_ERR_OPERATION_INVALID:
+            if (info == NULL)
+                    errmsg = _("Requested operation is not valid");
+            else
+                    errmsg = _("Requested operation is not valid: %s");
+            break;
     }
     return (errmsg);
 }
diff -r 2e97361b7242 src/xen_internal.c
--- a/src/xen_internal.c	Mon Apr 27 11:44:32 2009 +0100
+++ b/src/xen_internal.c	Mon Apr 27 12:12:23 2009 +0100
@@ -1068,9 +1068,14 @@ xenHypervisorGetSchedulerType(virDomainP
     }
 
     priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
-    if (priv->handle < 0 || domain->id < 0) {
+    if (priv->handle < 0) {
         virXenErrorFunc(domain->conn, VIR_ERR_INTERNAL_ERROR, __FUNCTION__,
-                        "priv->handle or domain->id invalid", 0);
+                        "priv->handle invalid", 0);
+        return NULL;
+    }
+    if (domain->id < 0) {
+        virXenError(domain->conn, VIR_ERR_OPERATION_INVALID,
+                    "%s", _("domain is not running"));
         return NULL;
     }
 
@@ -1143,9 +1148,14 @@ xenHypervisorGetSchedulerParameters(virD
     }
 
     priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
-    if (priv->handle < 0 || domain->id < 0) {
+    if (priv->handle < 0) {
         virXenErrorFunc(domain->conn, VIR_ERR_INTERNAL_ERROR, __FUNCTION__,
-                        "priv->handle or domain->id invalid", 0);
+                        "priv->handle invalid", 0);
+        return -1;
+    }
+    if (domain->id < 0) {
+        virXenError(domain->conn, VIR_ERR_OPERATION_INVALID,
+                    "%s", _("domain is not running"));
         return -1;
     }
 
@@ -1241,9 +1251,14 @@ xenHypervisorSetSchedulerParameters(virD
     }
 
     priv = (xenUnifiedPrivatePtr) domain->conn->privateData;
-    if (priv->handle < 0 || domain->id < 0) {
-        virXenErrorFunc (domain->conn, VIR_ERR_INTERNAL_ERROR, __FUNCTION__,
-                         "priv->handle or domain->id invalid", 0);
+    if (priv->handle < 0) {
+        virXenErrorFunc(domain->conn, VIR_ERR_INTERNAL_ERROR, __FUNCTION__,
+                        "priv->handle invalid", 0);
+        return -1;
+    }
+    if (domain->id < 0) {
+        virXenError(domain->conn, VIR_ERR_OPERATION_INVALID,
+                    "%s", _("domain is not running"));
         return -1;
     }
 
diff -r 2e97361b7242 src/xend_internal.c
--- a/src/xend_internal.c	Mon Apr 27 11:44:32 2009 +0100
+++ b/src/xend_internal.c	Mon Apr 27 12:12:23 2009 +0100
@@ -2970,7 +2970,7 @@ xenDaemonDomainSuspend(virDomainPtr doma
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }
@@ -2997,7 +2997,7 @@ xenDaemonDomainResume(virDomainPtr domai
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }
@@ -3025,7 +3025,7 @@ xenDaemonDomainShutdown(virDomainPtr dom
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }
@@ -3054,7 +3054,7 @@ xenDaemonDomainReboot(virDomainPtr domai
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }
@@ -3085,7 +3085,7 @@ xenDaemonDomainDestroy(virDomainPtr doma
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }
@@ -3160,7 +3160,7 @@ xenDaemonDomainSave(virDomainPtr domain,
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }
@@ -3197,7 +3197,7 @@ xenDaemonDomainCoreDump(virDomainPtr dom
     }
 
     if (domain->id < 0) {
-        virXendError(domain->conn, VIR_ERR_INVALID_ARG,
+        virXendError(domain->conn, VIR_ERR_OPERATION_INVALID,
                      _("Domain %s isn't running."), domain->name);
         return(-1);
     }


-- 
|: 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 :|


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]