从映像创建vsi时遇到问题

2024-04-18 11:15:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pythonapi创建vsi,当我试图从现有的图像模板创建vsi时遇到了一个问题。我的Python版本是3.6.3,我在windows7上运行Python脚本。你知道吗

您的文档(http://softlayer-python.readthedocs.io/en/latest/api/managers/vs.html)显示:

os_code (string) – The operating system to use. Cannot be specified if image_id is specified.

image_id (int) – The ID of the image to load onto the server. Cannot be specified if os_code is specified.

在python脚本中指定没有os\u代码的image\u id时,出现以下错误:

Traceback (most recent call last):
File "C:\Users\ods\Documents\slenv\lib\site-packages\SoftLayer\transports.py", line 173, in __call__
result = utils.xmlrpc_client.loads(resp.content)[0][0]
File "c:\users\ods\appdata\local\programs\python\python36\Lib\xmlrpc\client.py", line 1021, in loads
return u.close(), u.getmethodname()
File "c:\users\ods\appdata\local\programs\python\python36\Lib\xmlrpc\client.py", line 656, in close
raise Fault(**self._stack[0])
xmlrpc.client.Fault: <Fault SoftLayer_Exception_MissingCreationProperty: "The property 'operatingSystemReferenceCode' must be set to create an instance of 'SoftLayer_Virtual_Guest'.">

在处理上述异常时,发生了另一个异常:

Traceback (most recent call last):
File "C:\Users\ods\Documents\slenv\", line 66, in <module>
create_vsi()
File "C:\Users\ods\Documents\slenv\", line 50, in create_vsi
ssh_keys=ssh_keys)
File "C:\Users\ods\Documents\slenv\lib\site-packages\SoftLayer\managers\vs.py", line 514, in verify_create_instance
return self.guest.generateOrderTemplate(create_options)
File "C:\Users\ods\Documents\slenv\lib\site-packages\SoftLayer\API.py", line 392, in call_handler
return self(name, *args, **kwargs)
File "C:\Users\ods\Documents\slenv\lib\site-packages\SoftLayer\API.py", line 360, in call
return self.client.call(self.name, name, *args, **kwargs)
File "C:\Users\ods\Documents\slenv\lib\site-packages\SoftLayer\API.py", line 263, in call
return self.transport(request)
File "C:\Users\ods\Documents\slenv\lib\site-packages\SoftLayer\transports.py", line 195, in __call__
raise _ex(ex.faultCode, ex.faultString)
SoftLayer.exceptions.SoftLayerAPIError: SoftLayerAPIError(SoftLayer_Exception_MissingCreationProperty): The property 'operatingSystemReferenceCode' must be set to create an instance of 'SoftLayer_Virtual_Guest'.

所以我修改了我的脚本来指定操作系统代码和图像id,然后我就可以成功地创建一个vsi,但是图像模板没有加载到vsi中。你知道吗

你能帮个忙吗?谢谢。你知道吗


Tags: inpyselflibpackageslinesitecall
2条回答

以下是我用来实际创建vsi或只是尝试试运行的python脚本,供您参考:

from __future__ import print_function
import SoftLayer
from SoftLayer.managers.vs import VSManager

def create_vsi():
    #Create a client to the SoftLayer_Account API service.
    #Note: currently set without the user ID and API key since
    #it will by default use the values set in the CLI
    #to use other values use SoftLayer.Client(sl_username, sl_api_key)
    client = SoftLayer.create_client_from_env(username="your username", 
    api_key="your key")
    vsi_mgr = VSManager(client)

    # uncomment to display create options
    #print(vsi_mgr.get_create_options())

    # common values
    datacenter = 'tor01' # the data center code
    cpus = 4
    memory = 65536          # MB
    os_code = 'REDHAT_7_64' # the operating system code. Doesn't need this as we'll use image_id
    local_disk = False # local disk or SAN
    hourly = True # hourly or monthly billing
    dedicated = False # multi-tenant or single tenant
    image_id = '1211529' # slcli image list
    nic_speed = 1000 # speed of network device
    disks = [100] # size of the disks - GB
    private = False # private networking only or include public internet networking as well. When set to true, Public IP will not be available.

    #ssh_keys = [227113, 229467] # the IDs of the ssh keys to load on the server - use slcli sshkey list
    ssh_keys = [504233]             # Audrey's SSH Key

    # server properties
    hostname = 'testapi'
    domain = 'smartworks.com' # the domain name suffix for the host
    private_vlan = '1219977' # VLAN for the server see VLAN list above - use slcli vlan list
    #tags = 'owner:bob,project:poc,type:docker'

    # code that can verify the create operation without actually doing it
    template = vsi_mgr.verify_create_instance(hostname=hostname, domain=domain, 
    #   os_code=os_code,
        cpus=cpus, memory=memory, datacenter=datacenter,
        image=image_id, local_disk=local_disk,
        hourly=hourly, dedicated=dedicated,
        private_vlan=private_vlan, disks=disks,
        nic_speed=nic_speed, private=private,
        ssh_keys=ssh_keys)

    print(template)

    # Code that will actually create the instance
    # result = vsi_mgr.create_instance(hostname=hostname, domain=domain, os_code=os_code,
        # cpus=cpus, memory=memory, datacenter=datacenter,
        # image=image_id, local_disk=local_disk,
        # hourly=hourly, dedicated=dedicated,
        # private_vlan=private_vlan, disks=disks,
        # nic_speed=nic_speed, private=private,
        # ssh_keys=ssh_keys)

    # print(result)

if __name__ == '__main__':
    create_vsi()

文档中有一个问题,图像id不是INT类型。方法create\u实例调用Softlayer_Virtual_Guest::createObject方法,此方法使用映像的全局标识符而不是其id

要知道全局标识符,可以使用SLCLI或这里描述的方法get\u imagehttp://softlayer-python.readthedocs.io/en/latest/api/managers/image.html

mgr = ImageManager(client)
resp = mgr.get_image(image_id=1211529)

您可以使用以下python代码来测试它,用您自己的数据更改options变量中的值。你知道吗

import SoftLayer
from SoftLayer import VSManager
from pprint import pprint as pp

USERNAME = 'set me'
API_KEY = 'set me'

options = {
    'cpus': 4,
    'datacenter': 'tor01',
    'domain': 'softlayer.xyz',
    'hostname': 'hostname-test',
    'dedicated': False,
    'hourly': True,
    'local_disk': False,
    'memory': 65536,
    'nic_speed': 1000,    
    'image_id': '87be78f2-fa03-4250-840e-bfa66d14866c',
    'private_vlan': 12315455,
    'private': False    
}

# Creating client instance
client = SoftLayer.create_client_from_env(username=USERNAME, api_key=API_KEY)

# Get the Hardware Manager
vsi_mgr = VSManager(client)

try:
    # verify_create_instance() will check for errors
    # Change the method by create_instance(**options) when you ready to order.
    resp = vsi_mgr.verify_create_instance(**options)

    # Print retrieved value
    pp(resp)
except SoftLayer.SoftLayerAPIError as e:
    """
    If there was an error returned from the SoftLayer API then bomb out with
    the error message.
    """
    pp("Unable to create Virtual Instance: %s, %s " % (e.faultCode, e.faultString))

相关问题 更多 >