blob: fc6a6996b6252923db60c4fa3c9edbe07a212acd [file] [log] [blame]
Tomáš Pecka5e070252022-05-20 20:41:21 +02001From 2fda28e34623cd24752ecad2dcea14f7ab46cb02 Mon Sep 17 00:00:00 2001
Tomáš Peckaae301762021-10-13 10:50:37 +02002From: Tomas Pecka <peckato1@users.noreply.github.com>
3Date: Wed, 6 Oct 2021 10:11:31 +0200
4Subject: [PATCH 9/9] networkctl: JSON output in networkctl lldp
5
6`networkctl lldp` now outputs also in JSON format when `--json=*`
7argument passed. The LLDP neighbors are listed in per interface lists.
8---
Tomáš Pecka5e070252022-05-20 20:41:21 +02009 src/network/networkctl.c | 118 ++++++++++++++++++++++++++-------------
10 1 file changed, 79 insertions(+), 39 deletions(-)
Tomáš Peckaae301762021-10-13 10:50:37 +020011
12diff --git a/src/network/networkctl.c b/src/network/networkctl.c
Václav Kubernát8cd61562021-12-08 13:27:31 +010013index 9fbeed6908..0f48aa4bf7 100644
Tomáš Peckaae301762021-10-13 10:50:37 +020014--- a/src/network/networkctl.c
15+++ b/src/network/networkctl.c
Václav Kubernát8cd61562021-12-08 13:27:31 +010016@@ -2507,12 +2507,14 @@ typedef struct LLDPUserdata {
17 uint16_t capabilities_all;
18
19 Table *table;
20+ JsonVariant *json;
Tomáš Peckaae301762021-10-13 10:50:37 +020021
22 char *link_name;
Tomáš Peckaae301762021-10-13 10:50:37 +020023 } LLDPUserdata;
24
Václav Kubernát8cd61562021-12-08 13:27:31 +010025 static void lldp_userdata_freep(LLDPUserdata* p) {
26 table_unref(p->table);
27+ json_variant_unref(p->json);
28 }
Tomáš Peckaae301762021-10-13 10:50:37 +020029
Václav Kubernát8cd61562021-12-08 13:27:31 +010030 static int lldp_neighbors_varlink_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) {
31@@ -2556,6 +2558,34 @@ static int lldp_neighbors_varlink_reply(Varlink *link, JsonVariant *parameters,
32 udata->neighbors_count += 1;
33 udata->capabilities_all |= entry.capabilities;
34
35+ if (udata->json) {
Tomáš Peckaae301762021-10-13 10:50:37 +020036+ _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
Václav Kubernát8cd61562021-12-08 13:27:31 +010037+ _cleanup_(json_variant_unrefp) JsonVariant *neighbors = NULL;
Tomáš Peckaae301762021-10-13 10:50:37 +020038+
39+ capabilities = lldp_capabilities_to_string(entry.capabilities, false);
40+
41+ r = json_build(&v, JSON_BUILD_OBJECT(
42+ JSON_BUILD_PAIR("neighbor", JSON_BUILD_OBJECT(
43+ JSON_BUILD_PAIR_CONDITION(entry.chassis_id, "chassisId", JSON_BUILD_STRING(entry.chassis_id)),
44+ JSON_BUILD_PAIR_CONDITION(entry.port_id, "portId", JSON_BUILD_STRING(entry.port_id)),
45+ JSON_BUILD_PAIR_CONDITION(entry.system_name, "systemName", JSON_BUILD_STRING(entry.system_name)),
46+ JSON_BUILD_PAIR_CONDITION(entry.port_description, "portDescription", JSON_BUILD_STRING(entry.port_description)),
47+ JSON_BUILD_PAIR_CONDITION(entry.capabilities, "enabledCapabilities", JSON_BUILD_STRING(capabilities))))));
48+ if (r < 0)
49+ return r;
50+
Václav Kubernát8cd61562021-12-08 13:27:31 +010051+
52+ neighbors = json_variant_ref(json_variant_by_key(udata->json, udata->link_name));
53+
54+ r = json_variant_append_array(&neighbors, v);
55+ if (r < 0)
56+ return r;
57+
58+ r = json_variant_set_field(&udata->json, udata->link_name, neighbors);
Tomáš Peckaae301762021-10-13 10:50:37 +020059+ if (r < 0)
60+ return r;
61+ }
62+
63 return 0;
64 }
65
Václav Kubernát8cd61562021-12-08 13:27:31 +010066@@ -2568,7 +2598,6 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
Tomáš Peckaae301762021-10-13 10:50:37 +020067 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
68 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
Václav Kubernát8cd61562021-12-08 13:27:31 +010069 _cleanup_(lldp_userdata_freep) LLDPUserdata udata = {};
Tomáš Peckaae301762021-10-13 10:50:37 +020070- TableCell *cell;
71
72 r = varlink_connect_address(&link, address);
73 if (r < 0)
Václav Kubernát8cd61562021-12-08 13:27:31 +010074@@ -2590,47 +2619,54 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
Tomáš Peckaae301762021-10-13 10:50:37 +020075 return c;
76
Václav Kubernát8cd61562021-12-08 13:27:31 +010077 pager_open(arg_pager_flags);
Tomáš Peckaae301762021-10-13 10:50:37 +020078+ if (arg_json_format_flags == JSON_FORMAT_OFF) {
79+ TableCell *cell;
Tomáš Peckaae301762021-10-13 10:50:37 +020080+
Václav Kubernát8cd61562021-12-08 13:27:31 +010081+ udata.table = table_new("link",
82+ "chassis id",
83+ "system name",
84+ "caps",
85+ "port id",
86+ "port description");
87+ if (!udata.table)
Tomáš Peckaae301762021-10-13 10:50:37 +020088+ return log_oom();
Tomáš Pecka5e070252022-05-20 20:41:21 +020089
90- udata.table = table_new("link",
91- "chassis id",
92- "system name",
93- "caps",
94- "port id",
95- "port description");
96- if (!udata.table)
97- return log_oom();
98-
99- if (arg_full)
100- table_set_width(udata.table, 0);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100101+ if (arg_full)
102+ table_set_width(udata.table, 0);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200103
104- table_set_header(udata.table, arg_legend);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100105+ table_set_header(udata.table, arg_legend);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200106
107- assert_se(cell = table_get_cell(udata.table, 0, 0));
108- table_set_minimum_width(udata.table, cell, 16);
109- table_set_maximum_width(udata.table, cell, 16);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100110+ assert_se(cell = table_get_cell(udata.table, 0, 0));
111+ table_set_minimum_width(udata.table, cell, 16);
112+ table_set_maximum_width(udata.table, cell, 16);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200113
114- assert_se(cell = table_get_cell(udata.table, 0, 1));
115- table_set_minimum_width(udata.table, cell, 17);
116- table_set_maximum_width(udata.table, cell, 17);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100117+ assert_se(cell = table_get_cell(udata.table, 0, 1));
118+ table_set_minimum_width(udata.table, cell, 17);
119+ table_set_maximum_width(udata.table, cell, 17);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200120
121- assert_se(cell = table_get_cell(udata.table, 0, 2));
122- table_set_minimum_width(udata.table, cell, 16);
123- table_set_maximum_width(udata.table, cell, 16);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100124+ assert_se(cell = table_get_cell(udata.table, 0, 2));
125+ table_set_minimum_width(udata.table, cell, 16);
126+ table_set_maximum_width(udata.table, cell, 16);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200127
128- assert_se(cell = table_get_cell(udata.table, 0, 3));
129- table_set_minimum_width(udata.table, cell, 11);
130- table_set_maximum_width(udata.table, cell, 11);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100131+ assert_se(cell = table_get_cell(udata.table, 0, 3));
132+ table_set_minimum_width(udata.table, cell, 11);
133+ table_set_maximum_width(udata.table, cell, 11);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200134
135- assert_se(cell = table_get_cell(udata.table, 0, 4));
136- table_set_minimum_width(udata.table, cell, 17);
137- table_set_maximum_width(udata.table, cell, 17);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100138+ assert_se(cell = table_get_cell(udata.table, 0, 4));
139+ table_set_minimum_width(udata.table, cell, 17);
140+ table_set_maximum_width(udata.table, cell, 17);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200141
142- assert_se(cell = table_get_cell(udata.table, 0, 5));
143- table_set_minimum_width(udata.table, cell, 16);
144- table_set_maximum_width(udata.table, cell, 16);
Václav Kubernát8cd61562021-12-08 13:27:31 +0100145+ assert_se(cell = table_get_cell(udata.table, 0, 5));
146+ table_set_minimum_width(udata.table, cell, 16);
147+ table_set_maximum_width(udata.table, cell, 16);
Tomáš Pecka5e070252022-05-20 20:41:21 +0200148
149- if (table_set_empty_string(udata.table, "n/a") < 0)
150- return log_oom();
Václav Kubernát8cd61562021-12-08 13:27:31 +0100151+ if (table_set_empty_string(udata.table, "n/a") < 0)
152+ return log_oom();
Tomáš Peckaae301762021-10-13 10:50:37 +0200153+ } else {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100154+ r = json_build(&udata.json, JSON_BUILD_EMPTY_OBJECT);
Tomáš Peckaae301762021-10-13 10:50:37 +0200155+ if (r < 0)
156+ return r;
157+ }
158
Tomáš Peckaae301762021-10-13 10:50:37 +0200159 varlink_set_userdata(link, &udata);
160
Václav Kubernát8cd61562021-12-08 13:27:31 +0100161@@ -2653,13 +2689,17 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
Tomáš Peckaae301762021-10-13 10:50:37 +0200162 return r;
Tomáš Peckaae301762021-10-13 10:50:37 +0200163 }
164
Václav Kubernát8cd61562021-12-08 13:27:31 +0100165- r = table_print(udata.table, NULL);
Tomáš Peckaae301762021-10-13 10:50:37 +0200166- if (r < 0)
167- return table_log_print_error(r);
168+ if (arg_json_format_flags == JSON_FORMAT_OFF) {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100169+ r = table_print(udata.table, NULL);
Tomáš Peckaae301762021-10-13 10:50:37 +0200170+ if (r < 0)
171+ return table_log_print_error(r);
172
173- if (arg_legend) {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100174- lldp_capabilities_legend(udata.capabilities_all);
175- printf("\n%i neighbors listed.\n", udata.neighbors_count);
Tomáš Peckaae301762021-10-13 10:50:37 +0200176+ if (arg_legend) {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100177+ lldp_capabilities_legend(udata.capabilities_all);
178+ printf("\n%i neighbors listed.\n", udata.neighbors_count);
Tomáš Peckaae301762021-10-13 10:50:37 +0200179+ }
180+ } else {
Václav Kubernát8cd61562021-12-08 13:27:31 +0100181+ json_variant_dump(udata.json, arg_json_format_flags, NULL, NULL);
Tomáš Peckaae301762021-10-13 10:50:37 +0200182 }
183
184 return 0;
185--
Tomáš Pecka5e070252022-05-20 20:41:21 +02001862.35.1
Tomáš Peckaae301762021-10-13 10:50:37 +0200187