Find the service that manages the virtual machine:
Retrieve the representation of the virtual machine:
# Find the service that manages the virtual machine:
vms_service = system_service.vms_service()
vm_service = vms_service.vm_service('123')
# Retrieve the representation of the virtual machine:
vm = vm_service.get()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
响应是对应类型的实例,本例中为 Python 类 ovirtsdk4.types.Vm 的实例。
某些服务的 get 方法支持额外的参数,它们控制了如何检索对象的表示,或者检索对象的内容(如果存在多个参数)。例如,您可能要检索虚拟机的当前状态,或者下次启动时的状态,因为它们可能有所不同。管理虚拟机的服务的 get 方法支持 next_run 布尔值参数:
Retrieve the representation of the virtual machine, not the
current one, but the one that will be used after the next
boot:
# Retrieve the representation of the virtual machine, not the
# current one, but the one that will be used after the next
# boot:
vm = vm_service.get(next_run=True)
Copy to ClipboardCopied!Toggle word wrapToggle overflow
如果因为某种原因无法检索对象,则 SDK 会引发 ovirtsdk4.Error 异常,详细信息失败。这包括对象实际上不存在的情况。请注意,调用 get 服务方法时会引发异常。调用 service locator 方法永远不会失败,即使对象不存在,因为该调用不会向服务器发送请求。例如:
Call the service that manages a non-existent virtual machine.
This call will succeed.
Retrieve the virtual machine. This call will raise an exception.
# Call the service that manages a non-existent virtual machine.
# This call will succeed.
vm_service = vms_service.vm_service('junk')
# Retrieve the virtual machine. This call will raise an exception.
vm = vm_service.get()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Find the service that manages the collection of virtual
machines:
List the virtual machines in the collection
# Find the service that manages the collection of virtual
# machines:
vms_service = system_service.vms_service()
# List the virtual machines in the collection
vms = vms_service.list()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Add the virtual machine:
Find the service that manages the virtual machine:
Start the virtual machine
# Add the virtual machine:
vm = vms_service.add(
...
)
# Find the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Start the virtual machine
vm_service.start()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
对象异步创建。在创建新虚拟机时,添加 方法将在虚拟机完全创建并准备好使用前返回响应。最好轮询对象的状态,以确保它完全创建。对于虚拟机,您应该检查其状态,直至状态为 DOWN :
Add the virtual machine:
Find the service that manages the virtual machine:
Wait until the virtual machine is down, indicating that it is
completely created:
# Add the virtual machine:
vm = vms_service.add(
...
)
# Find the service that manages the virtual machine:
vm_service = vms_service.vm_service(vm.id)
# Wait until the virtual machine is down, indicating that it is
# completely created:
while True:
time.sleep(5)
vm = vm_service.get()
if vm.status == types.VmStatus.DOWN:
break
Copy to ClipboardCopied!Toggle word wrapToggle overflow
from ovirtsdk4 import types
# Find the virtual machine, and then the service that
# manages it:
vm = vms_service.list(search='name=vm1')[0]
vm_service = vm_service.vm_service(vm.id)
# Update the name:
updated_vm = vm_service.update(
vm=types.Vm(
name='newvm'
)
)
from ovirtsdk4 import types
# Find the virtual machine, and then the service that
# manages it:
vm = vms_service.list(search='name=vm1')[0]
vm_service = vm_service.vm_service(vm.id)
# Update the name:
updated_vm = vm_service.update(
vm=types.Vm(
name='newvm'
)
)
Copy to ClipboardCopied!Toggle word wrapToggle overflow
执行更新时,请避免发送对象 的完整 表示。仅发送您要更新的属性。不要:
Retrieve the complete representation:
Update the representation, in memory, without sending a request
to the server:
Send the update. Do *not* do this.
# Retrieve the complete representation:
vm = vm_service.get()
# Update the representation, in memory, without sending a request
# to the server:
vm.name = 'newvm'
# Send the update. Do *not* do this.
vms_service.update(vm)
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Update the memory of the virtual machine to 1 GiB,
not during the current run, but after next boot:
# Update the memory of the virtual machine to 1 GiB,
# not during the current run, but after next boot:
vm = vm_service.update(
vm=types.Vm(
memory=1073741824
),
next_run=True
)
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Find the virtual machine by name:
Find the service that manages the virtual machine using the ID:
Remove the virtual machine:
# Find the virtual machine by name:
vm = vms_service.list(search='name=123')[0]
# Find the service that manages the virtual machine using the ID:
vm_service = vms_service.vm_service(vm.id)
# Remove the virtual machine:
vm_service.remove()
Copy to ClipboardCopied!Toggle word wrapToggle overflow
Check if the storage domain is attached to a data center:
# Check if the storage domain is attached to a data center:
sds_service = system_service.storage_domains_service()
sd_service = sds_service.storage_domain_service('123')
if sd_service.is_attached():
...
Copy to ClipboardCopied!Toggle word wrapToggle overflow