This patch adds 3 API for libvirt_vm.
        1) vm.define()
        2) vm.attach_interface()
        3) vm.detach_interface()

Signed-off-by: Tang Chen <[email protected]>
---
 client/virt/libvirt_vm.py |   80 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 80 insertions(+), 0 deletions(-)

diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py
index eda5e8e..5d35888 100644
--- a/client/virt/libvirt_vm.py
+++ b/client/virt/libvirt_vm.py
@@ -432,6 +432,62 @@ def virsh_migrate(options, name, dest_uri, extra, uri = 
""):
     return True
 
 
+def _check_interface_type(type):
+    if not type:
+        logging.error("Interface type missing.")
+        return False
+
+    if type not in ('bridge', 'network', 'user', 'ethernet', 'direct',
+                    'hostdev', 'mcast', 'server', 'client'):
+        logging.error("Unknown interface type %s", type)
+        return False
+
+    return True
+
+
+def _build_run_at_dt_interface_cmd(cmd, type, extra="", uri=""):
+    if not _check_interface_type(type):
+        return False
+
+    if cmd.startswith("attach-interface"):
+        verb = "attach"
+    elif cmd.startswith("detach-interface"):
+        verb = "detach"
+    else:
+        logging.error("Unknown command: %s" % cmd)
+
+    cmd += " --type %s" % type
+
+    if extra:
+        cmd += " %s" % extra
+
+    try:
+        virsh_cmd(cmd, uri)
+        return True
+    except error.CmdError, detail:
+        logging.error("Interface %s on domain %s failed:\n%s",
+                      verb, name, detail)
+        return False
+
+
+def virsh_attach_interface(name, type, extra="", uri=""):
+    """
+    Attach a nic to VM.
+    """
+    cmd = "attach-interface --domain %s " % name
+
+    return _build_run_at_dt_interface_cmd(cmd, type, extra, uri)
+
+
+def virsh_detach_interface(name, type, extra="", uri=""):
+    """
+    Detach a nic to VM.
+    """
+    cmd = "detach-interface --domain %s " % name
+
+    return _build_run_at_dt_interface_cmd(cmd, type, extra, uri)
+
+
 def virsh_attach_device(name, xml_file, extra = "", uri = ""):
     """
     Attach a device to VM.
@@ -556,6 +612,16 @@ class VM(virt_vm.BaseVM):
             return False
 
 
+    def define(self, path):
+        """
+        Define the VM.
+        """
+        if not os.path.exists(path):
+            logging.error("File %s not found." % path)
+            return False
+        return virsh_define(path, self.connect_uri)
+
+
     def undefine(self):
         """
         Undefine the VM.
@@ -1234,6 +1300,20 @@ class VM(virt_vm.BaseVM):
         return result
 
 
+    def attach_interface(self, type, extra = ""):
+        """
+        Attach a nic to VM.
+        """
+        return virsh_attach_interface(self.name, type, extra, self.connect_uri)
+
+
+    def detach_interface(self, type, extra = ""):
+        """
+        Detach a nic from VM.
+        """
+        return virsh_detach_interface(self.name, type, extra, self.connect_uri)
+
+
     def attach_device(self, xml_file, extra = ""):
         """
         Attach a device to VM.
-- 
1.7.1

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to