[libvirt] [PATCH 2/2] cmdNetworkList: Introduce --name, --uuid, --table

Michal Privoznik mprivozn at redhat.com
Mon Jun 8 15:14:02 UTC 2015


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

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tools/virsh-network.c | 69 ++++++++++++++++++++++++++++++++++++++++-----------
 tools/virsh.pod       |  7 ++++++
 2 files changed, 62 insertions(+), 14 deletions(-)

diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 182293e..66123c4 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -644,6 +644,18 @@ static const vshCmdOptDef opts_network_list[] = {
      .type = VSH_OT_BOOL,
      .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}
 };
 
@@ -655,6 +667,11 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
 {
     vshNetworkListPtr list = NULL;
     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;
 
     if (vshCommandOptBool(cmd, "inactive"))
@@ -670,33 +687,57 @@ cmdNetworkList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
     FILTER("autostart", VIR_CONNECT_LIST_NETWORKS_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)))
         return false;
 
-    vshPrintExtra(ctl, " %-20s %-10s %-13s %s\n", _("Name"), _("State"),
-                  _("Autostart"), _("Persistent"));
-    vshPrintExtra(ctl,
-                  "----------------------------------------------------------\n");
+    if (optTable) {
+        vshPrintExtra(ctl, " %-20s %-10s %-13s %s\n", _("Name"), _("State"),
+                      _("Autostart"), _("Persistent"));
+        vshPrintExtra(ctl,
+                      "----------------------------------------------------------\n");
+    }
 
     for (i = 0; i < list->nnets; i++) {
         virNetworkPtr network = list->nets[i];
         const char *autostartStr;
         int is_autostart = 0;
 
-        if (virNetworkGetAutostart(network, &is_autostart) < 0)
-            autostartStr = _("no autostart");
-        else
-            autostartStr = is_autostart ? _("yes") : _("no");
+        if (optTable) {
+            if (virNetworkGetAutostart(network, &is_autostart) < 0)
+                autostartStr = _("no autostart");
+            else
+                autostartStr = is_autostart ? _("yes") : _("no");
 
-        vshPrint(ctl, " %-20s %-10s %-13s %s\n",
-                 virNetworkGetName(network),
-                 virNetworkIsActive(network) ? _("active") : _("inactive"),
-                 autostartStr,
-                 virNetworkIsPersistent(network) ? _("yes") : _("no"));
+            vshPrint(ctl, " %-20s %-10s %-13s %s\n",
+                     virNetworkGetName(network),
+                     virNetworkIsActive(network) ? _("active") : _("inactive"),
+                     autostartStr,
+                     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);
-    return true;
+    return ret;
 }
 #undef FILTER
 
diff --git a/tools/virsh.pod b/tools/virsh.pod
index 4e3f82a..9b57c8c 100644
--- a/tools/virsh.pod
+++ b/tools/virsh.pod
@@ -2834,6 +2834,7 @@ events until a timeout or interrupt key.
 Returns basic information about the I<network> object.
 
 =item B<net-list> [I<--inactive> | I<--all>]
+                  { [I<--table>] | I<--name> | I<--uuid> }
                   [I<--persistent>] [<--transient>]
                   [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
 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
 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
-- 
2.3.6




More information about the libvir-list mailing list