On 03/15/2012 01:56 PM, tangchen wrote: > This patch adds 3 API for libvirt_vm. > 1) vm.define() > 2) vm.attach_interface() > 3) vm.detach_interface() > > Signed-off-by: Tang Chen<tangc...@cn.fujitsu.com> > --- > client/virt/libvirt_vm.py | 40 ++++++++++++++++++++++++++++++++++++++++ > 1 files changed, 40 insertions(+), 0 deletions(-) > > diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py > index 46db911..8dd8d30 100644 > --- a/client/virt/libvirt_vm.py > +++ b/client/virt/libvirt_vm.py > @@ -431,6 +431,23 @@ def virsh_migrate(options, name, dest_uri, extra, uri = > ""): > return False > return True > > +def virsh_attach_detach_interface(name, command, type, extra = "", uri = ""): I don't suggest you use a function interface to deal with these 2 virsh operations, it will be hard to maintain if attach-interface and detach-interface have many change in the future. > + """ > + Attach a nic to VM. And you haven't comment dettach in here. > + """ > + cmd = "%s --domain %s " % (command, name) > + if type not in ('bridge', 'network', 'user', 'ethernet', 'direct', \ > + 'hostdev', 'mcast', 'server', 'client', ''): > + logging.error("Unknown interface type.") > + return False > + cmd += "--type %s %s" % (type, extra) > + try: > + virsh_cmd(cmd, uri) > + return True > + except error.CmdError: > + logging.error("Attaching interface to VM %s failed." % name) It's incorrect, because you define a comment function, including detaching interface, you had better to save virsh command output information then directly write log file instead of repeatedly define info/error information yourself. > + return False > + > def virsh_attach_device(name, xml_file, extra = "", uri = ""): > """ > Attach a device to VM. > @@ -578,6 +595,15 @@ class VM(virt_vm.BaseVM): > else: > return False > > + def define(self, path): > + """ > + Define the VM. > + @param path: VM xmlfile path. > + """ > + 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): > """ > @@ -1235,6 +1261,20 @@ class VM(virt_vm.BaseVM): > self.connect_uri = dest_uri > return result > > + def attach_interface(self, type, extra = ""): > + """ > + Attach a nic to VM. > + """ > + return virsh_attach_detach_interface(self.name, "attach-interface", \ > + type, extra, self.connect_uri) > + > + def detach_interface(self, type, extra = ""): > + """ > + Detach a nic from VM. > + """ > + return virsh_attach_detach_interface(self.name, "detach-interface", \ > + type, extra, self.connect_uri) > + > def attach_device(self, xml_file, extra = ""): > """ > Attach a device to VM.
_______________________________________________ Autotest mailing list Autotest@test.kernel.org http://test.kernel.org/cgi-bin/mailman/listinfo/autotest