[6.0] bf1d9979c Add JSON support for the CLI "vcl.list" command.

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Oct 31 13:08:28 UTC 2018


commit bf1d9979cfa8fe721805016777ed6de147246d9e
Author: Geoff Simmons <geoff at uplex.de>
Date:   Sun Sep 23 22:49:07 2018 +0200

    Add JSON support for the CLI "vcl.list" command.

diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index e705a86a1..ae846e0a6 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -664,6 +664,52 @@ vcl_cli_list(struct cli *cli, const char * const *av, void *priv)
 	}
 }
 
+static void v_matchproto_(cli_func_t)
+vcl_cli_list_json(struct cli *cli, const char * const *av, void *priv)
+{
+	struct vcl *vcl;
+
+	(void)priv;
+	ASSERT_CLI();
+	VCLI_JSON_begin(cli, 2, av);
+	VCLI_Out(cli, ",\n");
+	VTAILQ_FOREACH(vcl, &vcl_head, list) {
+		VCLI_Out(cli, "{\n");
+		VSB_indent(cli->sb, 2);
+		VCLI_Out(cli, "\"status\": ");
+		if (vcl == vcl_active) {
+			VCLI_Out(cli, "\"active\",\n");
+		} else if (vcl->discard) {
+			VCLI_Out(cli, "\"discarded\",\n");
+		} else
+			VCLI_Out(cli, "\"available\",\n");
+		VCLI_Out(cli, "\"state\": \"%s\",\n", vcl->state);
+		VCLI_Out(cli, "\"temperature\": \"%s\",\n", vcl->temp);
+		VCLI_Out(cli, "\"busy\": %u,\n", vcl->busy);
+		VCLI_Out(cli, "\"name\": \"%s\"", vcl->loaded_name);
+		if (vcl->label != NULL) {
+			VCLI_Out(cli, ",\n");
+			VCLI_Out(cli, "\"label\": {\n");
+			VSB_indent(cli->sb, 2);
+				VCLI_Out(cli, "\"name\": \"%s\"",
+					 vcl->label->loaded_name);
+			if (vcl->nrefs)
+				VCLI_Out(cli, ",\n\"refs\": %d", vcl->nrefs);
+			VCLI_Out(cli, "\n");
+			VCLI_Out(cli, "}");
+			VSB_indent(cli->sb, -2);
+		} else if (vcl->nlabels > 0) {
+			VCLI_Out(cli, ",\n");
+			VCLI_Out(cli, "\"labels\": %d", vcl->nlabels);
+		}
+		VSB_indent(cli->sb, -2);
+		VCLI_Out(cli, "\n}");
+		if (VTAILQ_NEXT(vcl, list) != NULL)
+			VCLI_Out(cli, ",\n");
+	}
+	VCLI_JSON_end(cli);
+}
+
 static void v_matchproto_(cli_func_t)
 vcl_cli_load(struct cli *cli, const char * const *av, void *priv)
 {
@@ -823,7 +869,7 @@ vcl_cli_show(struct cli *cli, const char * const *av, void *priv)
 
 static struct cli_proto vcl_cmds[] = {
 	{ CLICMD_VCL_LOAD,		"", vcl_cli_load },
-	{ CLICMD_VCL_LIST,		"", vcl_cli_list },
+	{ CLICMD_VCL_LIST,		"", vcl_cli_list, vcl_cli_list_json },
 	{ CLICMD_VCL_STATE,		"", vcl_cli_state },
 	{ CLICMD_VCL_DISCARD,		"", vcl_cli_discard },
 	{ CLICMD_VCL_USE,		"", vcl_cli_use },
diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 5974b589a..8c05c5215 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -795,6 +795,61 @@ mcf_vcl_list(struct cli *cli, const char * const *av, void *priv)
 	}
 }
 
