我想要多个数量的订单虚拟服务器,我如何生成订单容器

时间:2017-05-12 05:05:38

标签: python ibm-cloud-infrastructure

大家好我使用了使用python的softlayer api

我需要放置多个数量的虚拟服务器,如何生成订单容器? 我喜欢这个,

- moveToPoint:

我检查了qunatity

import SoftLayer
client = SoftLayer.Client(username='XXXXXX',api_key='xxxxxx')     
vmorderparmers = {
            'hostname':'testhost',
            'domain': 'exampledomain.com',
            'datacenter': 'sjc01',
            'startCpus':1,
            'maxMemory': 1024,

            'localDiskFlag': True,
            'hourlyBillingFlag': True,
            'operatingSystemReferenceCode':'CENTOS_6_64',
            "blockDevices": [
                {
                    "device": "0",
                    "diskImage": {
                        "capacity": 100
                    }
                }
            ]
        }
oc = client['Virtual_Guest'].generateOrderTemplate(vmorderparmers)

给我一​​个如何改变它 假设我改变这样的数量

  

OC ['量'] = 2

     

结果=客户端[' Product_Order&#39]。placeOrder(OC)

我收到错误无效订单contianer

1 个答案:

答案 0 :(得分:0)

订购多个虚拟服务器的方法有很多,在您的情况下,方法SoftLayer_Virtual_Guest::generateOrderTemplate会返回可以发送到方法SoftLayer_Container_Product_Order_Virtual_GuestSoftLayer_Product_Order::verifyOrder的容器类型SoftLayer_Product_Order::placeOrder

如果您在SoftLayer_Product_Order::placeOrder页面中查看虚拟服务器的示例。您会注意到需要修改数量 virtualGuest 参数,如下所示。

'virtualGuests': [
            {
                'domain': 'exampledomain.com',
                'hostname': 'testhost1'
            },
            {
                'domain': 'exampledomain.com',
                'hostname': 'testhost2'
            },
            {
                'domain': 'exampledomain.com',
                'hostname': 'testhost3'
            }
        ],
'quantity': 3

然后你的python代码应该是这样的:

"""
Create a VSI using the simplified way.

The script creates an order template by using the method generateOrderTemplate(), we'll
modify the response in order to create several virtual servers.
See below for more information.

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/generateOrderTemplate/
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Virtual_Guest
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder/
http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder/
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Virtual_Guest

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

# Your SoftLayer API username.
USERNAME = 'set me'
API_KEY = 'set me'

# To get the configuration options to create the server call the
# SoftLayer_Virtual_Guest::getCreateObjectOptions method.
vmorderparmers = {
    'hostname': 'testhost',
    'domain': 'exampledomain.com',
    'datacenter': {'name': 'sjc01'},
    'startCpus': 1,
    'maxMemory': 1024,
    'localDiskFlag': True,
    'hourlyBillingFlag': True,
    'operatingSystemReferenceCode': 'CENTOS_6_64',
    'blockDevices': [
        {
            'device': '0',
            'diskImage': {'capacity': 100}
        }
    ]
}

# Declare the API client
client = SoftLayer.create_client_from_env(username=USERNAME, api_key=API_KEY)

try:
    container = client['SoftLayer_Virtual_Guest'].generateOrderTemplate(vmorderparmers)

    # In order to create several VSI we modify the following parameters in the response
    container['quantity'] = 3

    # If you set quantity greater than 1 then you need to define
    # hostname/domain per virtual server you wish to order.
    container['virtualGuests'] = [
        {
            'domain': 'exampledomain.com',
            'hostname': 'testhost1'
        },
        {
            'domain': 'exampledomain.com',
            'hostname': 'testhost2'
        },
        {
            'domain': 'exampledomain.com',
            'hostname': 'testhost3'
        }
    ]

    """
    Now we are able to verify or place the order.
    verifyOrder() will check your order for errors. Replace this with a call to
    placeOrder() when you're ready to order. Both calls return a receipt object
    that you can use for your records.
    """
    receipt = client['SoftLayer_Product_Order'].verifyOrder(container)
    print (receipt)
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 create the VSI. %s, %s ' % (e.faultCode, e.faultString))

我希望这对你有所帮助。