]> xenbits.xensource.com Git - libvirt.git/commitdiff
New virsh feature: domif-setlink --domain --interface --state completer
authorSimon Kobyda <skobyda@redhat.com>
Thu, 12 Jul 2018 14:07:59 +0000 (16:07 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 12 Jul 2018 15:23:15 +0000 (17:23 +0200)
After you go through command mentioned above, completer
finds what state the device is currently in, and suggests
an opposite state.

Signed-off-by: Simon Kobyda <skobyda@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
tools/virsh-completer.c
tools/virsh-completer.h
tools/virsh-domain.c

index be59ea2e82a7cf40e80cc4741a66f6e3e8f0edb2..44ddada538c730e483bef870cab1b6767288bab6 100644 (file)
@@ -32,6 +32,7 @@
 #include "internal.h"
 #include "virutil.h"
 #include "viralloc.h"
+#include "virmacaddr.h"
 #include "virstring.h"
 #include "virxml.h"
 
@@ -777,6 +778,78 @@ virshPoolEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED,
 }
 
 
+char **
+virshDomainInterfaceStateCompleter(vshControl *ctl,
+                                   const vshCmd *cmd,
+                                   unsigned int flags)
+{
+    virshControlPtr priv = ctl->privData;
+    const char *iface = NULL;
+    char **ret = NULL;
+    xmlDocPtr xml = NULL;
+    xmlXPathContextPtr ctxt = NULL;
+    virMacAddr macaddr;
+    char macstr[VIR_MAC_STRING_BUFLEN] = "";
+    int ninterfaces;
+    xmlNodePtr *interfaces = NULL;
+    char *xpath = NULL;
+    char *state = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!priv->conn || virConnectIsAlive(priv->conn) <= 0)
+        return NULL;
+
+    if (virshDomainGetXML(ctl, cmd, flags, &xml, &ctxt) < 0)
+        goto cleanup;
+
+    if (vshCommandOptStringReq(ctl, cmd, "interface", &iface) < 0)
+        goto cleanup;
+
+    /* normalize the mac addr */
+    if (virMacAddrParse(iface, &macaddr) == 0)
+        virMacAddrFormat(&macaddr, macstr);
+
+    if (virAsprintf(&xpath, "/domain/devices/interface[(mac/@address = '%s') or "
+                            "                          (target/@dev = '%s')]",
+                           macstr, iface) < 0)
+        goto cleanup;
+
+    if ((ninterfaces = virXPathNodeSet(xpath, ctxt, &interfaces)) < 0)
+        goto cleanup;
+
+    if (ninterfaces != 1)
+        goto cleanup;
+
+    ctxt->node = interfaces[0];
+
+    if (VIR_ALLOC_N(ret, 2) < 0)
+        goto error;
+
+    if ((state = virXPathString("string(./link/@state)", ctxt)) &&
+        STREQ(state, "down")) {
+        if (VIR_STRDUP(ret[0], "up") < 0)
+            goto error;
+    } else {
+        if (VIR_STRDUP(ret[0], "down") < 0)
+            goto error;
+    }
+
+ cleanup:
+    VIR_FREE(state);
+    VIR_FREE(xpath);
+    VIR_FREE(interfaces);
+    xmlXPathFreeContext(ctxt);
+    xmlFreeDoc(xml);
+    return ret;
+
+ error:
+    virStringListFree(ret);
+    ret = NULL;
+    goto cleanup;
+}
+
+
 char **
 virshNodedevEventNameCompleter(vshControl *ctl ATTRIBUTE_UNUSED,
                                const vshCmd *cmd ATTRIBUTE_UNUSED,
index 1c14bb2a5430a6e261e0877b27d42565699fb63e..b4fd2a86c62babd2d501b641718df827e9962d09 100644 (file)
@@ -94,6 +94,10 @@ char ** virshPoolEventNameCompleter(vshControl *ctl,
                                     const vshCmd *cmd,
                                     unsigned int flags);
 
+char ** virshDomainInterfaceStateCompleter(vshControl *ctl,
+                                           const vshCmd *cmd,
+                                           unsigned int flags);
+
 char ** virshNodedevEventNameCompleter(vshControl *ctl,
                                        const vshCmd *cmd,
                                        unsigned int flags);
index 3959b5475b67bfe331d50ca075150e0ea65fd3f9..8adec1d9b18debe71b953b5e158de5a82be0852c 100644 (file)
@@ -3064,6 +3064,7 @@ static const vshCmdOptDef opts_domif_setlink[] = {
     {.name = "state",
      .type = VSH_OT_DATA,
      .flags = VSH_OFLAG_REQ,
+     .completer = virshDomainInterfaceStateCompleter,
      .help = N_("new state of the device")
     },
     {.name = "persistent",