cmdNetworkList: Introduce --name, --uuid, --table

When reviewing some network patches, I've noticed we don't have
those switches to the 'net-list' command. We should. They are
merely copied over from 'list' command.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Michal Privoznik 2015-06-08 16:13:28 +02:00
parent 0258d9ab46
commit c156856a2d
2 changed files with 62 additions and 14 deletions

View File

@ -644,6 +644,18 @@ static const vshCmdOptDef opts_network_list[] = {
.type = VSH_OT_BOOL, .type = VSH_OT_BOOL,
.help = N_("list networks with autostart disabled") .help = N_("list networks with autostart disabled")
}, },
{.name = "uuid",
.type = VSH_OT_BOOL,
.help = N_("list uuid's only")
},
{.name = "name",
.type = VSH_OT_BOOL,
.help = N_("list network names only")
},
{.name = "table",
.type = VSH_OT_BOOL,
.help = N_("list table (default)")
},
{.name = NULL} {.name = NULL}
}; };
@ -655,6 +667,11 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{ {
vshNetworkListPtr list = NULL; vshNetworkListPtr list = NULL;
size_t i; size_t i;
bool ret = false;
bool optName = vshCommandOptBool(cmd, "name");
bool optTable = vshCommandOptBool(cmd, "table");
bool optUUID = vshCommandOptBool(cmd, "uuid");
char uuid[VIR_UUID_STRING_BUFLEN];
unsigned int flags = VIR_CONNECT_LIST_NETWORKS_ACTIVE; unsigned int flags = VIR_CONNECT_LIST_NETWORKS_ACTIVE;
if (vshCommandOptBool(cmd, "inactive")) if (vshCommandOptBool(cmd, "inactive"))
@ -670,19 +687,32 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
FILTER("autostart", VIR_CONNECT_LIST_NETWORKS_AUTOSTART); FILTER("autostart", VIR_CONNECT_LIST_NETWORKS_AUTOSTART);
FILTER("no-autostart", VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART); FILTER("no-autostart", VIR_CONNECT_LIST_NETWORKS_NO_AUTOSTART);
if (optTable + optName + optUUID > 1) {
vshError(ctl, "%s",
_("Only one argument from --table, --name and --uuid "
"may be specified."));
return false;
}
if (!optUUID && !optName)
optTable = true;
if (!(list = vshNetworkListCollect(ctl, flags))) if (!(list = vshNetworkListCollect(ctl, flags)))
return false; return false;
if (optTable) {
vshPrintExtra(ctl, " %-20s %-10s %-13s %s\n", _("Name"), _("State"), vshPrintExtra(ctl, " %-20s %-10s %-13s %s\n", _("Name"), _("State"),
_("Autostart"), _("Persistent")); _("Autostart"), _("Persistent"));
vshPrintExtra(ctl, vshPrintExtra(ctl,
"----------------------------------------------------------\n"); "----------------------------------------------------------\n");
}
for (i = 0; i < list->nnets; i++) { for (i = 0; i < list->nnets; i++) {
virNetworkPtr network = list->nets[i]; virNetworkPtr network = list->nets[i];
const char *autostartStr; const char *autostartStr;
int is_autostart = 0; int is_autostart = 0;
if (optTable) {
if (virNetworkGetAutostart(network, &is_autostart) < 0) if (virNetworkGetAutostart(network, &is_autostart) < 0)
autostartStr = _("no autostart"); autostartStr = _("no autostart");
else else
@ -693,10 +723,21 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
virNetworkIsActive(network) ? _("active") : _("inactive"), virNetworkIsActive(network) ? _("active") : _("inactive"),
autostartStr, autostartStr,
virNetworkIsPersistent(network) ? _("yes") : _("no")); virNetworkIsPersistent(network) ? _("yes") : _("no"));
} else if (optUUID) {
if (virNetworkGetUUIDString(network, uuid) < 0) {
vshError(ctl, "%s", _("Failed to get network's UUID"));
goto cleanup;
}
vshPrint(ctl, "%s\n", uuid);
} else if (optName) {
vshPrint(ctl, "%s\n", virNetworkGetName(network));
}
} }
ret = true;
cleanup:
vshNetworkListFree(list); vshNetworkListFree(list);
return true; return ret;
} }
#undef FILTER #undef FILTER

View File

@ -2834,6 +2834,7 @@ events until a timeout or interrupt key.
Returns basic information about the I<network> object. Returns basic information about the I<network> object.
=item B<net-list> [I<--inactive> | I<--all>] =item B<net-list> [I<--inactive> | I<--all>]
{ [I<--table>] | I<--name> | I<--uuid> }
[I<--persistent>] [<--transient>] [I<--persistent>] [<--transient>]
[I<--autostart>] [<--no-autostart>] [I<--autostart>] [<--no-autostart>]
@ -2844,6 +2845,12 @@ by I<--persistent> to list the persistent ones, I<--transient> to list the
transient ones, I<--autostart> to list the ones with autostart enabled, and transient ones, I<--autostart> to list the ones with autostart enabled, and
I<--no-autostart> to list the ones with autostart disabled. I<--no-autostart> to list the ones with autostart disabled.
If I<--name> is specified, network names are printed instead of the table
formatted one per line. If I<--uuid> is specified network's UUID's are printed
instead of names. Flag I<--table> specifies that the legacy table-formatted
output should be used. This is the default. All of these are mutually
exclusive.
NOTE: When talking to older servers, this command is forced to use a series of NOTE: When talking to older servers, this command is forced to use a series of
API calls with an inherent race, where a pool might not be listed or might appear API calls with an inherent race, where a pool might not be listed or might appear
more than once if it changed state between calls while the list was being more than once if it changed state between calls while the list was being