blob: 5d43f1dc7c293612b3cf408139326fdca501338a [file] [log] [blame]
Tomáš Peckaae301762021-10-13 10:50:37 +02001From 7479e9e720909a2d2360f684e85821195301dce2 Mon Sep 17 00:00:00 2001
2From: 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---
9 src/network/networkctl.c | 123 ++++++++++++++++++++++++++-------------
10 1 file changed, 82 insertions(+), 41 deletions(-)
11
12diff --git a/src/network/networkctl.c b/src/network/networkctl.c
13index 89abaf26da..124ee8f0c7 100644
14--- a/src/network/networkctl.c
15+++ b/src/network/networkctl.c
16@@ -2510,6 +2510,7 @@ typedef struct LLDPUserdata {
17
18 char *link_name;
19 Table *table;
20+ JsonVariant **json_list;
21 } LLDPUserdata;
22
23 static int lldp_neighbours_varlink_reply(Varlink *link, JsonVariant *parameters, const char *error_id, VarlinkReplyFlags flags, void *userdata) {
24@@ -2556,6 +2557,26 @@ static int lldp_neighbours_varlink_reply(Varlink *link, JsonVariant *parameters,
25 if (udata->capabilities_all)
26 *(udata->capabilities_all) |= entry.capabilities;
27
28+ if (udata->json_list) {
29+ _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
30+
31+ capabilities = lldp_capabilities_to_string(entry.capabilities, false);
32+
33+ r = json_build(&v, JSON_BUILD_OBJECT(
34+ JSON_BUILD_PAIR("neighbor", JSON_BUILD_OBJECT(
35+ JSON_BUILD_PAIR_CONDITION(entry.chassis_id, "chassisId", JSON_BUILD_STRING(entry.chassis_id)),
36+ JSON_BUILD_PAIR_CONDITION(entry.port_id, "portId", JSON_BUILD_STRING(entry.port_id)),
37+ JSON_BUILD_PAIR_CONDITION(entry.system_name, "systemName", JSON_BUILD_STRING(entry.system_name)),
38+ JSON_BUILD_PAIR_CONDITION(entry.port_description, "portDescription", JSON_BUILD_STRING(entry.port_description)),
39+ JSON_BUILD_PAIR_CONDITION(entry.capabilities, "enabledCapabilities", JSON_BUILD_STRING(capabilities))))));
40+ if (r < 0)
41+ return r;
42+
43+ r = json_variant_append_array(udata->json_list, v);
44+ if (r < 0)
45+ return r;
46+ }
47+
48 return 0;
49 }
50
51@@ -2568,10 +2589,10 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
52 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
53 _cleanup_(link_info_array_freep) LinkInfo *links = NULL;
54 _cleanup_(table_unrefp) Table *table = NULL;
55+ _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
56 int neighbors_count = 0;
57 uint16_t capabilities_all = 0;
58 LLDPUserdata udata = {};
59- TableCell *cell;
60
61 r = varlink_connect_address(&link, address);
62 if (r < 0)
63@@ -2592,50 +2613,58 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
64 if (c < 0)
65 return c;
66
67- (void) pager_open(arg_pager_flags);
68+ if (arg_json_format_flags == JSON_FORMAT_OFF) {
69+ TableCell *cell;
70
71- table = table_new("link",
72- "chassis id",
73- "system name",
74- "caps",
75- "port id",
76- "port description");
77- if (!table)
78- return log_oom();
79+ (void) pager_open(arg_pager_flags);
80
81- if (arg_full)
82- table_set_width(table, 0);
83+ table = table_new("link",
84+ "chassis id",
85+ "system name",
86+ "caps",
87+ "port id",
88+ "port description");
89+ if (!table)
90+ return log_oom();
91
92- table_set_header(table, arg_legend);
93+ if (arg_full)
94+ table_set_width(table, 0);
95
96- assert_se(cell = table_get_cell(table, 0, 0));
97- table_set_minimum_width(table, cell, 16);
98- table_set_maximum_width(table, cell, 16);
99+ table_set_header(table, arg_legend);
100
101- assert_se(cell = table_get_cell(table, 0, 1));
102- table_set_minimum_width(table, cell, 17);
103- table_set_maximum_width(table, cell, 17);
104+ assert_se(cell = table_get_cell(table, 0, 0));
105+ table_set_minimum_width(table, cell, 16);
106+ table_set_maximum_width(table, cell, 16);
107
108- assert_se(cell = table_get_cell(table, 0, 2));
109- table_set_minimum_width(table, cell, 16);
110- table_set_maximum_width(table, cell, 16);
111+ assert_se(cell = table_get_cell(table, 0, 1));
112+ table_set_minimum_width(table, cell, 17);
113+ table_set_maximum_width(table, cell, 17);
114
115- assert_se(cell = table_get_cell(table, 0, 3));
116- table_set_minimum_width(table, cell, 11);
117- table_set_maximum_width(table, cell, 11);
118+ assert_se(cell = table_get_cell(table, 0, 2));
119+ table_set_minimum_width(table, cell, 16);
120+ table_set_maximum_width(table, cell, 16);
121
122- assert_se(cell = table_get_cell(table, 0, 4));
123- table_set_minimum_width(table, cell, 17);
124- table_set_maximum_width(table, cell, 17);
125+ assert_se(cell = table_get_cell(table, 0, 3));
126+ table_set_minimum_width(table, cell, 11);
127+ table_set_maximum_width(table, cell, 11);
128
129- assert_se(cell = table_get_cell(table, 0, 5));
130- table_set_minimum_width(table, cell, 16);
131- table_set_maximum_width(table, cell, 16);
132+ assert_se(cell = table_get_cell(table, 0, 4));
133+ table_set_minimum_width(table, cell, 17);
134+ table_set_maximum_width(table, cell, 17);
135
136- if (table_set_empty_string(table, "n/a") < 0)
137- return log_oom();
138+ assert_se(cell = table_get_cell(table, 0, 5));
139+ table_set_minimum_width(table, cell, 16);
140+ table_set_maximum_width(table, cell, 16);
141+
142+ if (table_set_empty_string(table, "n/a") < 0)
143+ return log_oom();
144
145- udata.table = table;
146+ udata.table = table;
147+ } else {
148+ r = json_build(&v, JSON_BUILD_EMPTY_OBJECT);
149+ if (r < 0)
150+ return r;
151+ }
152
153 udata.neighbors_count = &neighbors_count;
154 udata.capabilities_all = &capabilities_all;
155@@ -2643,10 +2672,14 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
156 varlink_set_userdata(link, &udata);
157
158 for (int i = 0; i < c; i++) {
159- _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL;
160+ _cleanup_(json_variant_unrefp) JsonVariant *cparams = NULL, *neighbors = NULL;
161
162 udata.link_name = links[i].name;
163
164+ if (arg_json_format_flags != JSON_FORMAT_OFF) {
165+ udata.json_list = &neighbors;
166+ }
167+
168 r = json_build(&cparams, JSON_BUILD_OBJECT(
169 JSON_BUILD_PAIR("ifindex", JSON_BUILD_UNSIGNED(links[i].ifindex))));
170 if (r < 0)
171@@ -2659,15 +2692,23 @@ static int link_lldp_status(int argc, char *argv[], void *userdata) {
172 r = varlink_observe_complete(link);
173 if (r < 0)
174 return r;
175+
176+ if (!json_variant_is_blank_array(neighbors)) {
177+ json_variant_set_field(&v, links[i].name, neighbors);
178+ }
179 }
180
181- r = table_print(table, NULL);
182- if (r < 0)
183- return table_log_print_error(r);
184+ if (arg_json_format_flags == JSON_FORMAT_OFF) {
185+ r = table_print(table, NULL);
186+ if (r < 0)
187+ return table_log_print_error(r);
188
189- if (arg_legend) {
190- lldp_capabilities_legend(capabilities_all);
191- printf("\n%i neighbors listed.\n", neighbors_count);
192+ if (arg_legend) {
193+ lldp_capabilities_legend(capabilities_all);
194+ printf("\n%i neighbors listed.\n", neighbors_count);
195+ }
196+ } else {
197+ json_variant_dump(v, arg_json_format_flags, NULL, NULL);
198 }
199
200 return 0;
201--
2022.33.0
203