从图像模板创建vsi时遇到问题

时间:2017-10-23 18:36:00

标签: python ibm-cloud-infrastructure

当我尝试从现有图像模板创建vsi时,我使用Python API创建vsi并遇到问题。我的Python版本是3.6.3,我在Windows 7上运行我的Python脚本。

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

  

os_code(string) - 要使用的操作系统。如果不能指定   image_id已指定。

     

image_id(int) - 要加载到服务器上的图像的ID。如果指定了os_code,则无法指定。

当我在python脚本中指定不带os_code的image_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'.

所以我更改了我的脚本以指定os_code和image_id,然后我能够成功创建一个vsi但是图像模板没有加载到vsi中。

你能帮忙吗?感谢。

2 个答案:

答案 0 :(得分:0)

供您参考,下面是我用来实际创建一个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()

答案 1 :(得分:0)

文档中存在问题,image_id不是INT类型。方法create_instance调用Softlayer_Virtual_Guest::createObject方法,此方法使用图像的全局标识符而不是其id。

要了解全局标识符,您可以使用SLCLI或此处描述的方法 get_image http://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))