]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
virsh: Teach attach-interface to --print-xml
authorMichal Privoznik <mprivozn@redhat.com>
Mon, 7 Sep 2015 10:13:47 +0000 (12:13 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 16 Sep 2015 14:10:48 +0000 (16:10 +0200)
We have the same argument to many other commands that produce an
XML based on what user typed. But unfortunately attach-interface
was missing it. Maybe nobody had needed it yet. Well, I did
just now.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
tools/virsh-domain.c
tools/virsh.pod

index b029b6577a308f98388a3808ff72aec2046b134f..73c476d196fc339fa0ea6314907da4454cd9f1b8 100644 (file)
@@ -862,6 +862,10 @@ static const vshCmdOptDef opts_attach_interface[] = {
      .type = VSH_OT_BOOL,
      .help = N_("affect current domain")
     },
+    {.name = "print-xml",
+     .type = VSH_OT_BOOL,
+     .help = N_("print XML document rather than attach the interface")
+    },
     {.name = NULL}
 };
 
@@ -921,7 +925,7 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
     int ret;
     bool functionReturn = false;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char *xml;
+    char *xml = NULL;
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
     bool current = vshCommandOptBool(cmd, "current");
     bool config = vshCommandOptBool(cmd, "config");
@@ -938,13 +942,6 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
     if (live)
         flags |= VIR_DOMAIN_AFFECT_LIVE;
 
-    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
-        return false;
-
-    if (persistent &&
-        virDomainIsActive(dom) == 1)
-        flags |= VIR_DOMAIN_AFFECT_LIVE;
-
     if (vshCommandOptStringReq(ctl, cmd, "type", &type) < 0 ||
         vshCommandOptStringReq(ctl, cmd, "source", &source) < 0 ||
         vshCommandOptStringReq(ctl, cmd, "target", &target) < 0 ||
@@ -1051,6 +1048,7 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
         virBufferAddLit(&buf, "</bandwidth>\n");
     }
 
+    virBufferAdjustIndent(&buf, -2);
     virBufferAddLit(&buf, "</interface>\n");
 
     if (virBufferError(&buf)) {
@@ -1060,13 +1058,24 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
 
     xml = virBufferContentAndReset(&buf);
 
+    if (vshCommandOptBool(cmd, "print-xml")) {
+        vshPrint(ctl, "%s", xml);
+        functionReturn = true;
+        goto cleanup;
+    }
+
+    if (!(dom = virshCommandOptDomain(ctl, cmd, NULL)))
+        goto cleanup;
+
+    if (persistent &&
+        virDomainIsActive(dom) == 1)
+        flags |= VIR_DOMAIN_AFFECT_LIVE;
+
     if (flags || current)
         ret = virDomainAttachDeviceFlags(dom, xml, flags);
     else
         ret = virDomainAttachDevice(dom, xml);
 
-    VIR_FREE(xml);
-
     if (ret != 0) {
         vshError(ctl, "%s", _("Failed to attach interface"));
     } else {
@@ -1075,7 +1084,9 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd)
     }
 
  cleanup:
-    virDomainFree(dom);
+    VIR_FREE(xml);
+    if (dom)
+        virDomainFree(dom);
     virBufferFreeAndReset(&buf);
     return functionReturn;
 }
index 83c445d35df3292b10fc10170c24a02523877d95..0212e7a011f562ab044412f365a0aa1bba0e6822 100644 (file)
@@ -2507,6 +2507,7 @@ Likewise, I<--shareable> is an alias for I<--mode shareable>.
 [[[I<--live>] [I<--config>] | [I<--current>]] | [I<--persistent>]]
 [I<--target target>] [I<--mac mac>] [I<--script script>] [I<--model model>]
 [I<--inbound average,peak,burst,floor>] [I<--outbound average,peak,burst>]
+[I<--print-xml>]
 
 Attach a new network interface to the domain.  I<type> can be
 I<network> to indicate connection via a libvirt virtual network, or
@@ -2536,6 +2537,9 @@ kilobytes in a single burst at I<peak> speed as described in the
 Network XML documentation at
 L<http://libvirt.org/formatnetwork.html#elementQoS>.
 
+If I<--print-xml> is specified, then the XML of the interface that would be
+attached is printed instead.
+
 If I<--live> is specified, affect a running domain.
 If I<--config> is specified, affect the next startup of a persistent domain.
 If I<--current> is specified, affect the current domain state.