[Libvirt-cim] [PATCH 1 of 3] Device: rename dom_device to enum_devices and make it configurable

Heidi Eckhart heidieck at linux.vnet.ibm.com
Fri Mar 7 12:26:41 UTC 2008


# HG changeset patch
# User Heidi Eckhart <heidieck at linux.vnet.ibm.com>
# Date 1204892341 -3600
# Node ID 3afbff37cc3946a1f584fff572717f75aa8facc2
# Parent  d09896378f5dc5dfd70054a9c514682a23cd4fd3
Device: rename dom_device to enum_devices and make it configurable

- update resource type to CIM_RES_TYPE_foo

Signed-off-by: Heidi Eckhart <heidieck at linux.vnet.ibm.com>

diff -r d09896378f5d -r 3afbff37cc39 src/Virt_Device.c
--- a/src/Virt_Device.c	Fri Mar 07 13:19:00 2008 +0100
+++ b/src/Virt_Device.c	Fri Mar 07 13:19:01 2008 +0100
@@ -236,22 +236,22 @@ static CMPIInstance *device_instance(con
 {
         CMPIInstance *instance;
 
-        if (dev->type == VIRT_DEV_NET)
+        if (dev->type == CIM_RES_TYPE_NET)
                 instance = net_instance(broker,
                                         &dev->dev.net,
                                         dom,
                                         ns);
-        else if (dev->type == VIRT_DEV_DISK)
+        else if (dev->type == CIM_RES_TYPE_DISK)
                 instance = disk_instance(broker,
                                          &dev->dev.disk,
                                          dom,
                                          ns);
-        else if (dev->type == VIRT_DEV_MEM)
+        else if (dev->type == CIM_RES_TYPE_MEM)
                 instance = mem_instance(broker,
                                         &dev->dev.mem,
                                         dom,
                                         ns);
-        else if (dev->type == VIRT_DEV_VCPU)
+        else if (dev->type == CIM_RES_TYPE_PROC)
                 instance = vcpu_instance(broker,
                                          &dev->dev.vcpu,
                                          dom,
@@ -271,23 +271,24 @@ uint16_t device_type_from_classname(cons
 uint16_t device_type_from_classname(const char *classname)
 {
         if (strstr(classname, "NetworkPort"))
-                return VIRT_DEV_NET;
+                return CIM_RES_TYPE_NET;
         else if (strstr(classname, "LogicalDisk"))
-                return VIRT_DEV_DISK;
+                return CIM_RES_TYPE_DISK;
         else if (strstr(classname, "Memory"))
-                return VIRT_DEV_MEM;
+                return CIM_RES_TYPE_MEM;
         else if (strstr(classname, "Processor"))
-                return VIRT_DEV_VCPU;
+                return CIM_RES_TYPE_PROC;
         else
-                return VIRT_DEV_UNKNOWN;
-}
-
-int dom_devices(const CMPIBroker *broker,
-                virDomainPtr dom,
-                const char *ns,
-                int type,
-                struct inst_list *list)
-{
+                return CIM_RES_TYPE_UNKNOWN;
+}
+
+static CMPIStatus _get_devices(const CMPIBroker *broker,
+                               const CMPIObjectPath *reference,
+                               const virDomainPtr dom,
+                               const uint16_t type,
+                               struct inst_list *list)
+{
+        CMPIStatus s = {CMPI_RC_OK, NULL};
         int count;
         int i;
         struct virt_device *devs = NULL;
@@ -299,7 +300,10 @@ int dom_devices(const CMPIBroker *broker
         for (i = 0; i < count; i++) {
                 CMPIInstance *dev = NULL;
 
-                dev = device_instance(broker, &devs[i], dom, ns);
+                dev = device_instance(broker,
+                                      &devs[i],
+                                      dom,
+                                      NAMESPACE(reference));
                 if (dev)
                         inst_list_add(list, dev);
 
@@ -308,57 +312,106 @@ int dom_devices(const CMPIBroker *broker
 
  out:
         free(devs);
-
-        return 1;
-}
-
-static int dom_list_devices(virConnectPtr conn,
-                            const CMPIObjectPath *ref,
-                            struct inst_list *list)
-{
-        virDomainPtr *doms;
-        int ndom;
+        return s;
+}
+
+static CMPIStatus _enum_devices(const CMPIBroker *broker,
+                                const CMPIObjectPath *reference,
+                                const virDomainPtr dom,
+                                const uint16_t type,
+                                struct inst_list *list)
+{
+        CMPIStatus s;
+
+        if (type == CIM_RES_TYPE_ALL) {
+                s = _get_devices(broker,
+                                 reference,
+                                 dom,
+                                 CIM_RES_TYPE_PROC,
+                                 list);
+                s = _get_devices(broker,
+                                 reference,
+                                 dom,
+                                 CIM_RES_TYPE_MEM,
+                                 list);
+                s = _get_devices(broker,
+                                 reference,
+                                 dom,
+                                 CIM_RES_TYPE_NET,
+                                 list);
+                s = _get_devices(broker,
+                                 reference,
+                                 dom,
+                                 CIM_RES_TYPE_DISK,
+                                 list);
+        }
+        else
+                s = _get_devices(broker,
+                                 reference,
+                                 dom,
+                                 type,
+                                 list);
+
+        return s;
+}
+
+CMPIStatus enum_devices(const CMPIBroker *broker,
+                        const CMPIObjectPath *reference,
+                        const char *domain,
+                        const uint16_t type,
+                        struct inst_list *list)
+{
+        CMPIStatus s = {CMPI_RC_OK, NULL};
+        virConnectPtr conn = NULL;
+        virDomainPtr *doms = NULL;
+        int count = 1;
         int i;
-        int type;
-
-        type = device_type_from_classname(CLASSNAME(ref));
-
-        ndom = get_domain_list(conn, &doms);
-        if (ndom == 0)
-                return 1;
-        else if (ndom < 0)
-                return 0;
-
-        for (i = 0; i < ndom; i++) {
-                dom_devices(_BROKER, doms[i], NAMESPACE(ref), type, list);
-        }
-
-        return 1;
-}
-
-static CMPIStatus enum_devices(const CMPIObjectPath *reference,
-                               const CMPIResult *results,
-                               int names_only)
-{
-        CMPIStatus s;
-        virConnectPtr conn;
+
+        conn = connect_by_classname(broker, CLASSNAME(reference), &s);
+        if (conn == NULL)
+                goto out;
+
+        if (domain) {
+                doms = calloc(1, sizeof(virDomainPtr));
+                doms[0] = virDomainLookupByName(conn, domain);
+        }
+        else
+                count = get_domain_list(conn, &doms);
+
+        for (i = 0; i < count; i++) {
+                s = _enum_devices(broker,
+                                  reference,
+                                  doms[i],
+                                  type,
+                                  list);
+
+                virDomainFree(doms[i]);
+        }
+
+ out:
+        virConnectClose(conn);
+        free(doms);
+
+        return s;
+}
+
+static CMPIStatus return_enum_devices(const CMPIObjectPath *reference,
+                                      const CMPIResult *results,
+                                      int names_only)
+{
+        CMPIStatus s = {CMPI_RC_OK, NULL};
         struct inst_list list;
 
         if (!provider_is_responsible(_BROKER, reference, &s))
-                return s;
+                goto out;
 
         inst_list_init(&list);
 
-        conn = connect_by_classname(_BROKER, CLASSNAME(reference), &s);
-        if (conn == NULL)
-                return s;
-
-        if (!dom_list_devices(conn, reference, &list)) {
-                cu_statusf(_BROKER, &s,
-                           CMPI_RC_ERR_FAILED,
-                           "Failed to list domains");
-                return s;
-        }
+        s = enum_devices(_BROKER, reference, NULL, 
+                         device_type_from_classname(CLASSNAME(reference)),
+                         &list);
+        if (s.rc != CMPI_RC_OK)
+                goto out;
 
         if (list.cur == 0)
                 goto out;
@@ -371,11 +424,6 @@ static CMPIStatus enum_devices(const CMP
         inst_list_free(&list);
 
  out:
-        cu_statusf(_BROKER, &s,
-                   CMPI_RC_OK,
-                   "");
-        virConnectClose(conn);
-
         return s;
 }
 
@@ -524,7 +572,7 @@ static CMPIStatus EnumInstanceNames(CMPI
                                     const CMPIResult *results,
                                     const CMPIObjectPath *reference)
 {
-        return enum_devices(reference, results, 1);
+        return return_enum_devices(reference, results, true);
 }
 
 static CMPIStatus EnumInstances(CMPIInstanceMI *self,
@@ -533,7 +581,7 @@ static CMPIStatus EnumInstances(CMPIInst
                                 const CMPIObjectPath *reference,
                                 const char **properties)
 {
-        return enum_devices(reference, results, 0);
+        return return_enum_devices(reference, results, false);
 }
 
 static CMPIStatus GetInstance(CMPIInstanceMI *self,
diff -r d09896378f5d -r 3afbff37cc39 src/Virt_Device.h
--- a/src/Virt_Device.h	Fri Mar 07 13:19:00 2008 +0100
+++ b/src/Virt_Device.h	Fri Mar 07 13:19:01 2008 +0100
@@ -27,19 +27,18 @@
  * Return a list of devices for a given domain
  *
  * @param broker A pointer to the CIM broker
- * @param dom The domain in question
- * @param ref The namespace
- * @param list A pointer to an array of CMPIInstance objects (should
- *             be NULL initially)
- * @param cur The number of items in the list (0 initially)
- * @param max The size of the list (0 initially)
- * @returns Nonzero on success
+ * @param reference Defines the libvirt connection to use
+ * @param domain The domain id (NULL means for all domains)
+ * @param type The device type or CIM_RES_TYPE_ALL to get 
+ *             all devices
+ * @param list A pointer to an array of CMPIInstance objects
+ *             (should be NULL initially)
  */
-int dom_devices(const CMPIBroker *broker,
-                virDomainPtr dom,
-                const char *ns,
-                int type,
-                struct inst_list *list);
+CMPIStatus enum_devices(const CMPIBroker *broker,
+                        const CMPIObjectPath *reference,
+                        const char *domain,
+                        const uint16_t type,
+                        struct inst_list *list);
 
 /**
  * Returns the device instance defined by the reference




More information about the Libvirt-cim mailing list