Code

Moved core/error to utils/error.
[sysdb.git] / src / backend / puppet / store-configs.c
index 601bc06d9a9e231f4671aa3e059760639c0fb892..a9a0ff777e9d6f35bd364885832b485d1f88beef 100644 (file)
@@ -28,8 +28,8 @@
 #include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
-#include "utils/error.h"
 #include "utils/dbi.h"
+#include "utils/error.h"
 
 #include "liboconfig/utils.h"
 
@@ -52,7 +52,8 @@ sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
                size_t n, sdb_data_t *data,
                sdb_object_t __attribute__((unused)) *user_data)
 {
-       sdb_host_t host = SDB_HOST_INIT;
+       const char *hostname;
+       sdb_time_t timestamp;
 
        int status;
 
@@ -60,22 +61,20 @@ sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
        assert((data[0].type == SDB_TYPE_STRING)
                        && (data[1].type == SDB_TYPE_DATETIME));
 
-       host.host_name = strdup(data[0].data.string);
-       host.host_last_update = data[1].data.datetime;
+       hostname = data[0].data.string;
+       timestamp = data[1].data.datetime;
 
-       status = sdb_store_host(&host);
+       status = sdb_store_host(hostname, timestamp);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
-                               "store/update host '%s'.\n", host.host_name);
-               free(host.host_name);
+                               "store/update host '%s'.", hostname);
                return -1;
        }
        else if (! status)
                sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
                                "Added/updated host '%s' (last update timestamp = "
-                               "%"PRIscTIME").\n", host.host_name, host.host_last_update);
-       free(host.host_name);
+                               "%"PRIscTIME").", hostname, timestamp);
        return 0;
 } /* sdb_puppet_stcfg_get_hosts */
 
@@ -84,36 +83,33 @@ sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
                size_t n, sdb_data_t *data,
                sdb_object_t __attribute__((unused)) *user_data)
 {
-       sdb_attribute_t attr = SDB_ATTR_INIT;
-
        int status;
 
+       const char *hostname;
+       const char *key;
+       const char *value;
+       sdb_time_t  last_update;
+
        assert(n == 4);
        assert((data[0].type == SDB_TYPE_STRING)
                        && (data[1].type == SDB_TYPE_STRING)
                        && (data[2].type == SDB_TYPE_STRING)
                        && (data[3].type == SDB_TYPE_DATETIME));
 
-       attr.hostname = strdup(data[0].data.string);
-       attr.attr_name = strdup(data[1].data.string);
-       attr.attr_value = strdup(data[2].data.string);
-       attr.attr_last_update = data[3].data.datetime;
+       hostname = data[0].data.string;
+       key = data[1].data.string;
+       value = data[2].data.string;
+       last_update = data[3].data.datetime;
 
-       status = sdb_store_attribute(&attr);
+       status = sdb_store_attribute(hostname, key, value, last_update);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
-                               "store/update host attribute '%s' for host '%s'.\n",
-                               attr.attr_name, attr.hostname);
-               free(attr.hostname);
-               free(attr.attr_name);
-               free(attr.attr_value);
+                               "store/update host attribute '%s' for host '%s'.",
+                               key, hostname);
                return -1;
        }
 
-       free(attr.hostname);
-       free(attr.attr_name);
-       free(attr.attr_value);
        return 0;
 } /* sdb_puppet_stcfg_get_attrs */
 
@@ -132,12 +128,12 @@ sdb_puppet_stcfg_init(sdb_object_t *user_data)
        client = SDB_OBJ_WRAPPER(user_data)->data;
        if (sdb_dbi_client_connect(client)) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                               "Failed to connect to the storeconfigs DB.\n");
+                               "Failed to connect to the storeconfigs DB.");
                return -1;
        }
 
        sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
-                       "connected to the storeconfigs DB.\n");
+                       "connected to the storeconfigs DB.");
        return 0;
 } /* sdb_puppet_stcfg_init */
 
@@ -152,7 +148,7 @@ sdb_puppet_stcfg_collect(sdb_object_t *user_data)
        client = SDB_OBJ_WRAPPER(user_data)->data;
        if (sdb_dbi_client_check_conn(client)) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                               "Connection to storeconfigs DB failed.\n");
+                               "Connection to storeconfigs DB failed.");
                return -1;
        }
 
@@ -160,7 +156,7 @@ sdb_puppet_stcfg_collect(sdb_object_t *user_data)
                                sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
                                /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
-                               "retrieve hosts from the storeconfigs DB.\n");
+                               "retrieve hosts from the storeconfigs DB.");
                return -1;
        }
 
