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/>.
23 #include "virsh-completer-network.h"
24 #include "virsh-util.h"
26 #include "virsh-network.h"
30 virshNetworkNameCompleter(vshControl
*ctl
,
31 const vshCmd
*cmd G_GNUC_UNUSED
,
34 virshControl
*priv
= ctl
->privData
;
35 virNetworkPtr
*nets
= 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
,
46 if (!priv
->conn
|| virConnectIsAlive(priv
->conn
) <= 0)
49 if ((nnets
= virConnectListAllNetworks(priv
->conn
, &nets
, flags
)) < 0)
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
]);
70 virshNetworkEventNameCompleter(vshControl
*ctl G_GNUC_UNUSED
,
71 const vshCmd
*cmd G_GNUC_UNUSED
,
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
);
89 virshNetworkPortUUIDCompleter(vshControl
*ctl
,
90 const vshCmd
*cmd G_GNUC_UNUSED
,
93 virshControl
*priv
= ctl
->privData
;
94 virNetworkPtr net
= NULL
;
95 virNetworkPortPtr
*ports
= NULL
;
100 virCheckFlags(0, NULL
);
102 if (!priv
->conn
|| virConnectIsAlive(priv
->conn
) <= 0)
105 if (!(net
= virshCommandOptNetwork(ctl
, cmd
, NULL
)))
108 if ((nports
= virNetworkListAllPorts(net
, &ports
, flags
)) < 0)
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)
119 ret
[i
] = g_strdup(uuid
);
121 virNetworkPortFree(ports
[i
]);
128 for (; i
< nports
; i
++)
129 virNetworkPortFree(ports
[i
]);
131 for (i
= 0; i
< nports
; i
++)
139 virshNetworkUUIDCompleter(vshControl
*ctl
,
140 const vshCmd
*cmd G_GNUC_UNUSED
,
143 virshControl
*priv
= ctl
->privData
;
144 virNetworkPtr
*nets
= NULL
;
148 g_auto(GStrv
) tmp
= NULL
;
150 virCheckFlags(0, NULL
);
152 if (!priv
->conn
|| virConnectIsAlive(priv
->conn
) <= 0)
155 if ((nnets
= virConnectListAllNetworks(priv
->conn
, &nets
, flags
)) < 0)
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)
166 tmp
[i
] = g_strdup(uuid
);
169 ret
= g_steal_pointer(&tmp
);
172 for (i
= 0; i
< nnets
; i
++)
173 virshNetworkFree(nets
[i
]);
180 virshNetworkDhcpMacCompleter(vshControl
*ctl
,
184 virshControl
*priv
= ctl
->privData
;
185 virNetworkDHCPLeasePtr
*leases
= NULL
;
186 g_autoptr(virshNetwork
) network
= NULL
;
190 g_auto(GStrv
) tmp
= NULL
;
192 virCheckFlags(0, NULL
);
194 if (!priv
->conn
|| virConnectIsAlive(priv
->conn
) <= 0)
197 if (!(network
= virshCommandOptNetwork(ctl
, cmd
, NULL
)))
200 if ((nleases
= virNetworkGetDHCPLeases(network
, NULL
, &leases
, flags
)) < 0)
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
);
214 for (i
= 0; i
< nleases
; i
++)
215 virNetworkDHCPLeaseFree(leases
[i
]);
223 virshNetworkUpdateCommandCompleter(vshControl
*ctl G_GNUC_UNUSED
,
224 const vshCmd
*cmd G_GNUC_UNUSED
,
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
));
246 virshNetworkUpdateSectionCompleter(vshControl
*ctl G_GNUC_UNUSED
,
247 const vshCmd
*cmd G_GNUC_UNUSED
,
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
));