diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-10-07 20:12:34 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-10-07 20:12:34 +0300 |
commit | 1fbcbdb432e945aa3f63be5b9170e94a0c9ef6c3 (patch) | |
tree | 209822ff7ef7766b94a714438473dcf8f12c4e1f /src | |
parent | cabb35e68d34bcccaa39a223eaa037b335c81558 (diff) | |
download | eclat-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 'src')
-rw-r--r-- | src/accfile.c | 1 | ||||
-rw-r--r-- | src/asscaddr.c | 1 | ||||
-rw-r--r-- | src/cmdline.opt | 6 | ||||
-rw-r--r-- | src/config.c | 11 | ||||
-rw-r--r-- | src/dscrinstattr.c | 1 | ||||
-rw-r--r-- | src/dscrinsts.c | 1 | ||||
-rw-r--r-- | src/dscrinststat.c | 3 | ||||
-rw-r--r-- | src/dscrsecgrps.c | 8 | ||||
-rw-r--r-- | src/eclat.c | 4 | ||||
-rw-r--r-- | src/eclat.h | 2 | ||||
-rw-r--r-- | src/getconout.c | 1 | ||||
-rw-r--r-- | src/startinst.c | 1 | ||||
-rw-r--r-- | src/util.c | 30 |
13 files changed, 66 insertions, 4 deletions
diff --git a/src/accfile.c b/src/accfile.c index 07c9c2e..3aa9bdb 100644 --- a/src/accfile.c +++ b/src/accfile.c @@ -121,6 +121,7 @@ access_file_lookup(const char *filename, grecs_txtacc_grow_char(acc, c); grecs_txtacc_grow_char(acc, 0); *secret_key_ptr = grecs_txtacc_finish(acc, 1); + grecs_txtacc_free(acc); rc = 0; } else rc = 1; diff --git a/src/asscaddr.c b/src/asscaddr.c index a5ba86e..ea4488d 100644 --- a/src/asscaddr.c +++ b/src/asscaddr.c @@ -36,6 +36,7 @@ eclat_associate_address(CURL *curl, int argc, char **argv) q = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0, endpoint, "/"); eclat_query_add_param(q, "Action", "AssociateAddress"); + translate_ids(1, argv, "InstanceId"); eclat_query_add_param(q, "InstanceId", argv[0]); if (vpc) { eclat_query_add_param(q, "AllocationId", argv[1]); diff --git a/src/cmdline.opt b/src/cmdline.opt index c19f628..94330ff 100644 --- a/src/cmdline.opt +++ b/src/cmdline.opt @@ -132,6 +132,12 @@ BEGIN sort_option = 1; END +OPTION(translate,x,, + [<translate resource IDs>]) +BEGIN + translate_option = 1; +END + GROUP(Preprocessor control) OPTION(include-directory,I,DIR, diff --git a/src/config.c b/src/config.c index 69835b9..7271b36 100644 --- a/src/config.c +++ b/src/config.c @@ -169,6 +169,8 @@ static struct grecs_keyword eclat_kw[] = { grecs_type_string, GRECS_MULT, NULL, 0, cb_define_format }, { "format-file", "file", "Read format from <file>", grecs_type_string, GRECS_DFLT, &format_file }, + { "map", "name: string", "Configure a map", + grecs_type_section, GRECS_INAC }, { NULL } }; @@ -207,6 +209,15 @@ config_finish(struct grecs_node *tree) { struct grecs_node *node; + for (node = tree; node; node = node->next) { + struct eclat_map *map; + + node = grecs_find_node(node, "map"); + if (!node) + break; + eclat_map_config(node, &map); + } + grecs_tree_reduce(tree, eclat_kw, GRECS_AGGR); if (debug_level(ECLAT_DEBCAT_CONF)) { grecs_print_node(tree, GRECS_NODE_FLAG_DEFAULT, stderr); diff --git a/src/dscrinstattr.c b/src/dscrinstattr.c index 543e456..7193bda 100644 --- a/src/dscrinstattr.c +++ b/src/dscrinstattr.c @@ -67,6 +67,7 @@ eclat_describe_instance_attribute(CURL *curl, int argc, char **argv) argc, argv, &i); argv += i; argc -= i; + translate_ids(argc, argv, "InstanceId"); if (argc != 2) die(EX_USAGE, "wrong number of arguments"); diff --git a/src/dscrinsts.c b/src/dscrinsts.c index 145c50d..865c2ce 100644 --- a/src/dscrinsts.c +++ b/src/dscrinsts.c @@ -147,6 +147,7 @@ eclat_describe_instances(CURL *curl, int argc, char **argv) parse_options(argc, argv, &i); argv += i; argc -= i; + translate_ids(argc, argv, "InstanceId"); q = describe_query_create(curl, "DescribeInstances", argc, argv, "InstanceId"); diff --git a/src/dscrinststat.c b/src/dscrinststat.c index 241e9ed..67e761a 100644 --- a/src/dscrinststat.c +++ b/src/dscrinststat.c @@ -27,7 +27,8 @@ eclat_describe_instance_status(CURL *curl, int argc, char **argv) parse_options(argc, argv, &i); argv += i; argc -= i; - + translate_ids(argc, argv, "InstanceId"); + q = describe_query_create(curl, "DescribeInstanceStatus", argc, argv, "InstanceId"); diff --git a/src/dscrsecgrps.c b/src/dscrsecgrps.c index 7049ddb..238c569 100644 --- a/src/dscrsecgrps.c +++ b/src/dscrsecgrps.c @@ -23,13 +23,15 @@ eclat_describe_security_groups(CURL *curl, int argc, char **argv) { int i; struct ec2_query *q; - + const char *resid = name_option ? "GroupName" : "GroupId"; + parse_options(argc, argv, &i); argv += i; argc -= i; - + translate_ids(argc, argv, resid); + q = describe_query_create(curl, "DescribeSecurityGroups", argc, argv, - name_option ? "GroupName" : "GroupId"); + resid); return eclat_send_query(curl, q); } diff --git a/src/eclat.c b/src/eclat.c index f229e77..704ac56 100644 --- a/src/eclat.c +++ b/src/eclat.c @@ -592,6 +592,8 @@ main(int argc, char **argv) proginfo.print_help_hook = listcmd; debug_init(); forlan_init(); + eclat_map_init(); + eclat_map_drv_register(&eclat_map_drv_file); config_init(); parse_options(argc, argv, &index); @@ -706,5 +708,7 @@ main(int argc, char **argv) fputc('\n', stdout); } + eclat_map_free_all(); + exit(rc); } diff --git a/src/eclat.h b/src/eclat.h index a82a28a..f371ea2 100644 --- a/src/eclat.h +++ b/src/eclat.h @@ -45,6 +45,7 @@ extern char *access_file_name; extern char *access_key; extern char *secret_key; extern char *format_file; +extern int translate_option; typedef int (*config_finish_hook_t) (void*); @@ -100,6 +101,7 @@ struct filter_descr *available_filters; void list_filters(FILE *fp); int get_scr_cols(void); +void translate_ids(int argc, char **argv, const char *mapname); int get_access_creds(const char *id, char **access_key_ptr, char **secret_key_ptr); diff --git a/src/getconout.c b/src/getconout.c index a5aab97..5ea5044 100644 --- a/src/getconout.c +++ b/src/getconout.c @@ -34,6 +34,7 @@ eclat_get_console_output(CURL *curl, int argc, char **argv) die(EX_USAGE, "not enough arguments"); else if (i > 1) die(EX_USAGE, "only one argument is allowed"); + translate_ids(argc, argv, "InstanceId"); q = describe_query_create(curl, "GetConsoleOutput", argc, argv, "InstanceId"); diff --git a/src/startinst.c b/src/startinst.c index d284694..3c34526 100644 --- a/src/startinst.c +++ b/src/startinst.c @@ -35,6 +35,7 @@ start_stop_instance(CURL *curl, const char *action, int argc, char **argv) if (argc == 0) die(EX_USAGE, "no instance ids"); + translate_ids(argc, argv, "InstanceId"); q = eclat_query_create(use_ssl ? EC2_QF_HTTPS : 0, endpoint, "/"); eclat_query_add_param(q, "Action", action); @@ -18,6 +18,36 @@ #include <termios.h> #include <sys/ioctl.h> +int translate_option; + +void +translate_ids(int argc, char **argv, const char *mapname) +{ + int i; + struct eclat_map *map; + char *val; + + if (!translate_option) + return; + map = eclat_map_lookup(mapname); + if (!map) + die(EX_UNAVAILABLE, "no such map: %s", mapname); + + if (eclat_map_open(map) != eclat_map_ok) + exit(EX_UNAVAILABLE); + + for (i = 0; i < argc; i++) { + if (!strchr(argv[i], '=')) { + int rc = eclat_map_get(map, argv[i], &val); + if (rc != eclat_map_ok) { + die(EX_UNAVAILABLE, "cannot translate %s: %s", + argv[i], eclat_map_strerror(rc)); + } + argv[i] = val; + } + } +} + int get_scr_cols() { |