@@ -178,7 +174,7 @@ sdb_puppet_stcfg_collect(sdb_object_t *user_data)
                                /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
                                SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
-                               "retrieve host attributes from the storeconfigs DB.\n");
+                               "retrieve host attributes from the storeconfigs DB.");
                return -1;
        }
        return 0;
@@ -202,7 +198,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
        if (oconfig_get_string(ci, &name)) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
                                "requires a single string argument\n"
-                               "\tUsage: <Connection NAME>\n");
+                               "\tUsage: <Connection NAME>");
                return -1;
        }
 
@@ -216,7 +212,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        if (oconfig_get_string(child, &driver)) {
                                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
                                                "DBAdapter requires a single string argument inside "
-                                               "<Connection %s>\n\tUsage: DBAdapter NAME\n",
+                                               "<Connection %s>\n\tUsage: DBAdapter NAME",
                                                name);
                        }
                        continue;
@@ -225,7 +221,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        if (oconfig_get_string(child, &database)) {
                                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
                                                "DBName requires a single string argument inside "
-                                               "<Connection %s>\n\tUsage: DBName NAME\n",
+                                               "<Connection %s>\n\tUsage: DBName NAME",
                                                name);
                        }
                        continue;
@@ -253,7 +249,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
                                                "DBIOption requires exactly two string arguments "
                                                "inside <Connection %s>\n"
-                                               "\tUsage: DBIOption KEY VALUE\n", name);
+                                               "\tUsage: DBIOption KEY VALUE", name);
                                continue;
                        }
 
@@ -264,14 +260,14 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                else {
                        sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
                                        "Ignoring unknown config option '%s' inside "
-                                       "<Connection %s>.\n", child->key, name);
+                                       "<Connection %s>.", child->key, name);
                        continue;
                }
 
                if (status) {
                        sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
                                        "'%s' requires a single string argument inside "
-                                       "<Connection %s>\n\tUsage: DBAdapter NAME\n",
+                                       "<Connection %s>\n\tUsage: DBAdapter NAME",
                                        child->key, name);
                        continue;
                }
@@ -282,7 +278,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                        if (! (options = sdb_dbi_options_create())) {
                                char errmsg[1024];
                                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                                               "Failed to create DBI options object: %s\n",
+                                               "Failed to create DBI options object: %s",
                                                sdb_strerror(errno, errmsg, sizeof(errmsg)));
                                continue;
                        }
@@ -291,7 +287,7 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                if (sdb_dbi_options_add(options, key, value)) {
                        char errmsg[1024];
                        sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                                       "Failed to add option '%s': %s\n", key,
+                                       "Failed to add option '%s': %s", key,
                                        sdb_strerror(errno, errmsg, sizeof(errmsg)));
                        continue;
                }
@@ -299,13 +295,13 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
 
        if (! driver) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                               "Connection '%s' " "missing the 'DBAdapter' option.\n",
+                               "Connection '%s' " "missing the 'DBAdapter' option.",
                                name);
                return -1;
        }
        if (! database) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                               "Connection '%s' missing the 'DBName' option.\n", name);
+                               "Connection '%s' missing the 'DBName' option.", name);
                return -1;
        }
 
@@ -316,19 +312,19 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
        if (! client) {
                char errbuf[1024];
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                               "Failed to create DBI client: %s\n",
+                               "Failed to create DBI client: %s",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
        sdb_dbi_client_set_options(client, options);
 
-       user_data = sdb_object_create_wrapper(client,
+       user_data = sdb_object_create_wrapper("dbi-client", client,
                        (void (*)(void *))sdb_dbi_client_destroy);
        if (! user_data) {
                sdb_dbi_client_destroy(client);
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
-                               "Failed to allocate sdb_object_t\n");
+                               "Failed to allocate sdb_object_t");
                return -1;
        }
 
@@ -353,7 +349,7 @@ sdb_puppet_stcfg_config(oconfig_item_t *ci)
                        sdb_puppet_stcfg_config_conn(child);
                else
                        sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
-                                       "Ignoring unknown config option '%s'.\n", child->key);
+                                       "Ignoring unknown config option '%s'.", child->key);
        }
        return 0;
 } /* sdb_puppet_stcfg_config */
@@ -373,7 +369,7 @@ sdb_module_init(sdb_plugin_info_t *info)
        if (dbi_initialize(/* driver dir = */ NULL) < 0) {
                sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
                                "initialize DBI; possibly you don't have any drivers "
-                               "installed.\n");
+                               "installed.");
                return -1;
        }