Post-release version bump to 11.0.0
[libvirt.git] / tools / virsh-completer-network.c
blobf28d724ca8d6aed41e8fecde859da6216d2df993
1 /*
2 * virsh-completer-network.c: virsh completer callbacks related to networks
4 * Copyright (C) 2019 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include "virsh-completer-network.h"
24 #include "virsh-util.h"
25 #include "viralloc.h"
26 #include "virsh-network.h"
27 #include "virsh.h"
29 char **
30 virshNetworkNameCompleter(vshControl *ctl,
31 const vshCmd *cmd G_GNUC_UNUSED,
32 unsigned int flags)
34 virshControl *priv = ctl->privData;
35 virNetworkPtr *nets = NULL;
36 int nnets = 0;
37 size_t i = 0;
38 char **ret = NULL;
39 g_auto(GStrv) tmp = NULL;
41 virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE |
42 VIR_CONNECT_LIST_NETWORKS_ACTIVE |
43 VIR_CONNECT_LIST_NETWORKS_PERSISTENT,
44 NULL);
46 if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
47 return NULL;
49 if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
50 return NULL;
52 tmp = g_new0(char *, nnets + 1);
54 for (i = 0; i < nnets; i++) {
55 const char *name = virNetworkGetName(nets[i]);
57 tmp[i] = g_strdup(name);
60 ret = g_steal_pointer(&tmp);
62 for (i = 0; i < nnets; i++)
63 virshNetworkFree(nets[i]);
64 g_free(nets);
65 return ret;
69 char **
70 virshNetworkEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
71 const vshCmd *cmd G_GNUC_UNUSED,
72 unsigned int flags)
74 size_t i = 0;
75 g_auto(GStrv) tmp = NULL;
77 virCheckFlags(0, NULL);
79 tmp = g_new0(char *, VIR_NETWORK_EVENT_ID_LAST + 1);
81 for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++)
82 tmp[i] = g_strdup(virshNetworkEventCallbacks[i].name);
84 return g_steal_pointer(&tmp);
88 char **
89 virshNetworkPortUUIDCompleter(vshControl *ctl,
90 const vshCmd *cmd G_GNUC_UNUSED,
91 unsigned int flags)
93 virshControl *priv = ctl->privData;
94 virNetworkPtr net = NULL;
95 virNetworkPortPtr *ports = NULL;
96 int nports = 0;
97 size_t i = 0;
98 char **ret = NULL;
100 virCheckFlags(0, NULL);
102 if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
103 return NULL;
105 if (!(net = virshCommandOptNetwork(ctl, cmd, NULL)))
106 return NULL;
108 if ((nports = virNetworkListAllPorts(net, &ports, flags)) < 0)
109 return NULL;
111 ret = g_new0(char *, nports + 1);
113 for (i = 0; i < nports; i++) {
114 char uuid[VIR_UUID_STRING_BUFLEN];
116 if (virNetworkPortGetUUIDString(ports[i], uuid) < 0)
117 goto error;
119 ret[i] = g_strdup(uuid);
121 virNetworkPortFree(ports[i]);
123 g_free(ports);
125 return ret;
127 error:
128 for (; i < nports; i++)
129 virNetworkPortFree(ports[i]);
130 g_free(ports);
131 for (i = 0; i < nports; i++)
132 g_free(ret[i]);
133 g_free(ret);
134 return NULL;
138 char **
139 virshNetworkUUIDCompleter(vshControl *ctl,
140 const vshCmd *cmd G_GNUC_UNUSED,
141 unsigned int flags)
143 virshControl *priv = ctl->privData;
144 virNetworkPtr *nets = NULL;
145 int nnets = 0;
146 size_t i = 0;
147 char **ret = NULL;
148 g_auto(GStrv) tmp = NULL;
150 virCheckFlags(0, NULL);
152 if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
153 return NULL;
155 if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
156 return NULL;
158 tmp = g_new0(char *, nnets + 1);
160 for (i = 0; i < nnets; i++) {
161 char uuid[VIR_UUID_STRING_BUFLEN];
163 if (virNetworkGetUUIDString(nets[i], uuid) < 0)
164 goto cleanup;
166 tmp[i] = g_strdup(uuid);
169 ret = g_steal_pointer(&tmp);
171 cleanup:
172 for (i = 0; i < nnets; i++)
173 virshNetworkFree(nets[i]);
174 g_free(nets);
175 return ret;
179 char **
180 virshNetworkDhcpMacCompleter(vshControl *ctl,
181 const vshCmd *cmd,
182 unsigned int flags)
184 virshControl *priv = ctl->privData;
185 virNetworkDHCPLeasePtr *leases = NULL;
186 g_autoptr(virshNetwork) network = NULL;
187 int nleases;
188 size_t i = 0;
189 char **ret = NULL;
190 g_auto(GStrv) tmp = NULL;
192 virCheckFlags(0, NULL);
194 if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
195 return NULL;
197 if (!(network = virshCommandOptNetwork(ctl, cmd, NULL)))
198 return NULL;
200 if ((nleases = virNetworkGetDHCPLeases(network, NULL, &leases, flags)) < 0)
201 goto cleanup;
203 tmp = g_new0(char *, nleases + 1);
205 for (i = 0; i < nleases; i++) {
206 virNetworkDHCPLeasePtr lease = leases[i];
207 tmp[i] = g_strdup(lease->mac);
210 ret = g_steal_pointer(&tmp);
212 cleanup:
213 if (leases) {
214 for (i = 0; i < nleases; i++)
215 virNetworkDHCPLeaseFree(leases[i]);
216 VIR_FREE(leases);
218 return ret;
222 char **
223 virshNetworkUpdateCommandCompleter(vshControl *ctl G_GNUC_UNUSED,
224 const vshCmd *cmd G_GNUC_UNUSED,
225 unsigned int flags)
227 char **ret = NULL;
228 size_t i;
230 virCheckFlags(0, NULL);
232 ret = g_new0(char *, VIR_NETWORK_UPDATE_COMMAND_LAST + 1);
234 /* The first item in the enum is not accepted by virsh, But there's "add"
235 * which is accepted an is just an alias to "add-last". */
236 ret[0] = g_strdup("add");
238 for (i = 1; i < VIR_NETWORK_UPDATE_COMMAND_LAST; i++)
239 ret[i] = g_strdup(virshNetworkUpdateCommandTypeToString(i));
241 return ret;
245 char **
246 virshNetworkUpdateSectionCompleter(vshControl *ctl G_GNUC_UNUSED,
247 const vshCmd *cmd G_GNUC_UNUSED,
248 unsigned int flags)
250 char **ret = NULL;
251 size_t i;
253 virCheckFlags(0, NULL);
255 ret = g_new0(char *, VIR_NETWORK_SECTION_LAST);
257 /* The first item in the enum is not accepted by virsh. */
258 for (i = 1; i < VIR_NETWORK_SECTION_LAST; i++)
259 ret[i - 1] = g_strdup(virshNetworkSectionTypeToString(i));
261 return ret;