Blob Blame History Raw
From b39e5538a174b0fd25f6551ed291b9e0a05ca195 Mon Sep 17 00:00:00 2001
From: Aravinda VK <avishwan@redhat.com>
Date: Tue, 19 Jul 2016 17:28:14 +0530
Subject: [PATCH 31/86] eventsapi: Volume Set and Reset Events

Example of published data for Volume Set:

{
    "nodeid": NODEID,
    "ts": TIMESTAMP,
    "event": "VOLUME_SET",
    "message": {
        "name": VOLUME_NAME,
        "options": [[KEY1, VALUE1], [KEY2, VALUE2],..]
     }
}

Example of published data for Volume Reset:

{
    "nodeid": NODEID,
    "ts": TIMESTAMP,
    "event": "VOLUME_RESET",
    "message": {
        "name": VOLUME_NAME,
        "option": KEY
     }
}

> Reviewed-on: http://review.gluster.org/14973
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: Atin Mukherjee <amukherj@redhat.com>

BUG: 1351589
Change-Id: If30cc95396459b2a9993b3412ee6d05d27f6a86a
Signed-off-by: Aravinda VK <avishwan@redhat.com>

Reviewed-on: https://code.engineering.redhat.com/gerrit/84737
Reviewed-by: Milind Changire <mchangir@redhat.com>
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
---
 cli/src/cli-cmd-volume.c      |   62 +++++++++++++++++++++++++++++++++++++++++
 events/eventskeygen.py        |    2 +
 events/src/eventtypes.py      |    2 +
 events/src/handlers.py        |   19 ++++++++++++
 libglusterfs/src/eventtypes.h |    2 +
 5 files changed, 87 insertions(+), 0 deletions(-)

diff --git a/cli/src/cli-cmd-volume.c b/cli/src/cli-cmd-volume.c
index 5296093..4202622 100644
--- a/cli/src/cli-cmd-volume.c
+++ b/cli/src/cli-cmd-volume.c
@@ -665,6 +665,10 @@ cli_cmd_volume_reset_cbk (struct cli_state *state, struct cli_cmd_word *word,
         call_frame_t            *frame = NULL;
         dict_t                  *options = NULL;
         cli_local_t             *local = NULL;
+#if (USE_EVENTS)
+        int                      ret1    = -1;
+        char                    *tmp_opt = NULL;
+#endif
 
         proc = &cli_rpc_prog->proctable[GLUSTER_CLI_RESET_VOLUME];
 
@@ -692,6 +696,18 @@ out:
                         cli_out ("Volume reset failed");
         }
 
+#if (USE_EVENTS)
+        if (ret == 0) {
+                ret1 = dict_get_str (options, "key", &tmp_opt);
+                if (ret1)
+                        tmp_opt = "";
+
+                gf_event (EVENT_VOLUME_RESET, "name=%s;option=%s",
+                          (char *)words[2],
+                          tmp_opt);
+        }
+#endif
+
         CLI_STACK_DESTROY (frame);
 
         return ret;
@@ -758,6 +774,15 @@ cli_cmd_volume_set_cbk (struct cli_state *state, struct cli_cmd_word *word,
         cli_local_t             *local = NULL;
         char                    *op_errstr = NULL;
 
+#if (USE_EVENTS)
+        int                      ret1          = -1;
+        int                      i             = 1;
+        char                     dict_key[50]  = {0,};
+        char                    *tmp_opt       = NULL;
+        char                    *opts_str      = NULL;
+        int                      num_options   = 0;
+#endif
+
         proc = &cli_rpc_prog->proctable[GLUSTER_CLI_SET_VOLUME];
 
         frame = create_frame (THIS, THIS->ctx->pool);
@@ -790,6 +815,43 @@ out:
                         cli_out ("Volume set failed");
         }
 