+static void v_matchproto_(cli_func_t)
+mcf_vcl_list_json(struct cli *cli, const char * const *av, void *priv)
+{
+	unsigned status;
+	char *p;
+	struct vclprog *vp;
+	struct vcldep *vd;
+
+	/* NB: Shall generate same output as vcl_cli_list() */
+
+	(void)priv;
+	if (MCH_Running()) {
+		if (!mgt_cli_askchild(&status, &p, "vcl.list -j\n")) {
+			VCLI_SetResult(cli, status);
+			VCLI_Out(cli, "%s", p);
+		}
+		free(p);
+	} else {
+		VCLI_JSON_begin(cli, 2, av);
+		VCLI_Out(cli, ",\n");
+		VTAILQ_FOREACH(vp, &vclhead, list) {
+			VCLI_Out(cli, "{\n");
+			VSB_indent(cli->sb, 2);
+			VCLI_Out(cli, "\"status\": \"%s\",\n",
+			    vp == active_vcl ? "active" : "available");
+			VCLI_Out(cli, "\"state\": \"%s\",\n", vp->state);
+			VCLI_Out(cli, "\"temperature\": \"%s\",\n",
+			    vp->warm ? VCL_STATE_WARM : VCL_STATE_COLD);
+			VCLI_Out(cli, "\"name\": \"%s\"", vp->name);
+			if (mcf_is_label(vp)) {
+				vd = VTAILQ_FIRST(&vp->dfrom);
+				AN(vd);
+				VCLI_Out(cli, ",\n");
+				VCLI_Out(cli, "\"label\": {\n");
+				VSB_indent(cli->sb, 2);
+				VCLI_Out(cli, "\"name\": \"%s\"", vd->to->name);
+				if (vp->nto > 0)
+					VCLI_Out(cli, ",\n\"refs\": %d",
+						 vp->nto);
+				VSB_indent(cli->sb, -2);
+				VCLI_Out(cli, "\n");
+				VCLI_Out(cli, "}");
+			} else if (vp->nto > 0) {
+				VCLI_Out(cli, ",\n");
+				VCLI_Out(cli, "\"labels\": %d", vp->nto);
+			}
+			VSB_indent(cli->sb, -2);
+			VCLI_Out(cli, "\n}");
+			if (VTAILQ_NEXT(vp, list) != NULL)
+				VCLI_Out(cli, ",\n");
+		}
+		VCLI_JSON_end(cli);
+	}
+}
+
 static void v_matchproto_(cli_func_t)
 mcf_vcl_label(struct cli *cli, const char * const *av, void *priv)
 {
@@ -888,7 +943,7 @@ static struct cli_proto cli_vcl[] = {
 	{ CLICMD_VCL_USE,		"", mcf_vcl_use },
 	{ CLICMD_VCL_STATE,		"", mcf_vcl_state },
 	{ CLICMD_VCL_DISCARD,		"", mcf_vcl_discard },
-	{ CLICMD_VCL_LIST,		"", mcf_vcl_list },
+	{ CLICMD_VCL_LIST,		"", mcf_vcl_list, mcf_vcl_list_json },
 	{ CLICMD_VCL_LABEL,		"", mcf_vcl_label },
 	{ NULL }
 };
diff --git a/bin/varnishtest/tests/b00032.vtc b/bin/varnishtest/tests/b00032.vtc
index 89cb26f52..7aa6cddfa 100644
--- a/bin/varnishtest/tests/b00032.vtc
+++ b/bin/varnishtest/tests/b00032.vtc
@@ -11,9 +11,13 @@ varnish v1 -vcl+backend {}
 varnish v1 -vcl+backend {}
 
 varnish v1 -cliok vcl.list
+varnish v1 -clijson "vcl.list -j"
 
 varnish v1 -cliok start
 
+varnish v1 -cliok vcl.list
+varnish v1 -clijson "vcl.list -j"
+
 varnish v1 -cliok "vcl.use vcl1"
 
 varnish v1 -clierr 300 "vcl.discard vcl1"
diff --git a/bin/varnishtest/tests/s00005.vtc b/bin/varnishtest/tests/s00005.vtc
index 67cd3a95a..4dac2f167 100644
--- a/bin/varnishtest/tests/s00005.vtc
+++ b/bin/varnishtest/tests/s00005.vtc
@@ -39,6 +39,7 @@ varnish v1 -clierr 106 "vcl.label foo vcl0"
 varnish v1 -cliok "vcl.label foo vcl2"
 varnish v1 -cliok "vcl.label bar vcl2"
 varnish v1 -cliok "vcl.list"
+varnish v1 -clijson "vcl.list -j"
 varnish v1 -clierr 300 "vcl.discard vcl2"
 varnish v1 -cliok "vcl.discard bar"
 varnish v1 -cliok "vcl.label foo vcl1"
@@ -100,6 +101,7 @@ varnish v1 -cliok "vcl.label label1 vcl1"
 varnish v1 -cliok "vcl.label label2 vcl1"
 varnish v1 -cliok "vcl.label label3 vcl1"
 varnish v1 -cliok "vcl.list"
+varnish v1 -clijson "vcl.list -j"
 
 varnish v1 -start
 varnish v1 -cliok "vcl.list"
@@ -111,6 +113,7 @@ client c1 -run
 #######################################################################
 
 varnish v1 -cliok vcl.list
+varnish v1 -clijson "vcl.list -j"
 
 varnish v1 -vcl+backend { }
 
diff --git a/bin/varnishtest/tests/v00045.vtc b/bin/varnishtest/tests/v00045.vtc
index a0c511465..0f9cdf5a6 100644
--- a/bin/varnishtest/tests/v00045.vtc
+++ b/bin/varnishtest/tests/v00045.vtc
@@ -18,6 +18,7 @@ varnish v1 -cliok "vcl.state vcl1 cold"
 delay 1
 
 varnish v1 -cliexpect "cold/cooling.*vcl1" vcl.list
+varnish v1 -clijson "vcl.list -j"
 
 # It can't be warmed up yet
 delay 1
@@ -26,6 +27,7 @@ varnish v1 -cliexpect "vmod-debug ref on vcl1" "vcl.state vcl1 warm"
 # It will eventually cool down
 delay 2
 varnish v1 -cliexpect "cold/cold.*vcl1" vcl.list
+varnish v1 -clijson "vcl.list -j"
 
 # At this point it becomes possible to warm up again
 varnish v1 -cliok "vcl.state vcl1 warm"


More information about the varnish-commit mailing list