From 7e29a1a2496a922457bdeb02332e9b78c97b4d81 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Wed, 22 Mar 2017 12:43:20 +0530 Subject: gluster-block: support json response This is how cli response looks like, on supplying '--json*' flag to cmd-args: $ gluster-block create block-test/sample-block1 ha 1 localhost.localdomain 1GiB --json (or) $ gluster-block create block-test/sample-block2 ha 1 localhost.localdomain 1GiB --json-spaced { "IQN": "iqn.2016-12.org.gluster-block:681af106-85f1-4a02-a122-57c80903458c", \ "PORTAL(S)": [ "localhost.localdomain:3260" ], "RESULT": "SUCCESS" } $ gluster-block create block-test/sample-block3 ha 1 localhost.localdomain 1GiB --json-plain {"IQN":"iqn.2016-12.org.gluster-block:0fdf6647-57f2-477f-8dd4-54a3de06e410",\ "PORTAL(S)":["localhost.localdomain:3260"],"RESULT":"SUCCESS"} $ gluster-block create block-test/sample-block4 ha 1 localhost.localdomain 1GiB --json-pretty { "IQN":"iqn.2016-12.org.gluster-block:e92ca4a0-5325-4c4b-a407-9e75790e4c7f", "PORTAL(S)":[ "localhost.localdomain:3260" ], "RESULT":"SUCCESS" } Change-Id: Ie51039e3dee0b3357d2347b4087e0fbe299aa29e Fixes: #3 Signed-off-by: Prasanna Kumar Kalever --- utils/Makefile.am | 2 +- utils/common.c | 24 ++++++++++++++++++++++++ utils/common.h | 15 +++++++++++++++ utils/utils.h | 30 +++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 6 deletions(-) (limited to 'utils') diff --git a/utils/Makefile.am b/utils/Makefile.am index 9a79c29..40442c3 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -4,7 +4,7 @@ libgb_la_SOURCES = common.c utils.c noinst_HEADERS = common.h utils.h -libgb_la_CFLAGS = -DDATADIR=\"$(localstatedir)\" +libgb_la_CFLAGS = -DDATADIR=\"$(localstatedir)\" -I$(top_builddir)/rpc/rpcl libgb_ladir = $(includedir)/gluster-block/utils diff --git a/utils/common.c b/utils/common.c index a605f0d..c42ce03 100644 --- a/utils/common.c +++ b/utils/common.c @@ -12,6 +12,30 @@ # include "common.h" +enum JsonResponseFormat +jsonResponseFormatParse(const char *opt) +{ + int i; + + + if (!opt) { + return GB_JSON_MAX; + } + + if (strlen (opt) < 2 || opt[0] != '-' || opt[1] != '-') { + /*json option is not given*/ + return GB_JSON_NONE; + } + + for (i = 0; i < GB_JSON_MAX; i++) { + if (!strcmp(opt, JsonResponseFormatLookup[i])) { + return i; + } + } + + return i; +} + ssize_t glusterBlockCreateParseSize(const char *dom, char *value) diff --git a/utils/common.h b/utils/common.h index c5ec334..b43220f 100644 --- a/utils/common.h +++ b/utils/common.h @@ -13,6 +13,7 @@ # define _COMMON_H 1 # include "utils.h" +# include "block.h" # define GB_LOGDIR DATADIR "/log/gluster-block" # define GB_INFODIR DATADIR "/run" @@ -37,6 +38,20 @@ # define SUN_PATH_MAX (sizeof(struct sockaddr_un) - sizeof(unsigned short int)) /*sun_family*/ +static const char *const JsonResponseFormatLookup[] = { + [GB_JSON_NONE] = "", + + [GB_JSON_PLAIN] = "--json-plain", + [GB_JSON_SPACED] = "--json-spaced", + [GB_JSON_PRETTY] = "--json-pretty", + [GB_JSON_DEFAULT] = "--json", + + [GB_JSON_MAX] = NULL, +}; + + +enum JsonResponseFormat jsonResponseFormatParse(const char *opt); + ssize_t glusterBlockCreateParseSize(const char *dom, char *value); # endif /* _COMMON_H */ diff --git a/utils/utils.h b/utils/utils.h index f551e46..b0cf778 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -46,6 +46,10 @@ # define FAILED_DEPENDENCY "failed dependency, check if you have targetcli and tcmu-runner installed" +# define FMT_WARN(fmt...) do { if (0) printf (fmt); } while (0) + +# define GB_ASPRINTF(ptr, fmt...) ({FMT_WARN (fmt); \ + int __ret=asprintf(ptr, ##fmt);__ret;}) # define LOCK(x) \ do { \ @@ -86,26 +90,32 @@ fclose(fd); \ } while (0) -# define GB_METALOCK_OR_GOTO(lkfd, volume, ret, label) \ +# define GB_METALOCK_OR_GOTO(lkfd, volume, errCode, errMsg, label) \ do { \ struct flock lock = {0, }; \ lock.l_type = F_WRLCK; \ if (glfs_posix_lock (lkfd, F_SETLKW, &lock)) { \ LOG("mgmt", GB_LOG_ERROR, "glfs_posix_lock() on " \ "volume %s failed[%s]", volume, strerror(errno)); \ - ret = -1; \ + errCode = errno; \ + if (!errMsg) { \ + GB_ASPRINTF (&errMsg, "Not able to acquire " \ + "lock on %s[%s]", volume, strerror(errCode));\ + } \ goto label; \ } \ } while (0) # define GB_METAUPDATE_OR_GOTO(lock, glfs, fname, \ - volume, ret, label,...) \ + volume, ret, errMsg, label,...) \ do { \ char *write; \ struct glfs_fd *tgmfd; \ LOCK(lock); \ ret = glfs_chdir (glfs, GB_METADIR); \ if (ret) { \ + GB_ASPRINTF(&errMsg, "Failed to update transaction log "\ + "for %s/%s[%s]", volume, fname, strerror(errno)); \ LOG("gfapi", GB_LOG_ERROR, "glfs_chdir(%s) on " \ "volume %s failed[%s]", GB_METADIR, volume, \ strerror(errno)); \ @@ -116,6 +126,8 @@ tgmfd = glfs_creat(glfs, fname, O_WRONLY | O_APPEND, \ S_IRUSR | S_IWUSR); \ if (!tgmfd) { \ + GB_ASPRINTF(&errMsg, "Failed to update transaction log "\ + "for %s/%s[%s]", volume, fname, strerror(errno)); \ LOG("mgmt", GB_LOG_ERROR, "glfs_creat(%s): on " \ "volume %s failed[%s]", fname, volume, \ strerror(errno)); \ @@ -123,12 +135,14 @@ ret = -1; \ goto label; \ } \ - if (asprintf(&write, __VA_ARGS__) < 0) { \ + if (GB_ASPRINTF(&write, __VA_ARGS__) < 0) { \ UNLOCK(lock); \ ret = -1; \ goto label; \ } \ if(glfs_write (tgmfd, write, strlen(write), 0) < 0) { \ + GB_ASPRINTF(&errMsg, "Failed to update transaction log "\ + "for %s/%s[%s]", volume, fname, strerror(errno)); \ LOG("mgmt", GB_LOG_ERROR, "glfs_write(%s): on " \ "volume %s failed[%s]", fname, volume, \ strerror(errno)); \ @@ -138,6 +152,8 @@ } \ GB_FREE(write); \ if (tgmfd && glfs_close(tgmfd) != 0) { \ + GB_ASPRINTF(&errMsg, "Failed to update transaction log "\ + "for %s/%s[%s]", volume, fname, strerror(errno)); \ LOG("mgmt", GB_LOG_ERROR, "glfs_close(%s): on " \ "volume %s failed[%s]", fname, volume, \ strerror(errno)); \ @@ -148,11 +164,15 @@ UNLOCK(lock); \ } while (0) -# define GB_METAUNLOCK(lkfd, volume, ret) \ +# define GB_METAUNLOCK(lkfd, volume, ret, errMsg) \ do { \ struct flock lock = {0, }; \ lock.l_type = F_UNLCK; \ if (glfs_posix_lock(lkfd, F_SETLK, &lock)) { \ + if (!errMsg) { \ + GB_ASPRINTF (&errMsg, "Not able to acquire " \ + "lock on %s[%s]", volume, strerror(errno)); \ + } \ LOG("mgmt", GB_LOG_ERROR, "glfs_posix_lock() on " \ "volume %s failed[%s]", volume, strerror(errno)); \ ret = -1; \ -- cgit