+#if (USE_EVENTS)
+        if (ret == 0) {
+                ret1 = dict_get_int32 (options, "count", &num_options);
+                if (ret1)
+                        num_options = 0;
+                else
+                        num_options = num_options/2;
+
+                /* Initialize opts_str */
+                opts_str = gf_strdup ("");
+
+                /* Prepare String in format options=KEY1,VALUE1,KEY2,VALUE2 */
+                for (i = 1; i <= num_options; i++) {
+                        sprintf (dict_key, "key%d", i);
+                        ret1 = dict_get_str (options, dict_key, &tmp_opt);
+                        if (ret1)
+                                tmp_opt = "";
+
+                        gf_asprintf (&opts_str, "%s,%s", opts_str, tmp_opt);
+
+                        sprintf (dict_key, "value%d", i);
+                        ret1 = dict_get_str (options, dict_key, &tmp_opt);
+                        if (ret1)
+                                tmp_opt = "";
+
+                        gf_asprintf (&opts_str, "%s,%s", opts_str, tmp_opt);
+                }
+
+                gf_event (EVENT_VOLUME_SET, "name=%s;options=%s",
+                          (char *)words[2],
+                          opts_str);
+
+                /* Allocated by gf_strdup and gf_asprintf */
+                GF_FREE (opts_str);
+        }
+#endif
+
         CLI_STACK_DESTROY (frame);
 
         return ret;
diff --git a/events/eventskeygen.py b/events/eventskeygen.py
index 656a7dc..f9bdb9f 100644
--- a/events/eventskeygen.py
+++ b/events/eventskeygen.py
@@ -25,6 +25,8 @@ keys = (
     "EVENT_VOLUME_START",
     "EVENT_VOLUME_STOP",
     "EVENT_VOLUME_DELETE",
+    "EVENT_VOLUME_SET",
+    "EVENT_VOLUME_RESET",
 )
 
 LAST_EVENT = "EVENT_LAST"
diff --git a/events/src/eventtypes.py b/events/src/eventtypes.py
index 4812e65..b09e5bc 100644
--- a/events/src/eventtypes.py
+++ b/events/src/eventtypes.py
@@ -6,4 +6,6 @@ all_events = [
     "EVENT_VOLUME_START",
     "EVENT_VOLUME_STOP",
     "EVENT_VOLUME_DELETE",
+    "EVENT_VOLUME_SET",
+    "EVENT_VOLUME_RESET",
 ]
diff --git a/events/src/handlers.py b/events/src/handlers.py
index 9b756a9..21d3e83 100644
--- a/events/src/handlers.py
+++ b/events/src/handlers.py
@@ -19,3 +19,22 @@ def generic_handler(ts, key, data):
     Ex: handle_event_volume_create(ts, key, data)
     """
     utils.publish(ts, key, data)
+
+
+def handle_event_volume_set(ts, key, data):
+    """
+    Recieved data will have all the options as one string, split into
+    list of options. "key1,value1,key2,value2" into
+    [[key1, value1], [key2, value2]]
+    """
+    opts = data.get("options", "").strip(",").split(",")
+    data["options"] = []
+    for i, opt in enumerate(opts):
+        if i % 2 == 0:
+            # Add new array with key
+            data["options"].append([opt])
+        else:
+            # Add to the last added array
+            data["options"][-1].append(opt)
+
+    utils.publish(ts, key, data)
diff --git a/libglusterfs/src/eventtypes.h b/libglusterfs/src/eventtypes.h
index 874f8cc..20c4b02 100644
--- a/libglusterfs/src/eventtypes.h
+++ b/libglusterfs/src/eventtypes.h
@@ -16,6 +16,8 @@ typedef enum {
     EVENT_VOLUME_START,
     EVENT_VOLUME_STOP,
     EVENT_VOLUME_DELETE,
+    EVENT_VOLUME_SET,
+    EVENT_VOLUME_RESET,
     EVENT_LAST
 } eventtypes_t;
 
-- 
1.7.1