[Libvirt-cim] [PATCH 1 of 6] ComputerSystem: enhance enum functionality for use in assocs

Heidi Eckhart heidieck at linux.vnet.ibm.com
Mon Mar 3 10:51:26 UTC 2008


# HG changeset patch
# User Heidi Eckhart <heidieck at linux.vnet.ibm.com>
# Date 1204541457 -3600
# Node ID 7a93b0ab4de3663e75f43176cce67b70e2064fd7
# Parent  27e78288c82426835126eaa07dba1e425c756f3f
ComputerSystem: enhance enum functionality for use in assocs
Signed-off-by: Heidi Eckhart <heidieck at linux.vnet.ibm.com>

diff -r 27e78288c824 -r 7a93b0ab4de3 src/Virt_ComputerSystem.c
--- a/src/Virt_ComputerSystem.c	Mon Mar 03 11:34:52 2008 +0100
+++ b/src/Virt_ComputerSystem.c	Mon Mar 03 11:50:57 2008 +0100
@@ -302,184 +302,168 @@ static int set_other_id_info(const CMPIB
 }
 
 /* Populate an instance with information from a domain */
-static int instance_from_dom(const CMPIBroker *broker,
-                             virDomainPtr dom,
-                             const char *prefix,
-                             CMPIInstance *instance)
-{
+static CMPIStatus set_properties(const CMPIBroker *broker,
+                                 virDomainPtr dom,
+                                 const char *prefix,
+                                 CMPIInstance *instance)
+{
+        CMPIStatus s = {CMPI_RC_ERR_FAILED, NULL};
         char *uuid = NULL;
-        int ret = 1;
 
         if (!set_name_from_dom(dom, instance)) {
                 /* Print trace error */
-                return 0;
+                goto out;
         }
 
         if (!set_uuid_from_dom(dom, instance, &uuid)) {
                 /* Print trace error */
-                ret = 0;
                 goto out;
         }
 
         if (!set_capdesc_from_dom(dom, instance)) {
                 /* Print trace error */
-                ret = 0;
                 goto out;
         }
 
         if (!set_state_from_dom(broker, dom, instance)) {
                 /* Print trace error */
-                ret = 0;
                 goto out;
         }
 
         if (!set_creation_class(instance)) {
                 /* Print trace error */
-                ret = 0;
                 goto out;
         }
 
         if (!set_other_id_info(broker, uuid, prefix, instance)) {
                 /* Print trace error */
-                ret = 0;
                 goto out;
         }
 
         /* More attributes here, of course */
 
+        cu_statusf(broker, &s,
+                   CMPI_RC_OK,
+                   "");
+
  out:
         free(uuid);
 
-        return ret;
-}
-
-/* Given a hypervisor connection and a domain name, return an instance */
-CMPIInstance *instance_from_name(const CMPIBroker *broker,
-                                 virConnectPtr conn,
-                                 const char *name,
-                                 const CMPIObjectPath *op)
-{
-        virDomainPtr dom;
-        CMPIInstance *instance;
-
-        dom = virDomainLookupByName(conn, name);
-        if (dom == NULL)
-                return NULL;
-
-        instance = get_typed_instance(broker,
-                                      pfx_from_conn(conn),
-                                      "ComputerSystem",
-                                      NAMESPACE(op));
-        if (instance == NULL)
-                goto out;
-
-        if (!instance_from_dom(broker, 
-                               dom, 
-                               pfx_from_conn(conn), 
-                               instance))
-                instance = NULL;
-
- out:
-        virDomainFree(dom);
-
-        return instance;
-}
-
-/* Enumerate domains on the given connection, return results */
-int enum_domains(const CMPIBroker *broker,
-                 virConnectPtr conn,
-                 const char *ns,
-                 struct inst_list *instlist)
-{
+        return s;
+}
+
+static CMPIStatus instance_from_dom(const CMPIBroker *broker,
+                                     const CMPIObjectPath *reference,
+                                     virConnectPtr conn,
+                                     virDomainPtr domain,
+                                     CMPIInstance **_inst)
+{
+        CMPIStatus s = {CMPI_RC_OK, NULL};
+        CMPIInstance *inst = NULL;
+
+        inst = get_typed_instance(broker,
+                                  pfx_from_conn(conn),
+                                  "ComputerSystem",
+                                  NAMESPACE(reference));
+        if (inst == NULL) {
+                cu_statusf(broker, &s,
+                           CMPI_RC_ERR_FAILED,
+                           "Unable to init ComputerSystem instance");
+                goto out;
+        }
+
+        s = set_properties(broker,
+                           domain, 
+                           pfx_from_conn(conn), 
+                           inst);
+        if (s.rc != CMPI_RC_OK)
+                goto out;
+
+        *_inst = inst;
+
+ out:
+        return s;
+}
+
+CMPIStatus enum_domains(const CMPIBroker *broker,
+                        const CMPIObjectPath *reference,
+                        struct inst_list *instlist)
+{
+        CMPIStatus s = {CMPI_RC_OK, NULL};
         virDomainPtr *list = NULL;
+        virConnectPtr conn = NULL;
         int count;
         int i;
 
+        conn = connect_by_classname(broker, CLASSNAME(reference), &s);
+        if (conn == NULL)
+                goto out;
+
         count = get_domain_list(conn, &list);
-        if (count <= 0)
-                goto out;
+        if (count <= 0) {
+                cu_statusf(broker, &s,
+                           CMPI_RC_ERR_FAILED,
+                           "Failed to get domain list");
+                goto out;
+        }
 
         for (i = 0; i < count; i++) {
-                CMPIInstance *inst;
-
-                inst = get_typed_instance(broker,
-                                          pfx_from_conn(conn),
-                                          "ComputerSystem",
-                                          ns);
-                if (inst == NULL)
+                CMPIInstance *inst = NULL;
+                
+                s = instance_from_dom(broker,
+                                      reference,
+                                      conn,
+                                      list[i],  
+                                      &inst);
+                if (s.rc != CMPI_RC_OK)
                         goto end;
 
-                if (instance_from_dom(broker, 
-                                      list[i], 
-                                      pfx_from_conn(conn), 
-                                      inst))
-                        inst_list_add(instlist, inst);
-
-        end:
+                inst_list_add(instlist, inst);
+
+          end:
                 virDomainFree(list[i]);
         }
- out:
+
+ out:
+        virConnectClose(conn);
         free(list);
 
-        return 1;
+        return s;
 }
 
 static CMPIStatus return_enum_domains(const CMPIObjectPath *reference,
                                       const CMPIResult *results,
-                                      int names_only)
+                                      bool names_only)
 {
         struct inst_list list;
-        CMPIStatus s;
-        virConnectPtr conn = NULL;
-        int ret;
-
-        conn = connect_by_classname(_BROKER, CLASSNAME(reference), &s);
-        if (conn == NULL)
-                return s;
+        CMPIStatus s = {CMPI_RC_OK, NULL};
 
         inst_list_init(&list);
-        ret = enum_domains(_BROKER, conn, NAMESPACE(reference), &list);
-        if (!ret) {
-                cu_statusf(_BROKER, &s,
-                           CMPI_RC_ERR_FAILED,
-                           "Failed to get domain list");
-                goto out;
-        }
+
+        s = enum_domains(_BROKER, reference, &list);
+        if (s.rc != CMPI_RC_OK)
+                goto out;
 
         if (names_only)
                 cu_return_instance_names(results, &list);
         else
                 cu_return_instances(results, &list);
 
-        cu_statusf(_BROKER, &s,
-                   CMPI_RC_OK,
-                   "");
  out:
         inst_list_free(&list);
 
-        virConnectClose(conn);
-
-        return s;
-}
-
-CMPIStatus get_domain(const CMPIBroker *broker,
-                      const CMPIObjectPath *reference,
-                      CMPIInstance **inst)
-{
-        CMPIInstance *_inst;
+        return s;
+}
+
+CMPIStatus get_domain_by_name(const CMPIBroker *broker,
+                              const CMPIObjectPath *reference,
+                              const char *name,
+                              CMPIInstance **_inst)
+{
+        CMPIInstance *inst = NULL;
         CMPIStatus s = {CMPI_RC_OK, NULL};
         virConnectPtr conn = NULL;
-        const char *name;
-
-        if (!provider_is_responsible(broker, reference, &s))
-                return s;
-
-        if (cu_get_str_path(reference, "Name", &name) != CMPI_RC_OK) {                
-                cu_statusf(broker, &s,
-                           CMPI_RC_ERR_FAILED,
-                           "No domain name specified");
-                return s;
-        }
+        virDomainPtr dom;
 
         conn = connect_by_classname(broker, CLASSNAME(reference), &s);
         if (conn == NULL) {
@@ -489,35 +473,59 @@ CMPIStatus get_domain(const CMPIBroker *
                 return s;
         }
 
-        _inst = instance_from_name(broker, conn, name, reference);
-        if (_inst == NULL) {
+        dom = virDomainLookupByName(conn, name);
+        if (dom == NULL) {
                 cu_statusf(broker, &s,
                            CMPI_RC_ERR_NOT_FOUND,
-                           "No such instance (%s)", name);
-                goto out;
-        }
-
-        s = cu_validate_ref(broker, reference, _inst);
-
- out:
+                           "No such instance (%s).",
+                           name);
+                goto out;
+        }
+
+        s = instance_from_dom(broker,
+                              reference,
+                              conn,
+                              dom,  
+                              &inst);
+        if (s.rc != CMPI_RC_OK)
+                goto out;
+
+        *_inst = inst;
+
+ out:
+        virDomainFree(dom);
         virConnectClose(conn);
-        *inst = _inst;
-
-        return s;
-}
-
-static CMPIStatus return_domain(const CMPIObjectPath *reference,
-                                const CMPIResult *results)
-{
-        CMPIStatus s = {CMPI_RC_OK, NULL};
-        CMPIInstance *inst;
-
-        s = get_domain(_BROKER, reference, &inst);
+
+        return s;
+}
+
+CMPIStatus get_domain_by_ref(const CMPIBroker *broker,
+                             const CMPIObjectPath *reference,
+                             CMPIInstance **_inst)
+{
+        CMPIStatus s = {CMPI_RC_OK, NULL};
+        CMPIInstance *inst = NULL;
+        const char *name = NULL;
+
+        if (cu_get_str_path(reference, "Name", &name) != CMPI_RC_OK) {                
+                cu_statusf(broker, &s,
+                           CMPI_RC_ERR_FAILED,
+                           "No domain name specified");
+                goto out;
+        }
+
+        s = get_domain_by_name(broker, reference, name, &inst);
         if (s.rc != CMPI_RC_OK)
-                return s;
-
-        CMReturnInstance(results, inst);
-
+                goto out;
+        
+        s = cu_validate_ref(broker, reference, inst);
+        if (s.rc != CMPI_RC_OK)
+                goto out;
+
+        *_inst = inst;
+        
+ out:
+        
         return s;
 }
 
