Change vm network label

680 views
Skip to first unread message

Nick Wiesmueller

unread,
Jan 26, 2012, 12:56:06 PM1/26/12
to pysphere
I'm trying to change the network label of a vm and I cribbed
your(Seba's) code for changing the mac address and made a modification
which I though would work, but doesn't appear to, even though in the
end I get a "Vm successfully reconfigured". What am I doing wrong?

vm_obj=self.server.get_vm_by_name(vm)
if vm_obj:
#Find Virtual Nic device
net_device = None
for dev in vm_obj.properties.config.hardware.device:
if dev._type in ["VirtualE1000", "VirtualE1000e",
"VirtualPCNet32", "VirtualVmxnet"]:
net_device = dev._obj
break

if not net_device:
raise Exception("The vm seems to lack a Virtual Nic")
#Set Nic macAddress to Manual and set address
#net_device.set_element_addressType("Manual")
net_device.DeviceInfo.set_element_label(label)
#Invoke ReconfigVM_Task
request = VI.ReconfigVM_TaskRequestMsg()
_this = request.new__this(vm_obj._mor)
_this.set_attribute_type(vm_obj._mor.get_attribute_type())
request.set_element__this(_this)
spec = request.new_spec()
dev_change = spec.new_deviceChange()
dev_change.set_element_device(net_device)
dev_change.set_element_operation("edit")
spec.set_element_deviceChange([dev_change])
request.set_element_spec(spec)
ret =
self.server._proxy.ReconfigVM_Task(request)._returnval

#Wait for the task to finish
task = VITask(ret, self.server)

status = task.wait_for_state([task.STATE_SUCCESS,
task.STATE_ERROR])
if status == task.STATE_SUCCESS:
print "VM %s successfully reconfigured" % vm
elif status == task.STATE_ERROR:
print "Error reconfiguring vm: %s" % vm,
task.get_error_message()
else:
print "Vm %s not found" % vm

Seba

unread,
Jan 31, 2012, 8:50:37 AM1/31/12
to pysp...@googlegroups.com, Nick Wiesmueller
Hi Nick,

   I think I've figured out what's wrong. It seem that there are some properties that can't be changed, they are just informative so they are read-only.

When you change a vm's network label from the vSphere Client, you are actually changing a different property. So, if you want to switch the vm virtual nic to a different network:

replace this line:

net_device.DeviceInfo.set_element_label(label) 

with this:

net_device.Backing.set_element_deviceName(label)

I hope this is what you were looking for.

Best regards,

Sebastian.


2012/1/26 Nick Wiesmueller

Nick Wiesmueller

unread,
Jan 31, 2012, 1:51:21 PM1/31/12
to Seba, pysp...@googlegroups.com
That worked. Thanks!

Karthikeyan Karunamoorthy

unread,
Jul 19, 2016, 6:14:46 PM7/19/16
to pysphere, nick.wie...@singlewire.com
Hi Seba,

we are trying to change network label from DVS (N1K) to Standard Switch. Our code did not work, Do you have any idea?

>>> _this.set_attribute_type(vm_object._mor.get_attribute_type())

>>> request.set_element__this(_this)
>>> spec = request.new_spec()
>>> dev_change = spec.new_deviceChange()
>>> dev_change.set_element_device(dev._obj)
>>> dev_change.set_element_operation("edit")
>>> devs_changed = []
>>> devs_changed.append(dev_change)
>>> spec.set_element_deviceChange(devs_changed)
>>> request.set_element_spec(spec)
>>> ret = s._proxy.ReconfigVM_Task(request)._returnval
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pysphere/resources/VimService_services.py", line 2784, in ReconfigVM_Task
    response = self.binding.Receive(ReconfigVM_TaskResponseMsg.typecode)
  File "pysphere/ZSI/client.py", line 545, in Receive
    return _Binding.Receive(self, replytype, **kw)
  File "pysphere/ZSI/client.py", line 464, in Receive
    raise FaultException(msg)
pysphere.ZSI.FaultException: The session is not authenticated.
<pysphere.resources.VimService_services_types.NotAuthenticatedFault_Dec_Holder object at 0x2b86babed450>

韩凌飞

unread,
Jan 9, 2017, 3:02:21 AM1/9/17
to pysphere, arg...@gmail.com
I want to edit the vm network, from a stand network to other stand network, but I a error occurred : "'DynamicData_Holder' object has no attribute 'set_element_deviceName'", hope you can help me ,thanks!

在 2012年2月1日星期三 UTC+8上午2:51:21,Nick Wiesmueller写道:
Reply all
Reply to author
Forward
0 new messages