gpt4 book ai didi

python - 如何使用 Softlayer Python API 重启虚拟机

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:36 24 4
gpt4 key购买 nike

我找不到在何处使用 SoftLayer Python API VSManager 重新启动或关闭/打开 虚拟机实例。

这些操作在 XMLRPC API 中描述,位于:

http://developer.softlayer.com/reference/services/SoftLayer_Virtual_Guest

但我在以下位置找不到等效项:

http://softlayer-python.readthedocs.org/en/latest/api/managers/vs.html

最佳答案

事实上,经理没有那个实现,你必须在这里调用 api 一些例子:

"""
Power off Guest

The scripts will look for a VSI which has an specific
hostname and the it powers off the VSI by making a single call
to the SoftLayer_Virtual_Guest::powerOff method.

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Acount/
http://sldn.softlayer.com/reference/services/SoftLayer_Acount/getVirtualGuests
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/setTags

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer

"""
# Your SoftLayer API username and key.
#
# Generate an API key at the SoftLayer Customer Portal:
# https://manage.softlayer.com/Administrative/apiKeychain
"""
username = 'set me'
key = 'set me'

# The name of the machine you wish to power off
virtualGuestName = 'rctest'

# Declare a new API service object
client = SoftLayer.Client(username=username, api_key=key)


try:
# Getting all virtual guest that the account has:
virtualGuests = client['SoftLayer_Account'].getVirtualGuests()
except SoftLayer.SoftLayerAPIError as e:
"""
If there was an error returned from the SoftLayer API then bomb out with the
error message.
"""
print("Unable to retrieve hardware. "
% (e.faultCode, e.faultString))

# Looking for the virtual guest
virtualGuestId = ''
for virtualGuest in virtualGuests:
if virtualGuest['hostname'] == virtualGuestName:
virtualGuestId = virtualGuest['id']

try:
# Power off the virtual guest
virtualMachines = client['SoftLayer_Virtual_Guest'].powerOff(id=virtualGuestId)
print ("powered off")
except SoftLayer.SoftLayerAPIError as e:
"""
If there was an error returned from the SoftLayer API then bomb out with the
error message.
"""
print("Unable to power off the virtual guest"
% (e.faultCode, e.faultString))

--

"""
Reboot Virtual Guest.
It reboots a SoftLayer Virtual Guest


Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/rebootDefault

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
# So we can talk to the SoftLayer API:
import SoftLayer

# From pprint import pprint as pp
# For nice debug output
from pprint import pprint as pp

# Your SoftLayer API username and key.
API_USERNAME = 'set me'
API_KEY = 'set me'

# If you don't know your server id you can call getVirtualGuests() in the
# SoftLayer_Account API service to get a list of Virtual Guests
serverId = 10403817

# Create a connection to API service.
client = SoftLayer.Client(
username=API_USERNAME,
api_key=API_KEY
)

# Reboot the Virtual Guest
try:

result = client['Virtual_Guest'].rebootDefault(id=serverId)
pp(result)

except SoftLayer.SoftLayerAPIError as e:
pp('Unable to reboot the server faultCode=%s, faultString=%s'
% (e.faultCode, e.faultString))

问候

关于python - 如何使用 Softlayer Python API 重启虚拟机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36790047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com