
# HG changeset patch # User Heidi Eckhart <heidieck@linux.vnet.ibm.com> # Date 1204709616 -3600 # Node ID 196480aa1c1b8358ae896782f831950c27ab232d # Parent 1626d88fa98b890ba251a450fca3d6c330bc27ad DevicePool: reorganize get and enum functions - the common look and feel of the get_..._by_name() and get_..._by_ref() is adopted - get_all_pools() is renamed to enum_pools and rewritten to be configurable dynamically - return either a specific or all resource pools. This also replaces get_pool_by_type(). - the device_pool_names array is removed and types are identified by the CIM_POOL_TYPE_..., as defined in svpc_types.h Signed-off-by: Heidi Eckhart <heidieck@linux.vnet.ibm.com> diff -r 1626d88fa98b -r 196480aa1c1b src/Virt_DevicePool.c --- a/src/Virt_DevicePool.c Wed Mar 05 10:33:35 2008 +0100 +++ b/src/Virt_DevicePool.c Wed Mar 05 10:33:36 2008 +0100 @@ -45,12 +45,6 @@ static const CMPIBroker *_BROKER; -char *device_pool_names[] = {"ProcessorPool", - "MemoryPool", - "DiskPool", - "NetworkPool", - NULL}; - struct disk_pool { char *tag; char *path; @@ -332,18 +326,32 @@ char *pool_member_of(const CMPIBroker *b return poolid; } +uint16_t pool_type_from_classname(const char *classname) +{ + if (strstr(classname, "NetworkPool")) + return CIM_POOL_TYPE_NET; + else if (strstr(classname, "DiskPool")) + return CIM_POOL_TYPE_DISK; + else if (strstr(classname, "MemoryPool")) + return CIM_POOL_TYPE_MEM; + else if (strstr(classname, "ProcessorPool")) + return CIM_POOL_TYPE_PROC; + else + return CIM_POOL_TYPE_UNKNOWN; +} + uint16_t device_type_from_poolid(const char *id) { if (STARTS_WITH(id, "NetworkPool")) - return VIRT_DEV_NET; + return CIM_POOL_TYPE_NET; else if (STARTS_WITH(id, "DiskPool")) - return VIRT_DEV_DISK; + return CIM_POOL_TYPE_DISK; else if (STARTS_WITH(id, "MemoryPool")) - return VIRT_DEV_MEM; + return CIM_POOL_TYPE_MEM; else if (STARTS_WITH(id, "ProcessorPool")) - return VIRT_DEV_VCPU; + return CIM_POOL_TYPE_PROC; else - return VIRT_DEV_UNKNOWN; + return CIM_POOL_TYPE_UNKNOWN; } static bool mempool_set_total(CMPIInstance *inst, virConnectPtr conn) @@ -673,177 +681,190 @@ static CMPIStatus diskpool_instance(virC return s; } -static CMPIStatus _get_pool(const CMPIBroker *broker, - virConnectPtr conn, - const char *type, +static CMPIStatus _get_pools(const CMPIBroker *broker, + const CMPIObjectPath *reference, + const uint16_t type, + const char *id, + struct inst_list *list) +{ + CMPIStatus s = {CMPI_RC_OK, NULL}; + virConnectPtr conn; + + conn = connect_by_classname(broker, CLASSNAME(reference), &s); + if (conn == NULL) + goto out; + + if ((type == CIM_POOL_TYPE_PROC) || + (type == CIM_TYPE_ALL)) + s = procpool_instance(conn, list, + NAMESPACE(reference), + id, broker); + + if ((type == CIM_POOL_TYPE_MEM) || + (type == CIM_TYPE_ALL)) + s = mempool_instance(conn, list, + NAMESPACE(reference), + id, broker); + + if ((type == CIM_POOL_TYPE_NET) || + (type == CIM_TYPE_ALL)) + s = netpool_instance(conn, list, + NAMESPACE(reference), + id, broker); + + if ((type == CIM_POOL_TYPE_DISK) || + (type == CIM_TYPE_ALL)) + s = diskpool_instance(conn, list, + NAMESPACE(reference), + id, broker); + + if (type == CIM_POOL_TYPE_UNKNOWN) + cu_statusf(broker, &s, + CMPI_RC_ERR_NOT_FOUND, + "No such instance - resource pool type unknow."); + + if (id && list->cur == 0) + cu_statusf(broker, &s, + CMPI_RC_ERR_NOT_FOUND, + "No such instance (%s).", id); + + out: + virConnectClose(conn); + return s; +} + +CMPIStatus get_pool_by_name(const CMPIBroker *broker, + const CMPIObjectPath *reference, const char *id, - const char *ns, - struct inst_list *list) -{ - if (STARTS_WITH(type, "MemoryPool")) - return mempool_instance(conn, list, ns, id, broker); - else if (STARTS_WITH(type, "ProcessorPool")) - return procpool_instance(conn, list, ns, id, broker); - else if (STARTS_WITH(type, "NetworkPool")) - return netpool_instance(conn, list, ns, id, broker); - else if (STARTS_WITH(type, "DiskPool")) - return diskpool_instance(conn, list, ns, id, broker); - - return (CMPIStatus){CMPI_RC_ERR_NOT_FOUND, NULL}; -} - -CMPIStatus get_pool_by_type(const CMPIBroker *broker, - virConnectPtr conn, - const char *type, - const char *ns, - struct inst_list *list) -{ - return _get_pool(broker, conn, type, NULL, ns, list); -} - -CMPIInstance *get_pool_by_id(const CMPIBroker *broker, - virConnectPtr conn, - const char *id, - const char *ns) -{ - CMPIInstance *inst = NULL; - CMPIStatus s; - char *type = NULL; + CMPIInstance **_inst) +{ + CMPIStatus s = {CMPI_RC_OK, NULL}; + virConnectPtr conn = NULL; + struct inst_list list; char *poolid = NULL; int ret; - struct inst_list list; + uint16_t type; inst_list_init(&list); - ret = sscanf(id, "%a[^/]/%as", &type, &poolid); - if (ret != 2) - goto out; - - s = _get_pool(broker, conn, type, poolid, ns, &list); - if ((s.rc == CMPI_RC_OK) && (list.cur > 0)) - inst = list.list[0]; - - out: + conn = connect_by_classname(broker, CLASSNAME(reference), &s); + if (conn == NULL) { + cu_statusf(broker, &s, + CMPI_RC_ERR_NOT_FOUND, + "No such instance."); + goto out; + } + + type = device_type_from_poolid(id); + + if (type == CIM_POOL_TYPE_UNKNOWN) { + cu_statusf(broker, &s, + CMPI_RC_ERR_NOT_FOUND, + "No such instance (%s) - resource pool type mismatch.", + id); + goto out; + } + + ret = sscanf(id, "%*[^/]/%as", &poolid); + if (ret != 1) { + cu_statusf(broker, &s, + CMPI_RC_ERR_NOT_FOUND, + "No such instance (%s).", + id); + goto out; + } + + s = _get_pools(broker, reference, type, poolid, &list); + if (s.rc != CMPI_RC_OK) + goto out; + + *_inst = list.list[0]; + + out: + free(poolid); + virConnectClose(conn); inst_list_free(&list); - return inst; -} - -CMPIStatus get_all_pools(const CMPIBroker *broker, - virConnectPtr conn, - const char *ns, - struct inst_list *list) -{ - int i; + return s; +} + +CMPIStatus get_pool_by_ref(const CMPIBroker *broker, + const CMPIObjectPath *reference, + CMPIInstance **instance) +{ CMPIStatus s = {CMPI_RC_OK}; - - for (i = 0; device_pool_names[i]; i++) { - s = get_pool_by_type(broker, - conn, - device_pool_names[i], - ns, - list); - if (s.rc != CMPI_RC_OK) - goto out; - } - + CMPIInstance *inst = NULL; + const char *id = NULL; + uint16_t type_cls; + uint16_t type_id; + + if (cu_get_str_path(reference, "InstanceID", &id) != CMPI_RC_OK) { + cu_statusf(broker, &s, + CMPI_RC_ERR_FAILED, + "Missing InstanceID"); + goto out; + } + + type_cls = pool_type_from_classname(CLASSNAME(reference)); + type_id = device_type_from_poolid(id); + + if ((type_cls != type_id) || + (type_cls == CIM_POOL_TYPE_UNKNOWN)) { + cu_statusf(broker, &s, + CMPI_RC_ERR_NOT_FOUND, + "No such instance (%s) - resource pool type mismatch.", + id); + goto out; + } + + s = get_pool_by_name(broker, reference, id, &inst); + if (s.rc != CMPI_RC_OK) + goto out; + + s = cu_validate_ref(broker, reference, inst); + if (s.rc != CMPI_RC_OK) + goto out; + + *instance = inst; + out: return s; } -static void __return_pool(const CMPIResult *results, - struct inst_list *list, - bool name_only) -{ - if (name_only) - cu_return_instance_names(results, list); - else - cu_return_instances(results, list); +CMPIStatus enum_pools(const CMPIBroker *broker, + const CMPIObjectPath *reference, + const uint16_t type, + struct inst_list *list) +{ + return _get_pools(broker, reference, type, NULL, list); } static CMPIStatus return_pool(const CMPIObjectPath *ref, const CMPIResult *results, - bool name_only, - bool single_only) -{ - CMPIStatus s; - char *type; - virConnectPtr conn; + bool names_only) +{ + CMPIStatus s = {CMPI_RC_OK, NULL}; struct inst_list list; if (!provider_is_responsible(_BROKER, ref, &s)) - return s; - - type = class_base_name(CLASSNAME(ref)); - if (type == NULL) { - cu_statusf(_BROKER, &s, - CMPI_RC_ERR_FAILED, - "Invalid classname `%s'", CLASSNAME(ref)); - return s; - } + goto out; inst_list_init(&list); - conn = connect_by_classname(_BROKER, CLASSNAME(ref), &s); - if (conn == NULL) - goto out; - - s = get_pool_by_type(_BROKER, - conn, - type, - NAMESPACE(ref), - &list); - if (s.rc == CMPI_RC_OK) { - __return_pool(results, &list, name_only); - cu_statusf(_BROKER, &s, - CMPI_RC_OK, - ""); - } else { - cu_statusf(_BROKER, &s, - CMPI_RC_ERR_FAILED, - "Pool type %s not found", type); - } - - out: - free(type); + s = enum_pools(_BROKER, + ref, + pool_type_from_classname(CLASSNAME(ref)), + &list); + if (s.rc != CMPI_RC_OK) + goto out; + + if (names_only) + cu_return_instance_names(results, &list); + else + cu_return_instances(results, &list); + + out: inst_list_free(&list); - - return s; -} - -CMPIStatus get_pool_inst(const CMPIBroker *broker, - const CMPIObjectPath *reference, - CMPIInstance **instance) -{ - CMPIStatus s; - CMPIInstance *inst = NULL; - virConnectPtr conn = NULL; - const char *id = NULL; - - if (cu_get_str_path(reference, "InstanceID", &id) != CMPI_RC_OK) { - cu_statusf(broker, &s, - CMPI_RC_ERR_FAILED, - "Missing InstanceID"); - goto out; - } - - conn = connect_by_classname(broker, CLASSNAME(reference), &s); - if (conn == NULL) { - cu_statusf(broker, &s, - CMPI_RC_ERR_NOT_FOUND, - "No such instance"); - goto out; - } - - inst = get_pool_by_id(broker, conn, id, NAMESPACE(reference)); - if (inst == NULL) - cu_statusf(broker, &s, - CMPI_RC_ERR_NOT_FOUND, - "No such instance (%s)", id); - - out: - virConnectClose(conn); - *instance = inst; return s; } @@ -853,7 +874,7 @@ static CMPIStatus EnumInstanceNames(CMPI const CMPIResult *results, const CMPIObjectPath *reference) { - return return_pool(reference, results, true, false); + return return_pool(reference, results, true); } static CMPIStatus EnumInstances(CMPIInstanceMI *self, @@ -862,7 +883,7 @@ static CMPIStatus EnumInstances(CMPIInst const CMPIObjectPath *reference, const char **properties) { - return return_pool(reference, results, false, false); + return return_pool(reference, results, false); } static CMPIStatus GetInstance(CMPIInstanceMI *self, @@ -871,13 +892,16 @@ static CMPIStatus GetInstance(CMPIInstan const CMPIObjectPath *reference, const char **properties) { - CMPIStatus s; + CMPIStatus s = {CMPI_RC_OK}; CMPIInstance *inst = NULL; - s = get_pool_inst(_BROKER, reference, &inst); - if ((s.rc == CMPI_RC_OK) && (inst != NULL)) - CMReturnInstance(results, inst); - + s = get_pool_by_ref(_BROKER, reference, &inst); + if (s.rc != CMPI_RC_OK) + goto out; + + CMReturnInstance(results, inst); + + out: return s; } diff -r 1626d88fa98b -r 196480aa1c1b src/Virt_DevicePool.h --- a/src/Virt_DevicePool.h Wed Mar 05 10:33:35 2008 +0100 +++ b/src/Virt_DevicePool.h Wed Mar 05 10:33:36 2008 +0100 @@ -26,20 +26,6 @@ #include <libcmpiutil/libcmpiutil.h> #include <stdint.h> -extern char *device_pool_names[]; - -CMPIStatus get_pool_by_type(const CMPIBroker *broker, - virConnectPtr conn, - const char *type, - const char *ns, - struct inst_list *list); - -CMPIInstance *get_pool_by_id(const CMPIBroker *broker, - virConnectPtr conn, - const char *id, - const char *ns); - - /** * Get the InstanceID of a pool that a given RASD id (for type) is in * @@ -56,6 +42,13 @@ char *pool_member_of(const CMPIBroker *b const char *id); /** + * Get the device type of a given pool from the pool's classname + * + * @param id The InstanceID of the pool + */ +uint16_t pool_type_from_classname(const char *classname); + +/** * Get the device type of a given pool from the pool's InstanceID * * @param id The InstanceID of the pool @@ -63,17 +56,19 @@ uint16_t device_type_from_poolid(const c uint16_t device_type_from_poolid(const char *id); /** - * Get all device pools on the system for the given connection + * Get all device pools on the system for the given type + * * * @param broker The current Broker - * @param conn The libvirt connection to use - * @param ns Namespace for the pools - * @param list Return instances in this struct + * @param reference Defines the libvirt connection to use + * @param type The device pool type or CIM_POOL_TYPE_ALL + * to get all resource pools + * @param list The list of returned instances */ -CMPIStatus get_all_pools(const CMPIBroker *broker, - virConnectPtr conn, - const char *ns, - struct inst_list *list); +CMPIStatus enum_pools(const CMPIBroker *broker, + const CMPIObjectPath *reference, + const uint16_t type, + struct inst_list *list); /** * Get a device pools instance for the given reference @@ -82,9 +77,23 @@ CMPIStatus get_all_pools(const CMPIBroke * @param reference The reference passed to the CIMOM * @param instance Return corresponding instance */ -CMPIStatus get_pool_inst(const CMPIBroker *broker, - const CMPIObjectPath *reference, - CMPIInstance **instance); +CMPIStatus get_pool_by_ref(const CMPIBroker *broker, + const CMPIObjectPath *reference, + CMPIInstance **instance); + +/** + * Get device pool instance specified by the id + * + * @param broker A pointer to the current broker + * @param ref The object path containing namespace and prefix info + * @param name The device pool id + * @param _inst In case of success the pointer to the instance + * @returns CMPIStatus + */ +CMPIStatus get_pool_by_name(const CMPIBroker *broker, + const CMPIObjectPath *reference, + const char *id, + CMPIInstance **_inst); #endif