@@ -526,7 +534,7 @@ static CMPIStatus EnumInstanceNames(CMPI
                                     const CMPIResult *results,
                                     const CMPIObjectPath *reference)
 {
-        return return_enum_domains(reference, results, 1);
+        return return_enum_domains(reference, results, true);
 }
 
 static CMPIStatus EnumInstances(CMPIInstanceMI *self,
@@ -536,7 +544,7 @@ static CMPIStatus EnumInstances(CMPIInst
                                 const char **properties)
 {
 
-        return return_enum_domains(reference, results, 0);
+        return return_enum_domains(reference, results, false);
 }
 
 static CMPIStatus GetInstance(CMPIInstanceMI *self,
@@ -545,7 +553,17 @@ static CMPIStatus GetInstance(CMPIInstan
                               const CMPIObjectPath *reference,
                               const char **properties)
 {
-        return return_domain(reference, results);
+        CMPIStatus s = {CMPI_RC_OK, NULL};
+        CMPIInstance *inst;
+        
+        s = get_domain_by_ref(_BROKER, reference, &inst);
+        if (s.rc != CMPI_RC_OK)
+                goto out;
+
+        CMReturnInstance(results, inst);
+
+ out:
+        return s;
 }
 
 DEFAULT_CI();
diff -r 27e78288c824 -r 7a93b0ab4de3 src/Virt_ComputerSystem.h
--- a/src/Virt_ComputerSystem.h	Mon Mar 03 11:34:52 2008 +0100
+++ b/src/Virt_ComputerSystem.h	Mon Mar 03 11:50:57 2008 +0100
@@ -24,33 +24,16 @@
 #include "misc_util.h"
 
 /**
- * Return an instance of a Virt_ComputerSystem, based on a name
- *
- * @param broker A pointer to the current broker
- * @param conn The libvirt connection to use
- * @param name The name of the desired domain instance
- * @param ns The namespace to use
- * @returns The instance or NULL on failure
- */
-CMPIInstance *instance_from_name(const CMPIBroker *broker,
-                                 virConnectPtr conn,
-                                 const char *name,
-                                 const CMPIObjectPath *ns);
-
-
-/**
  * Get a list of domain instances
  *
  * @param broker A pointer to the current broker
- * @param conn The libvirt connection to use
- * @param op The namespace to use
+ * @param reference The object path containing namespace and prefix info
  * @param instlist A pointer to an initialized inst_list to populate
- * @returns nonzero on success
+ * @returns CMPIStatus
  */
-int enum_domains(const CMPIBroker *broker,
-                 virConnectPtr conn,
-                 const char *ns,
-                 struct inst_list *instlist);
+CMPIStatus enum_domains(const CMPIBroker *broker,
+                        const CMPIObjectPath *reference,
+                        struct inst_list *instlist);
 
 /**
  * Get domain instance specified by the client given domain 
@@ -58,13 +41,26 @@ int enum_domains(const CMPIBroker *broke
  *
  * @param broker A pointer to the current broker
  * @param ref The client given object path
- * @param inst In case of success the pointer to the instance
+ * @param _inst In case of success the pointer to the instance
  * @returns CMPIStatus
  */
-CMPIStatus get_domain(const CMPIBroker *broker,
-                      const CMPIObjectPath *reference,
-                      CMPIInstance **inst);
+CMPIStatus get_domain_by_ref(const CMPIBroker *broker,
+                             const CMPIObjectPath *reference,
+                             CMPIInstance **_inst);
 
+/**
+ * Get domain instance specified by the domain name
+ *
+ * @param broker A pointer to the current broker
+ * @param ref The object path containing namespace and prefix info
+ * @param name The domain name
+ * @param _inst In case of success the pointer to the instance
+ * @returns CMPIStatus
+ */
+CMPIStatus get_domain_by_name(const CMPIBroker *broker,
+                              const CMPIObjectPath *reference,
+                              const char *name,
+                              CMPIInstance **_inst);
 
 #endif
 




More information about the Libvirt-cim mailing list