Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
tools/virsh-completer.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
tools/virsh-completer.h | 4 ++++
tools/virsh-network.c | 24 ++++++++++++-----------
3 files changed, 68 insertions(+), 11 deletions(-)
diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index f5b1e4261..2c0d4f640 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -297,3 +297,54 @@ virshInterfaceNameCompleter(vshControl *ctl,
VIR_FREE(ret);
return NULL;
}
+
+
+char **
+virshNetworkNameCompleter(vshControl *ctl,
+ const vshCmd *cmd ATTRIBUTE_UNUSED,
+ unsigned int flags)
+{
+ virshControlPtr priv = ctl->privData;
+ virNetworkPtr *nets = NULL;
+ int nnets = 0;
+ size_t i = 0;
+ char **ret = NULL;
+
+ virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE |
+ VIR_CONNECT_LIST_NETWORKS_ACTIVE |
+ VIR_CONNECT_LIST_NETWORKS_PERSISTENT |
+ VIR_CONNECT_LIST_NETWORKS_TRANSIENT |
+ VIR_CONNECT_LIST_NETWORKS_AUTOSTART |
+ VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART,
+ NULL);
+
+ if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+ return NULL;
+
+ if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0)
+ return NULL;
+
+ if (VIR_ALLOC_N(ret, nnets + 1) < 0)
+ goto error;
+
+ for (i = 0; i < nnets; i++) {
+ const char *name = virNetworkGetName(nets[i]);
+
+ if (VIR_STRDUP(ret[i], name) < 0)
+ goto error;
+
+ virNetworkFree(nets[i]);
+ }
+ VIR_FREE(nets);
+
+ return ret;
+
+ error:
+ for (; i < nnets; i++)
+ virNetworkFree(nets[i]);
+ VIR_FREE(nets);
+ for (i = 0; i < nnets; i++)
+ VIR_FREE(ret[i]);
+ VIR_FREE(ret);
+ return NULL;
+}
diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h
index 2323aaba3..20ba4cb55 100644
--- a/tools/virsh-completer.h
+++ b/tools/virsh-completer.h
@@ -50,4 +50,8 @@ char ** virshInterfaceNameCompleter(vshControl *ctl,
const vshCmd *cmd,
unsigned int flags);
+char ** virshNetworkNameCompleter(vshControl *ctl,
+ const vshCmd *cmd,
+ unsigned int flags);
+
#endif
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index cd55e384f..3b472ea67 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -34,11 +34,13 @@
#include "virtime.h"
#include "conf/network_conf.h"
-#define VIRSH_COMMON_OPT_NETWORK \
+#define VIRSH_COMMON_OPT_NETWORK(cflags) \
{.name = "network", \
.type = VSH_OT_DATA, \
.flags = VSH_OFLAG_REQ, \
- .help = N_("network name or uuid") \
+ .help = N_("network name or uuid"), \
+ .completer = virshNetworkNameCompleter, \
+ .completer_flags = cflags, \
}
virNetworkPtr
@@ -93,7 +95,7 @@ static const vshCmdInfo info_network_autostart[] = {
};
static const vshCmdOptDef opts_network_autostart[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = "disable",
.type = VSH_OT_BOOL,
.help = N_("disable autostarting")
@@ -240,7 +242,7 @@ static const vshCmdInfo info_network_destroy[] = {
};
static const vshCmdOptDef opts_network_destroy[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_ACTIVE),
{.name = NULL}
};
@@ -279,7 +281,7 @@ static const vshCmdInfo info_network_dumpxml[] = {
};
static const vshCmdOptDef opts_network_dumpxml[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = "inactive",
.type = VSH_OT_BOOL,
.help = N_("show inactive defined XML")
@@ -330,7 +332,7 @@ static const vshCmdInfo info_network_info[] = {
};
static const vshCmdOptDef opts_network_info[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = NULL}
};
@@ -779,7 +781,7 @@ static const vshCmdInfo info_network_start[] = {
};
static const vshCmdOptDef opts_network_start[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_INACTIVE),
{.name = NULL}
};
@@ -817,7 +819,7 @@ static const vshCmdInfo info_network_undefine[] = {
};
static const vshCmdOptDef opts_network_undefine[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = NULL}
};
@@ -856,7 +858,7 @@ static const vshCmdInfo info_network_update[] = {
};
static const vshCmdOptDef opts_network_update[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = "command",
.type = VSH_OT_DATA,
.flags = VSH_OFLAG_REQ,
@@ -1057,7 +1059,7 @@ static const vshCmdInfo info_network_edit[] = {
};
static const vshCmdOptDef opts_network_edit[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = NULL}
};
@@ -1304,7 +1306,7 @@ static const vshCmdInfo info_network_dhcp_leases[] = {
};
static const vshCmdOptDef opts_network_dhcp_leases[] = {
- VIRSH_COMMON_OPT_NETWORK,
+ VIRSH_COMMON_OPT_NETWORK(0),
{.name = "mac",
.type = VSH_OT_STRING,
.flags = VSH_OFLAG_NONE,
--
2.13.6
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
On 01/12/2018 09:37 AM, Michal Privoznik wrote: > Signed-off-by: Michal Privoznik <mprivozn@redhat.com> > --- > tools/virsh-completer.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ > tools/virsh-completer.h | 4 ++++ > tools/virsh-network.c | 24 ++++++++++++----------- > 3 files changed, 68 insertions(+), 11 deletions(-) > For as much as I recall there being complaints when I added the various VIRSH macros for various comments, I would think that made this task easier! > diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c > index f5b1e4261..2c0d4f640 100644 > --- a/tools/virsh-completer.c > +++ b/tools/virsh-completer.c > @@ -297,3 +297,54 @@ virshInterfaceNameCompleter(vshControl *ctl, > VIR_FREE(ret); > return NULL; > } > + > + > +char ** > +virshNetworkNameCompleter(vshControl *ctl, > + const vshCmd *cmd ATTRIBUTE_UNUSED, > + unsigned int flags) > +{ > + virshControlPtr priv = ctl->privData; > + virNetworkPtr *nets = NULL; > + int nnets = 0; > + size_t i = 0; > + char **ret = NULL; > + > + virCheckFlags(VIR_CONNECT_LIST_NETWORKS_INACTIVE | > + VIR_CONNECT_LIST_NETWORKS_ACTIVE | > + VIR_CONNECT_LIST_NETWORKS_PERSISTENT | > + VIR_CONNECT_LIST_NETWORKS_TRANSIENT | > + VIR_CONNECT_LIST_NETWORKS_AUTOSTART | > + VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART, > + NULL); Only 0, ACTIVE, and INACTIVE are used, but I think PERSISTENT needs to be added (per below). > + > + if (!priv->conn || virConnectIsAlive(priv->conn) <= 0) > + return NULL; > + > + if ((nnets = virConnectListAllNetworks(priv->conn, &nets, flags)) < 0) > + return NULL; > + > + if (VIR_ALLOC_N(ret, nnets + 1) < 0) > + goto error; > + > + for (i = 0; i < nnets; i++) { > + const char *name = virNetworkGetName(nets[i]); > + > + if (VIR_STRDUP(ret[i], name) < 0) > + goto error; > + > + virNetworkFree(nets[i]); > + } > + VIR_FREE(nets); > + > + return ret; > + > + error: > + for (; i < nnets; i++) > + virNetworkFree(nets[i]); > + VIR_FREE(nets); > + for (i = 0; i < nnets; i++) > + VIR_FREE(ret[i]); > + VIR_FREE(ret); > + return NULL; > +} > diff --git a/tools/virsh-completer.h b/tools/virsh-completer.h > index 2323aaba3..20ba4cb55 100644 > --- a/tools/virsh-completer.h > +++ b/tools/virsh-completer.h > @@ -50,4 +50,8 @@ char ** virshInterfaceNameCompleter(vshControl *ctl, > const vshCmd *cmd, > unsigned int flags); > > +char ** virshNetworkNameCompleter(vshControl *ctl, > + const vshCmd *cmd, > + unsigned int flags); > + > #endif > diff --git a/tools/virsh-network.c b/tools/virsh-network.c > index cd55e384f..3b472ea67 100644 > --- a/tools/virsh-network.c > +++ b/tools/virsh-network.c > @@ -34,11 +34,13 @@ > #include "virtime.h" > #include "conf/network_conf.h" > > -#define VIRSH_COMMON_OPT_NETWORK \ > +#define VIRSH_COMMON_OPT_NETWORK(cflags) \ > {.name = "network", \ > .type = VSH_OT_DATA, \ > .flags = VSH_OFLAG_REQ, \ > - .help = N_("network name or uuid") \ > + .help = N_("network name or uuid"), \ > + .completer = virshNetworkNameCompleter, \ > + .completer_flags = cflags, \ > } > > virNetworkPtr > @@ -93,7 +95,7 @@ static const vshCmdInfo info_network_autostart[] = { > }; > > static const vshCmdOptDef opts_network_autostart[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), Light dawns... Would only be reasonable for PERSISTENT networks right? Not going to autostart a transient one... Perhaps this should be true for previous ones already pushed and reviewed... That is I won't go back and I won't bring it up again. > {.name = "disable", > .type = VSH_OT_BOOL, > .help = N_("disable autostarting") > @@ -240,7 +242,7 @@ static const vshCmdInfo info_network_destroy[] = { > }; > > static const vshCmdOptDef opts_network_destroy[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_ACTIVE), > {.name = NULL} > }; > > @@ -279,7 +281,7 @@ static const vshCmdInfo info_network_dumpxml[] = { > }; > > static const vshCmdOptDef opts_network_dumpxml[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), > {.name = "inactive", > .type = VSH_OT_BOOL, > .help = N_("show inactive defined XML") > @@ -330,7 +332,7 @@ static const vshCmdInfo info_network_info[] = { > }; > > static const vshCmdOptDef opts_network_info[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), > {.name = NULL} > }; > > @@ -779,7 +781,7 @@ static const vshCmdInfo info_network_start[] = { > }; > > static const vshCmdOptDef opts_network_start[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(VIR_CONNECT_LIST_NETWORKS_INACTIVE), > {.name = NULL} > }; > > @@ -817,7 +819,7 @@ static const vshCmdInfo info_network_undefine[] = { > }; > > static const vshCmdOptDef opts_network_undefine[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), PERSISTENT Reviewed-by: John Ferlan <jferlan@redhat.com> John > {.name = NULL} > }; > > @@ -856,7 +858,7 @@ static const vshCmdInfo info_network_update[] = { > }; > > static const vshCmdOptDef opts_network_update[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), > {.name = "command", > .type = VSH_OT_DATA, > .flags = VSH_OFLAG_REQ, > @@ -1057,7 +1059,7 @@ static const vshCmdInfo info_network_edit[] = { > }; > > static const vshCmdOptDef opts_network_edit[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), > {.name = NULL} > }; > > @@ -1304,7 +1306,7 @@ static const vshCmdInfo info_network_dhcp_leases[] = { > }; > > static const vshCmdOptDef opts_network_dhcp_leases[] = { > - VIRSH_COMMON_OPT_NETWORK, > + VIRSH_COMMON_OPT_NETWORK(0), > {.name = "mac", > .type = VSH_OT_STRING, > .flags = VSH_OFLAG_NONE, > -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
© 2016 - 2025 Red Hat, Inc.