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



嗨,我使用python使用softlayer API

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

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['quantity']

给一个我如何改变它假设我像这样更改数量

oc['数量']=2

result=client['Product_Order'].placeOrder(oc(

我收到错误 无效的订单 contianer

有很多

方法可以订购多个虚拟服务器,在您的情况下,方法 SoftLayer_Virtual_Guest::generateOrderTemplate 返回可以发送到方法 SoftLayer_Product_Order::verifyOrder 或 SoftLayer_Product_Order::p laceOrder 的容器类型SoftLayer_Container_Product_Order_Virtual_Guest。

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

'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))

我希望这对你有所帮助。

最新更新