aboutsummaryrefslogtreecommitdiff
path: root/lib/map.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2012-10-07 20:12:34 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2012-10-07 20:12:34 +0300
commit1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3 (patch)
tree209822ff7ef7766b94a714438473dcf8f12c4e1f /lib/map.c
parentcabb35e68d34bcccaa39a223eaa037b335c81558 (diff)
downloadeclat-1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3.tar.gz
eclat-1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3.tar.bz2
Implement resource identifier mapping.
Resource identifier mapping permits to use more customized identifiers instead of the Amazon resource identifiers. For instance, you can use hostnames instead of the instance IDs, etc. * lib/Makefile.am: Add new files. * lib/filemap.c: New file. * lib/map.c: New file. * lib/libeclat.h (eclat_map_drv,eclat_map): New structs. (eclat_map_status): New enum. (eclat_map_init,eclat_map_lookup,eclat_map_config) (eclat_map_free,eclat_map_open,eclat_map_close) (eclat_map_get,eclat_map_strerror,eclat_map_drv_register) (eclat_map_foreach,eclat_map_free_all) (eclat_get_string_node): New protos. (eclat_map_drv_file): New extern. * src/accfile.c (access_file_lookup): Fix memory leak. * src/cmdline.opt: New option: --translate (-x) * src/config.c (eclat_kw) <map>: New block statement. (config_finish): Parse map configs. * src/asscaddr.c: Translate key. * src/dscrinstattr.c: Likewise. * src/dscrinsts.c: Likewise. * src/dscrinststat.c: Likewise. * src/dscrsecgrps.c: Likewise. * src/getconout.c: Likewise. * src/startinst.c: Likewise. * src/eclat.c (main): Initialize maps and free them when no more needed. * src/eclat.h (translate_option): New extern. (translate_ids): New proto. * src/util.c (translate_option): New variable. (translate_ids): New function.
Diffstat (limited to 'lib/map.c')
-rw-r--r--lib/map.c335
1 files changed, 335 insertions, 0 deletions
diff --git a/lib/map.c b/lib/map.c
new file mode 100644
index 0000000..5a8d2fd
--- /dev/null
+++ b/lib/map.c
@@ -0,0 +1,335 @@
+/* This file is part of Eclat.
+ Copyright (C) 2012 Sergey Poznyakoff.
+
+ Eclat is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ Eclat is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Eclat. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "libeclat.h"
+#include "wordsplit.h"
+#include <sysexits.h>
+#include <string.h>
+
+static int map_dbg = -1;
+static struct grecs_symtab *openmap_symtab, *mapdrv_symtab;
+
+void
+eclat_map_init()
+{
+ map_dbg = debug_register("map");
+}
+
+int
+eclat_get_string_node(struct grecs_node *node, const char *name,
+ int optional,
+ struct grecs_node **pret)
+{
+ struct grecs_node *p = grecs_find_node(node->down, name);
+
+ if (!p) {
+ if (optional)
+ return eclat_map_not_found;
+ grecs_error(&node->locus, 0,
+ "no \"%s\" statement found", name);
+ return eclat_map_failure;
+ }
+
+ if (p->type != grecs_node_stmt) {
+ grecs_error(&p->locus, 0, "must be simple statement");
+ return eclat_map_failure;
+ }
+
+ if (p->v.value->type != GRECS_TYPE_STRING) {
+ grecs_error(&p->locus, 0, "must be scalar");
+ return eclat_map_failure;
+ }
+
+ *pret = p;
+ return eclat_map_ok;
+}
+
+static int
+drv_copy(void *a, void *b)
+{
+ struct eclat_map_drv *drva = a;
+ struct eclat_map_drv *drvb = b;
+
+ *drva = *drvb;
+ drva->name = strdup(drvb->name);
+ return drva->name == NULL;
+}
+
+int
+eclat_map_drv_register(struct eclat_map_drv *drv)
+{
+ struct eclat_map_drv *p;
+ int install;
+
+ if (!mapdrv_symtab) {
+ mapdrv_symtab = grecs_symtab_create(sizeof(*drv),
+ NULL,
+ NULL,
+ drv_copy,
+ NULL,
+ NULL);
+ if (!mapdrv_symtab)
+ grecs_alloc_die();
+ }
+ install = 1;
+ if (!grecs_symtab_lookup_or_install(mapdrv_symtab, drv, &install))
+ die(EX_SOFTWARE, "cannot install map");
+ return install ? eclat_map_ok : eclat_map_failure;
+}
+
+struct eclat_map *
+eclat_map_lookup(const char *name)
+{
+ struct eclat_map key;
+
+ if (!openmap_symtab)
+ return NULL;
+ key.name = (char*) name;
+ return grecs_symtab_lookup_or_install(openmap_symtab, &key, NULL);
+}
+
+static void
+map_free(void *p)
+{
+ struct eclat_map *map = p;
+ if (map->flags & ECLAT_MAP_OPEN)
+ eclat_map_close(map);
+ if (map->data && map->drv->map_free)
+ map->drv->map_free(map_dbg, map->data);
+ free(map->name);
+ free(map->keytrans);
+ free(map);
+}
+
+int
+eclat_map_config(struct grecs_node *node, struct eclat_map **return_map)
+{
+ char *mapname;
+ struct grecs_node *p;
+ struct eclat_map key, *map;
+ struct eclat_map_drv dkey, *drv;
+ int install, rc;
+
+ if (node->v.value->type != GRECS_TYPE_STRING) {
+ grecs_error(&p->locus, 0, "value must be scalar");
+ return eclat_map_failure;
+ }
+
+ mapname = node->v.value->v.string;
+
+ if (debug_level(map_dbg) > 1)
+ diag(&node->locus, "debug", "configuring map \"%s\"",
+ mapname);
+ map = eclat_map_lookup(mapname);
+ if (map) {
+ debug(map_dbg, 1,
+ ("map \"%s\" already configured", mapname));
+ *return_map = map;
+ return eclat_map_ok;
+ }
+
+ if (eclat_get_string_node(node, "type", 0, &p))
+ return eclat_map_failure;
+
+ dkey.name = p->v.value->v.string;
+ if (!mapdrv_symtab) {
+ grecs_error(&p->locus, 0, "no drivers compiled");
+ return eclat_map_failure;
+ }
+
+ drv = grecs_symtab_lookup_or_install(mapdrv_symtab, &dkey, NULL);
+ if (!drv) {
+ grecs_error(&p->locus, 0, "no driver for this type");
+ return eclat_map_failure;
+ }
+
+ if (!openmap_symtab) {
+ openmap_symtab = grecs_symtab_create(sizeof(key),
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+ map_free);
+ if (!openmap_symtab)
+ grecs_alloc_die();
+ }
+ install = 1;
+ key.name = (char*) mapname;
+ map = grecs_symtab_lookup_or_install(openmap_symtab, &key, &install);
+ if (!map)
+ die(EX_SOFTWARE, "cannot install map");
+ map->drv = drv;
+
+ rc = drv->map_config(map_dbg, node, &map->data);
+ if (rc != eclat_map_ok) {
+ grecs_error(&node->locus, 0, "map configration failed");
+ eclat_map_free(map);
+ return eclat_map_failure;
+ }
+
+ if (eclat_get_string_node(node, "key", 1, &p) == 0)
+ map->keytrans = grecs_strdup(p->v.value->v.string);
+
+ debug(map_dbg, 1, ("map \"%s\" configured", mapname));
+
+ *return_map = map;
+ return eclat_map_ok;
+}
+
+void
+eclat_map_free(struct eclat_map *map)
+{
+ grecs_symtab_remove(openmap_symtab, map);
+}
+
+int
+eclat_map_open(struct eclat_map *map)
+{
+ debug(map_dbg, 1, ("opening map \"%s\"", map->name));
+
+ if (map->flags & ECLAT_MAP_OPEN) {
+ debug(map_dbg, 1, ("map \"%s\" already open", map->name));
+ return eclat_map_ok;
+ }
+
+ if (map->drv->map_open(map_dbg, map->data)) {
+ err("failed to open map %s", map->name);
+ return eclat_map_failure;
+ }
+
+ debug(map_dbg, 1, ("map \"%s\" opened successfully", map->name));
+ map->flags |= ECLAT_MAP_OPEN;
+ return eclat_map_ok;
+}
+
+int
+eclat_map_close(struct eclat_map *map)
+{
+ int rc = eclat_map_ok;
+
+ debug(map_dbg, 1, ("closing map \"%s\"", map->name));
+ if (!(map->flags & ECLAT_MAP_OPEN)) {
+ debug(map_dbg, 1, ("map \"%s\" not open", map->name));
+ return eclat_map_ok;
+ }
+ if (map->drv->map_close)
+ rc = map->drv->map_close(map_dbg, map->data);
+
+ if (rc == eclat_map_ok) {
+ debug(map_dbg, 1,
+ ("map \"%s\" closed successfully", map->name));
+ map->flags &= ~ECLAT_MAP_OPEN;
+ } else
+ err("failed to close map %s", map->name);
+
+ return rc;
+}
+
+int
+eclat_map_get(struct eclat_map *map, const char *key, char **value)
+{
+ int rc;
+ struct wordsplit ws;
+
+ debug(map_dbg, 1,
+ ("looking up \"%s\" in map \"%s\"", key, map->name));
+ if (!(map->flags & ECLAT_MAP_OPEN)) {
+ debug(map_dbg, 1, ("map \"%s\" not open", map->name));
+ return eclat_map_failure;
+ }
+
+ if (map->keytrans) {
+ const char *kve[5];
+ kve[0] = "key";
+ kve[1] = key;
+ kve[2] = "map";
+ kve[3] = map->name;
+ kve[4] = NULL;
+
+ ws.ws_env = kve;
+
+ if (wordsplit(map->keytrans, &ws,
+ WRDSF_NOSPLIT | WRDSF_NOCMD |
+ WRDSF_ENV | WRDSF_ENV_KV))
+ die(EX_SOFTWARE, "error transforming key: %s",
+ wordsplit_strerror(&ws));
+ debug(map_dbg, 1,
+ ("transformed key \"%s\" => \"%s\"",
+ key, ws.ws_wordv[0]));
+ key = ws.ws_wordv[0];
+ }
+
+ rc = map->drv->map_get(map_dbg, map->data, key, value);
+
+ if (map->keytrans)
+ wordsplit_free(&ws);
+
+ debug(map_dbg, 1, ("result: \"%s\"", eclat_map_strerror(rc)));
+ if (rc == eclat_map_ok)
+ debug(map_dbg, 2, ("found value: \"%s\"", *value));
+ return rc;
+}
+
+const char *
+eclat_map_strerror(int rc)
+{
+ switch (rc) {
+ case eclat_map_ok:
+ return "success";
+ case eclat_map_failure:
+ return "failure";
+ case eclat_map_not_found:
+ return "not found";
+ }
+ return "unknown error";
+}
+
+struct foreach_closure {
+ int (*fun)(struct eclat_map *, void *);
+ void *data;
+};
+
+static int
+map_foreach(void *item, void *data)
+{
+ struct eclat_map *map = item;
+ struct foreach_closure *cp = data;
+
+ return cp->fun(map, cp->data);
+}
+
+void
+eclat_map_foreach(int (*fun)(struct eclat_map *, void *), void *data)
+{
+ struct foreach_closure cl;
+
+ if (!openmap_symtab)
+ return;
+
+ cl.fun = fun;
+ cl.data = data;
+ grecs_symtab_enumerate(openmap_symtab, map_foreach, &cl);
+}
+
+void
+eclat_map_free_all()
+{
+ if (openmap_symtab) {
+ grecs_symtab_free(openmap_symtab);
+ openmap_symtab = NULL;
+ }
+}
+

Return to:

Send suggestions and report system problems to the System administrator.